0% found this document useful (0 votes)
15 views

Module II Python Notes

python notes

Uploaded by

senthil7111
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Module II Python Notes

python notes

Uploaded by

senthil7111
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

CGB1121-PYTHON PROGRAMMING

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.

e. Program to find the factorial of a number using Recursion.


Conditional statements- Introduction

A control structure directs the order of execution of the

statements in a program

A program needs to skip over some statements, execute a

series of statements repetitively, or choose between alternate

sets of statements to execute.


If
 IF is the most simple decision-making statement.
 It is used to decide whether a block of statements or a single
statement will be executed or not.
Syntax:
if condition:
# Statements
Ex:
i = 10
if (i > 15):
print("10 is less than 15")
print("I am Not in if")
If-Else Statement
 If we want to do something else if the condition is false, we
can use the else statement with the if statement to execute a
block of code when the if condition is false.
Syntax:
if (condition):
# Executes this block if the condition is true
else:
# Executes this block if the condition is false
EX

i = 20

if (i < 15):

print("i is smaller than 15")

print("i'm in if Block")

else:

print("i is greater than 15")

print("i'm in else Block")

print("i'm not in if and not in else Block")


Nested-If
we can place an if statement inside another if statement.
Syntax:
if (condition1):
# statements…
if (condition2):
# statements…
# end of if2
# end of if1
Ex.
i = 10
if (i == 10):
if (i < 15):
print("i is smaller than 15")
if (i < 12):
print("i is smaller than 12 too")
else:
print("i is greater than 15")
if-elif-else Ladder
one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the ladder is
bypassed.
If none of the conditions is true, then the final “else” statement will be
executed.
Syntax:
if (condition):
statement
elif (condition):
statement
else:
statement
Ex:
i = 20
if (i == 10):
print("i is 10")
elif (i == 15):
print("i is 15")
elif (i == 20):
print("i is 20")
else:
print("i is not present")
Iterative statements

Basics of while Loop, break, continue and pass statements

while Loop

Basics of “for” loop, Nested for loops,

When to Use ‘while’ and When to Use ‘for’ Loop

break - continue – pass


While
 While loop is used to repeatedly execute a set of statements
until a condition is met.
 When the condition becomes false, the line immediately after
the loop is performed.
 While loop is classified as an indefinite iteration.

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

 while loop executes a block of code until a specified condition is true.


 It repeats a block of code until a certain condition is met.
 Syntax:
while condition:
# do something
For loop
 for loop is used to Iterating over a sequence using (a list, a
tuple, a dictionary, a set, or a string).
Syntax:
for val in sequence:
…….loop body
for
Ex
nations = ['India’, ‘Sri Lanka ‘China']
for x in nations:
print(x)

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)

for i in range(5, 20):


print(i)

o/p:
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
range (start, stop, step)

for i in range(0, 10, 2):


print(i)
print()

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

 Passing Variables in a Function Call,

Function Arguments,

Providing Default Arguments or Parameter Values,

Passing of Arguments by Position


Function Definition
 Functions is a block of statements that return the result of a
specific task.
 Function can be defined using the def keyword.

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

 In function call if the first argument/value is assigned to name


& the second argument/value is assigned to age.
 By changing the order of the positions, the values can be used
in the wrong places.

def details_fun(name, age):


print(“Name: ", name)
print(“Age:", age)
details_fun(“swetha",31)
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
Contd…
Namespaces:

 A namespace is a system that assigns a unique name to each object (such as


a variable or a method)
 The lifetime of a namespace depends upon the scope of objects, if the scope of an
object ends, the lifetime of that namespace comes to an end.
# var1 is in the global namespace
var1 = 5
def func1():
var2 = 6 # var2 is in the local namespace
def func2():
var3 = 7 # var3 is in the nested local namespace
Lifetime of variable
 Scope refers to the coding region from which a
particular Python object is accessible.
def some_func():
print("Inside some_func")
def some_inner_func():
var = 10
print("Inside inner function, value of var:",var)
some_inner_func()
print("Try printing var from outer function: ",var)
some_func()
Return

A return statement is used to end the execution of the


function call
 “returns” the result (value of the expression following
the return keyword) to the caller.
Function in module

 A module can define functions, classes, and variables.


 A module can also include runnable code.
Lamda function
 A lambda function is a small anonymous function.
 A lambda function can take any number of arguments,
but can only have one expression.

Syntax:
 lambda arguments : expression

You might also like