Module II Python Notes
Module II Python Notes
Presented By
S.SENTHIL
AP/CSE
K.RAMAKRISHNAN COLLEGE OF TECHNOLOGY
COURSE OUTCOMES
CO1- Apply the Python fundamentals to solve elementary problems.
CO2-Demonstrate the use of Control Statements and Functions for efficient
program flow and modularity.
CO3-Perform various operations in strings, list and tuple.
CO4- Demonstrate the different methods in Dictionary, Set and Regular
Expression.
CO5-Integrate the knowledge of Python syntax, libraries, and frameworks in
building applications
Module-II
Control Flow and Functions
Conditional statements: if, if-else, if-elif-else
Iterative statements: While, For loop – break - continue –
pass.
Functions: Function Definition and Function Call
Variable scope and Lifetime
Return Statement
Built-in Functions
Recursion
Lambda functions
CDAP-TOPICS & SUBTOPICS
Introduction, Using ‘if’, If…elif…else statement, if…else
Conditional statements
Statement (Without elif), Nested if... else Statements
while Loop, Basics of while Loop, break, continue and pass
Iterative statements, While, For loop –
statements, for Loop, Basics of “for” loop, Nested for loops,
break - continue – pass
When to Use ‘while’ and When to Use ‘for’ Loop
range Function, The Function range([start], stop[, step]),
While, For loop – break - continue – pass Using “in” Operator versus Using range() Function in “for”
Loop
Need for Functions, Basics of Functions, Functions in
Functions: Function Definition and Modules, Calling a Function, Defining your own functions and
Function Call function syntax, Syntax for Writing/Defining Your Function,
Lifetime of Variables
Passing Variables in a Function Call, Function Arguments,
Functions: Function Definition and
Providing Default Arguments or Parameter Values, Passing of
Function Call
Arguments by Position
Variable scope and Lifetime – Return Scope namespace and Lifetime of Variables, Return
Statement Statement
Built-in Functions, Important Built-in Functions in Python,
Recursion, Recursion programs (Factorial of a Number,
Built-in Functions – Recursion
Recursive Function to Find sum of natural numbers,
Recursive Function to Generate Fibonacci Numbers
Lambda functions Lambda Functions, map() Function, filter() Function
Case Study
a. Python program to print whether the given number is even or odd with user input using if – else
statement.
b. Program to determine whether the given number is positive or negative using if-elif and else
statements.
c. Python program to print counting from 1 to 10 using for loop with break statement.
d. Program to add two numbers using function by passing arguments with and without return
statement.
statements in a program
i = 20
if (i < 15):
print("i'm in if Block")
else:
while Loop
Syntax
while expression:
statement(s)
workflow
Example
Output
count = 0; Senthil Singaram
Senthil Singaram
while count < 10: Senthil Singaram
Senthil Singaram
print(“Senthil Singaram") Senthil Singaram
Senthil Singaram
count += 1 Senthil Singaram
Senthil Singaram
Senthil Singaram
Infinite loop
count = 0
while count < 10:
print(“loop never stops…")
#break
Output
The sentence “loop never stops… will be printed on the output
screen infinite times
Difference between for and while
For loop iterates over a sequence
It executes a block of code a predetermined number of times, once for
each item in the sequence.
Syntax:
for items in sequence:
# do something with items
Output:
India
Sri Lanka
China
break Statement
We can stop the loop or take out the execution control from
the loop before its completion.
We stop the further execution of the for loop usually based on
some condition(s).
Syntax:
break
Example
numbers = [5, 9, 7, 13, 8, 1, 11]
for i in numbers:
if i%2 == 0:
print("\n break the loop once it receives even values ")
break
print(i, end=‘ ‘)
5 9 7 13
break the loop once it receives even values
Range() in for loop:
Range() function returns a sequence of numbers, in a given
range.
The most common use of it is to iterate sequences.
Types:
• range (stop) -takes one argument.
• range (start, stop) -takes two arguments.
• range (start, stop, step) -takes three arguments.
range (stop)
for i in range(6):
print(i)
print()
o/p:
012345
range (start, stop)
o/p:
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
range (start, stop, step)
o/p:
02468
Continue
Continue Statement skips the execution of the program
block after the continue statement
Also control to start the next iteration.
EX
for i in range(0,10):
if i==5:
continue
print(i)
pass
pass statement is a null statement
Ex1
for i in range(10):
if i==5:
pass
print(i)
Ex2:
i=0
if ==5:
pass
Functions: Function Definition and Function Call
Function Arguments,
Syntax:
def fun():
print(“welcome to python training session")
Function calling
Function must be able to call by using the name of the
function followed by parenthesis with/without
parameters of that particular function.
def fun():
print(“welcome to python class")
fun()
Function arguments
Arguments are the values passed inside the parenthesis of the
function.
A function can have any number of arguments separated by a
comma.
def posorneg(x):
if x<0:
print("+ve")
else:
print("-ve")
n=int(input("N:"))
print(posorneg(n))
Default Arguments parameter value
It is parameter that assumes a default value if a value is not
provided in the function call for that argument.
Passing of Arguments by Position
Syntax:
lambda arguments : expression