Mod 3 Flashcards

1
Q

What is the output:

2==2.0

A

True

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

What is the output:
x = 10

if x == 10:
    print(x == 10)
if x > 5:
    print(x > 5)
if x < 10:
    print(x < 10)
else:
    print("else")
A

True
True
else

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

What is the output:
x = “1”

if x == 1:
    print("one")
elif x == "1":
    if int(x) > 1:
        print("two")
    elif int(x) < 1:
        print("three")
    else:
        print("four")
if int(x) == 1:
    print("five")
else:
    print("six")
A

four

five

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
x = 1
y = 1.0
z = "1"
if x == y:
    print("one")
if y == int(z):
    print("two")
elif x == y:
    print("three")
else:
    print("four")
A

one

two

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

What is the output:
for i in range (2, 8, 3):
print(i)

A

2

5

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

What does break do in a loop?

A

Exits the loop. The program begins to execute the nearest instruction after the loops body

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

What does continue do in a loop?

A

Skips the current iteration and continues with the next.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
What is the output:
print("The break instruction:")
for i in range(1, 6):
    if i == 3:
        break
    print("Inside the loop.", i)
print("Outside the loop.")
A

The break instruction:
Inside the loop. 1
Inside the loop. 2
Outside the loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
What is the output:
print("The continue instruction:")
for i in range(1, 6):
    if i == 3:
        continue
    print("Inside the loop.", i)
print("Outside the loop.")
A
The continue instruction:
Inside the loop. 1
Inside the loop. 2
Inside the loop. 4
Inside the loop. 5
Outside the loop.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
What is the output:
i = 1
while i < 5:
    print(i)
    i += 1
else:
    print("else:", i)
A
1
2
3
4
else: 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
What is the output:
for i in range(5):
    print(i)
else:
    print("else:", i)
A
0
1
2
3
4
else: 4
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What type of loop executes a statement or a set of statements as long as a specified boolean condition is true?

A

while

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

What type of loop executes a set of statements many times, used to iterate over a sequence (often used with the range function)

A

for

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

In while and for loops the else clause executes after the loop finishes its execution as long as it has not been terminated by ____.

A

break

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

Provide the range function syntax

A

range(start, stop, step)

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

What is the output of:
for i in range(5):
print(i, end=” “)

A

0 1 2 3 4

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

What is the output of:
for i in range(6, 1, -2):
print(i, end=” “)

18
Q

What are the Bitwise Operations?

A

& conjunction (and)
| disjunction (or)
^ exclusive or
~ negation

19
Q

Rules and summary of outputs of bitwise operators

A

& requires exactly two 1s to provide 1 as the result.
| requires at least one 1 to provide 1 as the result.
^ requires exactly one 1 to provide 1 as the result.
The arguments must be integers (no floats)
They deal with every bit seperately

20
Q
What is the output of:
var = 6
varRight = var >> 1
varLeft = var << 2
print(var, varLeft, varRight)
21
Q

List facts (4):

A
  • Ordered and mutable
  • Collection of comma separated items (int, float, string, boolean, etc) in square brackets
  • Index starts with 0
  • Can be indexed, updated, nested, iterated, sorted, sliced and elements can be deleted
22
Q

How to assign 111 to the 2nd element:

numbers = [10, 5, 7, 2, 1]

A

numbers[1] = 111

23
Q

How to delete the 2nd element:

numbers = [10, 111, 7, 2, 1]

A

del numbers[1]

*note cannot access an element that doesnt exist, will cause a runtime error

24
Q
What is the output of:
numbers = [ 10, 7, 2, 1]
print(numbers[-1])
print(numbers[-4])
print(numbers[-5])
A

1
10
IndexError: list index out of range

25
Function vs Method (3)
- Function owned by the whole code - Method is owned by the data it works for - Method can change the data of a selected entity
26
Use the append method to add a value to the below list and how to show the final list: numbers = [ 10, 7, 2, 1]
numbers.append(111) print(numbers) output: [10, 7, 2, 1, 111]
27
Use the insert method to add a value to the below list in the 4th place position and how to show the final list: numbers = [10, 7, 2, 1, 111]
numbers.insert(3, 54) print(numbers) output: [10, 7, 2, 54, 1, 111]
28
What is the output: lst = [1, [2, 3], 4] print(lst[1]) print(len(lst))
[2, 3] | 3
29
What is the output: lst = [1, 2, 3, 4, 5] lst2 = [] add = 0 for number in lst: add += number lst2.append(add) print(lst2)
[1, 3, 6, 10, 15]
30
``` What is the output: list1 = [1] list2 = list1 list1[0] = 2 print(list2) ```
[2]
31
``` What is the output: list1 = [1] list2 = list1[:] list1[0] = 2 print(list2) ```
[1]
32
``` What is the output: myList = [10, 8, 6, 4, 2] newList = myList[1:3] newList1 = myList[1:-1] newList2 = myList[-1:1] print(newList) print(newList1) print(newList2) ```
[8, 6] [8, 6, 4] []
33
What is the output: myList = [0, 3, 12, 8, 2] print(5 in myList) print(5 not in myList) print(12 in myList)
False True True
34
``` What is the output: myList = [1, 2, 3, 4, 5] sliceOne = myList[2: ] sliceTwo = myList[ :2] sliceThree = myList[-2: ] ``` print(sliceOne) print(sliceTwo) print(sliceThree)
[3, 4, 5] [1, 2] [4, 5]
35
What is the output: 11 = ["A", "B", "C"] 12 = 11 13 = 12 del 11[0] del 12[:] print(13)
[]
36
What is the output: z = ["A", "B", "C"] x = z[:] y = x[:] del z[0] del x[0] print(y)
['A', 'B', 'C']
37
Replace the ??? with in or not in so the appropriate value outputs: myList = [1, 2, "in", True, "ABC"] print(1 ??? myList) # outputs True print("A" ??? myList) # outputs True print(3 ??? myList) # outputs True print(False ??? myList) # outputs False
in not in not in in
38
What does "squares" represent and what is the output: squares = [x ** 2 for x in range(10)] print(squares)
List comprehension | [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
39
How many columns and how many rows will be in the below array: array = [] for i in range(5): a = ["X" for i in range(4)] array.append(a) print(array)
``` There will be 4 columns and 5 rows. [['X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X']] ```
40
``` How do you print this using List Comprehension?: [['X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X'], ['X', 'X', 'X', 'X']] ```
array = [] array = [['X' for i in range(4)] for j in range(5)] print(array)
41
In a 3 x 3 box matrix (9 total boxes), what is the index of the bottommost center box?
[2][1]
42
How many columns, rows and tables is the below matrix made of? b = [[["Y" for r in range(20)] for f in range(15)] for t in range(3)]
20 columns 15 rows 3 tables