དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།
Department of School Education
Ministry of Education & Skills Development
Python Coding
January 2024
Class X ICT Curriculum
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
FUNCTIONS
Key Concept # 3
Part I
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Main concepts | Sub - concepts |
Function |
|
Concept Overview
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Objectives
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Understanding Functions
def function_name():
function body
function_name()#function called
Syntax
Code
1 2 3 | def greet(): print(“Hello World!”) greet()#function called |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Importance of Function
1. Modularity
Function break down a big task into smaller, more manageable parts.
2. Readable
Each function can do a specific job, making program easier to understand and build.
4. Troubleshooting and debugging
Functions make code debugging and troubleshooting faster as issues to specific function can be fixed separately and easily.
3. Reusability
Functions that have been created can be used by calling them in other parts of the code as necessary, eliminating the need to rewrite the code.
Given below is some function importance in Python program.
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Types of Functions
Built-in Functions | |
1
User-defined Functions | |
2
len(),print(),input(),range(),max(),min()
def greet():
print(“Kuzuzangpola”)
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 1 - Built-in Functions
Write a Python program to count the number of characters in the string “Pema Wangchuk”. Hint: use a built-in function.
Code
Total Char: 13
Output
Write a Python program find the sum of first five natural numbers. Hint: use a built-in function.
Code
Total:10
Output
1 2 3 | name = “Pema Wangchuk” count = len(name) print(“Total Char:”,count) |
1 2 3 | for x in range(1,5): total + = x print(“Total:”, total) |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
User-Defined Function
ACTIVITIES
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Defining User-Defined Function
syntax
syntax
def function_name(parameters):
#statements
return value
Non-parameterized Function
A function that doesn't have any parameters as an input.
Parameterized Function
Function that has one or more parameters as an input.
def function_name():
#statements
return value
1
2
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Examples
Non-parameterized Function
Parameterized Function
1
2
1 2 3 4 5 | def sub(x,y): print(x-y) sub(20,15) |
1 2 3 4 5 | def sub(): x=20 y=15 print(x-y) sub() |
5
Output
5
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Terms Related to User-Defined Functions
1
Keyword used to define a function.
def
2
Name of the function.
function_name
3
Input values that function accepts. It is optional.
parameters
4
Set of instructions that function will execute when called. It must be indented.
statements
5
Specifies the value that function returns when it is is called. It is optional, but if you don’t include it, function will return None by default.
return
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Understanding Functions
Code
1 2 3 4 5 | def addition(x, y): add = x + y return add cal = addition(10, 5) print(f’Addition:{cal}’) |
Function Name (addition)
Parameters are x and y
Return value (add) to the calling function
Calling function [addition(x, y)] with argument 10 and 5
Function Body
Key word
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Demo 1 - Creating Non-Parameterized Function
Code
Write a Python program to add any two numbers using non-parameterized function.
1 2 3 | def add(): x,y = 4,7 print(“Sum:”,x+y) |
What output will be given when you run the program?
The output will be empty because the function was not called.
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Demo 2 - Creating Parameterized Function
Code
Create a Python program to add any two numbers. Use parameterized function to add the numbers.
1 2 3 | def add(x,y): sum = x+y print(sum) |
How are the two programs in demo 1 & demo 2 different?
Parameter x & y has been set in the demo 2.
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 1 - Identifying the correct Function
Which of the following option given below is the correct way to define a function that will multiply two numbers?
def mult(x,y):
print(x*y)
def mult(x,y):
print(x*y)
A
Display empty screen
Def mult(x,y)
print(x*y)
B
C
D
def mult x,y:
print(x*y)
C
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 2 - Understanding Parameters and Arguments
Watch the video and explain the differences between parameter and argument.
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Calling a Function
Code
Code
1 2 3 4 5 | def add(x,y): print(“Sum:”,x+y) #Function not called |
1 2 3 4 5 | def add(a,b): print(“Sum:”,a+b) #Function called add(5,9) add(10,12) |
Sum: 14
Sum: 22
Output
Output
After defining a function, we need to call the function to use it. Function name followed by parenthesis is used to call the function as given below.
Empty
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 3 - Multiplying Two Numbers
Code
Write the Python program, that uses a function to multiple any two numbers entered by the user.
1 2 3 4 5 6 | def multiply(x,y): pro = x*y print(f“{x}X{y}={pro}”) a = int(input(“Enter a value:”)) b = int(input(“Enter a value:”)) multiply(a,b) |
Enter a value: 10
Enter a value: 2
10 X 2= 20
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 3 - Adding Two Numbers
Write the Python code that uses a function to add any two numbers.
Code
1 2 3 4 | def sum(num_1, num_2): add = num_1+ num_2 print(f“sum of {num_1} and {num_2} is {add}”) sum(3,4) |
sum of 3 and 4 is 7
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 4 - Practice Questions on Function
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Practice Question 1 - Solution
1. 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.
Code
1 2 3 4 5 | def calculate_Area(l,b): print(“area = ”,l * b,“unit square”) length = float(input(“Enter value length:”)) breadth = float(input(“Enter the breadth:”)) calculate_Area(length, breadth) |
Enter value length: 2
Enter value breadth:3
area = 6.0 unit square
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Practice Question 2 - Solution
2. Write a non-parameterized function, that takes input from the user and determine whether a user-entered number is even or odd.
Code
1 2 3 4 5 6 7 | def even_odd(): no=int(input("Enter a number:")) if no%2==0: print(f"{no} is even") else: print(f"{no} is odd") even_odd() |
Enter a number: 8
8 is even
Output
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Practice Question 3 - Solution
3. 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.
Code
1 2 3 4 5 6 7 | def print_is_positive(n): if n > 0: print("positive") else: print("negative") num=int(input("Enter a number:")) print_is_positive(num) |
Output
Enter a number: 9
positive
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Practice Question 4 - Solution
Code
Output
1 2 3 4 5 | def first_five_even_number(): for num in range(2,11): if num%2==0: print(num) first_five_number() |
2 4 6 8 10 |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Practice Question 5 - Solution
Code
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 | def average(n): i = 1 total = 0 if n==0: print(“Invalid input!”) else: while i <= n: total = total+i i += 1 average = total/n print(f‘Average:{average}’) num = int(input(“Enter number:”)) average(num) |
Enter number: 10 Average:5.5 |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Demo 3 - Drawing Circles
Write a Python program using Turtle module to draw two circles with a radius 50. Use function to perform the task.
Code
1 2 3 4 5 6 7 8 9 | from turtle import * title(“Drawing circles”) def draw_circle(): pendown() circle(50) penup() draw_circle() goto(50,-100) draw_circle() |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Demo 4 - Drawing Squares
Write a Python program to draw two squares with side length of 200 pixels in Turtle module. Use a function.
Code
1 2 3 4 5 6 7 8 9 10 11 12 | from turtle import * title(“Drawing squares”) goto(-100,0) def draw_square(): penup() for i in range(4): fd(100) lt(90) pendown() draw_sqaure() goto(0,-100) draw_square() |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 5 - Drawing a Hexagon
Write a Python program to draw hexagon shape with sides of 100 pixels using the Turtle module.
Code
1 2 3 4 5 6 7 | from turtle import * title(“Drawing Hexagon”) def draw_hexagon(): for i in range(6): forward(100) left(60) draw_hexagon() |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 6 - Drawing a Composite Shape
Write a Python program to create the following output.
Code
1 2 3 4 5 6 7 8 9 10 11 12 | from turtle import * title('Drawing Shape') def Shape(radius): for i in range(4): forward(100) left(90) color("blue") forward(radius) begin_fill() circle(radius) end_fill() Shape(50) |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Activity 7 - Drawing Olympic Logo
Write a Python program to draw the olympic logo using Turtle module.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 | from turtle import * pensize(10) def olympic_logo(x,y,color): penup() goto(x,y) pendown() pencolor(color) circle(50) olympic_logo(0,140,"blue") olympic_logo(70,100,"yellow") olympic_logo(140,140,"black") olympic_logo(210,100,"green") olympic_logo(280,140,"red") |
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
Key Points
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD
བཀྲིན་ཆེ།
THANK YOU
January 2024
PYTHON
Key stage IV
Class X
© ICT Curriculum, MoESD