02.conditional & Control Flow Statements - Jupyter Notebook
02.conditional & Control Flow Statements - Jupyter Notebook
Control_Flow Pic:
Line1
Line2
Line3
In [2]: 1 print("Line1");print("Line2");print("Line3")
Line1
Line2
Line3
In [1]: 1 n = 101
2 if n>100:
3 print("This is more then 100")
4 print("THis always executes")
In [2]: 1 n = 10
2 if n>100:
3 print("This is more then 100")
4 print("THis always executes")
if .. else Statement:-
An else statement can be combined with an if statement. An else statement contains the block
In [6]: 1 n = 101
2 if n>100:
3 print("This is more then 100")
4 else:
5 print("This is less then 100")
6 print("THis always executes")
In [7]: 1 n = 10
2 if n>100:
3 print("This is more then 100")
4 else:
5 print("This is less then 100")
6 print("THis always executes")
Python if...elif...else:-
The elif statement allows you to check multiple expressions for TRUE and execute a block of
code as soon as one of the conditions evaluates to TRUE.
In [21]: 1 n = 0
2 if n>100:
3 print("This is more then 100")
4 elif n<0:
5 print("This is negative number")
6 elif n>1000:
7 print("This is more then 1000")
8 else:
9 print("Value is zero")
Value is zero
In general nested if-else statement is used when we want to check more than one conditions.
Conditions are executed from top to bottom and check each condition whether it evaluates to
true or not.
In [23]: 1 num = int(input("Enter a number: "))
2 if num >= 0:
3 if (num == 0):
4 print("ZERO")
5 else:
6 print("Positive number")
7 else:
8 print("Negative number")
Enter a number: 0
ZERO
He can vote
What is a Loop?
A loop is a sequence of instructions that is continually repeated until a certain condition is
reached.
Why Loop?
In a loop structure, the loop asks a question. If the answer requires an action, it is executed.
The same question is asked again and again until no further action is required. Each time the
question is asked is called an iteration.
In [31]: 1 s = "PYTHON"
2 s_iter = iter(s)
3 print(next(s_iter))
4 print(next(s_iter))
5 print(next(s_iter))
6 print(next(s_iter))
7 print(next(s_iter))
8 print(next(s_iter))
P
Y
T
H
O
N
2
3
5
7
Range Function
It generates lists containing arithmetic progression. It returns a list of consecutive integers. The
function has one, two or three parameters where last two parameters are optional. It is widely
used in for loops.
range(1, 10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
<class 'range'>
0
1
2
3
4
5
6
7
8
9
In [5]: 1 print(list(range(10)))
2 print(list(range(10,30)))
3 print(list(range(10,30,5)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
29]
[10, 15, 20, 25]
0
1
2
3
4
No items left in list
In [9]: 1 # for with else with break
2 for i in range(5):
3 if i == 4:
4 break;
5 print(i)
6 else:
7 print("No items left in list")
0
1
2
3
NOTE:
if we stop the loop, say with a break statement, then the else suite will not be executed
break
To break out from a loop, you can use the keyword "break".
P
Y
T
H
P
Y
T
H
O
N
Loop Completed
Continue
The continue statement is used to tell Python to skip the rest of the statements in the current
loop block and to continue to the next iteration of the loop.
0
1
2
4
5
Else will execute eveytime in continue
0
1
2
3
4
5
Else will execute eveytime in continue
In [18]: 1 x = 0
2 while x<=5:
3 print(x)
4 x+=1
0
1
2
3
4
5
while and else statement
There is a structural similarity between while and else statement. Both have a block of
statement(s) which is only executed when the condition is true.
In [19]: 1 x=1
2 while x<=5:
3 print(x)
4 x=x+1
5 else:
6 print("loop Finished")
7
1
2
3
4
5
loop Finished
In [20]: 1 a=10
2 while a>0:
3 print("Value of a is",a)
4 a=a-2
5 else:
6 print("Loop is Completed")
7
Value of a is 10
Value of a is 8
Value of a is 6
Value of a is 4
Value of a is 2
Loop is Completed
In [2]: 1 x = 0
2 while x<6:
3 print(x)
4 x+=1
5 else:
6 print("While completed")
0
1
2
3
4
5
While completed
In [3]: 1 x = 0
2 while x<6:
3 if x == 3:
4 break;
5 print(x)
6 x+=1
7 else:
8 print("While completed")
0
1
2
In [ ]: 1 # This will have infinte loops as we have mentioned continue before incre
2 # when poniter reaches 3 it will keep executing continue there is no incr
3 x = 0
4 while x<6:
5 if x == 3:
6 continue;
7 print(x)
8 x+=1
9 else:
10 print("While completed")
0
1
2
In [1]: 1 x = 0
2 while x<6:
3 x+=1
4 if x == 3:
5 continue;
6 print(x)
7 else:
8 print("While completed")
1
2
4
5
6
While completed
Syntax
for [first iterating variable] in [outer loop]: # Outer loop
[do something] # Optional
f [ d it ti i bl ] i [ t dl ] #N t dl
In [26]: 1 for i in range(5):
2 for j in range(5):
3 print(i+j,end=" ")
4 print()
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
PASS KEYWORD
It is used when a statement is required syntactically but you do not want any command or code
to execute.
Why Pass?
It is an empty statement
It is null statement
It results into no operation (NOP)
Input In [35]
^
IndentationError: expected an indented block
In [36]: 1 for i in "PYTHON":
2 pass
Note:
Above we can understand that when we use pass keyword there is no error.
User Input
Python allows for user input.
That means we are able to ask the user for input.
The method is a bit different in Python 3.6 than Python 2.7.
Python 3.6 uses the input() method.
Python 2.7 uses the raw_input() method.
Evaluating expression
Enter expression : 2 + 3 + 5
2 + 3 + 5
In [21]: 1 res = eval(input("Enter expression : "))
2 print(res)
Enter expression : 2 + 3 + 5
10
Output: