0% found this document useful (0 votes)
22 views14 pages

NOS Presentation - Chapter 3 Grade 8-Python3.1 - Edited

Uploaded by

michelsindaha11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views14 pages

NOS Presentation - Chapter 3 Grade 8-Python3.1 - Edited

Uploaded by

michelsindaha11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

C0MPUTATIONAL THINKING

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)

Here is the complete program.


Print a list:
You can print a list just like any other variable.
print(colorlist)

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.

There are two types pf loop in python :


• for loop(counter loop)
• while loop (conditional loop)
Using a “for” loop

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy