Python Statements
A statement is an instruction that a Python interpreter can execute. In python we use
various statements to write the code 1 . Some of the statements available in Python are
assignment statement, print statement, control flow and the import statement.
Assignment Statement:
Assignment statements are used to set value to a variable
Ex: a = 10
in this statement value 10 is stored into variable a.
we can use assignment statement in evaluating an expression
f = c *9.0/5.0 + 32
This expression converts Celsius into Fahrenheit
We can assign several variables at once
a = b = c =10
Python permits number of variables to assign with different values with comma separated
values
x , y = 10,20
is same as
x = 10
y = 20
Input and Output Statements
Input from the user: We use input function to read data from user,
>>>input(“Enter a number”)
…Enter a number 10
'10' Here, we can see that the entered value 10 is a string, not a number. To convert this into a
number we can use int() or float() functions.
>>> int(10)
10
>>>float(10)
10.0
Output:
print statement: In python we use print statement to display output in console
>>> print(“Hello world”)
will display Hello world as output
formatting output
The print statement produces a more readable output, by omitting the enclosing quotes and by
printing escape sequences, comma separator and special characters:
\' display ' \" display "\t tab space \n new line
https://gvsnarasimha.blogspot.com/ 1
Control statements
Control statements are used to change the order of sequence of execution based on
some conditions. Various control statements in python are if statement, for statement,
while statement, break, continue and pass.
Decision making and Branching / Conditional / if statements
If statement is a powerful decision-making statement and used to control the flow of
execution of statements. Basically it is a two way decision statement and is used with a
keyword if and test condition. It allows computer to evaluate the condition first and then,
depending on the condition transfers the control to a particular statement. Python supports
the following statements in order to achieve decision making capabilities
1. If statement
2. If –else
3. Nested If
4. elif
If statement: If statement evaluates the test condition and will execute statement-block only if the
condition is True. If the condition is False, the statement-block is not executed and control is transferred
to next statements. The group of statements which has to be executed when the condition is true are
written in statements-block. Statement-block may be a single statement or group of statements with
proper indent.
Syntax:
if test condition:
Statements-block
Statement - x
Flow chart :
Ex: Python program to check whether the given number is positive or not
num = float(input("Enter a number: "))
if num > 0: Output:
print("Positive number") Enter a number 5
print("Simple if") Positive number
Simple if
https://gvsnarasimha.blogspot.com/ 2
If –else:
The if-else is an extension of the simple if statement. The if –else statement evaluates the
condition, whether the condition is true then the If block will be executed otherwise the else
block will be executed, not both. The general form is
Syntax
if test condition:
If block
else:
else block
statement-x
Flow-chart
Ex: num = float(input("Enter a number: "))
if num > 0:
print("Positive Number ")
else:
print("Negative Number ")
Output:
Enter a number 5
Positive Number
https://gvsnarasimha.blogspot.com/ 3
Nested if
To evaluate multiple conditions we use nested if statement. The general form is
Syntax
if test condition1:
if test condition2:
statement1
else:
statement2
else:
statement3
From the above syntax if the condition1 is true control transfers to condition2 when it is
true statement 1 executes, oterwise statement 2 will be executed.If the condition1 itself false the
control will be transferred to else part and statement3 will be executed.
Program Output:
https://gvsnarasimha.blogspot.com/ 4
elif: Python provides us elif statement as an alternative to Nested if statement, which is a
combination of else and if .As part of elif we write multiple conditions where the appropriate
condition will be executed
Syntax:
if condition1 : statement 1
elif condition2: statement2
elif condition3: statement3
…
else: else block
Flow Chart:
Program: Output:
https://gvsnarasimha.blogspot.com/ 5
Looping / Iterating statements:
When we want to execute a block of statements repeatedly we use looping statements
The looping statements are
1.while
2.for
while loop: while loop is a simple looping construct , we use while loop when we want to iterate
a block of statements based on a condition. While loop is used when we don’t know the
number of iterations of the statements block.
Syntax:
While Condition:
Statements
Flow Chart:
During the process the condition is evaluated first .when the condition is true the
statements are executed after that again the condition evaluated. The process continues till the
condition becomes false.
Program: Output:
https://gvsnarasimha.blogspot.com/ 6
for loop : The for loop in Python is used to iterate over a sequence (list, tuple, string) or other
iterable objects. Iterating over a sequence is called traversal.
Syntax of for Loop
for val in sequence:
statements
Here, val is the variable that takes the value of the item inside the sequence on each
iteration. Loop continues until we reach the last item in the sequence. The body of for loop is
separated from the rest of the code using indentation.
Flowchart of for Loop
The range() function: We can generate a sequence of numbers using range() function.
range(10) will generate numbers from 0 to 9 (10 numbers). We can also define
the start, stop and step size as range(start, stop, step size). step size defaults to 1 if not
provided
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10,2)
[1, 3, 5, 7, 9] .
Ex: Python program to display even numbers up to some range
n= int(input(“Enter the range to print Even Numbers”))
for x in range(2,n+1,2):
print(x)
output: Enter the range to print Even Numbers 5
https://gvsnarasimha.blogspot.com/ 7
Break statement: The break statement terminates the loop containing it. Control of the
program flows to the statement immediately after the body of the loop.
Syntax
break
Flow Chart:
Using break statement in loops:
Example:
for letter in 'Python':
if letter == 'h':
break
print 'Current Letter :', letter
When the above code is executed, it produces the following result −
Current Letter : P
Current Letter : y
Current Letter : t
https://gvsnarasimha.blogspot.com/ 8
Continue Statement:
The continue statement is used to skip the following statements and continues with the next
iteration.
Syntax: continue
Flowchart:
Using continue statement as part of loop
#python program to print odd numbers up to a given range usin g continue statement
n = int(input("enter range"))
count = 0
for i in range(1,n+1):
if i%2 ==0:
continue
print(i)
output: enter range
1
3
5
https://gvsnarasimha.blogspot.com/ 9
Pass Statement:
In Python pass is a null statement. The difference between a comment and pass statement in
Python is that, while the interpreter ignores a comment entirely, pass is not ignored. But
nothing happens when it is executed. It results into no operation (NOP).
Syntax of pass
pass
Suppose we have a loop or a function that is not imp lemented yet, but we want to implement
it in the future. They cannot have an empty body. The interpreter would complain. So, we use
the pass statement to construct a body that does nothing.
Example: pass Statement
for val in sequence:
pass
Infinite loops:
When the test condition is true then the loop will become infinite. To terminate
infinite loop we use ctrl+c
Ex:
while True:
print(“infinite loop”)
or
while 1:
print(“infinite loop”)
https://gvsnarasimha.blogspot.com/ 10