Module 3: Boolean Values, Conditional Execution, Loops, Lists and List Processing, Logical and Bitwise Operations Flashcards

1
Q

2 == 2.

A

True
It’s possible to compare an integer with a float

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

What kind of an operator is the == operator? (and ‘<’ ‘>’ ‘!=’ ‘<=’ ‘>=’ )
What will be the output of these operators

A

It is a binary operator with left-sided binding. It needs two arguments and checks if they are equal.

Output will be True or False

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

The operator priority table

A

Priority Operator
1 ~, +, - unary
2 **
3 *, /, //, %
4 +, - binary
5 «,&raquo_space;
6 <, <=, >, >=
7 ==, !=
8 &
9 |
10 =, +=, -=, *=, /=, %=, &=, ^=, |=,&raquo_space;=, «=

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

What build in functions return the largest and smallest number in a list or multiple variables?

A

max(<vars>)
Example:
>>>var = [0,2,5,7,6,2,7,8,5]
>>> max(var)
8</vars>

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

Is it possible to print multiple lines with one print statement?

A

Yes, uses “”” (3qoutes)
print(
“””
+================================+
| Welcome to my game, muggle! |
| Enter an integer number |
| and guess what number I’ve |
| picked for you. |
| So, what is the secret number? |
+================================+
“””)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
>>> test_dict = {1: 'Foo', 2: 'bar'}
>>> print(*test_dict)
A

1 2

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

What’s the difference between break, continue and pass?

A

Break: exits the loop
Continue: stop this itteration of the loop and start the next (skip)
Pass: no impact on code, mostly used as placeholder when working on new code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
for i in range(2, 1):
    print(i)
else:
    print("else:", i)
A

NameError: name ‘i’ is not defined

The control value of the loop is not available after the loop if the loop is not executed

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

What is the output of the following code?
~~~
n = 3

while n > 0:
print(n + 1)
n -= 1
else:
print(n)
~~~

A

4
3
2
0

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

What is the output of the following code?
~~~
for num in range(4):
print(num - 1)
else:
print(num)
~~~

A

-1
0
1
2
3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
for i in range(0, 6, 3):
    print(i)
A

0
3

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

Logical operators and there priority
Conjunction
Disjunction
Negation

A

And (Low priority)
Or (Low priority)
not (High priority, same as unary)

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

What is the output of the following code?
~~~
not (p and q) == (not p) or (not q)
not (p or q) == (not p) and (not q)
~~~

A

True
True

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

Bitwise operators:
bitwise conjunction (And)
bitwise disjunction (Or)
bitwise negation (Not)
bitwise exclusive or (xor)

A

& (ampersand) requires exactly two 1s to provide 1 as the result
(bar/ pipe) requires at least one 1 to provide 1 as the result;
~ (tilde)
^ (caret) requires exactly one 1 to provide 1 as the result

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

What is the output of the following code?
~~~
»> a = 1.
»> b = 1
»> a & b
~~~

A

TypeError: unsupported operand type(s) for &: ‘float’ and ‘int’

Bitwise operators can only be used on integers

17
Q

How to check the state of a specific bit?
How to set the specific bit to 1
How to set the specific bit to 0

A

& with a mask
| with a mask
^ with a mask

with a mask

18
Q

What happens when you bitshift an odd number
17>>1

A

It will be floor deviced, because the result must be an integer
8

19
Q
x = 4
y = 1

a = x & y
b = x | y
c = ~x  # tricky!
d = x ^ 5
e = x >> 2
f = x << 2

print(a, b, c, d, e, f)
A

0 5 -5 1 1 16

20
Q
len(25)
A

TypeError: object of type ‘int’ has no len()

21
Q

How to remove an element from a list

A

This is done with an instruction named del (delete). Note: it’s an instruction, not a function.
So no ()
~~~
testlist = [1,8,6,12,8]
del testlist[2]
print(testlist) # [1,8,12,8]
~~~

22
Q

How to insert a value in a list
What happens if you use a location out of range?

A
list.insert(location, value)

It will insert the value at the last position

23
Q
my_list = []  # Creating an empty list.

for i in range(5):
    my_list.insert(0, i + 1)

print(my_list)
A

[5, 4, 3, 2, 1]

24
Q

```

What will be the output of:
~~~
lst = []
del lst
print(lst)
~~~

A

NameError: name ‘lst’ is not defined

25
Q

What will be the output of:
~~~
lst = [“D”, “F”, “A”, “Z”]
lst.sort()
~~~

A

Nothing, the .sort method works inplace, it has no output.

26
Q
list_1 = [1]
list_2 = list_1
list_1[0] = 2
print(list_2)
A

[2]
Because the name of the var is only used to point to a memory location
Slicing is coping the content to a new memory location

27
Q
list_1 = [1]
list_2 = list_1[:]
list_1[0] = 2
print(list_2)
my_list = [10, 8, 6, 4, 2]
new_list = my_list[1:3]
print(new_list)
A
[1]

[8, 6]

With slicing the last index is the end number -1

28
Q
my_list = [10, 8, 6, 4, 2]
new_list = my_list[-1:1]
print(new_list)

```
my_list = [10, 8, 6, 4, 2]
new_list = my_list[3:]
print(new_list)
~~~

A

[]
Because if the start is further than the end it will return a empty list
[4, 2]

29
Q
my_list = [10, 8, 6, 4, 2]
del my_list[:]
print(my_list)

```
my_list = [10, 8, 6, 4, 2]
del my_list
print(my_list)
~~~

A
[]
NameError
30
Q
my_list = [0, 3, 12, 8, 2]

print(5 in my_list)
print(5 not in my_list)
print(12 in my_list)
A

False
True
True

31
Q
list_1 = ["A", "B", "C"]
list_2 = list_1
list_3 = list_2

del list_1[0]
del list_2

print(list_3)
A
['B', 'C']
32
Q
list_1 = ["A", "B", "C"]
list_2 = list_1
list_3 = list_2

del list_1[0]
del list_2

print(list_3)
A
['B', 'C']