PPS Unit 2
PPS Unit 2
PPS Unit 2
• The selection control statements usually jump from one part of code to another
depending on whether a particular condition is satisfied or not.
• It allow us to execute statements selectively (branch wise) based on certain
decisions.
• Python language supports different types of selection/conditional/branching
statements.
1. If statements
2. If....else statements
3. If…elif….else statements
4. Nested if
2.1.1 if
1. If statements:
• If statement is a simplest form of decision control statements that is frequently
used in decision making.
• If statements may include one statement or N statements enclosed within the if
block
• If the expression is true the statements of if block get executed, otherwise these
statements will be skipped and the execution will be jump to statement just
outside if block.
• Syntax:
if (expression/condition):
Block of statements
Statements ouside if
1
Andy@$
PPS Unit-II DIT,Pimpri
• Flowchart:
✓ Example:
a=2
if (a<3):
print(a);
if (a>3):
print('Hi')
Output: 2
2.1.2 if-else
• In simple if statement, test expression is evaluated and if it is true the statements
in if block get executed, But if we want a separate statements to be get executed
if it returns false in this case we have to use if..else control statement.
• if else statement has two blocks of codes, each for if and else.
• The block of code inside if statement is executed if the expression in if statement
is TRUE, else the block of code inside else is executed.
• Syntax:
if (expression/condition):
Block of statements
else:
Block of statements
2
Andy@$
PPS Unit-II DIT,Pimpri
• Flowchart:
✓ Example:
x=5
if (x<10):
print (“Condition is TRUE”)
else:
print (“Condition is FALSE”)
output:
Condition is TRUE'
2.1.3 if-elif-else
• Python supports if..elif..else to test additional conditions apart from the initial test
expression.
• It work just like a if…else statement.
• The programmer can have as many elif statements/branches as he wants depending
on the expressions that have to be tested.
• A series of if..elif statements can have a final else block, which is called if none of
the if or elif expression is true.
• Syntax:
if (expression/condition):
Block of statements
3
Andy@$
PPS Unit-II DIT,Pimpri
elif (expression/condition):
Block of statements
elif (expression/condition):
Block of statements
else:
Block of statements
• Flowchart:
Example:
x=5
if (x == 2):
print (“x is equal to 2”)
elif (x < 2):
print (“x is less than 2”)
else:
print (“x is greater than 2”)
Output:
x is greater than 2
4
Andy@$
PPS Unit-II DIT,Pimpri
2.1.4 nested if
• A statement that contains other statements is called nested/compound statements.
• In nested if else statements, if statement is nested inside an if statements. So, the
nested if statements will be executed only if the expression of main if statement
returns TRUE.
• Syntax:
if (expression/condition):
if (expression/condition):
statements
else:
statements
else:
statements
• Flowchart
• Example
x=5
if (x<=10):
if(x==10):
print (“Number is Ten”)
else:
print (“Number is less than Ten”)
else:
print (“Number is greater than Ten”)
5
Andy@$
PPS Unit-II DIT,Pimpri
• Iterative statements are decision control statements that are used to repeat the
execution of a list of statements.
• Python language supports two types of statements while loop and for loop.
• As shown in the syntax above, in the while loop condition is first tested.
• If the condition is True, only then the statement block will be executed.
• Otherwise, if the condition is False the control will jump to statement y, that is the
immediate statement outside the while loop block.
• Flowchart:
6
Andy@$
PPS Unit-II DIT,Pimpri
• Example:
Program to print numbers from 0 to 10.
i=0
while (i<10):
print(i, end=” “)
i=i+1
Output: 0 1 2 3 4 5 6 7 8 9
• Syntax:
for loop_control_var in sequence:
statement block1
statement x
7
Andy@$
PPS Unit-II DIT,Pimpri
• Flowchart:
• Example:.
Output: Output:
1 2 3 4 1 35 7 9
• If condition is not met in entry-controlled loop, then the loop will never execute.
• However, in case of post-test, the body of the loop is executed unconditionally for
the first time.
• If the requirement is to have a pre-test loop, then choose a for loop or a while loop.
• The table below shows comparison between ore-test loop and post-test loop.
Feature Pre-test Loop Post-test Loop
Initialization 1 2
Number of tests N+1 N
Statements executed N N
Loop control variable update N N
Minimum Iterations 0 1
9
Andy@$
PPS Unit-II DIT,Pimpri
10
Andy@$
PPS Unit-II DIT,Pimpri
print("* ",end="")
print("\r")
Output
*
**
***
****
*****
Example 2:
lastNumber = 6
for row in range(1, lastNumber):
for column in range(1, row + 1):
print(column, end=' ')
print("")
Output
1
12
123
1234
12345
11
Andy@$
PPS Unit-II DIT,Pimpri
• Example 1 (For ‘for loop’): Print numbers from 0 to 10 and print a message when
the loop is ended.
for x in range(11):
print(x)
else:
print(“Loop is finished”)
Output:
if(i=="c"):
break
print(i)
Output:
w
e
l
2.5.2 continue
• Continue statement can appear only in the body of loop.
• When the continue statement encounters, then the rest of the statements in the loop
are skipped
• The control is transferred to the loop-continuation portion of the nearest enclosing
loop.
• Its syntax is simple just type keyword continue as shown below
continue
• Example:
for i in "welcome":
if(i=="c"):
continue
print(i)
Output:
w
e
l
o
m
e
• We can say continue statement forces the next iteration of the loop to take place,
skipping the remaining code below continue statement.
13
Andy@$
PPS Unit-II DIT,Pimpri
while(…): for(…):
… …
if condition: if condition:
continue continue
…. …
……. …
2.5.3 pass
• It is used when a statement is required syntactically but you do not want any command
or code to execute.
• The pass statement is a null operation.
• Nothing happens when it executes.
14
Andy@$
PPS Unit-II DIT,Pimpri
• It is used as placeholder.
• In a program where for some conditions you don’t want to execute any action or what
action to be taken is not yet decided, but we may wish to write some code in future, In
such cases pass can be used.
• Syntax:
Pass
• Example:
for i in “welcome”:
if (i == 'c'):
pass
print (i)
Output:
w
e
l
c
o
m
e
2.6.1 Tuples
Q. What is tuple? Explain how to access, add and remove elements in a tuple
• Tuple is a data structure supported by python.
• A tuple is a sequence of immutable objects. That means you cannot change the
values in a tuple.
• Tuples use parentheses to define its elements.
15
Andy@$
PPS Unit-II DIT,Pimpri
• For example, to access values in tuple, slice operation is used along with the index or
indices to obtain value stored at that index.
• Example:
Tup1=(1,2,3,4,5,6,7,8,9,10)
print(“Tup[3:6]=”,Tup1[3:6])
print(“Tup[:8]=”,Tup1[:4])
print(“Tup[4:]=”, Tup1[4:])
output:
Tup[3:6]=(4,5,6)
Tup[:8]=(1,2,3,4)
Tup[4:]=(5,6,7,8,9,10)
• Tuples are immutable means we cannot add new element into it.
• Also it is not possible to change the value of tuple.
Program:
tup1=(“Kunal”,”Ishita”,”Shree”,”Pari”,“Rahul”)
tup1[5]=”Raj”
print(tup1)
Output:
It will raise an error because new element cannot be added.
• Since tuple is an immutable data structure, you cannot delete value(s) from it.
Program:
16
Andy@$
PPS Unit-II DIT,Pimpri
Tup1=(1,2,3,4,5)
del Tup1[3]
print(Tup1)
output: It will raise error as ‘tuple’ object doesn’t support item deletion.
• However, we can delete the entire tuple by using the “del” statement.
Example:
Tup1=(1,2,3,4,5)
del Tup1
Operations on Tuple
Q. Explain various operations on Tuple.
• Tuple behave like similar way as string when operators like concatenation (+),
and repetition (*) are used and returns new list rather a string.
• Tuple supports various operations as listed below:
17
Andy@$
PPS Unit-II DIT,Pimpri
2.6.2 Lists
Q. What is list? Explain how elements are accessed, added and removed from list.
• List is a data type in python.
• List is a collection of values (items / elements).
• The items in the list can be of different data types.
• The elements of the list are separated by comma (,) and written between square
brackets [].
• List is mutable which means the value of its elements can be changed.
• Syntax:
List = [value1, value2, …]
• Example1:
List1 = [1,2,3,4]
print(List1)
Output:
[1, 2, 3, 4]
• Example 2:
List2 = ['John','PPS',94]
print(List2)
18
Andy@$
PPS Unit-II DIT,Pimpri
Output:
['John','PPS',94]
19
Andy@$
PPS Unit-II DIT,Pimpri
Output:
['Sam', 'PPS', 94, 'FE']
['Sam', 94, 'FE']
[94, 'FE']
['FE']
Operations on list
Q. Explain various operations on list.
• List behave like similar way as string when operators like concatenation (+), and
repetition (*) are used and returns new list rather a string.
• List supports various operations as listed below:
20
Andy@$
PPS Unit-II DIT,Pimpri
2.6.3 Dictionary
Q. What is dictionary? Explain how to create dictionary, access, add and remove
elements from dictionary.
21
Andy@$
PPS Unit-II DIT,Pimpri
➢ Creating Dictionary
• The syntax to create an empty dictionary can be given as:
Dict = { }
• The syntax to create a dictionary with key value pair is:
Dict = {key1: value1, key2: value2, key3: value3}
• Example1:
Dict = { }
Print(Dict)
Output:
{}
• Example2:
Dict = {1: "PPS", 2: "PHY"}
print(Dict)
Output:
{1: 'PPS', 2: 'PHY'}
22
Andy@$
PPS Unit-II DIT,Pimpri
23
Andy@$
PPS Unit-II DIT,Pimpri
Operations on Dictionary:
Method Description
24
Andy@$