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

1
Q

What is the difference between = and ==?

A

= is an assignment operator (assign value to variables)
== is the equality operator (binary operator with left sided binding)

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

2 == 2.0

A

True

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

True or false: comparison operators have higher priority than equality operators

A

True

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

What is an if statement known as?

A

Condition statement/instruction

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

What is an if statement within an if statement called?

A

Nested if statement

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

Explain the difference between if and while

A

When the condition is met:
- if performs its statements only once
- while repeats the execution as long as the condition evaluates to True

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

What is an endless loop?

A

An infinite loop is a sequence of instructions in a program which repeat indefinitely

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

What is ‘while number:’ equivalent to?

A

while number != 0:

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

What is used to exit the loop immediately?

A

break

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

What is continue used for?

A

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.

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

What are the two types of loops in python?

A

for and while

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

What is and?

A

a conjunction

(binary operator with lower priority than comparison operators)

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

What is or?

A

a disjunction

(binary operator with lower priority than and)

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

What are logical operators?

A

AND and OR

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

What is the unary operator performing a logical negation?

A

NOT

(same priority are unary +/- (v high))

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

What are the four bitwise operators?

A

& (ampersand) - bitwise conjunction;
(bar) - bitwise disjunction
~ (tilde) - bitwise negation;
^ (caret) - bitwise exclusive or (xor)

(bar) - bitwise disjunction

17
Q

What do bitwise operators allow you to do?

A

allow you to manipulate single bits of data

18
Q

True or False:
The arguments for bitwise operators must be integers

A

True

cannot use floats

19
Q

What is the difference in the operation of the logical and bit operators?

A

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.

20
Q

What is a bit mask?

A

a sequence of zeros and ones, whose task is to grab the value or to change the selected bits

21
Q

What is shifting a value one bit to the left equivalent to?

A

Multiplying by 2 as 2 is the base for binary numbers

22
Q

What are the shift operators?

A

&laquo_space;and&raquo_space;

The left argument of these operators is an integer value whose bits are shifted. The right argument determines the size of the shift.

23
Q

What does 17&raquo_space; 1 equal?

A

17 // 2 (17 floor-divided by 2 to the power of 1) → 8

24
Q

What does 17 &laquo_space;2 equal?

A

17 * 4 (17 multiplied by 2 to the power of 2) → 68

25
Q

What is the operation of selecting an element from the list known as?

A

indexing

26
Q

What instruction is used to delete elements from a list?

A

del

27
Q

If a function is invoke like this result = function(arg), how is a method invoked?

A

result = data.method(arg)

A method is owned by the data it works for, while a function is owned by the whole code.

28
Q

How would you swap variable 1 with variable 3 in a list?

A

variable_1, variable_2 = variable_2, variable_1

29
Q

What is the bubble sort method?

A

we compare the adjacent elements, and by swapping some of them, we achieve our goal.

30
Q

How are lists stored differently to ordinary variables?

A

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.

31
Q

What is a slice and how do you do it?

A

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[:]

32
Q

list = [1,2,3,4,5]
list_new = list[1:3]

What is new list?

A

[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.

33
Q

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?

A

The list will be empty

34
Q

my_list[0:end] is equal to

A

my_list[:end]

35
Q

What is the difference between: del mylist and del mylist[:]

A

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

36
Q

What does elem in list do?

A

checks if elem is in list, prints True if it is

(can also used not in)

37
Q

Rewrite this as a list comprehension:
for element in list:
if conditional:
expression

A

[expression for element in list if conditional]

38
Q

x = [1,2,3,4,5]
print(x[x[2])

What is the output?

A

x[2] = 3

therefore x[x[2]] = x[3] = 4