Module 3: Boolean Values, Conditional Execution, Loops, Lists and List Processing, Logical and Bitwise Operations Flashcards
2 == 2.
True
It’s possible to compare an integer with a float
What kind of an operator is the == operator? (and ‘<’ ‘>’ ‘!=’ ‘<=’ ‘>=’ )
What will be the output of these operators
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
The operator priority table
Priority Operator
1 ~, +, - unary
2 **
3 *, /, //, %
4 +, - binary
5 «,»_space;
6 <, <=, >, >=
7 ==, !=
8 &
9 |
10 =, +=, -=, *=, /=, %=, &=, ^=, |=,»_space;=, «=
What build in functions return the largest and smallest number in a list or multiple variables?
max(<vars>)
Example:
>>>var = [0,2,5,7,6,2,7,8,5]
>>> max(var)
8</vars>
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")
one
two
Is it possible to print multiple lines with one print statement?
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? |
+================================+
“””)
>>> test_dict = {1: 'Foo', 2: 'bar'} >>> print(*test_dict)
1 2
What’s the difference between break, continue and pass?
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
for i in range(2, 1): print(i) else: print("else:", i)
NameError: name ‘i’ is not defined
The control value of the loop is not available after the loop if the loop is not executed
What is the output of the following code?
~~~
n = 3
while n > 0:
print(n + 1)
n -= 1
else:
print(n)
~~~
4
3
2
0
What is the output of the following code?
~~~
for num in range(4):
print(num - 1)
else:
print(num)
~~~
-1
0
1
2
3
for i in range(0, 6, 3): print(i)
0
3
Logical operators and there priority
Conjunction
Disjunction
Negation
And (Low priority)
Or (Low priority)
not (High priority, same as unary)
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)
~~~
True
True
Bitwise operators:
bitwise conjunction (And)
bitwise disjunction (Or)
bitwise negation (Not)
bitwise exclusive or (xor)
& (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
What is the output of the following code?
~~~
»> a = 1.
»> b = 1
»> a & b
~~~
TypeError: unsupported operand type(s) for &: ‘float’ and ‘int’
Bitwise operators can only be used on integers
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
& with a mask
| with a mask
^ with a mask
with a mask
What happens when you bitshift an odd number17>>1
It will be floor deviced, because the result must be an integer
8
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)
0 5 -5 1 1 16
len(25)
TypeError: object of type ‘int’ has no len()
How to remove an element from a list
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]
~~~
How to insert a value in a list
What happens if you use a location out of range?
list.insert(location, value)
It will insert the value at the last position
my_list = [] # Creating an empty list. for i in range(5): my_list.insert(0, i + 1) print(my_list)
[5, 4, 3, 2, 1]
```
What will be the output of:
~~~
lst = []
del lst
print(lst)
~~~
NameError: name ‘lst’ is not defined
What will be the output of:
~~~
lst = [“D”, “F”, “A”, “Z”]
lst.sort()
~~~
Nothing, the .sort method works inplace, it has no output.
list_1 = [1] list_2 = list_1 list_1[0] = 2 print(list_2)
[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
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)
[1] [8, 6]
With slicing the last index is the end number -1
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)
~~~
[]
Because if the start is further than the end it will return a empty list[4, 2]
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)
~~~
[] NameError
my_list = [0, 3, 12, 8, 2] print(5 in my_list) print(5 not in my_list) print(12 in my_list)
False
True
True
list_1 = ["A", "B", "C"] list_2 = list_1 list_3 = list_2 del list_1[0] del list_2 print(list_3)
['B', 'C']
list_1 = ["A", "B", "C"] list_2 = list_1 list_3 = list_2 del list_1[0] del list_2 print(list_3)
['B', 'C']