Functions
In
Python
Learning Objectives
● Explain how to define and write functions
● Illustrate how to call functions
● Analyze how to pass different arguments to
functions
● Demonstrate how to return a value from
functions
Computer Science Department, UET Lahore.
Working Example
Write a Python Program to calculate the mean of five
numbers input by user.
Formula for calculating the mean is:
For Example:
Computer Science Department, UET Lahore.
Input: 1, 2, 3, 4
Output: 2.5
Mean = {Sum of numbers} ÷ {Total numbers}
Working Example: Solution
Computer Science Department, UET Lahore.
add = 0
for x in range(0,5,1):
num = int(input(“Enter Number: ”))
add = add + num
print(“Mean: ”, add/5)
Output:
Enter Number: 1
Enter Number: 2
Enter Number: 3
Enter Number: 4
Enter Number: 5
Mean: 3.0
To calculate the mean,
We can use this method
Is there any better Solution?
Computer Science Department, UET Lahore.
add = 0
for x in range(0,5,1):
num = int(input(“Enter Number: ”))
add = add + num
print(“Mean: ”, add/5)
Output:
Enter Number: 1
Enter Number: 2
Enter Number: 3
Enter Number: 4
Enter Number: 5
Mean: 3.0
To calculate the mean,
We can use this method
Is there any better Solution?
Computer Science Department, UET Lahore.
● Better solution in terms of readability and
simplicity.
● Yes, there is.
● But before moving to the solution lets take a real
life example of Automobile factory.
Automobiles in Factory
● How do the automobiles are created in the
factory?
Computer Science Department, UET Lahore.
Components of Automobiles
● Wheels, Brakes, Engine, Steering Wheels etc are
the components of the car.
Computer Science Department, UET Lahore.
Automobiles in Factory
● Wheels, Brakes, Engine, Steering Wheels are
manufactured separately and they are assembled
in the automobile factory.
Computer Science Department, UET Lahore.
Functions
● Functions are like short programs that can take
input, process on it, and may return the results
● Functions are like building blocks. They let you
divide complicated programs into manageable
pieces
Computer Science Department, UET Lahore.
Functions: How to Create?
● Functions are defined using “def” keyword in python.
● Parameters are inputs to the function and they are
separated by commas.
● “return” is the keyword for returning the output
● Example:
Computer Science Department, UET Lahore.
Functions: How to Call?
● Functions are called by writing the name of the
function and passing the parameters to the
functions.
● Output of the function is received in a new
variable.
● Example:
Computer Science Department, UET Lahore.
Functions: Previous Example
Computer Science Department, UET Lahore.
Let’s Use Functions to solve the same problem.
Functions: Previous Example
Computer Science Department, UET Lahore.
def calculate_sum():
add = 0
for x in range(0,5,1):
num = int(input("Enter Number: "))
add = add + num
return add
To calculate the sum,
We can use this
function
def calculate_avg(sum):
avg = sum/5
return avg
To calculate the average,
We can use this method
sum = calculate_sum()
print("Mean: ", calculate_avg(sum))
The rest of program
would look like this.
User-Defined Functions:
● These Functions are called user-defined functions
because they are defined by the users.
● For the program’s simplicity and more readability,
users can define as many functions as they want.
Computer Science Department, UET Lahore.
User-Defined Functions: Benefits
● Functions make the code reusable. We can declare
them once and use them multiple times.
● Functions make the program easier as each small
task is divided into a function.
● Functions increase readability.
Computer Science Department, UET Lahore.
Two-types of Functions:
● User-Defined Functions
● Pre-Defined (Library) Functions
Computer Science Department, UET Lahore.
Pre-Defined (Library) Functions:
● Library functions are the built-in functions in
Python programming.
● Programmers can use library functions by invoking
the functions directly; they don't need to write
the functions themselves.
Computer Science Department, UET Lahore.
Functions: Previous Example
Computer Science Department, UET Lahore.
Instead of writing this complete code, Python library (statistics) has
already written this code.
Pre-Defined Functions: Extended
Computer Science Department, UET Lahore.
import statistics
my_list = []
for x in range(0,5,1):
y = int(input("Enter Number: "))
my_list.append(y)
mean = float(statistics.mean(my_list))
print("Mean: ", mean)
Output:
Enter Number: 1
Enter Number: 2
Enter Number: 3
Enter Number: 4
Enter Number: 5
Mean: 3.0
To calculate the mean,
We can use Pre-defined Functions
Don’t Worry. We will learn
about Lists in Coming Lectures.
Built-in Functions: Python Standard library
Computer Science Department, UET Lahore.
Built-in Functions: with importing math library
Computer Science Department, UET Lahore.
● import maths
Built-in Functions: with importing math library
Computer Science Department, UET Lahore.
● For Example:
Learning Objective
In this lecture, we learnt about functions,
how to define functions with different
parameters, differentiate between built-in
and user-defined functions.
Computer Science Department, UET Lahore.
Conclusion
● A function is a block of organized, reusable code
that is used to perform a single, related action.
● Functions are of 2 types
○ Built-in Functions User-defined
Functions
● Functions can have zero or more parameters.
● Syntax to define and call Functions is as follows:
Computer Science Department, UET Lahore.
Take Home Tasks
1. Create a function that takes the age in years and returns
the age in days.
Note:
● Use 365 days as the length of a year for this challenge.
● Ignore leap years and days between last birthday and now.
● Expect only positive integer inputs.
Examples:
● calcAge(65) ➞ 23725
● calcAge(0) ➞ 0
● calcAge(20) ➞ 7300
Computer Science Department, UET Lahore.

Updated Week 06 and 07 Functions In Python.pptx

  • 1.
  • 2.
    Learning Objectives ● Explainhow to define and write functions ● Illustrate how to call functions ● Analyze how to pass different arguments to functions ● Demonstrate how to return a value from functions Computer Science Department, UET Lahore.
  • 3.
    Working Example Write aPython Program to calculate the mean of five numbers input by user. Formula for calculating the mean is: For Example: Computer Science Department, UET Lahore. Input: 1, 2, 3, 4 Output: 2.5 Mean = {Sum of numbers} ÷ {Total numbers}
  • 4.
    Working Example: Solution ComputerScience Department, UET Lahore. add = 0 for x in range(0,5,1): num = int(input(“Enter Number: ”)) add = add + num print(“Mean: ”, add/5) Output: Enter Number: 1 Enter Number: 2 Enter Number: 3 Enter Number: 4 Enter Number: 5 Mean: 3.0 To calculate the mean, We can use this method
  • 5.
    Is there anybetter Solution? Computer Science Department, UET Lahore. add = 0 for x in range(0,5,1): num = int(input(“Enter Number: ”)) add = add + num print(“Mean: ”, add/5) Output: Enter Number: 1 Enter Number: 2 Enter Number: 3 Enter Number: 4 Enter Number: 5 Mean: 3.0 To calculate the mean, We can use this method
  • 6.
    Is there anybetter Solution? Computer Science Department, UET Lahore. ● Better solution in terms of readability and simplicity. ● Yes, there is. ● But before moving to the solution lets take a real life example of Automobile factory.
  • 7.
    Automobiles in Factory ●How do the automobiles are created in the factory? Computer Science Department, UET Lahore.
  • 8.
    Components of Automobiles ●Wheels, Brakes, Engine, Steering Wheels etc are the components of the car. Computer Science Department, UET Lahore.
  • 9.
    Automobiles in Factory ●Wheels, Brakes, Engine, Steering Wheels are manufactured separately and they are assembled in the automobile factory. Computer Science Department, UET Lahore.
  • 10.
    Functions ● Functions arelike short programs that can take input, process on it, and may return the results ● Functions are like building blocks. They let you divide complicated programs into manageable pieces Computer Science Department, UET Lahore.
  • 11.
    Functions: How toCreate? ● Functions are defined using “def” keyword in python. ● Parameters are inputs to the function and they are separated by commas. ● “return” is the keyword for returning the output ● Example: Computer Science Department, UET Lahore.
  • 12.
    Functions: How toCall? ● Functions are called by writing the name of the function and passing the parameters to the functions. ● Output of the function is received in a new variable. ● Example: Computer Science Department, UET Lahore.
  • 13.
    Functions: Previous Example ComputerScience Department, UET Lahore. Let’s Use Functions to solve the same problem.
  • 14.
    Functions: Previous Example ComputerScience Department, UET Lahore. def calculate_sum(): add = 0 for x in range(0,5,1): num = int(input("Enter Number: ")) add = add + num return add To calculate the sum, We can use this function def calculate_avg(sum): avg = sum/5 return avg To calculate the average, We can use this method sum = calculate_sum() print("Mean: ", calculate_avg(sum)) The rest of program would look like this.
  • 15.
    User-Defined Functions: ● TheseFunctions are called user-defined functions because they are defined by the users. ● For the program’s simplicity and more readability, users can define as many functions as they want. Computer Science Department, UET Lahore.
  • 16.
    User-Defined Functions: Benefits ●Functions make the code reusable. We can declare them once and use them multiple times. ● Functions make the program easier as each small task is divided into a function. ● Functions increase readability. Computer Science Department, UET Lahore.
  • 17.
    Two-types of Functions: ●User-Defined Functions ● Pre-Defined (Library) Functions Computer Science Department, UET Lahore.
  • 18.
    Pre-Defined (Library) Functions: ●Library functions are the built-in functions in Python programming. ● Programmers can use library functions by invoking the functions directly; they don't need to write the functions themselves. Computer Science Department, UET Lahore.
  • 19.
    Functions: Previous Example ComputerScience Department, UET Lahore. Instead of writing this complete code, Python library (statistics) has already written this code.
  • 20.
    Pre-Defined Functions: Extended ComputerScience Department, UET Lahore. import statistics my_list = [] for x in range(0,5,1): y = int(input("Enter Number: ")) my_list.append(y) mean = float(statistics.mean(my_list)) print("Mean: ", mean) Output: Enter Number: 1 Enter Number: 2 Enter Number: 3 Enter Number: 4 Enter Number: 5 Mean: 3.0 To calculate the mean, We can use Pre-defined Functions Don’t Worry. We will learn about Lists in Coming Lectures.
  • 21.
    Built-in Functions: PythonStandard library Computer Science Department, UET Lahore.
  • 22.
    Built-in Functions: withimporting math library Computer Science Department, UET Lahore. ● import maths
  • 23.
    Built-in Functions: withimporting math library Computer Science Department, UET Lahore. ● For Example:
  • 24.
    Learning Objective In thislecture, we learnt about functions, how to define functions with different parameters, differentiate between built-in and user-defined functions. Computer Science Department, UET Lahore.
  • 25.
    Conclusion ● A functionis a block of organized, reusable code that is used to perform a single, related action. ● Functions are of 2 types ○ Built-in Functions User-defined Functions ● Functions can have zero or more parameters. ● Syntax to define and call Functions is as follows: Computer Science Department, UET Lahore.
  • 26.
    Take Home Tasks 1.Create a function that takes the age in years and returns the age in days. Note: ● Use 365 days as the length of a year for this challenge. ● Ignore leap years and days between last birthday and now. ● Expect only positive integer inputs. Examples: ● calcAge(65) ➞ 23725 ● calcAge(0) ➞ 0 ● calcAge(20) ➞ 7300 Computer Science Department, UET Lahore.