(IT20APC301) PYTHON PROGRAMMING
UNIT - I :: 1. Introduction to Python 2. Basics of Python Programming [Link] and Expressions
UNIT - II :: 1. Decision statements 2. Loop control statements 3 . Functions
UNIT - III :: 1. Strings 2. Lists 3. Tuples 4. Sets 5. Dictionaries
UNIT - IV :: 1. Exceptions 2. File Handling 3. Modules
UNIT - V :: 1. Object Oriented Programming 2. Case Study
TEXT BOOKS:
1. Programming and problem solving with Python by Ashok Namdev Kamthane, Amit Ashok Kamthane (2018):
McGraw Hill Education (India) Private Limited.
2. Allen B. Downey, “Think Python”, 2nd edition, SPD/O’Reilly, 2016.
3. Python 3 for Absolute Beginners, Tim Hall and J-P Stacey, Apress.
REFERENCE BOOKS:
1. R. Nageswara Rao, “Core Python Programming”, 2nd edition, Dreamtech Press, 2019.
2. Python Pocket Reference 5ed: Python in Your Pocket, Mark Lutz, 2014.
e-Resources:
[Link]
UNIT-II : FUNCTIONS IN PYTHON
• If a group of statements is repeatedly required then it is not recommended to write
these statements every time separately .
• We have to define these statements as a single unit and we can call that unit any
number of times based on our requirement without rewriting. This unit is called as a
function.
• Definition: A function can be defined as the organized block of reusable code, which
can be called whenever required.
• Python allows us to divide a large program into the basic building blocks known as
functions.
• The function contains the set of programming statements in a block. A function can be
called multiple times to provide reusability and modularity to the Python program.
• The Function helps to programmer to break the program into the smaller part. It
organizes the code very effectively and avoids the repetition of the code. As the
program grows, function makes the program more organized.
• Python provide us various inbuilt functions like range() or print(). Although, the user
can create its functions, which can be called user-defined functions.
.
FUNDUMENTAL CONCEPTS OF THE FUNCTIONS
[Link] of Functions: Python supports 2 types of functions
[Link]-in/pre-defined functions 2. User defined functions.
1. Built in Functions: The functions which are coming along with Python software automatically , are
called built in functions or pre-defined functions.
Eg: id(), type() ,input(), eval(), factorial(),power() etc..
[Link] Defined Functions: The functions which are developed by programmer explicitly according to
problem requirements ,are called user defined functions.
Syntax to create user defined functions: Ex.2 def display(str): # Function definition
def function_name(parameters) : Print(“Hello”,str,”Welcome to Functions”)
""" doc string""" display(“python”) # Function call
----
-----
return value Ex.-3 def add(a,b): # Function definition
c=a+b
While creating functions we can use 2 keywords
return c
1. def (mandatory) 2. return (optional) res=add(10,20) # Function call
Ex1 def display(): # Function definition print(res)
print(“Welcome to Functions”) Output: 30
display() # Function call
FUNDUMENTAL CONCEPTS OF THE FUNCTIONS Conti.
Parameters :
Parameters are inputs to the function. If a function contains parameters, then at the time of
calling, compulsory we should provide values otherwise, we will get error.
def wish(name):
print("Hello",name," Good Morning") # Function Definition
wish(“Rama")
wish(“Gopal") # Function Call
Types of Arguments: There are 4 types of arguments in python.
The variables that are passed at function definition are called formal parameters.
The values that are passed to function call are called actual arguments.
[Link] Arguments:They are called positional arguments. No. of parameters should
match with function call and function definition .
Eg. def add(a,b)
print(a+b)
add(10,20)
The number of arguments and position of arguments must be matched. If we change the
order then result may be changed.
[Link] Arguments [Link](variable length) Arguments:
i)Nomber of parameters may not be equal in both i)In function def-only one argument
function call and function definition . ii)On function call- any no. of arguments
ii)They should be equal at the end of function [Link] display(*marks): #Function definition
parameter print(marks)
Ex1 def display(a,b=20): # Function definition display(60,80,90) # Function call
print(a,b) [Link] Returns: In C,C++ and Java,
display(10) # Function call function can return atmost one value. But
[Link] Arguments:We can pass argument in Python, a function can return any
values by keyword i.e by parameter name. number of values.
i)Parameters may or may not match
ii)Variable name is used [Link] display(a, b):# Function definition
iii)No need to follow position return a,b
res=display(10,20,50,60)#Function call
print(res)
Ex1 def display(a,b,c): # Function definition
print(a,b,c)
display(c=10,b=20,a=40) # Function call
[Link] and Global variables:
The variables which are declared inside a function are called local variables. Local variables are
available only for the function in which we declared it
The variables which are declared outside of function are called global variables. These
variables can be accessed in all functions of that module.
Ex1 def display(): # Function definition a=100 # Global variable
a=100 # Local variable Ex2 def display(): # Function definition
print(a) print(a)
display() # Function call display() # Function call
print(a) print(a)
[Link] Function: We can define by using lambda keyword
Syntax of lambda Function: lambda argument_list : expression
Ex. Add= lambda a,b:a+b
print(add(10,20))
By using Lambda Functions we can write very concise code so that readability of
the program will be improved.
[Link]: A function that calls itself is known as Recursive Function.
Eg: factorial(3) =3*factorial(2)
=3*2*factorial(1)
=3*2*1*factorial(0)
=3*2*1*1
=6
So, factorial(n)= n*factorial(n-1)
The main advantages of recursive functions are:
1. We can reduce length of the code and improves readability
2. We can solve complex problems very easily.
Note: To avoid infinitive recursion, base condition recursive call is must.
Ex.
def fact(n):
if n==0 or n==1:
return 1
else:
return n*fact(n-1)
print(fact(4)) # Function Call
Function Aliasing:
• For the existing function we can give another name,
which is nothing but function aliasing. Eg:
Eg: def wish(name):
print("Good Morning:",name)
def wish(name): greeting=wish
print("Good Morning:",name) greeting(‘Rama')
greeting=wish # Defining alias wish(‘Rama')
del wish
print(id(wish))
#wish('Durga') ==>NameError: name 'wish' is not
print(id(greeting)) defined
greeting(‘Rama') greeting(‘Gopal')
wish(‘Rama') Output :
Good Morning: Rama
Output : Good Morning: Rama
4429336 Good Morning: Gopal
4429336
Good Morning: Rama
Good Morning: Rama
Note: In the above example only one function is available
but we can call that function by using either wish name or
greeting name. If we delete one name still we can access
that function by using alias name
Nested Functions:
• We can define a function inside another function, such type of functions are called Nested functions.
• Eg:
def outer():
print("outer function started")
def inner():
print("inner function execution")
print("outer function calling inner function")
inner()
outer()
#inner() ==>NameError: name 'inner' is not defined
Output:
outer function started
outer function calling inner function
inner function execution
In the above example inner() function is local to outer() function and hence it is not possible to call
directly from outside of outer() function.