Skip to main content

Functions & Modules


Functions:
A group of statements into a single logical unit is called as 
function.
Functions are used to perform the task.
Function is not called automatically.
Function body is executed whenever we call the function
Function can be called any no. of times.

Syntax:
def function_name():
       =============
       =============

Advantages of Functions:
1) Modularity
2) Reusability

Example:
def display():
      print("Welcome")
display()
display()
display()

Functions are divided into 4 categories:
1) Functions with arguments with return value.
2) Functions with arguments and without return value.
3) Functions without arguments and with return value.
4) Functions without arguments and without return value.

Examples:
def add(a, b):
       return a+b
def sub(a, b):
       print(a-b)
def mul():
       a=5
       b=10
       return a*b
def div():
       a=5
       b=10
       print(a//b) 

Parameters:
The variables that are declared in a function declaration is 
called parameters.
Parameters of a function can be accessed with in the same 
function only.

Python supports two types of parameters:
1) Non default parameters
2) Default parameters

Non default parameters:
The parameters that are declared without assigning  values are 
called non default parameters.
At the time of calling a function we should pass values of the
non default parameters of the function.

Example:
def add(a, b):
      c=a+b
      print(c)
add(10, 20)
add(30, 83)

Default Parameters:
The parameters that are declared by assigning values in a 
function declaration are called default parameters.
At the time of calling a function, we need not to pass the values.

Example:
def add(a=10, b=20):
      c=a+b
      print(c)
add()
add(30)
add(40, 50)

Arguments:
The values that are passed to a function at the time of calling 
a function are called arguments.

Types of arguments:
1) Non keyword arguments
2) Keyword arguments
3) Arbitrary arguments

Non keyword Arguments:
The arguments that are passed without assigning to variables
are called non keyword arguments.

Example:
def sub(a, b):
     print(a-b)
sub(10, 2)
sub(2, 10)

Keyword arguments:
The arguments that are passed with assigning to variables
are called non keyword arguments.

Example:
def sub(a, b):
     print(a-b)
sub(a=10, b=2)
sub(b=2, a=10)

Arbitrary arguments:
The arguments that are prefixed with * are called arbitrary
arguments.
Arbitrary arguments are by default tuple type.
It allows to pass 0 to any number of arguments to a function.

Example:
def add(*a):
      for i in a:
           print(i)
add()
add(10)
add(33, 45)
add(31, 13, 54)

Lambda Function (or) Anonymous Function:
A function that has no name is known as lambda function or
anonymous function.

Syntax:      variable=lambda arguments : expression

Examples:
1) fun=lambda a : a*a*a
    b=fun(10)
    print(b)

2) add=lambda a, b : print(a+b)
    add(10, 20)

Modules:
In python, every file treated as a module.
A module can contains variables, functions, classes,.. etc.,
Python supports two types of import statements to access
modules data.
1) Normal import
2) From import

1) Normal import: 
In normal import we can access variables and functions by 
using module name

Example:
test.py
a=10
def add(a, b):
     print(a+b)

demo.py
import test
print(test.a)
test.add(10, 20)

In normal import, instead of module name we can use any
alias name also


Example:
test.py
a=10
def add(a, b):
     print(a+b)

demo.py
import test as t
print(t.a)
t.add(10, 20)

2) From import: 
In from import we can access variables and functions directly

Example:
test.py
a=10
def add(a, b):
     print(a+b)

demo.py
from test import a, add
print(a)
add(10, 20)

In from import, we can import all variable and functions by
using * symbol


Example:
test.py
a=10
def add(a, b):
     print(a+b)

demo.py
from test import *
print(a)
add(10, 20)

Module search path:
By default python interpreter will search in the following
locations:
1) Current directory
2) sys.path
3) pythonpath variable in Environment variables.

Packages:
A package is a folder or directory, which contains modules.
A package can also contain sub packages.
To access modules of package, we use packagename.modulename

Popular posts from this blog

Control Flow Statements

Control Flow Statements are divided into three categories in Python: 1) Decision Making Statements 2) Iteration Statements(Loops) 3) Jump Statements 1) Decision Making Statements: Decision making statements contain conditions. If the condition is true then a set of statements executed and if the condition is false then another set of statements are executed. Decision making statements are also called selection statements. i) if Statement ii) if else Statement iii) if elif ....... else Statement iv) Nested if Statement 2) Iteration Statements(Loops): A set of statements executed repeatedly until the condition becomes false is called as iterative statement or loop. i) while loop ii) while loop with else block iii) for loop iv) for loop with else block v) Nested loops 3) Jump Statements: Jump statements are used to terminate the loop or to skip the part of a loop. i) break Statement ii) continue Statement iii) pass Statement Assignment4: ...
Core Python Advanced Python By Venkatesh Mansani venkatesh.mansani@yahoo.com

Python Fundamentals

Python: Python is an interpreted, high level, general purpose programming language. Differences between compiler and interpreter 1) Compiler converts whole program at a time where as Interpreter conver line by line 2) Compiler generates a file where as Interpreter does not generates a file 3) Compiler is fast where as Interpreter is slow Both Compiler and Interpreter are translation softwares. Whenever we run python program, internally source code converted into byte code by python compiler, byte code converted into bit code by python virtual machine and bit code executed under operating system to get the output. Python supports scripting, structured programming, modular programming and Object Oriented Programming. Applications of python: 1) Artificial Intelligence & Machine Learning Applications 2) Data Science & Data Visualization Applications 3) Web Scrapping Applications 4) Scientific & Numeric Applications 5) IOT(Internet Of Things) Applic...