0% found this document useful (0 votes)
3 views

Python M2

Uploaded by

akhilpb09
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python M2

Uploaded by

akhilpb09
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Module 2

Python Functions and Lists


FUNCTIONS
• A major purpose of functions is to group code
that gets executed multiple times.

• Functions is a block of statements that runs the


specific task
Types of functions
• function without parameter with no return type
• function with parameter with no return type
• function without parameter with return type
• function with parameter with return type
Syntax for without parameter
• If the value is already assigned inside the
function then passing value is not required

def function_name():
Body of function(statements)
function_call()
Syntax for with parameter
• If you have to pass any value to the function, then
function with parameter is used:
def function_name(parameter1,parameter2):
Body of function(statements)
function_call(argument1,argument2)

Note:
Argument=value
Parameter=variable
Variable=value i.e. Parameter=Argument
Function without parameter
def subjects(): # function definition
print("Python")
print("Web programming")
print("cryptography")
print("Full Stack development")
print("Deep Learning")
subjects() # function call
Function with parameter
def hello(name): #parameter(function definition)
print('Hello ' + name)
hello('Alice') #argument(function call)
hello('Bob') # argument(function call)

Note:
name=‘Alice’ #when hello(‘Alice’) is called
Name=‘Bob’ #when hello(‘Bob’) is called
Function with parameter
def add(num1, num2):
sum = num1 + num2
print(sum)
def subtract(num1, num2):
sub = num1 - num2
print(sub)
def multiply(num1, num2):
mul = num1 * num2
print(mul)
def divide(num1, num2):
div = num1 / num2
print(div)
add(2, 4)
subtract(4, 2)
multiply(3, 2)
divide(6, 2)
RETURN VALUES AND RETURN STATEMENTS
A return statement consists of the following:
• The return keyword
• The value or expression that the function should return

• Syntax
def function_name(parameter1, parameter2):
body of the function
return return_value
function_name(argument1, argument2)
1. Write the function with using parameter with no return type
def add(num1, num2):
sum = num1 + num2
print(sum)
add(2, 4)

RETURN VALUE
2. Write the function with using parameter with return type
def add(num1, num2):
sum = num1 + num2
return sum
print(add(2, 4))
3. Write the function without using
parameter with return type

def get_user_info():
user_name = "John Doe"
return user_name
name = get_user_info()
print(name)
4. Write the program to demonstrate the use of
function to display the details of an employee
using parameter and with return type
• def get_user_info(student1):
user_name = "John Doe"
user_age = 45
return user_name, user_age
name, age = get_user_info(1)
print( name, age)
None Value
• Python adds return None to the end of any
function definition with no return statement

• spam = print('Hello!')
Hello
• None == spam
True
def addition(a, b):
add=a+b
print(add) #outputs 5
print(addition(2, 3)) #outputs None

Output:
5
None
KEYWORD ARGUMENTS AND THE PRINT()
FUNCTION
• print('Hello')
• print('World')

• print('Hello', end='')
print('World')
• print('cats', 'dogs', 'mice')

• print('cats', 'dogs', 'mice', sep='.')


• print('cats', 'dogs', 'mice', sep=‘,)
• print('cats', 'dogs', 'mice', sep=‘:')
• print('cats', 'dogs', 'mice', sep=‘;')
Exception handling
Consider the following program which has a divide-by-zero error
• def spam(divideBy):
return 42 / divideBy
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
• Output:
• 21.0
• 3.5
• ERROR!
• Traceback (most recent call last):
• File "<main.py>", line 7, in <module>
• File "<main.py>", line 3, in spam
• ZeroDivisionError: division by zero
• def spam(divideBy):
try:
return 42 / divideBy
except ZeroDivisionError:
print('Error: Invalid argument.')
print(spam(2))
print(spam(12))
print(spam(0))
print(spam(1))
• Output:
• 21.0
• 3.5
• Error: Invalid argument.
• None
• 42.0

You might also like