Exam2023_01 Flashcards

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

Ad a) a is true because the code could be simplyfied :

if x:
    # some code
elif not x:
    # some code

if x: will execute the block if x is True.
elif not x: will execute the block if x is False.

This simplification works because x is a boolean, and the if statement directly checks the truthiness of x.

Ad c) c is also true because the code could be simplified further:

if x:
    # some code
else:
    # some code

Since x can only be True or False, the else block will naturally handle the case where x is False.

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

The key operation here is flattened += row, which concatenates each sublist to the flattened list, effectively flattening the nested list.

Using preserved.append(row) statement would append each sublist to the preserved list, maintaining the 2D structure.

def preserve_2d_structure(nested_list):
    preserved = []  # Initialize an empty list
    for row in nested_list:  # Iterate through each sublist in nested_list
        print(row)  # Print the current sublist (for debugging purposes)
        preserved.append(row)  # Append the current sublist to preserved
    return preserved  # Return the preserved 2D list
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A

Note Option (a) is not correct because zip() behaves differently from nested loops in this case. Let me explain the difference:

The original code (nested loops) creates a Cartesian product of list1 and list2, meaning it pairs every element from list1 with every element from list2.

For example, if:

list1 = [1, 2]
list2 = ['a', 'b']

The original code would produce:

[(1,'a'), (1,'b'), (2,'a'), (2,'b')]

However, zip(list1, list2) pairs elements at corresponding indices only, creating:

[(1,'a'), (2,'b')]

zip() creates pairs of elements based on their position in each list, not all possible combinations. It will pair the first elements together, second elements together, and so on, and will stop when the shorter list is exhausted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
A
18
Q
A
19
Q
A
20
Q
A
21
Q
A
22
Q
A
23
Q
A
24
Q
A
25
Q
A
26
Q
A
27
Q
A

E.g.

x=[1,2,3,4,5,6,7,8,9]
x[::-2] # output [9, 7, 5, 3, 1]
28
Q
A
29
Q
A
30
Q
A
31
Q
A
32
Q
A
33
Q
A
34
Q
A
35
Q
A
36
Q
A
37
Q
A
38
Q
A
39
Q
A
  • a1 is a3: This checks if a1 and a3 are the same object in memory. Since a3 is assigned to a1, they reference the same object. Therefore, this is True.
  • a1 == a3: This checks if a1 and a3 are equal. By default, the == operator checks for object identity unless the __eq__ method is overridden in the class. Since a1 and a3 are the same object, this is True.
  • a1 == a2: This checks if a1 and a2 are equal. Since the Animal class does not override the __eq__ method, the default behavior is to check for object identity. a1 and a2 are different objects, so this is False.
  • a1 is a2: This checks if a1 and a2 are the same object in memory. They are not, so this is False.

Let’s clarify the concepts of “object identity” and “object in memory”:

Object Identity
* Object identity refers to whether two references point to the exact same object in memory.
* In Python, the is operator is used to check for object identity.
* If a is b evaluates to True, it means that a and b are references to the same object in memory.

Object Equality
* Object equality refers to whether two objects are considered equal in value.
* In Python, the == operator is used to check for object equality.
* By default, if a class does not override the __eq__ method, the == operator will check for object identity (i.e., it will behave like the is operator).
* If a class overrides the __eq__ method, the == operator will use this method to determine if two objects are equal based on their attributes or other criteria defined in the method.

Object in Memory
* Object in memory refers to the actual location in memory where an object is stored.
* When we say two references point to the same object in memory, it means they both refer to the same memory address.

40
Q
A