1 of 23

Lesson 5: range( ) Function

Challenge focus: 31 - 40

Coding Concepts : range( )

December 2023

Banana Tales I

Key stage III

Class VII

2 of 23

LESSON OVERVIEW

CODING CONCEPT

Range

CHALLENGE

31

32

33

34

35

36

37

38

39

40

SUB CONCEPTS

  • range( ) Function.
  • The syntax to define the range() Function.
  • Using the range( ) function to generate index of the list
  • Use of for loop to extract elements of the list.
  • print( ) function

RESOURCES

Lesson Plan 25 Range

December 2023

Banana Tales I

Key stage III

Class VII

3 of 23

Lesson Objectives

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

  • define range( ) function with correct syntax.
  • use the range() function for iteration.
  • use the print() function to display the values of variables.
  • solve Challenges 31 - 40.

December 2023

Banana Tales I

Key stage III

Class VII

4 of 23

Understanding range( ) Function

  • The range() function is used to generate a sequence of numbers. It starts from 0 (by default), increments by 1 (by default) and ends at a specified number.
  • The syntax to use the range( ) function:

range([start], [stop], [step])

  • The range will have one, two or three arguments.
    1. start (optional) - from which number to start in sequence; the default is 0.
    2. stop (required) - at which number to stop the sequence; this number is not included in the sequence; the last number in the sequence will be stop minus one.
    3. step (optional) - by how much to increment the numbers; the default is 1.

December 2023

Banana Tales I

Key stage III

Class VII

5 of 23

Example

Usages

Description

Sequenced Created

Comments

range(4)

range() function with one argument

0, 1, 2, 3

start = 0, stop = 4, step = 1

range( 2, 7)

range() function with two arguments

2, 3, 4, 5, 6

start = 2, stop = 7, step = 1

range(2, 11, 2)

range() function with three arguments

2, 4, 6, 8, 10

start = 2, stop = 11, step = 2

December 2023

Banana Tales I

Key stage III

Class VII

6 of 23

The for Loop with range( ) Function

  • When a for loop is used with range(), the loop repeats for all items in the sequence created by the range( ).
  • The syntax to use a for loop with range() is:

1

2

for loop_variable in range([start], [stop], [step]):

#Your code here

December 2023

Banana Tales I

Key stage III

Class VII

7 of 23

Example 1

Write a Python code in your notebook to display the sequence of number from 1 to 5 using the range( ) function.

1

2

for x in range(1,6,1):

print(x)

start = 1

stop = 5+1 = 6

step = 1

December 2023

Banana Tales I

Key stage III

Class VII

8 of 23

Concept explanation of above example

Example 1 (Cont.)

iteration

x

Condition ( x < 6 )

print(x)

1st

1

1 < 6 = True

1

2nd

2

2 < 6 = True

2

3rd

3

3 < 6 = True

3

4th

4

4 < 6 = True

4

5th

5

5 < 6 = True

5

6th

6

6 < 6 = False

The condition is false and it will exit out of the loop

The following table shows the iteration concept of the for loop from previous slide.

1

2

for x in range(1,6,1):

print(x)

1

2

3

4

5

# Output

1

2

3

4

5

December 2023

Banana Tales I

Key stage III

Class VII

9 of 23

Demo 1

Study the given challenge and write a code using the range( ) function to reach the banana to the monkey(challenge 31).

December 2023

Banana Tales I

Key stage III

Class VII

10 of 23

Demo 1 Solution

December 2023

Banana Tales I

Key stage III

Class VII

11 of 23

Demo 1

This table shows how the giraffes’ height get changed as per the value of index variable as the for loop iteration happens.

Iteration

Value of index

Giraffes’ height

1st

index = 0

giraffes[0].height = 10

2nd

index = 1

giraffes[1].height = 10

3rd

index = 2

giraffes[2].height = 10

4th

index = 3

giraffes[3].height = 10

5th

index = 4

giraffes[4].height = 10

6th

index = 5

giraffes[5].height = 10

December 2023

Banana Tales I

Key stage III

Class VII

12 of 23

Activity 1

Study the challenge given below and rewrite the codes using the range( ) function to make the banana reach to the monkey in your notebook.(challenge 32).

December 2023

Banana Tales I

Key stage III

Class VII

13 of 23

Activity 1 Solution

1

2

for i in range(4):

snakes[i].length = 3

December 2023

Banana Tales I

Key stage III

Class VII

14 of 23

Activity 2

  • Log into your CodeMonkey account.
  • Open Banana Tales Part I
  • Complete challenges 31 to 40.

December 2023

Banana Tales I

Key stage III

Class VII

15 of 23

Activity 2 Solution

33

December 2023

Banana Tales I

Key stage III

Class VII

16 of 23

Activity 2 Solution

34

December 2023

Banana Tales I

Key stage III

Class VII

17 of 23

Activity 2 Solution

35

December 2023

Banana Tales I

Key stage III

Class VII

18 of 23

Activity 2 Solution

36

December 2023

Banana Tales I

Key stage III

Class VII

19 of 23

Activity 2 Solution

37

December 2023

Banana Tales I

Key stage III

Class VII

20 of 23

Activity 2 Solution

38

December 2023

Banana Tales I

Key stage III

Class VII

21 of 23

Activity 2 Solution

39

December 2023

Banana Tales I

Key stage III

Class VII

22 of 23

Activity 2 Solution

40

December 2023

Banana Tales I

Key stage III

Class VII

23 of 23

Key Points

  • The range() function is used to generate a sequence of numbers. It creates a range object that represents a sequence of numbers within a specified range.
  • The syntax to use the range( ) function is range([start], [stop], [step])
  • The range can have one, two or three arguments.
    • start (optional) - from which number to start the sequence; the default is 0.
    • stop (required) - at which number to stop the sequence; this number is not included in the sequence; the last number in the sequence will be stop minus one.
    • step (optional) - by how much to increment the numbers; the default is 1.

December 2023

Banana Tales I

Key stage III

Class VII