Python Programming - Mail
Python Programming - Mail
Python Programming
By:
Dr. Vaneeta M
Associate Professor
Dept. of AI & DS
RIT
1
Overview of Syllabus
UNIT 1:
• Functions:
• def Statements with Parameters
• Return Values and return Statements
• Importing Modules
• The None Value
• Keyword Arguments and print()
• Local and Global Scope
• The global Statement
2
Functions
Function is a sub program which consists of set of instructions used to
perform a specific task. A large program is divided into basic building
blocks called function.
4
Functions
Two Main Parts of functions:
1. Function Definition
2. Function Call
Function Definition Example:
Function Definition
def function_name(): def first():
Statement 1 print(“Iam Learning Python Functions”)
Statement 2
Statement # Function Call
first()
Function Call first()
function_name() 5
Functions
There are four types of user-defined functions divided on the basis of arguments they
accept and the value they return:
1. Function with no arguments and no return value.
2. Function with no arguments and a return value.
3. Function with arguments and no return value.
4. Function with arguments and with return value.
6
Functions
1. Function with no arguments and no return value
7
Functions
2. Function with no arguments and with a return value.
#user defined function
def add(): # Definition of add() function
a=10
b=5
sum=a+b
return sum
#main function
result=add() # calling add() function
print(result) 8
Functions
3. Function with arguments and no return value.
def add(a,b): # Definition of add() function with parameters
sum=a+b
print(sum)
9
Functions
4. Function with arguments and with a return value.
def add(a,b): # Definition of add() function
sum=a+b
return sum #return sum
10
Functions
11
Advantages of Functions
• The use of functions makes a program more readable.
• Breaking the code down into smaller functions keeps the
program structured, understandable, and reusable.
• The function can be reused countless times after it is defined.
• Using a function, it is possible to reduce the size of a program
by calling and using the function at different places in the
program.
• Functions help in code modularity, entire code is divided into
separate blocks, each of which is self-contained and
performs a different task. This makes each block
implementation and debugging much easier.
12
Predefined / Built-in Functions
Importing Modules
13
from import Statements
14
The None Value
spam = print('Hello!')
Hello!
print(spam)
None
• Behind the scenes, Python adds return None to the end of
any function definition with no return statement.
• If you use a return statement without a value (that is, just the
return keyword by itself), then None is returned.
16
Keyword Arguments and the print() Function
17
Keyword Arguments and the print() Function
end Keyword in print
for i in range(10):
print(i) for i in range(10):
Output: print(I,end=‘ ‘)
1 Output:
2 123456789
3
4
5
6
7
8
9
18
Keyword Arguments and the print() Function
sep Keyword in print
a=12
b=12
c=2022
print(a,b,c,sep="-")
Output:
12-12-2022
19
Local and Global Scope of variables
20
Local and Global Scope of variables
21
Local and Global Scope of variables
• There is only one global scope, and it is created when your
program begins. When your program terminates, the global scope
is destroyed, and all its variables are forgotten.
• A local scope is created whenever a function is called.
• Any variables assigned in the function exist within the function’s
local scope. When the function returns, the local scope is
destroyed, and these variables are forgotten.
22
Local and Global Scope of variables
def spam():
es = 31337 # es is local variable
spam()
print(es) # es cannot be accessed
24
Local and Global Scope
26
Practice program
What will be the output of the a)x is 50
Changed local x to 2
following Python code? x is now 50
b)x is 50
x = 50 Changed local x to 2
def func(x): x is now 2
print('x is', x)
c) x is 50
x=2 Changed local x to 2
print('Changed local x to', x) x is now 100
func(x) d) None of the mentioned
print('x is now', x)
27
Practice program
a) x is 50
. What will be the output of the following Python Changed global x to 2
code? Value of x is 50
b)
x = 50
def func(): x is 50
global x Changed global x to 2
print('x is', x) Value of x is 2
x=2 c)
print('Changed global x to', x)
func() x is 50
print('Value of x is', x) Changed global x to 50
Value of x is 50
d) None of the mentioned
28