🚨 Python Testing Series 🚨
Employ testing to gamify your coding.
Learn how to test code using pytest.
Build a Cash Dispenser project in Test Driven style.
Ongoing series of posts, see README at:
github.com/CodingComputing/g…
Answer: True
Solution: {1, 2} and {2, 1} are sets.
In sets, the order of the elements doesn't matter.
A set is like a bag with fruits, shuffled. There is no "first" or "second". The only meaningful question is,
"Is this item in the bag or not?"
{1, 2} has two items: 1, 2
+
Answer: 3
Solution: The // symbol stands for "floor division".
How is that different from the usual / division?
/ gives you an exact division as a float.
// gives the quotient rounded down.
Eg. 10 / 3 would be 3.333
10 // 3 is that rounded down to the lower int, ie, 3.
Answer: break
Solution: When python encounters the break keyword, it exits the currently running loop.
The break statement works for both while and for loops.
Answer: 0 2 4 (on separate lines)
Solution: The for-loop lets you run the same code multiple times with different values.
Let's start by understanding
range(3)
this produces a sequence of numbers: 0, 1, and 2.
In each iteration of the for-loop,
+
Answer: B
Solution: x and y are sets.
What does the `-` do on sets?
It computes the "set difference".
The diagram makes it clear (credit in description):
The shaded region is the part of A which is not in B.
That is the set difference.
+
ALT https://mathmonks.com/sets/set-operations#Difference
Answer: A
Solution: a and b are sets.
The code computes
a ^ b
What is this operator ^?
The operator's action is called Symmetric Difference.
The diagram shows it the symmetric difference of A and B as shaded:
+
ALT Source: https://math.stackexchange.com/a/2948656
Answer: A. HelloWorld
Solution: "Hello" + "World"
Here is an addition of two strings.
As we know, strings are sequences of characters.
How do you add two of these?
The answer is that the + operator does different things for different types.
Numbers get summed up. But,
+
Answer: 6
Solution: sum() produces the sum of the items of this list.
Here it is 3+6+9=18
len() produces the length of the list.
Here it is 3 because there are 3 items.
The // gets the floor division (division that gives integer result by ignoring the remainder)
18//3 = 6
Answer: A. Pn
Solution: x is a string "Python"
x[0] gets the leftmost character, 'P'.
x[-1] has a negative index.
It gets the rightmost character, 'n'.
When you sum these two, the characters get joined.
x[0]+x[-1]
is the same as
'P' + 'n'
ie, 'Pn'.
So,
Pn gets printed.
Answer: B. False
Solution: This is easy to get wrong!
"10" > "2" looks like a comparison of numbers, but look carefully.
"10" and "2" are strings. They are treated as sequences of characters.
How do you compare sequences of characters?
We do that by comparing
+