Composite lists Part 1 Flashcards
simple (atomic) variables (4)
interesting fact abt it+3ex+1practical ex+ what it holds
- integer, boolean, float
- cannot be divided further into smaller components
- ex: 7
- hold a single value
Aggregate (composite) (4)
3ex+what they hold+what they do+ex
- lists, tuples, strings
- data types that can hold multiple values or elements together as a single unit.
- they aggregate or combine several individual elements into a composite structure
- Ex: a string (sequence of charaters) can be decomposed into individuals characters
Write a program that wll track the percentage grades for a class of students. The program should allow the user to enter the grade for each student. Then it will display the grades for the whole class along with the average.
def initialize(): classGrades = [] # Create a list with 5 elements with each element initialized to zero # Within the body of the loop create a new list element that contains 0 # and append / add it to the end of the list. for i in range (0, CLASS_SIZE, 1): classGrades.append(-1) return(classGrades) Step through the list an element at a time and get the user to input the # grades. def read(classGrades): total = 0 average = 0 for i in range (0, CLASS_SIZE, 1): # Because list indices start at zero add one to the student number. temp = i + 1 print("Enter grade for student no.", temp, ":") classGrades[i] = float(input (">")) total = total + classGrades[i] average = total / CLASS_SIZE return(classGrades, average) Traverse the list and display the grades. def display(classGrades, average): print() print("GRADES") print("The average grade is %0.2f%%" %(average)) for i in range (0, CLASS_SIZE, 1): # Because list indices start at zero add one to the student number. temp = i + 1 print("Student No. %d: %0.2f%%" %(temp,classGrades[i])) PROGRAM BEGINS EXECUTION HERE def start(): classGrades = initialize() classGrades, average = read(classGrades) display(classGrades,average) start()
How to access each element in a list individually: +ex (2)
use the name of the list variable and an index
-Ex: print (alist[i])
Format for a list:
<list_name> = [<value 1>, <value 2>, <value 3>]
what is the index ranges for a list with 5 elements?
0- (5-1)
when creating a list of strings, you need to …
enclose the string elements in quotes.
my_list = ["apple", "banana", "cherry"]
What is happening here:
~~~
aList1 = [” “] * 7
~~~
this would mean 7 blank spaces:
~~~
aList1 = [” “, “ “, “ “,” “,” “,” “,” “,]
~~~
What would this mean:
~~~
aList1 = [“-1] * 7
~~~
“-1” is repeated “Number_Elements” times
all elementas are -1
Because a list is comppsote you can
acess the entire list or individual elements
Acess the whole list code:
print(percentages)
Acess an element of a list code + what you need (2):
You need the name of list and an index:
print(percentages[1])
your Index should be….. ranging from…
a positive integer ranging frim zero to (list size- 1)
create a variable that refers to empty list
listname = []
What does “Append” do in a loop?
- Within the body of a loop it creates each element and then adds the new element on the end of the list