Python Boiler Plate
It's essential to learn the syntax, or rules for writing a language, so you can learn to write clean, concise, functional code that is easy to read and debug.
Indentation
Indentation refers to the spaces at the beginning of a line of code. In many languages indentation is for readability only but in Python it is mandatory, meaning the code will not run if it is not indented correctly. Hence, it's essential that you copy the indentation guidelines we provide in the upcoming lessons.
The number of spaces you choose to use is up to you but have at least one. Generally, programmers use tab, equivalent to 4 spaces, for indenting. Also, make sure to use the same number of spaces for the same block of code. If indentation varys randomly in your code, then the computer will have a difficult time understanding the code structure or scope of each item and it will throw you an syntax error. Generally, items that are indented have a local scope while those that are not indented have a global scope.
Correct Indentation:
Incorrect Indentation:
Python Comments
In python, comments are added to help the programmer navigate through files, especially when they start writing hundreds of lines of code. Generally, comments are placed above blocks of text to describe their functionality or purpose in the program. They can be a phrase or 1-2 sentences but they should not be any longer because you don't want to clutter your code.
In python, one line comments are started with a hashtag. They can be placed on a seperate line or the end of a line of code. Multiline comments start and end with triple quotes. Anything you place inside comments will not be read by the computer so the comments are only there for human eyes and have no impact on the program itself. You should also have comments in your code so other programmers can easily navigate through and understand your code.
You can also place code that you don't want to run or delete inside comments so the computer ignores the code but you have it accessible to you. This tactic is very useful in debugging since it helps programmers understand the functionality of different parts of their code without having to delete any important code block.
In this example, the program will ignores the first print statement, since its in a comment, and only print the second print statement, 'Hello World'.
Python Mathematical Operators
Operators are used to perform operations on variables and values. You can compute in Python similiar to how you would on a calculator. For instance print(2+2) in python would yeild 4 as it would on a regular calculator. This similicity is why Python is often used to perform complex mathematical operations and given preference over other languages for math. Use the follow table to learn about key operators in Python.
Here are some examples to help you understand how these operators work. We strongly suggest running these commands yourself so you can get familiar with Python syntax and operators.