G8 - Unit 2 - Coding
G8 - Unit 2 - Coding
Learning Objectives
Learners will learn to:
understand and use the concept of iteration
understand the use of arrays, write values into and read values from an array
using iteration
Vocabulary
iteration
loop
arrays
list
Let Us Start
1
Let Us Explore
You have used Python programming language.
Complete the KWL (Know, Want to know, Learned) chart shown below o recall the
programming concepts.
Name the concepts you Explore the concepts After the lesson, name
know you want to know the concepts you learnt
Get, Set, Go
2.1 Iteration
Iteration is the process of repeating a set of instructions until a specific condition is
Iteration is often referred to as looping since the program ‘loops’ back to an earlier
Iteration allows programmers to simplify a program and make it more efficient. Instead
of repeating the same lines of code again and again, a programmer can write a section
of code once and ask the program to execute the same line repeatedly until no longer
needed.
2
The following are the main types of iteration:
Count-controlled loops
Count-controlled loops
When a program needs to iterate a set number of times, this is known as definite
iteration and uses a FOR loop. A FOR loop uses an extra variable called a loop counter
that keeps track of the number of times the loop has been run. The counter variable is
incremented or decremented after each iteration. The loop continues until the counter
Output
The for loop is set up to iterate over the range of numbers from 1 to 6. The loop
variable ‘i’ is a counter that starts at 1 and increments by 1 in each iteration. Inside
the loop, the current value of i is printed. The loop will execute 5 times, printing the
numbers from 1 to 5.
3
The range() function
The range() function in Python generates a sequence of numbers. It is commonly
step: The step between each number in the sequence. The default value is 1.
Example:
range(10) will generate a sequence of numbers from 0 to 9. Here, 10 is the
default.
taking step_size 2.
Example: Program that uses a ‘for’ loop to print the numbers 1, 3, 5, 7, and 9. It uses
4
Output
Example: The following program prints each character of the string message on a
Output
5
Activity 1
Type the given program to find the sum of even numbers in Python. Run the
Python executes a block of code as long as the test expression (condition) is true. We
generally use this loop when we do not know how many times the loop will execute in
advance.
The statements inside the loop are executed only if the result of the test_expression is
True. After one iteration of the loop, the test_expression is checked once more. This
6
Output
We use a while loop to print numbers from 1 to 5. We initialize a variable i with the starting
value of 1. The while loop continues executing as long as the condition i <= 5 is true. Inside
the loop, we print the value of i and then increment it by 1 using i += 1. This ensures that
the loop progresses and eventually terminates when i becomes greater than 5. The loop will
iterate 5 times, printing the numbers 1 to 5. After printing 5, the condition i <= 5 becomes
The break and continue statements are used to modify the behaviour of loops.
comes across a ‘break’ statement inside a loop, the current iteration will stop and
control is transferred to the statement that appears immediately after the loop. It is
useful if you wish to stop the loop from executing further. For example, while printing
Example: The program counts from 1 to 10 using a while loop and prints it. When
the count reaches 5, the break statement is executed, which exits the loop. So only
7
Program
Output
executing the remaining statements in the current iteration. It is also used based on a
condition. It can be useful when you wish to skip the execution of a particular iteration
of the loop.
For example, while printing numbers in a range from 1 to 10, if you do not wish to
Example: The program in which the while loop counts from 1 to 10. However, when
the value of a number is even (divisible by 2), the ‘continue’ statement is executed. As
a result, the ‘print(number)’ statement is skipped, and the loop proceeds to the next
8
Program
output:
Activity 2
1. Write a program in Python to print all even numbers from 1 to 20. Break the loop
2. Write a program in Python to print all numbers from 1 to 10 except for the
number 5.
that perform a specific task or set of tasks. Subroutines are often used to simplify a
program by breaking it into smaller, more manageable parts. All functions must return
a value using the return statement, otherwise it isn’t function – it’s a procedure. They
9
Defining a function
In Python, a function is defined using the def keyword. It can be called or executed
using its name along with the same pair of brackets used while defining it.
Example: The greet function returns the string “Hello, world!” to the program and
Output
Input Parameters
A function can accept input data to work on. These are called parameters. Parameters
Parameters are specified after the function name inside the brackets. You can add
more than one parameter by separating them with commas. When a function is called,
10
Returning data
A function can also return data. The return statement is used to exit from a function
In the following example, the function is_evenodd() accepts a parameter ‘n’. It checks
whether the value of number is even or odd and returns True or False.
Program
Output
11
Global and Local Variables
Global variables are those which are not defined inside any function and have a
global scope whereas local variables are those which are defined inside a function
Local Variables
Local variables in are those which are initialized inside a function and belong only to
Program
Output
Global Variables
Global variables are defined outside any function and which are accessible
12
Program
Output
Activity 3
Type the following program in Python and find the output.
13
2.3 Arrays
An array is a data structure that holds similar, related data. An array is like a collection
of boxes, each of which is called an element. Each element has a position in the array
and can hold a value. The data in an array must all be of the same data type.
Declaring an array
To declare an array, a programmer gives it at least two properties:
an identifier
Once declared, the name and structure of an array cannot be changed. The position
One-dimensional array
A one-dimensional array can be referred to as a list. It is a collection of items of the
14
Output
starts from 0. Thus, a list with 5 elements will have an index ranging from 0 to 4
Program
Output
Two-dimensional array
A two-dimensional (2D) array is an array of arrays creating a grid-like structure.
15
Example: Program using two-dimensional array
Program
Output
index refers to the main or parent array and another to the position of the data element
in the inner array. If we mention only one index, the entire inner array is printed for
Output
16
Example: In the following program, the ‘for’ loop displays the item of the ‘x’ list one
by one.
Program
output
Example : A program to display the sum of all numbers in a list using for loop.
Program
Output
Activity 3
Type the following program in Python and find the output.
17
Unit Review
1. Iteration is the process of repeating a set of instructions until a specific
condition is met.
2. When a program needs to iterate a set number of times, this is known as
definite iteration and uses a FOR loop.
3. The range () function generates a sequence of numbers commonly used in ‘for’
4. You can loop through each character in a string using a for loop.
5. The condition-controlled loop is implemented as a ‘while’ loop.
6. When writing code, there are times when you may want to control the flow of
a loop. The break and continue statements are used to modify the behaviour of
loops.
7. Procedures and functions are types of subroutines. They are a sequence of
instructions that perform a specific or set of tasks.
8. A function is defined using the def keyword.
9. Parameters are variables that store the values passed to a function or procedure.
10. The return statement is used to exit from a function and go back to the point
from where it was called.
11. Global variables are not defined inside any function and have a global scope.
12. Local variables are defined inside a function and their scope is limited to that
function only.
18
15. A two-dimensional (2D) array is an array of arrays creating a grid-like
structure.
b.
c.
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
19
b. What is the use of functions in a program?
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
c. You want to print the multiplication table using a loop. Which loop will
___________________________________________________________________________________
___________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
____________________________________________________________________________________
___________________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
20
Lab Activity
1. Write a Python program to find the square of a number using a function.
2. Write a program in Python to:
Resources
[Link]
Reference Material
[Link]
21