Hey guys, this is the fourth tutorial of the Python for Beginners Series. If you haven’t checked out our previous tutorials, you should go and check them for a better understanding. In this tutorial, we are going to discuss the statements and escape sequences. This tutorial is focused on statements so you will learn about statements in general and then comments, what are comments, how to write comments in Python, docstrings, escape sequences, and the python blocks.
So, without further ado, let’s start now.
Statements
Any instruction that you give to python to execute is a statement. You say
print("Hello World")
This is a print statement because you are printing some data, which in this case is a string.
Anything you write is a statement. You assign a value to a variable, that’s a declaration/assignment statement. Now some of you might have heard about if, else, for, while, etc… These all are statements.
Now statements can be of single-line or multi-line. Till now, you may have only used single line statements like print(“Hello World”) but there are multi-line statements as well.
Multi-Line Statements
You can extend a single statement to multiple lines. Try making a multi-line statement like this one.
a = "Coding
Ground"
print(a)
It will throw an error like
a = "Coding
^
SyntaxError: EOL while scanning string literal
To extend the statement to multiple lines, all you have to do is put a backslash ( \ ) at the end of the statement. For example,
a = "Coding\
Ground"
print(a)
And the output will be
Coding Ground
You can also put your statement under brackets for some data types and you won’t need to use backslash but it is not true for strings. For example
a = ['item1',
'item2']
print(a)
The output will be
['item1', 'item2']
It will work with tuples as well.
Now you might be wondering why should we even use it when we can just write it in one line. Well, it is done to make your code cleaner and increase its readability.
Comments
You must have noticed that in some code there is a text written beside it which describes what the code is for. That text is called a comment. There are two types of comments, single-line comments which are normal comments and multi-line comments which are also known as docstrings
Single Line Comments
These are written in a single line for example,
a = "Coding Ground" #Assigns value to variable a
Single Line comments start with a hashtag. They are written to describe what the code is about, it’s working what the variable stores and so on. Comments are only for user understanding and are never executed with the code.
You can write as many comments as you would like and it’s a good practice to write comments so that others can understand your code without much hassle. Not only for other people, but it will also help you in reading your code after a long time.
Docstrings or Multi-Line Comments
There are times when you have to add a full documentation about a function or a class you create or sometimes, it’s just that you have to write multi-line comments, for that we use docstring.
Docstring is any text written under triple quotes like ”’ or “””. For example,
"""This is a
docstring"""
'''This
is
also a
docstring'''
If you want to add a documentation of a function you can do this like
def codinground():
"""prints Coding Ground"""
return ("Coding Ground")
Docstring for functions, classes, or modules is written right after their declaration before any other statement.
Now if you want to access the documentation of a function/class/module, all you have to do is call the attribute __doc__.
You can, in this case, do this by printing function.__doc__. For example,
print(codinground.__doc__)
It will print out whatever is written in the docstring of that function or module or class. It will be really helpful whether you are experienced or beginner in python.
The output in this case will be
prints Coding Ground
So, docstrings will help you write documentations in an efficient and a standard way for everyone to follow.
Now, this isn’t a norm but a personal preference that you should write docstrings when you are writing a function or a class or module and single-line comments when you are writing loops or variables.
You might be confused about functions, classes, or modules but don’t worry, we’ll be covering them in our future tutorials in this series. But if you are confused about variables, I recommend checking out our tutorial on Datatypes and Variables in Python
Coming over to our next topic which is Escape Sequences.
Escape Sequences
There are some characters in python which have a different meaning. These are called escape sequences and are written after the escape character backslash( \ ).
For example, if I told you to print out a string whose output is “Coding Ground”. How would you do it? simply using print(“”Coding Ground””) will throw a Syntax Error.
There are many such cases where we want our text to be under quotes or in a new line without actually writing it in another line etc., in these cases, we use escape sequences.
To print a text under quotes just put a backslash before the quotes.
print("\"Coding Ground\"")
The output of this will be “Coding Ground”. This is because the double quotes are written after the escape character backslash. If you try to put backslash without any other character, it will leave whitespace. The backslash defines that the character after after the escape character has a different meaning than the normal one.
TIP
If it’s just printing the text under double or single quotes you can just do it like this print(‘”Coding Ground”‘). What I have done is, I started the string with a single quote and wrote the word under double quote so python interpreter knows that ok, the main string is whatever text is under single quotes. So, it treats double quotes as an ordinary string character.
But this is not recommended
You can also use \n to make it the next line. I know this is not clear so let’s consider this example, You are making a menu for your program and you want to list multiple items in it.
Then you can do this by using the \n like
print("1. Item One\n2. Item Two\n3. Item Three")
The output of this will be
1. Item One
2. Item Two
3. Item Three
You can see that it is much more efficient than writing it in multiple lines.
Similarly, there are many such escape sequences, you can find a list of them below.
Escape Sequence | Meaning | |
---|---|---|
\newline | Backslash and newline ignored | |
\\ | Backslash (\ ) | |
\' | Single quote (' ) | |
\" | Double quote (" ) | |
\a | ASCII Bell (BEL) | |
\b | ASCII Backspace (BS) | |
\f | ASCII Formfeed (FF) | |
\n | ASCII Linefeed (LF) | |
\r | ASCII Carriage Return (CR) | |
\t | ASCII Horizontal Tab (TAB) | |
\v | ASCII Vertical Tab (VT) | |
\ooo | Character with octal value ooo | |
\xhh | Character with hex value hh |
These are all the escape sequences available in Python. Play with them and you will have a better understanding.
Python Blocks
In our previous example on docstring, we made a function and then wrote a docstring.
If you have noticed carefully then you must be wondering why we put a colon after the function definition and then we leave two spaces in the statement we wrote under that function?
This is because unlike other languages, which use curly braces {} to define a block, python uses colon and indentation which is usually two or four spaces.
According to Python’s Official Documentation,
A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block.
All conditional statements, functions, classes and modules use a python block.
Let us look at a if statement to make it more clear.
a = 10
if a == 10:
print("This code executes under if block")
print("This executes anyway")
Since the condition is true, which is a is equal to 10, if block will run and the output will be
This code executes under if block
This executes anyway
If we tweak the value of variable a, suppose 1, then the if condition will become false and if block will not run and the output will simply be
This executes anyway
So basically, a block is a piece of code that executes as a unit whenever called.
Hopefully it should have given you some basic insight on loops as well.
We will discuss python blocks in detail when we cover control flow statements.
This is it for this tutorial, hope you like it. Tell me what you think of this tutorial in the comments section below. Also, if you have any doubts, comment down below and I’ll help you.
References