0% found this document useful (0 votes)
6 views21 pages

G8 - Unit 2 - Coding

Grade 8 coding IGCSE ict notes

Uploaded by

demblasamaira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views21 pages

G8 - Unit 2 - Coding

Grade 8 coding IGCSE ict notes

Uploaded by

demblasamaira
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit 2: Coding

Learning Objectives
Learners will learn to:
 understand and use the concept of iteration

 understand what is meant by procedures, functions and parameters

 define and use procedures and functions, with or without parameters

 understand and use local and global variables

 declare and use one-dimensional (1D) and two-dimensional (2D) arrays

 understand the use of arrays, write values into and read values from an array

using iteration

Vocabulary
iteration
loop
arrays
list

Let Us Start

Hey! I have been learning Python


Of course! In programming, iteration
recently, and I am curious about
iteration concept. Can you refers to the process of repeating a set
explain what it is? of instructions or a block of code
multiple times. Python provides
several ways to achieve iteration, such
as using loops. Let's learn about loops.

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

met. It is an important programming concept and is used to automate repetitive tasks.

Iteration is often referred to as looping since the program ‘loops’ back to an earlier

line of code. Iteration is also known as repetition.

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

 Condition-controlled loop - Precondition loops and Postcondition 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

reaches a specific value

Example: Program using a for loop as a counter-controlled loop.


Program

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

used in ‘for’ loops to specify the number of iterations.

The syntax of the range() function is

range (start, stop, step_size)

start: The starting value of the sequence. The default value is 0.

stop: The ending value of the sequence.

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

‘stop’ value, the ‘start’ value is taken as 0 and ‘step_size’ is taken as 1 by

default.

 range(1, 11) will generate a sequence of numbers from 1 to 10.

 range(1, 11, 2) will generate a sequence of odd numbers from 1 to 10 by

taking step_size 2.

 range(2, 11, 2) will generate a sequence of even numbers from 2 to 10 by

taking the step_size 2.

Example: Program that uses a ‘for’ loop to print the numbers 1, 3, 5, 7, and 9. It uses

the range() function to generate a sequence.


Program

4
Output

Looping through a string


You can loop through each character in a string using a for loop. Since a string is a

sequence of characters, you can treat it as a sequence.

Example: The following program prints each character of the string message on a

separate line using a for loop.


Program

Output

5
Activity 1
Type the given program to find the sum of even numbers in Python. Run the

program and find the output if the user enters 25.

Condition controlled loop


The condition-controlled loop is implemented as a ‘while’ loop. The ‘while’ loop in

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.

In a ‘while’ loop, a test_expression or condition is checked before entering the loop.

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

process is repeated until the result of the test_expression becomes False.

Example: Program using a while loop.


Program

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

false, and the loop exits.

The 'break' and ‘continue’ statements


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.

The break statement


The ‘break’ statement exits a loop in between based on a condition. When a program

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

odd numbers, if you do not wish to print beyond a particular

number, you can use a break statement.

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

the numbers from 1 to 5 are printed.

7
Program

Output

The continue statement


The ‘continue’ statement is used to go ahead to the next iteration of the loop without

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

print the number 5, use the continue statement with a condition.

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

iteration. This causes only the odd numbers to be printed.

8
Program

output:

Activity 2
1. Write a program in Python to print all even numbers from 1 to 20. Break the loop

when the value of a number is 10.

2. Write a program in Python to print all numbers from 1 to 10 except for the

number 5.

2.2 Procedures and Functions


Procedures and functions are types of subroutines. They are a sequence of instructions

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

can be used to:

 Avoid duplicating code and can be reused throughout a program,

 Improve the readability and maintainability of code,

 Perform calculations, to retrieve data, or to make decisions based on input.

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

the print function prints it out to the screen.


Program

Output

Input Parameters
A function can accept input data to work on. These are called parameters. Parameters

are variables that store the values passed to a function or procedure.

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,

you need to pass the values of the parameters to the function.

Example: Program in which you define a function to add two integers

10
Returning data
A function can also return data. The return statement is used to exit from a function

and go back to the point from where it was called.

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

and their scope is limited to that function only.

Local Variables
Local variables in are those which are initialized inside a function and belong only to

that particular function. It cannot be accessed anywhere outside the function.

Example: Program using local variable

Program

Output

Global Variables
Global variables are defined outside any function and which are accessible

throughout the program, i.e., inside and outside of every function.

Example: Program using global variable

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

 a size - the number of elements it will hold

Once declared, the name and structure of an array cannot be changed. The position

of each element in an array is identified using the index of the array.

One-dimensional array
A one-dimensional array can be referred to as a list. It is a collection of items of the

same data type.


Creating a list
mylist=[1,2,3] # creates a list of integers 1, 2, 3

mylist=[1, “A”, -5.4] # creates a list of mixed data types

mylist=[] # creates an empty list

print(mylist) # prints the list

Example: Program using the list


Program

14
Output

Accessing elements of a list


You can use the index [] operator to access the items of a list. The index value always

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

Accessing elements in two-dimensional arrays


The data elements in two-dimensional arrays can be accessed using two indices. One

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

that index position.


Program

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’

loops to specify the number of iterations.

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.

13. An array is a data structure that holds similar, related data.


14. A one-dimensional array can be referred to as a list.

18
15. A two-dimensional (2D) array is an array of arrays creating a grid-like

structure.

Check for Understanding


1. What will be the output of the following program?
a.

b.

c.

2. Answer the following questions.

a. What is the difference between a ‘while’ and a ‘for’ loop?


___________________________________________________________________________________

___________________________________________________________________________________

___________________________________________________________________________________

___________________________________________________________________________________

___________________________________________________________________________________

___________________________________________________________________________________

___________________________________________________________________________________

___________________________________________________________________________________

___________________________________________________________________________________

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

you select for the same and why?

___________________________________________________________________________________

___________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

___________________________________________________________________________________

d. Look at the given Python program and answer the questions.

i. Write the parameters specified in the program.

________________________________________________________________________

ii. What will be the output of the program?

________________________________________________________________________

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:

a. create a list of integers from 1 to 20

b. display only odd numbers from the list

c. calculate the sum of the even numbers

Resources
[Link]

Reference Material
[Link]

21

You might also like