1 of 18

Lesson 6 : Variables

Challenge focus: 41 - 47

Coding Concepts : variables

December 2023

Banana Tales I

Key stage III

Class VII

2 of 18

LESSON OVERVIEW

CODING CONCEPT

Variable

CHALLENGE

41

42

43

44

45

46

47

SUB CONCEPTS

  • Definition of a variable.
  • Rules for creating a variable.
  • Assignment of a value (x=x+2 or x=4) to a variable(=).

RESOURCES

Lesson Plan 26 Variables

December 2023

Banana Tales I

Key stage III

Class VII

3 of 18

Lesson Objectives

At the end of the lesson, each child should be able to:

  • define a variable.
  • use a variable to store a numerical value.
  • assign to a variable using an arithmetic expression.
  • solve Challenges 41 - 47.

December 2023

Banana Tales I

Key stage III

Class VII

4 of 18

Understanding Variables

  • A variable can be thought of as a container that holds one value at a time.
  • A variable storing certain value can be related to a bag storing fruits in it.

December 2023

Banana Tales I

Key stage III

Class VII

5 of 18

Understanding Variables

  • The value can be any kind of data (number, string, or list).
  • A variable is referred to it, with a name.
  • An assignment operator is used to assign a value to a variable.

my_name = “Tshewang”

my_num= 23

my_menu=[“chicken”, “Rice”, “egg”]

Variable name

String

Number

List

Assignment operator

December 2023

Banana Tales I

Key stage III

Class VII

6 of 18

Understanding Variables

  • A variable's value can be changed.
  • If you want to change the value of the variable, you can assign a different number/ string/ list to it.

Example:

my_var = 2

Change the value:

my_var = 5

Now the value of my_var is 5.

December 2023

Banana Tales I

Key stage III

Class VII

7 of 18

Variable Naming Rules

  • We must adhere to guidelines and rules when creating variables in Python.

Variable name must start with a letter (a-z, A-Z) or an underscore (_).

Example: name, _name

Reserved keywords cannot be used as a variable name.

Example: print, for, in, and, or, etc

Variable names cannot contain spaces or most special characters, except underscores (_).

Example: first name, name#123

A variable name cannot start with a numeric value.

Example: 89age, 123name.

Variable names are case-sensitive.

Example: age and Age are different.

Variable naming

Coding Adventure Part I

Key stage II

Class IV

8 of 18

Activity 1

Study the challenge given below to answer the questions. (challenge 41):

  1. List all the variables.
  2. Identify the bugs in the given codes and rewrite them so that the banana reach to the monkey.

December 2023

Banana Tales I

Key stage III

Class VII

9 of 18

Activity 1 Solution

  1. The variables are:
  2. goal_length
  3. index

1

2

3

4

5

goal_length = snakes[3].length

print(“goal_length is:”)

print(goal_length)

for index in range(3):

snakes[index].length = goal_length

December 2023

Banana Tales I

Key stage III

Class VII

10 of 18

Demo 1

Study the given challenge below carefully and answer the questions that follows (challenge 42).

  1. Create a variable “height” and store the height of the first giraffe.
  2. Print the height of the giraffe stored in the variable “height”.
  3. Complete the given codes so that the banana reaches the monkey.

1

2

for i in range(1, 8):

…………………………………….

December 2023

Banana Tales I

Key stage III

Class VII

11 of 18

Demo 1 Solution

1

height = giraffes[0].height

2

print(“The height of the first giraffe is”,height)

1

2

3

height = giraffes[0].height

for i in range(1,8):

giraffes[i].height = height

Coding Adventure Part I

Key stage II

Class IV

12 of 18

Activity 2

Study the given challenge below and answer the questions that follow (challenge 45).

  1. List down the variables if there are any.
  2. How does the value of those variables change after each iteration?

December 2023

Banana Tales I

Key stage III

Class VII

13 of 18

Activity 2 Solution

  • List down the variables if there are any.

Ans: There are two variables. They are height and giraffe.

  • How does the value of those variables change after each iteration?

Iteration

Value of h

Value of g

1st

h = 4

g = giraffes[0]

2nd

h = 5

g = giraffes[1]

3rd

h = 6

g = giraffes[2]

4th

h = 7

g = giraffes[3]

5th

h = 8

g = giraffes[4]

6th

h = 9

g = giraffes[5]

December 2023

Banana Tales I

Key stage III

Class VII

14 of 18

Activity 3

  • Log into your CodeMonkey account.
  • Open Coding Adventure Part 1
  • Complete challenges 41 to 47.

December 2023

Banana Tales I

Key stage III

Class VII

15 of 18

Activity 3 Solution

44

December 2023

Banana Tales I

Key stage III

Class VII

16 of 18

Activity 3 Solution

46

December 2023

Banana Tales I

Key stage III

Class VII

17 of 18

Activity 3 Solution

47

December 2023

Banana Tales I

Key stage III

Class VII

18 of 18

Key Points

  • A variable acts like a container that stores data when we place data in it.
  • A variable is a named reference to a value stored in the computer's memory.
  • It is a symbolic name that represents a memory location where data is stored.
  • Variables are used to store and manipulate data during the execution of a program.
  • The assignment operator (=) is used to assign a value to a variable.
  • Variable name must start with a letter (a-z, A-Z) or an underscore (_).
  • Reserved words or keywords cannot be used as a variable name.
  • Variable names cannot contain spaces or most special characters, except underscores (_).
  • A variable name cannot start with a numeric value.

December 2023

Banana Tales I

Key stage III

Class VII