NOS Presentation - Chapter 3 Grade 8-Python3.1 - Edited
NOS Presentation - Chapter 3 Grade 8-Python3.1 - Edited
Variables
A variable: is named area of storage. A variable stores one value or piece of data.
Examples:
Number= 5
Color=“red”
Lists
list: is a special kind of variable. A list can store several different items of data. A list is
shown inside square brackets.
Example1:
A computer company sold devices in a range of colors. This command will make a list
of those colors . This list is called colorlist
Each item in a list is called an element. This list has four elements. The elements are
separated by commas.
Append an element
Append means ”add to the end ”. You can append items to a list. That means a new
item will be added to the list. This command will append “black” to the list
Append user input:
You can get input from the user and append that value to the list.
color=input(“Enter a color”)
colorlist.append(color)
Example1:
A computer company sold devices in a range of colors. This python command will make a list of those colors.
This list has four elements.
Make and run a program which:
1. Creates an empty list called teamlist.
2. Gets 4 names from the user.
3. Appends the names to the list.
4. Print the list.
Append elements using a loop:
You want to add more than one element. A loop is a program structure that repeats commands. By using a
loop structure, you can add multiple elements to the list.
A for loop is controlled by a counter. It is a counter-controlled loop. Most programmers name the counter i. The
program will still work if you use another name for the counter.
When you write the program, you set the number of times the loop will repeat.
You can use a for loop if you know exactly how many elements you want to add to a list.
Make and run a program which:
1. Creates an empty list called teamlist.
2. Uses a for loop to append 11 names to the list.
3. Print the list.
Using a “while” loop:
A while loop is controlled by a logical test. It is condition-controlled loop. When you write the program, you set
a logical test. When the test is False the loop will stop. You can use a while loop if you don’t know exactly how
many elements you want to add to a list.
Here is an example. This program adds values to colorlist. The loop is controlled by a question:
Do you want to add another? (Y/N)
If the user types ‘Y’, the loop will repeat. If they type anything else, the loop will stop.
Make and run a program which:
1. Creates an empty list called teamlist.
2. Uses a while loop to append 11 names to the list.
3. Print the list.
Resources :
Page 62-65, A., Howard, L., & Held, K. (2020). Computing. 8, student
book. Oxford University Press.
شكًر ا
Thank you