PPS UNIT 2 Decision_Making_Statements
PPS UNIT 2 Decision_Making_Statements
Selection/conditional branching
Statements: if, if-else, nested if, if-elif-
else statements .
if (expression):
code to be executed under
the condition statement1
statement2
var1 = 10
var2 = 12
print(“var2 is greater”)
if var1 > var1: #if flase, if block will not not executed
print(“Var1 is greater”)
print(“Other statements”)
If….else Statement
The else statement can be used with the if statement. It
usually contains the code which is to be executed at the
time when the expression in the if statement returns the
FALSE. There can only be one else in the program with every
single if statement
It is optional to use the else statement with if statement it
depends on your condition.
The syntax for If….Else Statement is as:
if (expression): #body of if
statement1
statement2
else: #body of else
statement1
statement2
var1 = 10
var2 = 12
e.g:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Python Nested if..(if within if)
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
O/P:?
e.g:
primes = (2, 3, 5, 7)
for prime in primes:
print(prime)
e.g:
# Program to find the sum of all numbers stored in a list
numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]
sum = 0
for val in numbers:
sum = sum+val
print("The sum is", sum)
o/p?
The range() function in for loop
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, stepsize). step size defaults to
1 if not provided.
Syntax:
for var in range(start, stop, stepsize)
e.g:
for x in range(5):
print(x)
e.g:
for x in range(3,6):
print(x)
e.g:
for x in range(3,16,2):
print(x)
Syntax:
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
e.g:
for x in range(1,3):
for y in range(1,4):
print("Python")
o/p:??
Que:Check given number is prime or not
num=int(input("Enter a number: "))
if num>1:
for i in range(2,num): #from 2 to given no it checks remainder
#whether 0 or not.
if(num % i) == 0:
print(num,"is not a prime number")
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
o/p:
4
4 is not a prime number
else with for and while loop..
A for loop and while loop can have an optional else block as
well. The else part is executed if the items in the sequence
used in loop are exhausts..or after loop terminated.
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
digits = [0, 1, 5]
e.g:
for i in range(0,5):
print(i)
else:
print("No items left.")
pass Loop:
In Python, pass keyword is used to execute nothing; it
means, when we don't want to execute code,
the pass can be used to execute empty.
It is same as the name refers to. It just makes the
control to pass by without executing any code.
If we want to bypass any code pass statement can be
used.
e.g: o/p:
1
for num in range(1,10): 2
if num==6: 3
4
pass 5
else: 7
8
print(num) 9
o/p:
e.g:
for num in [5,6,8]:
if num==6:
pass
print(“at the time pass”,num)
print(num)
o/p:
5
at the time pass 6
6
8
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.
If break statement is inside
a nested loop (loop inside
another loop), break will
terminate the innermost loop.
Syntax :
break
for val in range(1,5):
if val==“3":
break
print(val)
print("The end")
o/p:
1
2
The end
e.g:
for i in range(1,8):
if i==5:
continue;
print("%d"%i);
e.g:
i=0
while i<=3:
print(i)
continue
i=i+1
Lists:
How to create a list?
In Python programming, a list is created by placing all the
items (elements) inside a square bracket [ ], separated by
commas.
It can have any number of items and they may be of different
types (integer, float, string etc.).
It is mutable, so we can add, remove, sort, reverse elements
from list.
We can create list as…
my_list = []
my_list = [1, 2, 3]
my_list = [1, "Hello", 3.4]
nested list:
my_list = [“Tiger", [8, 4, 6], ['a']]
Access elements from a list?
We can use the index operator [] to access an item
in a list.
If there is no element then returns indexError
If index is not integer then returns typeError
e.g
my_list = ['p','r','o','b','e']
print(my_list[0])
print(my_list[4])
OP:
p
e
Negative indexing
Python allows negative indexing for its sequences.
The index of -1 refers to the last item, -2 to the
second last item and so on.
my_list = ['p','r','o','b','e']
print(my_list[-1])
print(my_list[-5])
Output:
e
p
Slice lists in Python:
We can access a range of items in a list by using the
slicing operator:(colon)
my_list = ['p','r','o','g','r','a','m','i','z']
print(my_list[2:5])
print(my_list[:5])
print(my_list[:-5]) #prints 0 to -6
print(my_list[5:])
print(my_list[:])
OP:
['o', 'g', 'r']
['p', 'r', 'o', 'g„, 'r']
['p', 'r', 'o', 'g']
['a', 'm', 'i', 'z']
['p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z']
Add elements to list:
e.g:
odd = [1, 3, 5]
odd.append(7)
print(odd)
odd.extend([9, 11, 13])
print(odd)
# Output:
[1, 3, 5, 7, 9, 11, 13]
[1, 3, 5, 7]
delete or remove elements from a list:
We can delete one or more items from a list using
the keyword del. It can even delete the list
entirely.
del my_list[1:5]
print(my_list)
del my_list
print(my_list)
Output:
['p', 'r', 'b', 'l', 'e', 'm']
['p', 'm']
#Error: List not defined
my_list = ['p','r','o','b','l','e','m']
my_list.remove('p')
print(my_list)
print(my_list.pop(1))
print(my_list)
print(my_list.pop())
print(my_list)
my_list.clear()
print(my_list)
Output:
['r', 'o', 'b', 'l', 'e', 'm']
'o'
['r', 'b', 'l', 'e', 'm']
'm'
['r', 'b', 'l', 'e']
[]
List Methods:
len(): returns number of an elements in list
e.g:
num=[2,5,9]
print(len(num))
OP: 3
Sum: This method will make addition of all the numbers in list
e.g:
num=[2,5,9]
print(sum(num))
OP: 16
all: Returns true if all elements of list are true
num=[2,5,9]
print(all(num))
OP: True
OP: True
creating a dictionary:
We can insert elements using curly braces {} separated
by comma.
An item has a key and the corresponding value
expressed as a pair, key: value.
While values can be of any data type and can repeat,
keys must be of immutable type (string, number and
tuple with immutable elements) and must be unique.
We can create dictionary by following ways:
my_dict = {}