Statements in Python
In this article, You’ll learn about Statements in Python Programming language. While going through this article you’ll realise why python programming language syntax is considered same as english language.
Single line statement
Statements are usually in single line. For example:
my_name = “Python Warrior” print(my_name) x = 10 y = 20 z = x + y print(z)
All these five lines of code contain five separate statements. We know that there are five statements since each of them are in a separate line.
Multi-line statement
Every new statement in Python is marked by a new line (cartridge return – Enter key). However, there is a way in which a single statement can be continued to multiple lines with the help of continuation character, denoted by a forward slash at the end of each line (\). For example:
a = 10 b = 15 c = 20 d = 25 e = 30 f = 35 g = 40 s = a + b + c + \ d + e + f + \ g
As you can see, since adding all a + b + … + g would take too much space in one line, we’ve broken down the statement into multiple lines without changing the meaning of the statement.
There is one more way to implement the same logic, with slightly different syntax – you can prefer whichever you find simpler:
a = 10 b = 15 c = 20 d = 25 e = 30 f = 35 g = 40 s = (a + b + c + d + e + f + g)
Parentheses can be used to implicitly declare a statement.
Well, enough with the talks of breaking a single statement into multiple lines. What about putting multiple statements into a single line? That is possible with the help of something we don’t usually use in Python. The semicolon.
The above value assignments can be converted as follows:
a = 10; b = 15; c = 20; d = 25; e = 30; f = 35; g = 40
Indentations in Python
Indentations play a very crucial role in Python programming since we don’t use semicolons or curly braces here. C, C++, Java, C#, Javascript, and many other programming languages use semicolons (to declare statement termination) and curly braces (to define the scope of a function, class, method, etc). Well, Python uses indentations only.
Here is an example to print “I love Python Warriors” five times and “I love it forever” once:
for (i in range(1,5): print(“I love Python Warriors”) if i == 5: print(“I love it forever”)
As you can see, these three statements are wrapped inside the for loop:
print(“I love Python Warriors”) if i == 5: print(“I love it forever”)
Therefore, they’ll be executed five times. On the other hand, print(“I love it forever”) is inside the if statement, which will be true only once – that means print(“I love it forever”) will be printed only once.
The use of indentations make the code look elegant and cleaner as opposed to other object oriented programming languages.
Any code block, which is inside another code block (in the above example, the if block inside for loop block) must start with an indentation, otherwise it’d be treated as a new block (outside the older block). To make an indentation, we can use the Tab key from the keyboard or we can use four spaces. It is recommended to use four spaces instead of Tab key because sometimes, while saving a text file (.py) from one encoding type to another, tabs might be removed. So, just to be safe, always use spaces. Four hits ain’t too difficult.
It is possible to write multiple statements in one line inside the first code block by using semicolons – and it is highly recommended not to do it unless heavily required. For instance,
if (10 > 2): print(“Here’s a fact:”) print(“10 is greater than 2!”)
Can be re-written as:
if (10 > 2): print(“Here’s a fact:”); print(“10 is greater than 2!”)
Syntactically, both are correct. However, clearly, the former looks far better and cleaner. This is why we don’t do programming in one line.
Comments in Python
Comments are one of the most important aspects of any programming language. It tells exactly what a code is supposed to do so that when a different programmer reads your code, he doesn’t have to spend 8 hours scratching his head trying to understand the code you’ve written in 2 hours. Comments make programmers’ life easier. When you write some code, you’re usually very confident about its working and understanding. But if you open the same code after few months of time, you’ll feel like it’s written by some alien. That’s why, although it needs some extra effort, comments are very helpful.
Single-line comments in Python
In Python, we use a (#) symbol to start writing a comment. Programmatically, a comment is a statement that is completely ignored by the compiler or interpreter. Instead, it is written for the programmer’s own understanding of the code. Here are some examples of single line comments:
#Print Hello World to the screen print(“Hello World!”) #Add two numbers and output the sum x = 20 y = 30 z = x + y print(x+y)
Multi-line comments in Python
Doing multiline comments in Python is pretty darn simple. Either you can use multiple (#) symbols at the beginning of each line to write a comment, like this:
#Hi this is a #multi line comment #broken in three parts print(“Hello World!”)
Or you can use a set of triple quotes (‘’’) or double quotes (“””) at the beginning and end of comments. One thing worth noticing is that, if you start with a set of three single quotes, then you must end it with three single quotes as well. Same stands true for set of three double quotes as well.
Multi-line comments using single quotes
‘’’ This is an example of multi line comments ‘’’
Multi-line comments using double quotes
“”” This is an example of multi line comments” “””