Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Python Programming - Mail

The output will be: x is 50 Changed local x to 2 x is now 50 The variable x inside the function func refers to the local variable x which is passed as a parameter. Changing the local x inside the function doesn't change the global x.

Uploaded by

johanjojo.10c
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python Programming - Mail

The output will be: x is 50 Changed local x to 2 x is now 50 The variable x inside the function func refers to the local variable x which is passed as a parameter. Changing the local x inside the function doesn't change the global x.

Uploaded by

johanjojo.10c
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Ramaiah Institute of Technology

Department of Artificial Intelligence


and Data Science

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.

Need For Function:


1. When the program is too complex and large they are divided into
parts. Each part is separately coded and combined into single
program. Each subprogram is called as function.
2. Functions are used to avoid rewriting same code again and again in a
program.
3. Function provides code re-usability
4. The length of the program is reduced. 3
Types of Functions

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)

add(10,5) # calling add() function passing arguments


add(39,8)

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

result=add(10,5) # calling add() function accept return value in result


print(result)
print(add(20,8))

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

• Each module is a Python program contains a related group of


functions and constants that can be embedded in your programs.
• math module:
• Has mathematics related functions,
• Example: sqrt(), pow(), tan(),cos() etc..
• Ex:
import math
print(math.pi)
print(math.ceil(2.3))

13
from import Statements

• An alternative form of the import statement is composed


of the from keyword from, followed by the module name,
the import keyword, and a star; for example,
• from math import *.
• With this form of import statement, calls to functions in
random will not need the math prefix.

14
The None Value

• In Python, the None keyword is used to define a null value, or no


value at all.
• None is not the same as 0, False, or an empty string.
• None is a data type of its own (NoneType)
• Other programming languages might call this value null, nil, or
undefined.
• Just like the Boolean True and False values, None must be typed
with a capital N.
• One place where None is used is as the return value of print().
• The print() function displays text on the screen, but it doesn’t need
to return anything. But since all function calls need to evaluate to a
return value, print() returns None.
15
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

• Most arguments are identified by their position in the function call.

• The print() function has the optional parameters keyword:


1. end: By default prints ‘\n’
2. sep: By default has space

• Specify what should be printed at the end of its arguments and


between its arguments (separating them), respectively.

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

• Parameters and variables that are assigned in a called


function are said to exist in that function’s local scope.
• Variables that are assigned outside all functions are
said to exist in the global scope.
• A variable that exists in a local scope is called a local
variable, while a variable that exists in the global scope
is called a global variable.
• A variable must be one or the other; it cannot be both
local and global

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

1. Local Variables Cannot Be Used in the Global Scope


Consider this program, which will cause an error when you run it:

def spam():
es = 31337 # es is local variable
spam()
print(es) # es cannot be accessed

24
Local and Global Scope

2. Local Scopes Cannot Use Variables in Other Local Scopes


A new local scope is created whenever a function is called, including
when a function is called from another function. Consider this program:
def spam():
e = 99
print(e) # It will print e values as 99
print(ham) # It will show an error
def bacon():
ham = 101
e=0
print(e) # It will print e values as 0
spam()
bacon() 25
Local and Global Scope

3. Global Variables Can Be Read from a Local Scope


Consider the following program:
def spam():
print(e) # print e as 42
e = 42 # Global variable
spam()
print(e)

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

You might also like