

Python functions
Presentation
•
Instructional Technology
•
10th Grade
•
Practice Problem
•
Easy
s tobgyal
Used 34+ times
FREE Resource
47 Slides • 4 Questions
1
Python functions
2
What is Python Function?
~is a blocks of reusable code that perform a specific task when it is called.
~We can pass data, known as parameters, into a function.
~Functions should be given a meaningful name to make it more readable.
~A function can return data as a result.
3
Importance of functions
Modularity:
means breaking something big into smaller, independent pieces that can work on their own. This makes it easier to build, fix, or change parts without affecting the whole.
Readable
is one that is easy to understand because it does one clear job. This makes it simple to use, fix, or change later.
4
Reusability:
means making something that can be used over and over again in different situations.
It saves time because you don’t have to create the same thing repeatedly.
debugging and troubleshooting:
Functions make code debugging and troubleshooting faster as issues to specific function can be fixed separately and easily.
5
Types of functions:
There are two types of functions:
Built-in functions: are predefined functions that are available in the Python package for use.
Example:
print(),input(),len(),range() max(), min(), sum().
User-defined functions: are declared and defined by the programmer based on their needs to perform the specified task.
6
Demonstration to create user defined function....
7
Syntax to define a user-defined function:
def functionName(parameters):
…………#function body
return function body
def - keyword is used to declare a function.
function_name - any name given to the function.
parameters - any value passed to function.
return (optional) - returns value from a function.
Note:Indentation is essential before you start the function body.
8
Function calling:
~making use of the functions defined...
~use the function name followed by parentheses
9
Argument and parameter:
variable in the parentheses is parameter
value assigned to the variable is argument
10
Parameter & Arguments:
~A parameter is the variable listed inside the parentheses in the function definition.
~An argument is the value that are sent to the function when it is called.
11
Multiple Choice
Which keyword is used to define a user-defined function in Python?
function
def
func
define
12
Multiple Choice
Which of the following statements about user-defined functions in Python is TRUE?
A user-defined function can be called before it is defined in the program.
A user-defined function is defined using the keyword def followed by the function name and parentheses.
User-defined functions cannot take any input arguments.
A user-defined function must always return a value.
13
Write a Python program using function to check if a number entered by the user is a multiple of 5.
14
Create a Python program that use a function named “print_is_positive” that takes a number as input and then displays whether the number is positive or negative
15
Test your understanding:
def????
tatula????
abc???
10,20,30????????
identify the built in functions used in the program????
16
Classification of variables
1.Local
2. global
3. nonlocal
17
Local Variables:
~When we declare variables inside the function, these variables will have a local scope (within the function).
~We cannot access them outside the function.
Example:
def greet():
message="Kuzuzangpo la"
print("Local", message)
greet()
18
nonlocal:
are used in nested functions to assign values to variables in the outer function.
Use nonlocal keyword to create nonlocal variables
19
Change the local to global:
Used inside a function to modify a local variable to global by using global keyword. Simply type 'global', followed by the variable name
Example1
Example2
20
More Demonstrations
21
Global Variables:
~a variable declared outside the function
~ a global variable can be accessed inside or outside of the function.
Example:
my_message="Good morning python lovers"
def greet(): #declare function...
print("Local message--", my_message)
greet() #functiona call
print("Global message--", my_message)
22
Check your understanding:
Study the code given below and identify the function name, parameters, global variable and local variables.
23
Differentiate between a local variable and a global variable.
Local variable | Global variable |
|---|---|
A local variable is declared inside a function or block and is accessible only within that function or block. print(num) demo() |
print(num) demo() |
24
Scope of variable:
~A variable scope specifies the region where we can access a variable...
~we can declare variables in two different scopes:
1. local scope
3. global scope
25
Check Your Understanding
26
Multiple Choice
Which of the following is true about local variables?
Accessible throughout the program
Accessible in all functions
Accessible only in that function
Accessible only in the similar functions
27
Multiple Choice
What is a global variable in programming?
A variable that is only accessible within a specific function
A variable that is accessible from any part of the program
A variable that is defined inside a loop
A variable that is passed as an argument to a function
28
non-parameterized function :
a function that does not take any input parameters when it is called.
29
parameterized function :
a function that takes input values (called parameters or arguments) when it is called.
30
Activity 4 - Practice Questions on Function
Write a Python program to add any two numbers using non-parameterized function.
Write the Python program, that uses a function to multiple any two numbers entered by the user.
Write a parameterized function named "calculate_Area" that takes in two parameters, length and width, and calculates the area of a rectangle. Display the result.
Write a non-parameterized function, that takes input from the user and determine whether a user-entered number is even or odd..
Write a Python program that uses a non-parameterized function to display the first five even numbers.
Write a Python program that uses parameterized function to calculate the average of a list of numbers entered by the user.
31
32
Argument:
argument is a value that is passed into a function when it is called.
There are four types of arguments:
1. positional argument
2. keyword argument
3. default argument
4. variable length arguments
33
As an ICT student, your teacher has assigned you a project to create a School Result System that automates student result calculations.
Project Requirements:
Write a Python program using a function named result_system() that, when executed, must:
The program must display a clear title for the result system.
It should prompt the user to enter:
Student’s name
Marks for multiple subjects (e.g. English, Dzongkha, Maths, Science)
The program should store the marks in an appropriate data structure (list or tuple).
It should display the marks in ascending order.
The program must identify and display the highest and lowest marks.
It should calculate and display the total marks.
It should calculate and display the percentage.
The program should display the student’s result as Pass or Fail, based on the condition:
Pass if percentage >= 40%
Fail if percentage < 40%
34
1. Positional argument
arguments that are passed in the same order as the parameters defined in the function.
35
2.Default argument
These arguments have default values.
If a value is not provided when calling the function, the default value is used.
36
3.keyword argument
arguments that are passed to a function by explicitly specifying the name of the parameter along with its value
The order of the arguments does not matter because the function matches the value to the parameter name rather than relying on their position in the argument list
37
4.variable length argument
can accept any number of arguments without knowing in advance how many arguments you will pass to the function.
This is useful when you want your function to be flexible and handle different numbers of arguments, depending on the situation.
There are two main types of variable-length arguments in Python
1. *args (Non-keyword variable-length arguments)
2. **kwargs (Keyword variable-length arguments)l
38
1. Using args (For Positional Arguments):
*args allows a function to accept an arbitrary number of non-keyword arguments
The name *args is just a convention; you can use any name, but it's typical to use args
39
Example 2:
Output:
40
Example 3:
41
Example 4: Printing All Arguments
A function that prints each argument it receives.
Output????
42
2. Using **kwargs (For Keyword Arguments):
When you use **kwargs, it collects all the extra keyword arguments (key-value pairs) and stores them in a dictionary.
43
Example 1: Printing Keyword Arguments:
A function that prints each keyword argument and its value.
Output?????
44
3. function with return statement:
Send a value back to the part of the program where the function was called
This allows the result of the function to be stored in a variable or used in further calculations.
syntax:
def function_name(parameters):
# some code
return value
45
Example 1:
46
Example 2:
47
4. Recursive function:
A recursive function is a function that calls itself in order to solve a problem.
In Python, recursion is often used when a problem can be broken down into smaller, similar subproblems.
48
Basic Structure of a Recursive Function
Every recursive function must have:
Base Case – stops the recursion (prevents infinite calls).
Recursive Case – the part where the function calls itself.
49
50
Example: Factorial
Factorial of a number n (written as n!) is the product of all positive integers up to n. For example, 5! = 5 4 3 2 1
51
Python functions
Show answer
Auto Play
Slide 1 / 51
SLIDE
Similar Resources on Wayground
44 questions
L6- De Compras
Presentation
•
9th - 10th Grade
47 questions
Y10 National Migration
Presentation
•
10th Grade
46 questions
História: "Um papá à medida"
Presentation
•
KG
46 questions
Vocabulary Study Guide Sophomores
Presentation
•
10th Grade
48 questions
Chemistry Unit 6 Lesson 1: Molecular Lewis Structures
Presentation
•
10th Grade
42 questions
SSWH15 Industrialization and Urbanization
Presentation
•
10th Grade
44 questions
Determiners
Presentation
•
10th Grade
45 questions
4t ESO. LA CÈL·LULA I ELS SEUS COMPONENTS (PART 1)
Presentation
•
10th Grade
Popular Resources on Wayground
11 questions
Hallway & Bathroom Expectations
Quiz
•
6th - 8th Grade
10 questions
HCS SCI 03 Summer School Assessment 2
Quiz
•
3rd Grade
11 questions
Home Scope
Quiz
•
7th - 8th Grade
12 questions
2026 TAP Technology in the Classroom
Presentation
•
Professional Development
15 questions
HCS SCI 05 Summer School Assessment 2 Review
Quiz
•
5th Grade
15 questions
HCS SCI 04 Summer School Review 2
Quiz
•
4th Grade
59 questions
Geometry Unit 3 Review
Quiz
•
9th - 12th Grade
14 questions
FAST ELA READING SMAPLE TEST MATERIALS
Passage
•
3rd Grade