1.1python Statement
1.1python Statement
Table of contents
Multi-Line Statements
Python Compound Statements
Simple Statements
Expression statements
The pass statement
The del statement
The return statement
The import statement
The continue and break statement
Example
# statement 1
print('Hello')
# statement 2
x = 20
# statement 3
print(x)
Run
Output
Hello
20
# statement 3
print('Area of rectangle:', l * b)
Multi-Line Statements
Python statement ends with the token NEWLINE character. But
we can extend the statement over multiple lines using line
continuation character (\). This is known as an explicit
continuation.
Example
addition = 10 + 20 + \
30 + 40 + \
50 + 60 + 70
print(addition)
# Output: 280
Run
Implicit continuation:
Example:
addition = (10 + 20 +
30 + 40 +
50 + 60 + 70)
print(addition)
# Output: 280
Run
As you see, we have removed the the line continuation
character (\) if we are using the parentheses ().
Example:
# list of strings
names = ['Emma',
'Kelly',
'Jessa']
print(names)
Run
Output:
Simple Statements
Apart from the declaration and calculation statements,
Python has various simple statements for a specific
purpose. Let’s see them one by one.
Expression statements
Expression statements are used to compute and write a
value. An expression statement evaluates the expression
list and calculates the value.
5
x
x + 20
Example:
x = 5
# right hand side of = is a expression statement
# x = x + 10 is a complete statement
x = x + 10
Run
Example:
# create a function
def fun1(arg):
pass # a function that does nothing (yet)
Run
del target_list
Example:
x = 10
y = 30
print(x, y)
# delete x and y
del x, y
# try to access it
print(x, y)
Run
Output:
10 30
Example:
# Define a function
# function acceptts two numbers and return their sum
def addition(num1, num2):
return num1 + num2 # return the sum of two numbers
# result is the return value
result = addition(10, 20)
print(result)
Run
Output:
30
import datetime
Run
Output:
2021-08-30 18:30:33.103945