Boolean Values, Conditional Execution, Loops, Lists and List Processing, Logical and Bitwise Operations Flashcards
What is the difference between = and ==?
= is an assignment operator (assign value to variables)
== is the equality operator (binary operator with left sided binding)
2 == 2.0
True
True or false: comparison operators have higher priority than equality operators
True
What is an if statement known as?
Condition statement/instruction
What is an if statement within an if statement called?
Nested if statement
Explain the difference between if and while
When the condition is met:
- if performs its statements only once
- while repeats the execution as long as the condition evaluates to True
What is an endless loop?
An infinite loop is a sequence of instructions in a program which repeat indefinitely
What is ‘while number:’ equivalent to?
while number != 0:
What is used to exit the loop immediately?
break
What is continue used for?
behaves as if the program has suddenly reached the end of the body; the next turn is started and the condition expression is tested immediately.
What are the two types of loops in python?
for and while
What is and?
a conjunction
(binary operator with lower priority than comparison operators)
What is or?
a disjunction
(binary operator with lower priority than and)
What are logical operators?
AND and OR
What is the unary operator performing a logical negation?
NOT
(same priority are unary +/- (v high))
What are the four bitwise operators?
& (ampersand) - bitwise conjunction;
(bar) - bitwise disjunction
~ (tilde) - bitwise negation;
^ (caret) - bitwise exclusive or (xor)
(bar) - bitwise disjunction
What do bitwise operators allow you to do?
allow you to manipulate single bits of data
True or False:
The arguments for bitwise operators must be integers
True
cannot use floats
What is the difference in the operation of the logical and bit operators?
the logical operators do not penetrate into the bit level of its argument. They’re only interested in the final integer value.
Bitwise operators are stricter: they deal with every bit separately.
What is a bit mask?
a sequence of zeros and ones, whose task is to grab the value or to change the selected bits
What is shifting a value one bit to the left equivalent to?
Multiplying by 2 as 2 is the base for binary numbers
What are the shift operators?
«_space;and»_space;
The left argument of these operators is an integer value whose bits are shifted. The right argument determines the size of the shift.
What does 17»_space; 1 equal?
17 // 2 (17 floor-divided by 2 to the power of 1) → 8
What does 17 «_space;2 equal?
17 * 4 (17 multiplied by 2 to the power of 2) → 68
What is the operation of selecting an element from the list known as?
indexing
What instruction is used to delete elements from a list?
del
If a function is invoke like this result = function(arg), how is a method invoked?
result = data.method(arg)
A method is owned by the data it works for, while a function is owned by the whole code.
How would you swap variable 1 with variable 3 in a list?
variable_1, variable_2 = variable_2, variable_1
What is the bubble sort method?
we compare the adjacent elements, and by swapping some of them, we achieve our goal.
How are lists stored differently to ordinary variables?
the name of an ordinary variable is the name of its content;
the name of a list is the name of a memory location where the list is stored.
The assignment: list_2 = list_1 copies the name of the array, not its contents. In effect, the two names (list_1 and list_2) identify the same location in the computer memory. Modifying one of them affects the other, and vice versa.
What is a slice and how do you do it?
A slice is an element of Python syntax that allows you to make a brand new copy of a list, or parts of a list.
It actually copies the list’s contents, not the list’s name.
list_2 = list_1[:]
list = [1,2,3,4,5]
list_new = list[1:3]
What is new list?
[2,3]
start is the index of the first element included in the slice;
end is the index of the first element not included in the slice.
If the start of the slice specifies an element further from the one described at the end e.g. list[3:1] what will happen?
The list will be empty
my_list[0:end] is equal to
my_list[:end]
What is the difference between: del mylist and del mylist[:]
the second one deletes the contents of list so mylist will print [] the first one deletes the list variable altogether so mylist will not print and cause an error
What does elem in list do?
checks if elem is in list, prints True if it is
(can also used not in)
Rewrite this as a list comprehension:
for element in list:
if conditional:
expression
[expression for element in list if conditional]
x = [1,2,3,4,5]
print(x[x[2])
What is the output?
x[2] = 3
therefore x[x[2]] = x[3] = 4