Control Structures¶
Control Structures construct a fundamental part of language along with syntax,semantics and core libraries. It is the Control Structures which makes the program more lively. Since they contol the flow of execution of program, they are named Control Structures
if statement¶
Usage:¶
if condition:
statement_1
statement_2
...
statement_n
Note
In Python, block of code means, the lines with same indentation(
i.e., same number of tabs or spaces before it). Here statement_1
upto statement_n are in if block. This enhances the code
readability
Example:¶
In [1]:
response = input("Enter an integer : ")
num = int(response)
if num % 2 == 0:
print("{} is an even number".format(num))
Enter an integer : 4
4 is an even number
Note Typecasting
int(response) converted the string response to integer. If user
enters anything other than integer, ValueError is raised
if-else statement¶
Usage:¶
if condition:
statement_1
statement_2
...
statement_n
else:
statement_1
statement_2
...
statement_n
Example:¶
In [59]:
response = input("Enter an integer : ")
num = int(response)
if num % 2 == 0:
print("{} is an even number".format(num))
else:
print("{} is an odd number".format(num))
Enter an integer : 5
5 is an odd number
Single Line if-else¶
This serves as a replacement for ternery operator avaliable in C
Usage:¶
C ternery
result = (condition) ? value_true : value_false
Python Single Line if else
result = value_true if condition else value_false
Example:¶
In [60]:
response = input("Enter an integer : ")
num = int(response)
result = "even" if num % 2 == 0 else "odd"
print("{} is {} number".format(num,result))
Enter an integer : 9
9 is odd number
if-else ladder¶
Usage:¶
if condition_1:
statements_1
elif condition_2:
statements_2
elif condition_3:
statements_3
...
...
...
elif condition_n:
statements_n
else:
statements_last
Note
Python uses elif instead of else if like in C,Java
or C#
Example:¶
In [63]:
response = input("Enter an integer (+ve or -ve) : ")
num = int(response)
if num > 0:
print("{} is +ve".format(num))
elif num == 0:
print("Zero")
else:
print("{} is -ve".format(num))
Enter an integer (+ve or -ve) : -78
-78 is -ve
Note: No switch-case
There is no switch-case structure in Python. It can be realized
using if-else ladder or any other ways
while loop¶
Usage:¶
while condition:
statement_1
statement_2
...
statement_n
Example:¶
In [65]:
response = input("Enter an integer : ")
num = int(response)
prev,current = 0,1
i = 0
while i < num:
prev,current = current,prev + current
print('Fib[{}] = {}'.format(i,current),end=',')
i += 1
Enter an integer : 5
Fib[0] = 1,Fib[1] = 2,Fib[2] = 3,Fib[3] = 5,Fib[4] = 8,
Note
- Multiple assignments in single statement can be done -
Pythondoesn’t support++and--operators as inC - There is no
do-whileloop in Python
for loop¶
Usage:¶
for object in collection:
do_something_with_object
Notes
Clikefor(init;test;modify)is not supported in Python- Python provides
rangeobject for iterating over numbers
Usage of range object:
x = range(start = 0,stop,step = 1)
now x can be iterated, and it generates numbers including start
excluding stop differing in the steps of step
Example:¶
In [66]:
for i in range(10):
print(i, end=',')
0,1,2,3,4,5,6,7,8,9,
In [67]:
for i in range(2,10,3):
print(i, end=',')
2,5,8,
In [68]:
response = input("Enter an integer : ")
num = int(response)
prev,current = 0,1
for i in range(num):
prev,current = current,prev + current
print('Fib[{}] = {}'.format(i,current),end=',')
Enter an integer : 5
Fib[0] = 1,Fib[1] = 2,Fib[2] = 3,Fib[3] = 5,Fib[4] = 8,
Note
Loop control statements break and continue work in the same way
as they work in C