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

Computational Thinking-Y8-Part2

The document discusses lists in Python, including how to create and modify lists, add and remove elements, check indices and lengths, and traverse lists using for loops. It also covers validating user input when working with lists to avoid errors and make the programs more robust. Examples are provided for each concept along with some multiple choice and short answer questions to test understanding.

Uploaded by

DMF Dargon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
25 views21 pages

Computational Thinking-Y8-Part2

The document discusses lists in Python, including how to create and modify lists, add and remove elements, check indices and lengths, and traverse lists using for loops. It also covers validating user input when working with lists to avoid errors and make the programs more robust. Examples are provided for each concept along with some multiple choice and short answer questions to test understanding.

Uploaded by

DMF Dargon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 21

3.

1 Make a list
A variable is :
• a named area of storage
• stores one value
A list is:
• A special kind of variable can store several items
• Is shown inside square brackets [ ]
• Each item in a list is called an element
• Items are separated by commas
Append an element
• Append : add to the end
• Rule: list name.append(item)
• Ex: colorlist.append(“orange”)
• Append user input:
Color=input(“enter a color”) get input from user and save it in variable called Color
colorlist.append(Color)

To display the list to user : print(list name)


Ex: print(colorlist)
Loop: is a program structure that repeat commands
use it to add multiple items to the list
• While loop: condition-controlled loop
• for loop: counter controlled loop
• You set the number of times the loop will repeat
• You can use for loop if you know exactly how many elements you
want to add to the list
3.2 Work with list elements
• Index number: tells you the position of an item in the list
• Each item of a list is a variable in its own right
• List numbering starts at 0
• the first element in the goals list is goals[0]
• To print first element: print(goals[0])
• Edit means make changes to something
• You can change a single element of a list by giving it a new value
• Ex: Edit the third element in the list : goals[2]=“buy a car”
• The user choose witch element to edit:
Delete an item from a list
• The command to delete is: del
• Ex..To delete first element: del goals[0]
• The user choose witch element to delete:
Assignment
• Complete missing words:
1. ………….loop controlled by a counter
2. A while loop is a …………………….controlled loop
3. To create a list with name colors write the instruction…………………………….
4. To add a new item to a list use the command……………………..
5. Each item of a list is a ………………………. in its own right
6. Edit means make ……………………………………..
7. Complete the program to input exactly 5 values to colorlist then print colorlist:
Colorlist=[ ]
For i in range(…….)
Color=…………..(“enter a color”)
Colorlist. ……………(………………)
……………(colorlist)
• What is the output if the user write number 1 as an input:

Output: -------------------------------------------------------------------------
• Put (T) or (F):
1. List numbering starts at 0 ( )
2. the second element in the goals list is goals[1] ( )
3. To print first element: print(goals) ( )
4. To delete first element: del goals[0] ( )
5. To edit the second element in the list : goals[2]=“buy a car” ( )
3.3 Block bad input
• Python function len() tell you the length of a string or a list
• Ex: len(“hello”) the output of this command will be 5

colorList=[“red”,”black”,”green”]
Output will be 3 because the list has 3
Listlength=len(colorList) elements
Print(Listlength)
• Allowed number range from 0 to one less than the number of items in the list
• A program which doesn’t crash called a robust program
• To make robust program you need to block bad input
• A check which blocks bad input is called a validation check
• If….else • While loop

Logical test to test the input number • This program will handle out of bounds error
when the user enter a number:
• If the test is true , carry out the command
• Greater than or equal to the length of the list
• If the test is false, show an error message
3.4 traverse a list
• Traversing a list is to look at every value in it
• Example in real life:
• Social media apps wants to check that every user is still active
• Business want to check every item in stock
• Manager wants to send email to every team member
• Traversing a list using for loop
Colorlist=[“red”,”yellow”,”blue”,”green”]
For I in range(4)
Print(I,colorlist[i])
What if you use wrong stop value?
• You have to find list length at first
Stop=len(colorlist)
for I in range(stop):
Print(I,colorlist[i])
Assignment
trees=[“oak”,”beech”,”willow”]
Number=len(trees)
Print (number)
1. What would be the out put of these commands
2. What is the valid index numbers
3. Write a logical test to check whether the user enter a valid index number
4. To traverse this list using for loop what is the stop value
5. What is the command we use to get length of the list
6. “a list can change in size while a program is running” is this statement
true or false and why?
This program is for traversing trees list.what is the output of this
program,find the error in the program and correct it

trees=[“oak”,”beech”,”willow”,”ash”]
For I in range (3):
print(trees[i])
• Create an empty list called teamlist
• Use a while loop to append names to this list
• Use a for loop to traverse this list

You might also like