1 of 33

དཔལ་ལྡན་འབྲུག་གཞུང་། ཤེས་རིག་དང་རིག་རྩལ་གོང་འཕེལ་ལྷན་ཁག།

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

2 of 33

FUNCTIONS

Key Concept # 3

Part I

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

3 of 33

Main concepts

Sub - concepts

Function

  • Definition of function
  • Purpose of function
  • Types of function (built-in,User-defined)
  • Creating a function
  • Calling a function
    • Calling function without parameter, Calling function with parameter(s)
  • Understanding function through Turtle module

Concept Overview

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

4 of 33

  • Define a function in Python programming.
  • Recognize the benefits of using functions, such as code reuse and modular design.
  • Write the correct syntax for declaring functions in Python.
  • Identify the key components of a function declaration, including the function name, parameters, and the optional return statement.
  • Differentiate between parameter and argument in a function.
  • Create functions that handle multiple arguments to solve coding problem.
  • Create drawings or shapes in Turtle module using functions.

Objectives

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

5 of 33

Understanding Functions

def function_name():

function body

function_name()#function called

Syntax

Code

  • Functions are blocks of reusable code that perform a specific task whenever it is called.
  • Functions should be given a meaningful name to make it more readable.
  • A function returns data as a result by using the return statement within its body.
  • A function is called by writing its name with the appropriate arguments.

1

2

3

def greet():

print(“Hello World!”)

greet()#function called

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

6 of 33

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

7 of 33

Types of Functions

Built-in Functions

1

User-defined Functions

2

  • Built-in functions are part of the Python standard library. They are available for use without the need to create separately.
  • Examples:

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

  • User-defined functions are created by the users based on their needs and requirements to perform a specified task.
  • Example:

def greet():

print(“Kuzuzangpola”)

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

8 of 33

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

9 of 33

User-Defined Function

ACTIVITIES

  1. Understanding User-defined function
  2. Demo 1 - Creating Non-Parameterized Function
  3. Demo 2 - Creating Parameterized Function
  4. Activity 1 - Identifying the Correct Function
  5. Activity 2 - Understanding Parameters and Arguments
  6. Activity 3 - Practice Question in Function
  7. Activity 4 - Practice Questions on Function
  8. Demo 3 - Functions in Turtle Module
  9. Activity 6 - Function in Turtle Module

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

10 of 33

Defining User-Defined Function

  • In Python a user-defined function is defined using the def keyword.
  • There are two ways of defining a 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

11 of 33

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

12 of 33

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

13 of 33

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

14 of 33

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

15 of 33

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

16 of 33

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

17 of 33

Activity 2 - Understanding Parameters and Arguments

Watch the video and explain the differences between parameter and argument.

  1. Parameters are placeholders that stores values.
  2. Arguments are values assigned to parameters.
  3. Parameters are used when you define a function.
  4. Arguments are used when you call a function.

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

18 of 33

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

19 of 33

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

20 of 33

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

21 of 33

Activity 4 - Practice Questions on Function

  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.
  2. Write a non-parameterized function, that takes input from the user and determine whether a user-entered number is even or odd.
  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.
  4. Write a Python program that uses a non-parameterized function to display the first five even numbers.
  5. Write a Python program that uses parameterized function to calculate the average of a list of numbers entered by the user.

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

22 of 33

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

23 of 33

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

24 of 33

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

25 of 33

Practice Question 4 - Solution

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

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

26 of 33

Practice Question 5 - Solution

  1. Write a Python program that uses parameterized function to calculate the average of all the numbers upto the number entered by the user.

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

27 of 33

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

28 of 33

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

29 of 33

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

30 of 33

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

31 of 33

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

32 of 33

  • Function is a block of reusable code that performs a specific task.
  • Using functions in programs help in code readability, reusability and debugging.
  • Functions are defined using the def keyword followed by function name and parenthesis.
  • There are two ways of defining a function - parameterized and non-parameterized.
  • Arguments are values assigned to parameters and they are used when calling a function.
  • Functions can be used to solve different types of problems including drawing shapes using Turtle module.

Key Points

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD

33 of 33

བཀྲིན་ཆེ།

THANK YOU

January 2024

PYTHON

Key stage IV

Class X

© ICT Curriculum, MoESD