G11_C6_TeacherCopy

Introduction to lists

Activity Flow Slide No. Topic Time
TA 4-7 WarmUp Quiz 3 min
8-9 Revision 2 min
10-21 Introduction to Lists 10 min
22-30 Creating and Iterating through Lists 10  min
SA 31-33 Brick Creation 5 min
TA 34 Precap 2 min
Wrap - Up 35-38 Quiz 3 min
SA 39-44 Additional Activity 10 min

Class Structure

Slide No. Topic
15 TA1 Solution
18 TA2 Solution
21 TA3 Solution
27 TA4 Solution
29 TA5 Solution
31 TA6 Solution
34 SA1 solution
41 SAA1 solution
43 SAA2 Solution

Preparation and Reference

Prerequisites

FOR TEACHER

FOR STUDENTS

 

  1. Computer with an Internet connection.

  2. The latest browser installed.

  3. Spyder IDE.

  4. Projector to present the screen.

1. Computer with an Internet connection.

2. The latest browser installed.

3.Spyder IDE.

(WARM-UP QUIZ)

Which loop we would preferably use if we want to perform a task a fixed number of times? 

Q1.

for loop

  • A "for" loop is a better choice when the task has to be performed a predefined number of times.

  • A "while" loop is more useful when the decision on whether to perform a task depends on some condition that needs to be dynamically changed.

What will be the output of the following code? 

Q2.

print 0 infinitely

for i in range(10):
  prod=10*i
  while prod<=10:
    print(prod)

check again

condition never becomes false!!

for i in range 10:

for loop starts: i=0

prod=10*0=0

prod<=10? : True

prod which is "0" is printed

carryOn = True
while carryOn:
    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                  carryOn = False  
                  pygame.quit()

Revision

Learning Programming

B

Variables that hold multiple values

Single value

Multiple values

List

Learning Programming

B

roll_no=[12,56,90]

Box Brackets

Comma

Lists in real life

roll_no=[12,56,90]
print(roll_no)

TA1:Solution

list.append(item)

dot operator: helps the append function understand which list it has to work on.

Adding items to a list

List to which we want to append an item

Here: roll_no

item to be appended to list

append function:

appends one item to list

roll_no=[12,56,90]
print(roll_no)
roll_no.append(67)
print(roll_no)
roll_no.append(39)
print(roll_no)

TA2 :Solution

list.remove(item)

Removing items from a list

dot operator: helps the remove function understand which list it has to work on.

List to which we want to remove an item

Here: roll_no

item to be removed

from list

remove

function:

removes one item to list

roll_no=[12,56,90]
print(roll_no)
roll_no.append(67)
print(roll_no)
roll_no.append(39)
print(roll_no)
roll_no.remove(90)
print(roll_no)

TA3: Solution

GREAT!

Manual list creation

Create a list of numbers 1 to 100.

numbers=[1,2,3,4,5,6,...]
numbers=[]
for i in range(1,101):
  numbers.append(i)

1. Create an "empty" list

2. Use a "for" loop to iterate through the desired range.

3. Append the desired element to the created list.

Deploying loops to create lists

Recreating the red bricks

[
]
[
]

1. Create an "empty" list

2. Use a "for" loop to create a rectangle object

and add to the "empty" list.

+

X 6 times

bricksR=[]
for i in range(6):
  brick=pygame.Rect(10 + i* 100,60,80,30)
  red_bricks.append(brick)

TA4: Solution

List comprehension

Too many steps!!

[
]
for
i
in
range(6)

What item to append?

How many times to append?

[
]
[
]

1. Create an "empty" list

2. Use a "for" loop to create

    rectangle object and it add to the "empty" list.

+

X6 times

bricksR=[pygame.Rect(10 + i* 100,60,80,30) for i in range(6)]

TA5: Solution

Drawing the bricks

[
]
for
i
in
list:
for i in bricksR:
        pygame.draw.rect(screen,RED,i)

TA6: Solution

Write to code to see the orange bricks on the screen.

SA1: Orange bricks

Hints:

bricksO=[pygame.Rect(10 + i* 100,100,80,30) for i in range(6)]
for i in bricksO:
        pygame.draw.rect(screen,ORANGE,i)
ORANGE = [255,100,0]

SA1: Solution

Removing a brick on collision

[
]
for
i
in
list:

Remove brick

What will be the output:

 

B

2,3,4,5,6,7

Error

No output

Q.1

A

Error

B

C

Since 10 is not present it will throw us a value error.

num=[2,3,4,6,7]
num.remove(10)
print(num)

What will be the output:

 

B

2,3,4,6,7,10

Error

10,2,3,4,6,7

Q.2

A

Error

B

C

The syntax is :

list.append(item)

num=[2,3,4,6,7]
append(num,10)
print(num)

Write a code to see the yellow bricks on the screen

SAA1: Yellow bricks("For" loop)

Hints:

bricksY=[]
for i in range(7):
  brick=pygame.Rect(10 + i* 100,140,80,30)
  bricksY.append(brick)
for i in bricksY:
        pygame.draw.rect(screen,YELLOW,i)
YELLOW = [255,255,0]

SAA1: Solution

Code to be able to see the yellow bricks on the screen

SAA2: Yellow bricks(List Comprehension)

Hints:

bricksY=[pygame.Rect(10 + i* 100,140,80,30) for i in range(7)]
for i in bricksY:
        pygame.draw.rect(screen,YELLOW,i)
YELLOW = [255,255,0]

SAA2: Solution

Activity Activity Name Link
TEACHER ACTIVITY 1,2,3 List creation
TEACHER ACTIVITY 4 Red brick creation 
TEACHER ACTIVITY 5 List comprehension
TEACHER ACTIVITY 6 List iteration
TEACHER ACTIVITY 1 SOLUTION Solution of TA1
TEACHER ACTIVITY 2 SOLUTION Solution of TA2
TEACHER ACTIVITY 3 SOLUTION Solution of TA3
TEACHER ACTIVITY 4 SOLUTION Solution of TA4
TEACHER ACTIVITY 5 SOLUTION Solution of TA5
TEACHER ACTIVITY 6 SOLUTION Solution of TA6
STUDENT ACTIVITY 1 Orange Brick Creation
TEACHER REFERENCE: STUDENT ACTIVITY 1 SOLUTION Solution of SA1
STUDENT ADDITIONAL ACTIVITY 1 Yellow brick creation
STUDENT ADDITIONAL ACTIVITY 2 Yellow brick creation
(List Comprehension)
TEACHER REFERENCE: STUDENT ADDITIONAL ACTIVITY 1 SOLUTION Solution of SAA1
TEACHER REFERENCE: STUDENT ADDITIONAL ACTIVITY 2 SOLUTION Solution of SAA2

G11 C6_TeacherCopy

By Sanjukta Bhattacharya

G11 C6_TeacherCopy

  • 169