Functions In python
Functions In python
Functions in Python
Functions provide a systematic way of problem solving by dividing the given
problem into several sub-problems, finding their individual solutions, and
integrating the solutions of individual problems to solve the original problem.
Advantages :
Program handling becomes easier
Built-in functions :
Built-in functions are predefined functions that are already available in python
in the standard library and we don’t have to import any module(file) for using
them.
A) Type conversion functions:
B) Mathematical functions :
Functions in Python 1
abs() : Returns the absolute value of a number.
>>> x=9
>>> type(x)
<class 'int'> #returns integer for x=9
>>> type(type(x))
<class 'type'>
Modules :
A module in Python is a file containing Python definitions and statements. It
allows you to organize related code into a single unit, which can then be
imported and used in other Python programs. Modules help in breaking down
large programs into small manageable and organized files.
Functions in Python 2
from <module_name> import <objects>
#examples
from math import *
import math
import math
math.sqrt(-1) #gives an error
Module functions:
A) Math module functions:
floor(x): Returns the largest integer less than or equal to a given number.
Functions in Python 3
random(): Returns a random float between 0 and 1.
1 is excluded : [0.0 , 1.0)
User-defined functions :
User-defined functions are custom functions created by programmers to
perform specific tasks, allowing for code reusability and modularity in Python
programs.
Syntax:
def function_name(comma_seperated_list_of_parameters):
"""Doc string"""
statement 1
statements2
Statements below def begin with four spaces. This is called indentation.
Note : A function that does not contain a return statement is called void
function.
Functions in Python 4
→ Void functions returns a value called None if we try to assign the function to
some variable.
Return statements :
return statement in function is used to end the execution of the function call
and also specifies what value is to be returned back to the calling function..
→ Python the supports returning of multiple values.
def add_sub(x,y):
add=x+y
sub=x-y
sum,difference=add_sub(10,20)
Types of arguments:
Positional arguments :
They are arguments that are passed in to the function in the correct
positional order, i.e , in the same order as in the function header.
They are also known as required arguments.
Default arguments :
Functions in Python 5
def greet_msg(msg="good morning"):
print(msg)
greet()
greet("hi bye")
Keyword arguments :
This allows to call functions with arguments in any order using the name of
the arguments. They can be placed in any order in the function call.
Advantages :
Variable-length arguments :
def sum(*n):
total=0
for i in n:
total+=i
print(total)
sum() #returns 0
sum(20,30) #returns 50
Functions in Python 6
sum(10,20,30,40) #returns 100
def sum(**n):
print(n) #{'n': (10, 20, 30, 40)}
total=0
for i in n["n"]:
total+=i
print(total)
Scope of variables :
Scope of a variable refers to part of the program where it is visible i.e, area
where we can refer (use) it.
Global variables:
Functions in Python 7
Global variables are variables defined outside of any function or class,
accessible from anywhere in the program.
Names assigned at the top level of a module, i.e , outside of any function or
directly in the interpreter.
Life time of global variable : entire program run (i.e they live in memory as
long as the program is running)
Local variables:
Local variables are variables defined within a function and are only accessible
within that function's scope.
Life time is their function’s run (i.e as long as their function is being
executed)
Flow of execution :
It can be defined as the order in which the statements in a program are
executed. The python interpreter starts executing the instructions in a program
from the first statement. The statements are executed one by one, in order of
appearance from top to bottom.
→ If a def statement is encountered all other statements of the functions are
skipped but the function header is interpreted to check if it’s valid.
→ If a function call is encountered the statements in the called function are
executed from top-bottom.
LEGB rule :
The LEGB rule defines the order in which Python looks up variable names. It
stands for Local, Enclosing, Global, and Built-in. This rule determines the scope
of variables and how they are resolved in a Python program.
Functions in Python 8
Then checks the Global environment
Illustration :
def outside():
pi=20 #enclosing scope
def inside():
pi=30 #local scope
return pi
return inside()
Functions in Python 9