Maker Pro
Custom

Getting Started With Python

January 02, 2019 by Daniel Hertz
Share
banner

Get started with the programming language Python by learning the most important concepts for beginners.

Python is a very popular programming language among makers. This article discusses the basics of the language for beginners, offering an overview of the most important concepts of the language.

Before you dive in and start writing your code, evaluate whether Python is the right language for your project. You might also want to take a look at this introduction to Java.

Variables in Python

Python is a dynamically typed language, meaning you don’t have to define a variable’s type when compiling your program—in fact the syntax doesn’t even allow adding a type to a variable definition. 

The type is defined by whatever you assign to the variable. Therefore a variable can start as a String and then become an Integer at any given time:

python variables and strings

While this sounds good, especially for beginners, it’s advisable not to mix data types. This can lead to a lot of runtime errors and an unstable program.

However, you can always call type(variableName) to check what the type of a variable is:

python variable types

Python and Programming Paradigms

Another peculiarity of Python is that the language doesn’t follow one programming paradigm unlike, for example, Java, which is strictly object-oriented or C, which is imperative. It’s up to you whether you follow the functional, imperative, object-oriented, or procedural programming paradigm style. Each style has its pros and cons.

While functional programming is a good paradigm for parallel processing and lambda calculus, imperative programming is better for manipulating data-structures. However, Python is best used as a procedural programming language, especially because data-hiding is not supported by Python even though it is a very important concept of object-oriented programming.

So if you want to write object-oriented software, you might want to take a look at Java or C++ instead. Haskell is a popular alternative for functional programming.

Formatting the Code

Unlike other languages, Python doesn’t use parentheses for structures and blocks. It uses indentation levels instead:

def add(one, two):
 res = one

 # The for-loop is only used as an example
 # for another indentation level
 for x in range(two):
   res += 1

 return res

if __name__ == '__main__':
 result = add(12, 28)
 print(result)

One important tip: Don’t mix tab-stops and spaces in your code! Go for either of them. Many IDEs will allow you to do that (or they’ll convert your code to follow one style, usually tab-stops), but the Python interpreter on a Raspberry Pi or other embedded system might not accept your code and throw cryptical error messages. Save yourself the trouble!

Magic Methods

Python allows you to define methods for basic operations for custom data types. Therefore, you can use the standard operations (+, -, *, etc.) for your own classes. 

For example, If you write a class that stores a length together with a unit (“cm”, “m”, “inch”, etc.), you can define the __add(self, other)__ method in your code and handle the non-numeric information together with the number and correctly add two different lengths (i.e. inches and meters).

You can also define magic-methods for comparisons which will allow you to directly compare two objects using the <, >, ==, etc. operators instead of having to call a method.

Magic methods are also used to define the constructor in a class and they have many more uses. For a full list, refer to Python's official documentation.

Related Content

Categories

Comments


You May Also Like