Instructions that a python interpreter can execute are called statements. There are many types of statements in python like assignment statements, conditional statements, looping statements, etc. Let’s look at an example
Code :
Python Code
# example of statments in python
for i in range(1, 5): # looping statement
if i % 2 == 0: # conditional statement
print(i) # print statement
Output :
2
4
Multi-Line statement :
In python end of the statement is noticed by the newline character. We can extend the continuation of the statement over multiple lines using the continuation Character slash (\), parenthesis ( ) ,braces { } , square brackets [ ] and semi-colon ( ; ) .
Code:
Python Code
# Declared using the continuation Character ( \ )
a = 1 + 2 + 3 + 4 + \
5 + 6 + 7 + \
8 + 9 + 10
# Declared using the parenthesis ()
b = (1+2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)
# Declared using the braces { }
c = {1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 10}
# Declared using the []
d = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Declared using the ;
e = 3; f = 5; g = 7
Python Indentation :
Python uses indentation to define the block of code, whereas other programming languages like C, C++, and Java uses { } to define the block of code.
A code block starts with an indented line and ends with an unindented line. Indentation is provided using the white space. The amount of indentation is up to us, but indentation must be consistent throughout the block.
Generally, 4 white spaces are used for indentation. Let’s look at an example
Code :
Python Code
#This is one block of code
def func():
s = "takeuforward"
while(i < 10):
pass
#This is another block of code.
for i in range(1, 5):
if i % 2 == 0:
pass
else:
pass
In the above code, func() is one block of code, string ‘ s ‘, and while loop belongs to func() block because they have the same indentation. The first unindented line after func() is for loop, so for loop doesn’t belong to the func(). From for loop another block of code starts. If and else, are part of the for loop block because they have the same indentation from left.
In line, continuation Indentation can be ignored. But it is always good to maintain indentation, which makes code readable and feasible.
Let’s see an example,
def func(): s = "takeuforward" print(s)
def func(): s = "takeuforward" ; print(s)
Both func() are the same and do the same work, but in one func() we used indentation in the other we used line continuation.
Incorrect Indentation raisses the IndentationError.
Code:
Python Code
for i in range(1,5) :
if i%2 == 0 :
pass
Output :
IndentationError: expected an indented block after ‘if’ statement on line 2
Python Comments :
In any programming, language comments are very important. They are used to describe what the program is doing, or what some part of the code is doing, etc. They make the reader who is reading the source code to understand what is going on inside the program and the logic of the program.
Writing comments for the code is a good habit. After you revisit to review the code, comments help to understand what we had done in the code earlier.
There are two types of Comments in python
- Single Line Comment
- Multi-Line Comment.
Single Line Comment :
To start a comment, we use the hash (#) symbol in python. Till the newline is reached python treats the text written after the hash as the comment. If we want the next line to be also commented on, use # at the start of the other line.
Code:
Python Code
# This is the comment
# python ignores the comment
print("takeuforward")
Output: takeuforward
Python interpreter ignores the comment when encountered.
Multi-Line Comments :
They are two ways to write the multi-line comment,
- Using # at the beginning of the new line.
- Using the triple quotes either ‘ ‘ ‘ or “ “ “.
Whatever is written between the triple quote is treated as a comment.
"""This is the multi-line comment
In python, they are very useful while writing text which does not fit in a single line and requires multiple lines.
Python interpreter neglects the multi-line comments.
Doc strings :
Doc string is short for Documentation string.
They generally appear after the definition of a function, method, class, or module. They describe what the function, method, or class is doing. They are like documentation of them.
Triple quotes are used to write the Docstrings.
Code :
Python Code
# docstring inside a function
def greet(name):
"""This function greets the person."""
print("Hello " + name + ", " + "Welcome to takeuforward")
# docstring inside a class
class tuf:
"""This class has an attribute and two methods.
The attribute ' team ' contains the name of the team members.
The Methods are :
1.wish() method, which wishes the freshers who join the team.
2.add() method ,adds the freshers to the 'team' attribute.
"""
team = ["raj", "sai", "monica", "harsh"]
def wish(self, name):
print("Hello " + name + ", " + "Welcome to takeuforward")
def add(self, name):
self.team.append(name)
print(name + "added to data team members sucessfully")
Docstrings are written right after the function, class, or module definition. This separates the docstrings from multi-line comments.
Docstring in the function or class can be accessed using the __doc__ attribute.
Code:
Python Code
# docstring inside a class
class tuf:
"""This class has an attribute and two methods.
The attribute ' team ' contains the name of the team members.
The Methods are :
1.wish() method, which wishes the freshers who join the team.
2.add() method ,adds the freshers to the 'team' attribute.
"""
team = ["raj", "sai", "monica", "harsh"]
def wish(self, name):
print("Hello " + name + ", " + "Welcome to takeuforward")
def add(self, name):
self.team.append(name)
print(name + "added to data team members sucessfully")
print(tuf.__doc__)
Output:
This class has an attribute and two methods.
The attribute ‘ team ‘ contains the name of the team members.
The Methods are :
1.wish) method, which wishes the freshers who join the team.
2.add) method ,adds the freshers to the ‘team’ attribute.
Special thanks to SaiSri Angajala for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article. If you want to suggest any improvement/correction in this article please mail us at [email protected]