0% found this document useful (0 votes)
62 views22 pages

Python Function

The document discusses Python functions. It defines what functions are in Python and their key aspects like parameters, arguments, defining and calling functions. It also covers function return values, arbitrary arguments, keyword arguments, passing lists to functions, recursion, and provides examples of writing functions to find max of 3 numbers, sum/multiply a list, reverse a string, calculate factorial, check if a number is in range, and count upper/lower case letters in a string. The document aims to demonstrate how to write and use functions in Python.

Uploaded by

elezear repil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
62 views22 pages

Python Function

The document discusses Python functions. It defines what functions are in Python and their key aspects like parameters, arguments, defining and calling functions. It also covers function return values, arbitrary arguments, keyword arguments, passing lists to functions, recursion, and provides examples of writing functions to find max of 3 numbers, sum/multiply a list, reverse a string, calculate factorial, check if a number is in range, and count upper/lower case letters in a string. The document aims to demonstrate how to write and use functions in Python.

Uploaded by

elezear repil
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 22

Python Functions

Activity: Loop

Provide one example each for a for loop and a while loop, and
identify the logic behind each loop. Alternatively, you may use the
problem below to demonstrate both types of loops.
• Write a program that uses a loop to print numbers from 1 to N,
where N is an input positive integer.
• Write a program in python to display the cube of the number up
to an input integer value.
• Count and print the number of consonants and vowel in a given
string using a loop.
• Write a program in python to display a pattern like a right angle
triangle using an asterisk.
• Write a program in python to display the n terms of a harmonic
series and their sum.
Python Functions

Elezear Repil
Learning Objectives:

• Write and declare a function in Python using the def keyword.


• Call the function with appropriate parameters to perform a specific
task.
• Differentiate between parameters and arguments.
• Demonstrate how a function enhances code modularity and
reusability.
Functions
• A function is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a function.
• A function can return data as a result.
• They are defined using the def keyword, followed by the function
name, parentheses, and a colon.
The syntax to declare a function is:
Creating a Function

In Python a function is defined using


the def keyword:
Calling a Function

To call a function, use the function name followed by parenthesis:


Parameter

• A parameter is the variable defined within the


parentheses during function definition. Simply they
are written when we declare a function.
Arguments

• Information can be passed into functions as arguments.


• Arguments are specified after the function name, inside the
parentheses.
• You can add as many arguments as you want, just separate
them with a comma.
Parameters or Arguments
The terms parameter and argument can be used for the same thing:
information that are passed into a function.
From a function's perspective:
• A parameter is the variable listed inside the parentheses in the
function definition.
• An argument is the value that is sent to the function when it is
called.
Number of Arguments

• By default, a function must be called with the correct number of


arguments.
• Meaning that if your function expects 2 arguments, you have to
call the function with 2 arguments, not more, and not less.
Arbitrary Arguments,

*args
• If you do not know how many arguments that will be passed into
your function, add a * before the parameter name in the function
definition.
• This way the function will receive a tuple of arguments, and can
access the items accordingly
• Arbitrary Arguments are often shortened to *args in Python
documentations.
If the number of arguments is unknown, add a * before the parameter
name:
Keyword Arguments

• You can also send arguments with the key = value syntax.
• This way the order of the arguments does not matter.
• The phrase Keyword Arguments are often shortened to kwargs in Python
documentations.
Arbitrary Keyword Arguments, **kwargs

• If you do not know how many keyword arguments that will be passed into
your function, add two asterisk: ** before the parameter name in the
function definition.
• This way the function will receive a dictionary of arguments, and can access
the items accordingly:
Passing a List as an Argument
Recursion

• Python also accepts function recursion, which means a defined


function can call itself.
• Recursion is a common mathematical and programming concept.
It means that a function calls itself. This has the benefit of
meaning that you can loop through data to reach a result.
Example of recursion in Python

This function counts down from a given number to 1, printing each number
along the way, and then prints "Liftoff!" when it reaches 0.
Example of recursion in Python

This function
calculates the sum
of integers from 1
to a given number
n using recursion.
Activity:
Write a Python function to:
• find the Max of three numbers
• to sum all the numbers in a list
• to multiply all the numbers in a list
• to reverse a string
• to calculate the factorial of a number
• to check whether a number falls in a given range
• that accepts a string and calculate the number of upper case letters
and lower case letters
End

You might also like