Python Statement, Indentation, and Comments
You will learn about statements, why indentation is important in Python, and why you need to comment your code.
Python Statement
A statement is an instruction that a Python interpreter uses to execute your code. Some examples of statements: a = 1
is a user defined statement, if
statement, and while
statement.
Multi-line Statement
A new statement is marked by a new line character or the enter key. But that does not stop us! We can break it into multiple lines with a line continuation character or the \
and this one is an explicit. Here is an example:
number = 1 + 2 + 3 + \
4 + 5 + 6 + \
7 + 8 + 9
This is what is called an explicit line continuation, basically meaning you are wanting a the code to be on the next line of the screen. Though Python also has implicit line continuation with the use of parentheses ( )
, brackets [ ]
, and braces { }
. TO recreate the example above with an implicit line continuation.
number = (1 + 2 + 3 +
4 + 5 + 6 +
7 + 8 + 9)
Another way to create multiple statements on one line is with the use of a semicolon.
1
a = 1; b = 'blue'; c = 14.1
Python Indentation
What makes Python an easy language to learn is that the code flows by indentation. What I mean by this is that what separates two different parts in your code is by what “level” the code is on. The indentation is the same that you would see on papers when the first sentence is tabbed in. Other programming languages uses { }
to block their code. Here is an example that will show the two different examples:
1
2
3
4
5
for i in range(5):
print(i)
j=i*4
if j < 20:
print('less than zero')
This was an example of how Python uses indentation to block of its code. As a rule of thumb, four whitespaces are used for indentation rather than tabs but most modern IDEs will put four whitespace inplace of a tab. So when writing your code, you can just use tab.
The purpose of the indentation is to make sure the code looks neat and clean for you and any future reads (may god help them, you’ll understand this as you try to read other peoples code or even your own for that matter)
Indentation can also be completely ingnored but it is always best to indent. If you happen to have a super simple loop that is basically just counting, then that can be in one line
1
2
3
if True:
print('CSTechnic is awesome!')
z = 1
The code above can turn into this:
1
if True: print('CSTechnic is awesome!'); z = 1
Python Comments
Ah comments, things we should all use and yet do not really. Comments are used to document your code, to describe what is happening inside the program, for anyone that happens to read your code.
Comments are meant to be a way for you to remind you how the code works when you come back to either reuse your code or rewrite it months later. So take time to comment your code, take time now or spend more time figuring it out later.
The way to use comments in Python are to start is the pound symbol or as it has been taken over by my generation as the hashtag #.
The comment extends upto the newline character or the enter key on your keyboard. Python just ignores comments.
1
2
3
4
5
6
#this is a comment but
this is no longer a comment
#this code will loop 5 times
for i in range(5)
print('CSTechnic is awesome')
But lets say that you do not want to use the hash symbol for every comment since you have multiple lines of it. Well that is when multi-line comments. To make a multi-line comment you just need to use triple quotes, either ‘’’ or “””
1
2
3
""" Here is an example
of multi-line comments
with out the use of a hash"""
Python Docstrings
A Docstring is a string literals that go dirrectly under the definition of a function, class, module, or method. They are created by triple quotes. When you want to return what the docstring is, you use the associated object which is doc attribute. This is what it would look like:
1
2
3
4
def doMath(number):
"""Function to do some math"""
return num*2**4
print(doMath.__doc__)
1
2
Output
Function to do some math