Professional Documents
Culture Documents
Conditional Statemenst
Conditional Statemenst
If conditional statement
Syntax Rule
y
if Conditions conditions have to end
with semicolon
ayyy
Mt
If python statements
can be n number of statements
Indentation
default is
4 white spaces
ex a to 9 10
if a 5 if a 5 and at 2 0
2 1 n 1
4 1 9 1
z nty 2 2 9
Print 2 print Cz
2 2
Since statements do not execute if the condition
is false we have other techniques
Syntax Rules
if conditions
me
Indntation
indentation
5 If if block is executed then else block is
el if in 2 is 1 1 2
elif in 3 n n n 3
Elif in n a l c h m
Syntax Rules
if_Ci
WE
eliffz
y 2
WE
elif Cn
WE
else
WE
1 Here if elif and else keywords are used
2 All the if and if else conditional statements
7
only one set of statements are executed
ex
if number t 2 20
if number to 21 0
if opera t
else
print wrong inputs
Rock ROCK N A
Person 2
Rock Paper
Rock Scissor Person 1
Paper Rock Person 1
a
print person 2 wins the hand
else
print wrong inputs are given
Ternary operations
Ternary operators also known as conditional
Syntax
on true on false
if expression else
statements
statements
shorthand if
use this only when we are evaluating one
if 275
print hi
short hand if
if n 5 print hi
if 275
print hi
20
elif 2 2
print welcome
else
print bye
shorthand
syntax is correct
executed
while loop
To write a while loop we use a keyword while
Syntax space
own at the end
while condition
body
In while
indentation
while condition
we a
ex a l
while a 5
print hi X here a is always 5
so D iterations
instead we have to write
act
while a 5
Print hi
a att
ex 9 10
while a
print Chi
as a 1
ex a in abcd
True
a not in abed
False
or a string
With the for loop we can execute a set of
set etc
to be set beforehand
Syntax
ex n 1 2,3 4
for y in se
Point hi
ext 2 1
for y in se
K abcd
for y in n
Print y
sequential data
lenca y
output
b
function
Range Function In built junction
It is an Inbuilt function which inputs starting
value and ending value and gives out sequential
data type where ending value is not included
range Su Er step step size default 1
starting Ending
valve
this is not
included
value
not included in the
ext range i if sequential data output
I 2 3 4 5 6,7 8 9
Printchi
by default as o
so range EV is range o EV
range Cl Io D
I 2 3 4 5 6 7 8 9
1 10 2
range
1 3 51 7 9
so for y in range 1,10 3
printcy
I
range C 10 1 1
8 7 6 5 4 3 2 1
10 9
so n abedef
for y in range lenca 1 1 1
print
E
C
b
a
Break and continue in Python
Break statement
A break statement is used inside both while
and for loops
keyword
ex a l
while acs
Print hi
if a 3
break
a cat
let
1 5 345 3
hi hi hi of the
1 3X 2 3X 3 3 Engg't
loopbreakas is
of the loop
if y b
break
print Y
ex act
while a 5
hi 2 hi 4 hi 5
ex n abcd
for y in se
if y b
continue
print y
when y b the iteration
g is skipped and
b is not
output
printed
Pas
Dfntionandusage
The pass statement is used as a placeholder
for future code
pass
Code to write a pattern for A using while
loop
AI
while 9 5
if a 1
Print
el if a 2
Print 7
el if a 3
Print
elif a 4
Print
elif a 5
print
azatt
output
code to write a pattern for CA using for loop
print
el if a 2
Print 7
el if a 3
Print
elif a 2 4
Print
elif a 5
print
azatt
output
while loop
using
9
Assignment write code to print alphabets a 2
ex
if 2 24 if 2 4
break continue
print z
print z
I
3
ex n abedef me ab def
for y in se for y in range tenens
if y 5
a
continue b
C
Print NCD d
e
Nested loops
Orin t 9,2
1st outer loop 2nd outer loop
9 2
4 1
1st inner 2nd inner 1st inner 2nd inner
hoop loop loop loop
2 1 2 2 2 1 2 2
for y in range c y
2 in range 1,2Mt
Print izz L
CL C D
output
lil
Zil
3 1
Else in for loop the else keyword in
a for loop specifies a block of code to be
executed when the loop is finished
The else block will not be executed
if the loop is stopped by a break
statement
ex
if n 3
break
Printer
else
Print finally finished
a
else not executed
6
for n in range
printer
else
Print finally finished
O
finally finished
while else with the else statement we can
no longer true
ex
i l
while ICG
printci
its
else
b i
2
3
4
5
i is no longer less than 6
Practice questions of Basic Python
N ab d f if we use ninputC
count D then code becomes
dynamic
for y in ne
count count I
Print count
y
Isthoop 2nd loop 3rd loop 4th loop
2 61 2 3 4 5,6 7,8
Counts 0
for y in e
counts country
print count
36
write a program in which we calculate the
g
product of all the values
N 1 2 3 4 5 6 7 8
Printy
40320
Print Str
Input output
SUBHADRA Subhadra
Cor
Print Str
Input output
SUBHADRA Subhadra
if 9 21 0
Print Str
Input output
Python Python
Q write a python program to convert only
even indened characters of dynamic input string
to lower case Ex PYTHON python
if yo 2 20
Print Str
Input output
Python PYTHON
Q K abcd
y ABCD
a A Ca B c
d D
for 2 in se
for vin y
print Z V
Q 11 a s 1,2 2 2
11 a s I 2 2.2
12 L wi sÉt
is alpha
di
true Yin
for y in if
if stress is alpha c
ez append Y
ja s
join L2
ab
if 01 t
3 93 93 n 1,01 n2 n1tn27
print format
elito1
n1
3 93 93 format ns 01 n2
n2 elito1
print
n2 n1 n2
print 3 93 933 format n 1,01
I C 43 E 31
elif 01
N1 027
print 3193 93 format n 1,01 n2
elifo1 Y
n1 n27
print 3 93 93 format n 1,01 n2
elif 01 K
else
Print wrong inputs
A input Type C for breaking
String
for y in list
2 4 partition o
string string zt
Print string
names names names
Q Given a
string of odd length greater than 9
made of the middle
return a new
string
three characters of a given string
ex mynameissan mei
Output m
Sayn
m
string
for y in se
if y a or y e'or y i'on y o or y n
Continue
else
string string y
Print String
Assignment
Binary Search Algorithm
If a sorted list of elements is given and
the position of particular
we are looking for a
element
Normally we would use a
for loop to look
element wise