G11_C6
Introduction to lists
Activity Flow | Slide No. | Topic | Time |
---|---|---|---|
TA | 3 | Ice-breaker | 1 min |
5-10 | Programming Intro | 2 min | |
11-16 | Object Oriented Programming | 3 min | |
17-22 | Introduction to Python + Spyder + Pygame | 3 min | |
23-28 | TA - Coding | 15 | |
SA | 29-31 | SA | 5 min |
Wrap - Up | 32-36 | Quiz | 1 min |
38 | Additional Activity | 5 min |
Class Structure
Slide No. | Topic |
---|---|
11 | Random number code |
23-25 | Basic pygame code |
31-32 | Making rectangles |
36 | SA Code- Creating enemy rect. |
45 | Additional Activity- Code |
Preparation and Reference
Pre-Requisites
FOR TEACHER
FOR STUDENTS
-
Computer with an Internet connection.
-
The latest browser installed.
-
Spyder IDE.
-
Projector to present the screen.
1. Computer with an Internet connection.
2. The latest browser installed.
Which loop would we preferably use if we want to perform a task for a fixed number of times?
Q1.
for loop
- A for loop is better choice when the task has to performed for a predefined number of times.
- A while loop is more useful when the decision on whether to preform a task depends on some condition that needs to be dynamically changed.
What will be the output of the following code?
Q2.
infinitely print 0
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
Revise!!
carryOn = True
while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
pygame.quit()
The game loop
Learning Programming
B
Variables that hold multiple values
Single value
Multiple values
List
Learning Programming
B
Lists in real life
12
56
90
Roll No.s:
roll_no=[12,56,90]
Box Brackets
Comma
roll_no=[12,56,90]
print(roll_no)
Adding items to a list
67
39
List to which we want to append an item
Here: roll_no
append function:
appends one item to list
item to be appended to list
dot operator: helps the append function understand which list it has to work on.
12
56
90
Roll No.s:
roll_no=[12,56,90]
print(roll_no)
roll_no.append(67)
print(roll_no)
roll_no.append(39)
print(roll_no)
Removing items from a list
remove function:
removes one item to list
item to be removed
from list
dot operator: helps the remove function understand which list it has to work on.
List from which we want to remove
an item
Here: roll_no
90
67
39
Roll No.s:
12
56
90
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)
GREAT!
Create a list of numbers 1 to 100.
numbers=[1,2,3,4,5,6,...]
Manual list creation
Deploying loops to create lists
numbers=[]
for i in range(1,101):
numbers.append(i)
1.Create an empty list
2. Use a for loop to iterate through desired range.
3.Append the desired element to the created list.
Recreating the red bricks
1.Create an empty list
2.Use a for loop to create rectangle object
and add to the empty list
X6 times
bricksR=[]
for i in range(6):
brick=pygame.Rect(10 + i* 100,60,80,30)
red_bricks.append(brick)
List comprehension
Too many steps!!
What item to append?
How many times to append?
1.Create an empty list
2.Use a for loop to create rectangle object
and add to the empty list
X6 times
bricksR=[pygame.Rect(10 + i* 100,60,80,30) for i in range(6)]
Drawing the bricks
for i in bricksR:
pygame.draw.rect(screen,RED,i)
Code to be able to see the orange bricks on the screen
SA1: Orange bricks
bricksO=[pygame.Rect(10 + i* 100,100,80,30) for i in range(7)]
for i in bricksO:
pygame.draw.rect(screen,ORANGE,i)
ORANGE = [255,100,0]
Removing a brick on collision
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)
Code to be able to see the yellow bricks on the screen
SAA1: Yellow bricks
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]
Copy of G11 C6
By Sanjukta Bhattacharya
Copy of G11 C6
- 109