Search Header Logo
Python functions

Python functions

Assessment

Presentation

Instructional Technology

10th Grade

Practice Problem

Easy

Created by

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:

  1. Built-in functions:  are predefined functions that are available in the Python package for use. 

  • Example: 

print(),input(),len(),range() max(), min(), sum(). 

  1. 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....

media

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

media

9

Argument and parameter:

media

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.

media

11

Multiple Choice

Which keyword is used to define a user-defined function in Python?

1

function

2

def

3

func

4

define

12

Multiple Choice

Which of the following statements about user-defined functions in Python is TRUE?

1

A user-defined function can be called before it is defined in the program.

2

A user-defined function is defined using the keyword def followed by the function name and parentheses.

3

User-defined functions cannot take any input arguments.

4

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.

media

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

media

15

Test your understanding:
def????
tatula????
abc???
10,20,30????????

identify the built in functions used in the program????

media

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()

media

18

​nonlocal:
are used in
nested functions to assign values to variables in the outer function.

Use nonlocal keyword to create nonlocal variables

media

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

media
media

Example1

Example2

20

More Demonstrations

media

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)

media

22

Check your understanding:
Study the code given below and identify the function name, parameters, global variable and local variables.

media

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.

def demo():
num=100

print(num)

demo()


A global variable is declared outside any function or block and is accessible throughout the entire program.

num=100
def demo()

print(num)

demo()
print(num)



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?

1

Accessible throughout the program

2

Accessible in all functions

3

Accessible only in that function

4

Accessible only in the similar functions

27

Multiple Choice

What is a global variable in programming?

1

A variable that is only accessible within a specific function

2

A variable that is accessible from any part of the program

3

A variable that is defined inside a loop

4

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.


media

29

parameterized function :
a function that takes input values (called parameters or arguments) when it is called.


media

30

​Activity 4 - Practice Questions on Function

  1. ​Write a Python program to add any two numbers using non-parameterized function.

  2. Write the Python program, that uses a function to multiple any two numbers entered by the user.

  3. 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.

  4. Write a non-parameterized function, that takes input from the user and determine whether a user-entered number is even or odd..

  5. Write a Python program that uses a non-parameterized function to display the first five even numbers.

  6. Write a Python program that uses parameterized function to calculate the average of a list of numbers entered by the user.

31

media

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:

  1. The program must display a clear title for the result system.

  2. It should prompt the user to enter:

    • Student’s name

    • Marks for multiple subjects (e.g. English, Dzongkha, Maths, Science)

  3. The program should store the marks in an appropriate data structure (list or tuple).

  4. It should display the marks in ascending order.

  5. The program must identify and display the highest and lowest marks.

  6. It should calculate and display the total marks.

  7. It should calculate and display the percentage.

  8. 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.

media

35

2.Default argument

These arguments have default values.
If a value is not provided when calling the function, the default value is used.

media

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

media

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

media

39

​Example 2:

media

​Output:

media

40

​Example 3:

media

41

​Example 4: Printing All Arguments

A function that prints each argument it receives.

media

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.

media
media

43

Example 1: Printing Keyword Arguments:

A function that prints each keyword argument and its value.

media

​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:

media

46

​Example 2:

media

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:

  1. Base Case – stops the recursion (prevents infinite calls).

  2. Recursive Case – the part where the function calls itself.

49

media
media

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

media

51

media

​Python functions

Show answer

Auto Play

Slide 1 / 51

SLIDE

Discover more resources for Instructional Technology