python part 3
python part 3
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/if1.py
3 is greater
done
-1 a is smaller
Finish
--------------------------------
a=10
if a>9:
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/if2.py
A is Greater than 9
Alternative if (If-Else):
An else statement can be combined with an if statement. An else statement contains the
block of code (false block) that executes if the conditional expression in the if statement
resolves to 0 or a FALSE value.
The else statement is an optional statement and there could be at most only one else
Statement following if.
Syntax of if - else :
if test expression:
Body of if stmts
else:
Body of else stmts
If - else Flowchart :
37
PYTHON PROGRAMMING III YEAR/II SEM MRCET
Example of if - else:
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/ifelse.py
enter the number 2
a is smaller than the input given
----------------------------------------
a=10
b=20
if a>b:
print("A is Greater than B")
else:
print("B is Greater than A")
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/if2.py
B is Greater than A
38
PYTHON PROGRAMMING III YEAR/II SEM MRCET
The elif statement allows us to check multiple expressions for TRUE and execute a
block of code as soon as one of the conditions evaluates to TRUE. Similar to the else,
the elif statement is optional. However, unlike else, for which there can be at most one
statement, there can be an arbitrary number of elif statements following an if.
If test expression:
Body of if stmts
elif test expression:
Body of elif stmts
else:
Body of else stmts
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/ifelse.py
enter the number5
enter the number2
enter the number9
a is greater
>>>
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/ifelse.py
enter the number2
enter the number5
enter the number9
c is greater
-----------------------------
var = 100
if var == 200:
print("1 - Got a true expression value")
print(var)
elif var == 150:
print("2 - Got a true expression value")
print(var)
elif var == 100:
print("3 - Got a true expression value")
print(var)
else:
print("4 - Got a false expression value")
print(var)
print("Good bye!")
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/ifelif.py
100
Good bye!
40
PYTHON PROGRAMMING III YEAR/II SEM MRCET
Iteration:
A loop statement allows us to execute a statement or group of statements multiple times as
long as the condition is true. Repeated execution of a set of statements with the help of loops
is called iteration.
Loops statements are used when we need to run same code again and again, each time with a
different value.
Statements:
In Python Iteration (Loops) statements are of three types:
1. While Loop
2. For Loop
3. Nested For Loops
While loop:
Loops are either infinite or conditional. Python while loop keeps reiterating a block of
code defined inside it until the desired condition is met.
The while loop contains a boolean expression and the code inside the loop is
repeatedly executed as long as the boolean expression is true.
The statements that are executed inside while can be a single line of code or a block of
multiple statements.
Syntax:
while(expression):
Statement(s)
Flowchart:
41
PYTHON PROGRAMMING III YEAR/II SEM MRCET
Example Programs:
1. --------------------------------------
i=1
while i<=6:
print("Mrcet college")
i=i+1
output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/wh1.py
Mrcet college
Mrcet college
Mrcet college
Mrcet college
Mrcet college
Mrcet college
2. -----------------------------------------------------
i=1
while i<=3:
print("MRCET",end=" ")
j=1
while j<=1:
print("CSE DEPT",end="")
j=j+1
i=i+1
print()
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/wh2.py
3. --------------------------------------------------
i=1
42
PYTHON PROGRAMMING III YEAR/II SEM MRCET
j=1
while i<=3:
print("MRCET",end=" ")
while j<=1:
print("CSE DEPT",end="")
j=j+1
i=i+1
print()
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/wh3.py
4. ----------------------------------------
i=1
while (i < 10):
print (i)
i = i+1
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/wh4.py
1
2
3
4
5
6
7
8
9
2. ---------------------------------------
a=1
b=1
while (a<10):
print ('Iteration',a)
a=a+1
b=b+1
43
PYTHON PROGRAMMING III YEAR/II SEM MRCET
if (b == 4):
break
print ('While loop terminated')
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/wh5.py
Iteration 1
Iteration 2
Iteration 3
While loop terminated
--------------------------
count = 0
while (count < 9):
print("The count is:", count)
count = count + 1
print("Good bye!")
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/wh.py =
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
For loop:
Python for loop is used for repeated execution of a group of statements for the desired
number of times. It iterates over the items of lists, tuples, strings, the dictionaries and other
iterable objects
44
PYTHON PROGRAMMING III YEAR/II SEM MRCET
Sample Program:
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/fr.py
1
4
16
36
121
400
Flowchart:
45
PYTHON PROGRAMMING III YEAR/II SEM MRCET
Iterating over a list:
#list of items
list = ['M','R','C','E','T']
i=1
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/lis.py
college 1 is M
college 2 is R
college 3 is C
college 4 is E
college 5 is T
tuple = (2,3,5,7)
print ('These are the first four prime numbers ')
#Iterating over the tuple
for a in tuple:
print (a)
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/fr3.py
These are the first four prime numbers
2
3
5
7
#creating a dictionary
college = {"ces":"block1","it":"block2","ece":"block3"}
46
PYTHON PROGRAMMING III YEAR/II SEM MRCET
for keys in college:
print (keys)
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/dic.py
Keys are:
ces
it
ece
Values are:
block1
block2
block3
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/strr.py
M
R
C
E
T
for i in range(1,6):
for j in range(5,i-1,-1):
print('')
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/nesforr.py
Output:
11111
2222
333
44
48
PYTHON PROGRAMMING III YEAR/II SEM MRCET
Break and continue:
In Python, break and continue statements can alter the flow of a normal loop. Sometimes
we wish to terminate the current iteration or even the whole loop without checking test
expression. The break and continue statements are used in these cases.
Break:
The break statement terminates the loop containing it and 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.
Flowchart:
The following shows the working of break statement in for and while loop:
49
PYTHON PROGRAMMING III YEAR/II SEM MRCET
while test expression
Example:
print("The end")
Output:
M
R
C
E
T
The end
Output:
11
9
88
The number 88 is found
50
PYTHON PROGRAMMING III YEAR/II SEM MRCET
Terminating the loop
#-------------------------------------
for letter in "Python": # First Example
if letter == "h":
break
print("Current Letter :", letter )
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/br.py =
Current Letter : P
Current Letter : y
Current Letter : t
Continue:
The continue statement is used to skip the rest of the code inside a loop for the current
iteration only. Loop does not terminate but continues on with the next iteration.
Flowchart:
The following shows the working of break statement in for and while loop:
51
PYTHON PROGRAMMING III YEAR/II SEM MRCET
for var in sequence:
# code inside for loop
If condition:
continue (if break condition satisfies it jumps to outside loop)
# code inside for loop
# code outside for loop
Example:
print("The end")
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/cont.py
s
t
r
n
g
The end
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/cont2.py
11
9
89
#------------------
for letter in "Python": # First Example
if letter == "h":
continue
print("Current Letter :", letter)
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/con1.py
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Pass:
Example:
sequence = {'p', 'a', 's', 's'}
for val in sequence:
pass
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/f1.y.py
53
PYTHON PROGRAMMING III YEAR/II SEM MRCET
>>>
54
PYTHON PROGRAMMING III YEAR/II SEM MRCET
UNIT – III
FUNCTIONS, ARRAYS
Fruitful functions: return values, parameters, local and global scope, function composition,
recursion; Strings: string slices, immutability, string functions and methods, string module;
Python arrays, Access the Elements of an Array, array methods.
Functions, Arrays:
Fruitful functions:
We write functions that return values, which we will call fruitful functions. We have seen
the return statement before, but in a fruitful function the return statement includes a return
value. This statement means: "Return immediately from this function and use the following
expression as a return value."
(or)
Any function that returns a value is called Fruitful function. A Function that does not return
a value is called a void function
Return values:
The Keyword return is used to return back the value to the called function.
def area(radius):
temp = 3.14 * radius**2
return temp
print(area(4))
(or)
def area(radius):
return 3.14 * radius**2
print(area(2))
def absolute_value(x):
if x < 0:
55
PYTHON PROGRAMMING III YEAR/II SEM MRCET
return -x
else:
return x
Since these return statements are in an alternative conditional, only one will be executed.
As soon as a return statement executes, the function terminates without executing any
subsequent statements. Code that appears after a return statement, or any other place the
flow of execution can never reach, is called dead code.
In a fruitful function, it is a good idea to ensure that every possible path through the program
hits a return statement. For example:
def absolute_value(x):
if x < 0:
return -x
if x > 0:
return x
This function is incorrect because if x happens to be 0, both conditions is true, and the
function ends without hitting a return statement. If the flow of execution gets to the end of a
function, the return value is None, which is not the absolute value of 0.
By the way, Python provides a built-in function called abs that computes absolute values.
# Write a Python function that takes two lists and returns True if they have at least one
common member.
Output:
C:\Users\MRCET\AppData\Local\Programs\Python\Python38-32\pyyy\fu1.py
56
PYTHON PROGRAMMING III YEAR/II SEM MRCET
True
True
None
#-----------------
def area(radius):
b = 3.14159 * radius**2
return b
Parameters:
Parameters are passed during the definition of function while Arguments are passed during
the function call.
Example:
#here a and b are parameters
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/paraarg.py
25
#function defination
def display():
print("vandemataram")
print("i am in main")
57