Composite lists Part 1 Flashcards

1
Q

simple (atomic) variables (4)
interesting fact abt it+3ex+1practical ex+ what it holds

A
  • integer, boolean, float
  • cannot be divided further into smaller components
  • ex: 7
  • hold a single value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Aggregate (composite) (4)
3ex+what they hold+what they do+ex

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to access each element in a list individually: +ex (2)

A

use the name of the list variable and an index
-Ex: print (alist[i])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Format for a list:

A
<list_name> = [<value 1>, <value 2>, <value 3>]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what is the index ranges for a list with 5 elements?

A

0- (5-1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

when creating a list of strings, you need to …

A

enclose the string elements in quotes.

my_list = ["apple", "banana", "cherry"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is happening here:
~~~
aList1 = [” “] * 7
~~~

A

this would mean 7 blank spaces:
~~~
aList1 = [” “, “ “, “ “,” “,” “,” “,” “,]
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What would this mean:
~~~
aList1 = [“-1] * 7
~~~

A

“-1” is repeated “Number_Elements” times
all elementas are -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Because a list is comppsote you can

A

acess the entire list or individual elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Acess the whole list code:

A

print(percentages)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Acess an element of a list code + what you need (2):

A

You need the name of list and an index:
print(percentages[1])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

your Index should be….. ranging from…

A

a positive integer ranging frim zero to (list size- 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

create a variable that refers to empty list

A

listname = []

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does “Append” do in a loop?

A
  • Within the body of a loop it creates each element and then adds the new element on the end of the list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is occuring here? (2)

classGrades = []
def initialize():
    for i in range (0, 4, 1)
         classGrades.append(-1)
A

Each time through the loop: creates new element “-1”
Adds new element to the end of the list

17
Q

Declaring a list variable will result in two memory locations allocated in memory. (2)

A

One memory location is allocated for the list variable, which holds the reference to the list object.

Another memory location is allocated to store the list object itself, which contains the elements of the list.

18
Q

def Initialize

A

Overall, the initialize() function is responsible for creating and initializing a list with a specific length by appending a default value to it.

19
Q

reference

A

A reference is like a pointer or a label that points to the memory location where the object is stored. It allows you to access and modify the object’s data. When you assign a value to a variable or pass an object as an argument to a function, you are actually creating a reference to that object.

20
Q

You can have multiple references referring to the same underlying object in memory. Modifying the object through one reference will affect all other references that point to the same object. Why?

A

In Python, objects are passed by reference, which means that when you assign an object to multiple variables or pass it as an argument to multiple functions, those variables or functions will hold references to the same object in memory. Any modifications made to the object through one reference will be visible through all other references.

21
Q

x = [1, 2, 3]
y = x

x = [4, 5, 6]

print(y)

Output: ?

explain

x = [1, 2, 3]
y = x

x[0] = 4

print(y)

Output:

A

[1, 2, 3]
[4, 2, 3]
In the above example, x and y initially refer to the same list object [1, 2, 3]. However, when we assign a new list [4, 5, 6] to x, it creates a new list object in memory. y still points to the old list object, so its value remains unchanged. Therefore, modifying x does not affect the value of y. It’s important to note that in this case, we are modifying the contents of the list object itself, not reassigning x or y to point to a different list object. Modifying the object itself through one reference will affect all other references to the same object.

22
Q

What is the output?
~~~
num = 123
list1 = [1,2,3]
list2 = list1
print(“Before”)
print(list1)
print(list2)
print(“\nAfter”)
list1[0] = 888
list2[2] = 777
print(list1)
print(list2)
~~~

A
Before
[1, 2, 3]
[1, 2, 3]

After
[888, 2, 777]
[888, 2, 777]
23
Q

Passing a list as a parameter

A

A reference to the list is passed, in the function a local variable which is another reference can allow access to the list

24
Q

Traversing a list means + how to traverse (2)

A

iterating over each element of the list and performing some operation or accessing the elements one by one. It involves visiting each element in the list in a sequential manner.

In Python, you can traverse a list using various techniques such as a for loop or a while