Class 10 - Functions - Part II
Class 10 - Functions - Part II
དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་
འཕེལ་ལྷན་ཁག།
Department of School Education
Ministry of Education & Skills Development
Python Coding
Class X ICT Curriculum
January 2024
Key Concept #
4
FUNCTIONS
Part II
Concept Overview
Objectives
ACTIVITIES
1. Understanding the Variable Scope
2. Demo 1 - Scope of Variable
3. Activity 1 - Check Your
Understanding
4. Demo 2 - Checking Pythagorean
Triple
VARIABLE SCOPE
5. Activity 2 - Subtracting Two
Numbers
6. Demo 3 - Checking Pythagorean
Triple
7. Activity 3 - Squaring a Number’
8. Demo 4 - Checking Pythagorean
Triple
ACTIVITIES
Global Nonlocal
Variable Local Variable
Variable
January 2024
PYTHON Key stage IV Class X
Variable Scope
● A variable declared outside of the function is
1. Global known as a global variable.
variables ● This means that a global variable can be accessed
inside or outside of the function.
● A variable declared inside a function have a scope
within that function only.
2. Local
● It is not accessible beyond a function's scope, but
variables
it can be made available by using the global
keyword.
● Nonlocal variables are used in nested functions to
3. Nonlocal assign values to variables in the outer function.
variables ● Use the nonlocal keyword to create nonlocal
variables.
January 2024
PYTHON Key stage IV Class X
Code
global
1 global_variable = 10 variable
2 def outer_function():
local
3 local_variable = 5
variable
4 def inner_function():
5 nonlocal local_variable
6 local_variable = 15 nonlocal
7 inner_function() variable
8 outer_function()
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
Code
1 def display_name():
2 name="Sonam"
3 print(name)
Ans - The program will display the NameError: name 'name' is not
defined because the variable name is a local variable defined within
display_name function. To display the name Sonam, we have to make
the variable name global (outside of the function).
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
Arguments in Function
● When calling a function, it is important to provide the exact number
of arguments expected by its parameters to prevent errors.
● The different ways of passing argument to the function are as
follows:
Positional Keyword
Arguments Arguments
1 2 3 4
Default Variable -
Arguments length
Arguments
January 2024
PYTHON Key stage IV Class X
1. Positional Arguments
● During a function call, values passed should be in the order of
parameters in the function definition.
● The position of the argument matters, as it determines which
parameter in the function receives which value.
January 2024
PYTHON Key stage IV Class X
Code
1 def pythagorean_triple(a, b, c):
2 if a**2+b**2==c**2 or a**2+c**2==b**2 or b**2+c**2==a**2:
3 print(f"{a},{b},and{c} is Pythagorean triple.")
4 else:
5 print(f"{a},{b},and{c} not Pythagorean triple.")
6 # Input three integers from the user
7 a = int(input("Enter the first integer: "))
8 b = int(input("Enter the second integer: "))
9 c = int(input("Enter the third integer: "))
10 pythagorean_triple(a, b, c)
Output
Enter value of x:25
Enter value of y:45
Difference of 25.0 and 45.0 is -20.0
January 2024
PYTHON Key stage IV Class X
2. Default Arguments
● Sometimes the value of the parameters can be preassigned
depending on the need of the program. Such predefined values are
called default arguments.
● These default values are used when an argument for that
parameter is not provided during the function call.
January 2024
PYTHON Key stage IV Class X
Output
Default c value 5 is taken in
2,4,and 5 not Pythagorean triple. place of missing arguments.
Output
Enter value of any number:4
4.0 to the power 2 is 16.0
January 2024
PYTHON Key stage IV Class X
3. Keyword Arguments
● Keyword arguments passes arguments to a function using the
parameter names explicitly.
● It specifies argument along with its corresponding parameter name.
● The order of arguments is not important, but the number of
arguments must be matched.
January 2024
PYTHON Key stage IV Class X
Code
1 def pythagorean_triple(a, b, c):
2 if a**2+b**2==c**2 or a**2+c**2==b**2 or b**2+c**2==a**2:
3 print(f"{a},{b},and{c} form a Pythagorean triple.")
4 else:
5 print(f"{a},{b},and{c} are not Pythagorean triple.")
6 n_1 = int(input("Enter the first integer: "))
7 n_2 = int(input("Enter the second integer: "))
8 n_3 = int(input("Enter the third integer: "))
9 pythagorean_triple(a=n_1, b=n_3, c=n_2)
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
Output
"Tashi" and "Dawa" are passed
His last name is Dawa as keyword arguments to the
function and collected into the
kwargs dictionary. So, kwargs
becomes {"fname": "Tashi",
"lname": "Dawa"}.
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
Output
Enter speed in km/hr:50
Enter time in hour:4
The distance covered is 200.0 km/hr
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
Code Output
1 def biodata(name, age, address): enter your name: Dema
2 enter your age: 16
3 print(f’Name:{name}’) enter your address: Paro
4 print(f’Age:{age}’) Name:Dema
5 print(f’Address:{address}’) Age:16
6 n = input(“enter your name: ”) Address:Paro
7 a = int(input(“enter your age:”))
8 ad = input(“enter your address: ”)
biodata(name=n,age=a,address=ad)
January 2024
PYTHON Key stage IV Class X
Activity 9 - Solution
Code 10 circle(50,90)
11 lt(30)
1 from turtle import* 12 end_fill()
2 shape("turtle") 13 up()
3 speed(0) 14 #calling function()
4 def flower(petal_color="cyan"): 15 flower(petal_color="red")
5 for i in range(6): 16 goto(100,100)
6 down() 17 flower("blue")
7 begin_fill() 18 goto(-100,100)
8 fillcolor(petal_color) 19 flower()
9 20 hideturtle()
10 circle(50,90)
lt(90)
ACTIVITIES
1. Understanding the Return
Statement
2. Demo 1 - Checking Even or
Odd Number
3. Activity 1 - Calculating Area of
RETURN STATEMENT
Rectangle
4. Activity 2 - Determining the
Multiple of 5
5. Activity 3 - Calculating
Perimeter of a Rectangle (BCSE
trial)
Syntax
Keyw
def function_name (parameters):
ord
#statement Body of
return statement
statement
Function
returns
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
Code
1 def even_or_odd(number):
2 if number % 2 == 0:
3 return f"The number {number} is even."
4 else:
5 return f"The number {number} is odd."
6 user_number=int(input("Enter an integer:"))
7 print(even_or_odd(user_number))
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
Output
Perimeter of a rectangle is 30
January 2024
PYTHON Key stage IV Class X
ACTIVITIES
Recursive Function
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
Code - Example
1 def sum_of_natural_numbers(n):
2 if n == 1:#base case
3 return 1
4 else:
5 return n + sum_of_natural_numbers(n-1)#recursive call
6 result = sum_of_natural_numbers(5)
7 print(f"sum of first 5 natural no. is {result}")
Output
sum of first 5 natural no. is 15
January 2024
PYTHON Key stage IV Class X
Output
Enter number to find factorial:5
The factorial of 5 is 120
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class X
January 2024
PYTHON Key stage IV Class IX
Key
Points
● Variable scope in functions refers to the visibility and accessibility of
variables within different parts of a program, particularly within functions.
● Local, global and nonlocal variables are three types of variables scope in
Python.
● Positional, default, keyword and variable-length arguments are different
ways to assign arguments to a function parameter.
● The return statement is used within a function to send back a value or a
result to the code that called the function.
● A recursive function in Python is a function that calls itself within its own
definition.
● A recursive function is a way of solving a problem by breaking it down into
smaller, more manageable subproblems.
January 2024
PYTHON Key stage IV Class IX
བཀྲིན་ཆེ།
THANK YOU
January 2024