03012025123656 Pm
03012025123656 Pm
Unit-02.1
Python
Operators
Unit-02.2
Conditional and
Looping
Statements
Prof. Shruti R. Maniar
Computer Engineering
Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected]
Looping
Outline
If statement
if-else statement
nested if statement
elif statement
For loop statement
While loop statement
break
continue
pass keywords
Introduction
The statements that help us to control the flow of execution in a program
called control statements.
There are two types of control statements.
Branching Statement :
The statements that help us to select some statements for execution and skip other
statements.
These statements are also called as decision making statements because they
make decision based on some condition and select some statements for execution.
If statement
if-else statement
nested if statement
elif statement
Looping statements:
The statements that help us to execute set of statements repeatedly are called as
looping statements.
For loop statement
Prof.
While loop
Shruti R. statement
Maniar # 2301CS404 (PP) Unit- 2 14
If statement
if statement is written using the if keyword followed by condition and
colon(:) .
Code to execute when the condition is true will be ideally written in the
next line with Indentation (white space).
Python relies on indentation to define scope in the code (Other
programming languages often use curly-brackets for this purpose).
Syntax if statement ends with :
1 if some_condition :
2 # Code to execute when condition is true
Indentation (tab/whitespace) at the
beginning
ifdemo.py Output
1 x = 10 1 X is greater than 5
2
3 if x > 5 :
4 print("X is greater than 5"
)
ifelsedemo.p
y Output
1 x = 3 1 X is less than 5
2
3 if x > 5 :
4 print("X is greater than 5"
5 )
6 else :
print("X is less than 5")
Example2.py Output
1 # Program checks if the number is Odd or Even Enter Number = 4
2 num = int(input("Enter Number=")) Positive number
3 if num % 2 == 0:
4 print("Even number")
5 else:
6 print("Odd number")
ifelifdemo.py Output
1 x = 10 1 X is greater than 5
2
3 if x > 12 :
4 print("X is greater than 12
5 ")
6 elif x > 5 :
7 print("X is greater than 5"
8 )
else :
print("X is less than 5")
fordemo1. fordemo2.
Output : Output :
py py
1 my_list = [1, 2, 3, 4] 1 1 my_list = [1,2,3,4,5,6,7,8,9] 2
2 for list_item in my_list : 2 2 for list_item in my_list : 4
3 print(list_item) 3 3 if list_item % 2 == 0 : 6
4 4 print(list_item) 8
Output Output
1 1
2
3
No Break
Example2.py
Output
1 #series 1 + 4 + 9 + 16 + 25 + 36 + ...n
2 num = int(input("Enter Number = ")) Enter Number = 10
3 sum = 0 Sum= 385
4 for i in range(1,num+1):
5 sum = sum + i**2
6 print("Sum=",sum)
Example4.py
Output
1 #series 1 – 2 + 3 – 4 + 5 – 6 + 7 ... n
2 num = int(input("Enter Number = ")) Enter Number = 10
3 sum = 0 Sum= -5
4 for i in range(1,num+1):
5 if i % 2 == 0:
6 sum = sum - i
7 else:
8 sum = sum + i
9 print("Sum=",sum)
Example2.py
Output
1 #series 1 + 4 + 9 + 16 + 25 + 36 + ...n
2 num = int(input("Enter Number = ")) Enter Number = 10
3 sum = i = 0 Sum= 385
4 while(i<=num):
5 sum = sum + i**2
6 i+=1
7 print("Sum=",sum)
continuedemo
.py Output :
1 for temp in range(5) : 0
2 if temp == 2 : 1
3 continue 3
4
4
5 print(temp)
passdemo.py Output :
1 for temp in range(5) : (nothing)
2 pass
Unit-02.3
Functions
An asterisk (*) is placed before the variable name that will hold the values of all non
keyword variable arguments.
This tuple remains empty if no additional arguments are specified during the function
call
Prof. Shruti R. Maniar # 2301CS404 (PP) Unit- 2 46
Function Arguments(cont.)
Arbitrary positional arguments example
Demo.py Output
1 def add_number(n1,*n): Sum = 2
2 sum = n1 Sum = 40
3 for i in n:
4 sum = sum + i
5 print("Sum = ", sum)
6
7 add_number(2)
8 add_number(10,10,10,10)
Demo.py Output
1 def add_Number(**a): Sum= 7
2 sum = 0 ('a', 4)
3 sum = a['a'] + a['b'] ('b', 3)
4 print("Sum=",sum) ('c', 4)
5 for i in a.items(): {'a': 4, 'b': 3, 'c': 4}
6 print(i)
7 print(a)
8
9 add_Number(a=4,b=3,c=4)
Output Output
Inside Function: Darshan University Inside Function: [1, 2, 3, 4, 5,
Outside Function: Darshan 50]
Outside Function: [1, 2, 3, 4, 5,
50]
x=2 x= [1,2]
printA(x) printList(x)
print("A = ",x) print("List",x)
X a X a
2 2 [1,2]
The parameters are optional, and if supplied they are normally just
comma- separated variable names, that is, positional arguments.
Lambda functions accept all kinds of arguments, just like normal def
function
The expression can not contain branches or loops (although conditional
expressions are allowed), and cannot have a return statement. The result
of a lambda expression is an anonymous function.
When a lambda function is called it returns the result of computing the
Example.py
expression as its
1 sum = lambda result.arg1 +
arg1,arg2:
2 arg2
print("Total",sum(5,5))
Outp
ut
Total 10
Outpu
WithIfElse.py
t
1 # Example of lambda function using if-else 2
2 Max = lambda a, b : a if(a > b) else b
3 print(Max(1, 2))
call.py Outpu
1 # Lambda functions can be Immediately Invoked t
9
2
print((lambda x: x*x)(3))
Outpu
demo1.py
t
1 def addition(n): [1, 4, 9,
2 return n * n 16]
3
4 numbers = [1, 2, 3, 4]
5 result = list(map(addition, numbers))
6 # result = list(map(lambda n:n*n,numbers))
7 print(result)
demo2.py Outpu
1 strdata = ["Darshan","University"] t
[7, 10]
2 lengthdata = list(map(len,strdata))
3 print(lengthdata)
demo2.py Outpu
1 from functools import reduce t
10
2 lista = [1,2,3,4,5,6,7,8,9,10]
3 ans = reduce(lambda a,b : a if a>b else b,lista)
4 Print(ans)