Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Python’s syntax rules are simple and easy to follow. While writing code in Python is almost intuitive, it is important to understand the basic rules and structure of Python syntax to ensure you write clean code and avoid frustrating errors. Read through these three basic rules and concepts to make sure you have a fundamental understanding of how Python code works.
Indentation is the most important element of Python’s syntax rules. In most other programming languages, indentation in code is for readability only. It does not serve any technical function. Python is different.
In Python, indentation is used to indicate a block of code. When Python code is interpreted, the interpreter will look for indentation to tell it where blocks of code begin and end. For this reason, within any given block of code, the method of indentation must be uniform.
Indentation is just the white space at the beginning of a line of code. In Python, the number of spaces used for indentation is up to you as a programmer. You can use either spaces or tabs. The only constraint is that indentation must be at least one space.
The most common indentation unit in computer programming is four spaces. I strongly recommend using four spaces for your indentation. In my experience, this will make your code much more portable. And as someone who started by using tabs I can say that tabs do not play well with Github, so you might as well start using spaces.
By always using four spaces as your indentation increment, you ensure that all the code you write is uniform. You’ll also be practicing the most commonly used method of indentation, which will better prepare you for coding as part of a team.
Now that we understand the importance of indentation and how it is used in Python, let’s look at some examples.
As you learned earlier, indentation is used by the Python interpreter to differentiate between blocks of code. In the following example, we see an if statement followed by a colon (‘:’), indicating a block of code to follow.
Notice the indented code that will be run if the condition in the if statement is true. If there is no indented code after a colon, the program will throw a syntax error, telling you that indentation is expected!
Input:
if 1 < 2:
print("1 is less than two!")
Output:
1 is less than two!
You can also have blocks of code nested within other blocks of code. In the below example, we define a function called myfunction
, which starts a code block. Inside myfunction
we have two if statements and an else statement, each of which defines another code block that is nested inside the function.
x = 145
y = 378
def myfunction():
x = 145
y = 378
if x > 100:
print('x is greater than 100.')
if y > 500:
print('y is greater than 500.')
else:
print('y is less than 500')
Indentation is important in Python because it serves the technical function of defining blocks of code for the interpreter. But indentation is also important to Python because it makes Python code readable. By making indentation part of the syntax rules, the creators of Python ensured that readability was built in.
In most other programming languages, indentation is purely aesthetic. In Python, the failure to use proper indentation in your code will cause a syntax error when you run your code! This has the effect of ensuring that all Python code is readable.
Whether it is written by a self-taught beginner, artificial intelligence, or an MIT graduate, Python code must have proper indentation in order to run. Because Python’s authors considered readability as they developed the language, all Python code bases have a very similar look and feel. This makes reading other people’s code very easy once you are familiar with Python.
Now that we understand the function and importance of indentation, let’s look at how to use comments in your Python code!
There are two basic kinds of comments in Python: single-line comments and multi-line comments. A single line comment begins with a hashtag (‘#’) symbol.
Below is an example of a single-line comment:
#This is a comment in Python.
Although single line comments can be as long as you’d like, if you want to continue to the next line, you must use another ‘#’ to start the new line.
#Python will read this as a comment
#Python will also read this as a comment
But Python will not read this as a comment
If you have a comment that you know will span multiple lines, you can use a multi-line comment.
A multi-line comment is actually a multi-line string; however, because Python ignores string literals that are not assigned to a variable, you can add multi-line strings to your code and place your comments inside them.
(If that sentence sounded like gibberish to you, don’t worry! We will discuss strings in great detail in a later post!)
A multi-line string is enclosed in either triple-single or triple-double quotes. This is easier to demonstrate than it is to explain! The following examples are both acceptable ways to use a multi-line string comment.
'''
This is my multi-line comment.
I think that PyTaster is a great place to learn Python.
You should tell your friends about PyTaster!
'''
"""
This is my multi-line comment.
I think that PyTaster is a great place to learn Python.
You should tell your friends about PyTaster!
"""
As you can see, commenting in Python is quite simple and straightforward. And remember, don’t worry about the ‘string’ lingo right now. We’ll get into Python’s different data types in a later lesson. For now, just remember you can use triple quotes to put a multi-line comment in your code.
In Python, there are no specific commands for declaring a variable. Python also does not require you to declare a new variable’s type. Instead, Python creates a variable at the instant that you assign a value to it. It is therefore important that you use proper syntax when declaring your variables in Python.
For example, consider the following two blocks of code. Each of them declares a variable, and each appears to assign the same value to the variable. However, because the declaration of the variable y
places the value of the variable inside quotation marks, y
will be read by the interpreter as a string rather than an integer.
x = 100
y = '100'
This is important because a variable’s type determines how you are able to use the variable later on. If you try to perform an operation or function on y
that is expecting a number, such as a mathematical operation, then you will generate a data type error when you run your code.
All of this is discussed in much more detail in this post on Python data types. For now, it is sufficient to understand that you declare a Python variable by using the syntax variable_name = variable_value
.
Believe it or not, that’s all you really need to know about Python syntax to get started! Python is designed to be simple, intuitive, and readable. Almost as a result, its syntax rules are sparse and easy to understand.
Now that you understand the basic syntax rules of Python, you are ready to move on to exploring and understanding Python data types!