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