Python is a high-level, interpreted, general-purpose programming language, and if you are just starting out, there are a few key things to note about this powerful programming language.
- Case sensitivity.
- Spacing and indentation.
- Order of arithmetic operations
When defining variables, you should note that Python is case sensitive. This means that the variable
Fan is different from
fan and thus, calling one in place of the other will return a NameError as illustrated bellow:
>>> word = "Lamp" >>> print(Word) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'Word' is not defined >>>
An error is returned because the variable word is defined with lowercase
w while an uppercase
W was passed into the print function.
Spacing and Indentation
Python uses whitespace indentation to indicate a code block. This means that statements indented on the same level are grouped together. Although you can decide the amount of indentation, and whether to use tabs or spaces as long as you are consistent, The recommended indent size is four spaces. This can be used for statements that ends with a colon such as
try, among others and can be seen in the example below:
>>> my_list = ['food', 'cloth', 'shoes'] >>> for item in my_list: ... print(item) ... food cloth shoes >>>
Order of arithmetic operations
Python makes use of the Mathematical Order of Operations. According to the English Wikipedia, order of operations is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given mathematical expression. For example, multiplication is granted a higher precedence than addition. in
1 + 3 * 5, the miltiplication operation will be executed first before addition.
It is important to note this because the operator's precedence determines how it will be interpreted and the output of your code as seen in the illustration below:
>>> 1 + 3 * 5 16 >>> (1 + 3) * 5 20 >>>
In the illustration above, the first output is
16 because the multiplication operation is carried out first before addition. If you want addition or subtraction to be executed first, you can wrap in a set of parentheses as seen in the second illustration which outputs
You can learn more about order of operations here
Final thoughts Learning a new programming language can be overwhelming sometimes, but there are so may resources available to point you in the right direction. One of such that I would recommend is Udacity's Introduction to Python Programming. Happy learning!