🚨 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:
https://t.co/yOR9qXSARE
@PythonPr Answer: 0 2 4 (on separate lines)
Solution: In this for-loop, the iteration happens over
range(3)
which is simply a sequence from 0 to 3 (excluding 3)
ie, 0, 1, and 2.
So, i gets the values 0, 1, and 2 in the 3 iterations.
In each iteration, (i*2) gets printed
When i=0,
+
@PythonPr 0 is printed.
When i=1, 1*2=2 gets printed.
When i=2, 2*2=4 gets printed.
So, in the 3 iterations, the output is:
0
2
4
And the loop ends with that, so that is the output.
@PythonPr Answer: 0 2 4 (on separate lines)
Solution: In this for-loop, the iteration happens over
range(3)
which is simply a sequence from 0 to 3 (excluding 3)
ie, 0, 1, and 2.
So, i gets the values 0, 1, and 2 in the 3 iterations.
In each iteration, (i*2) gets printed
When i=0,
+
@clcoding Answer: ** is python exponentiation operator. Mathematically, this is simply the "power" or "raised to".'
2 ** -1
is 2 raised to the power (-1),
ie, 1/2 which is 0.5
@clcoding Answer: ** is python exponentiation operator. Mathematically, this is simply the "power" or "raised to".'
2 ** -1
is 2 raised to the power (-1),
ie, 1/2 which is 0.5
@clcoding Answer: False
Solution: Let us compare the values on the sides of ==.
The right hand side is 77.
The left hand side is x*2, and x is "7", a string.
"7"*2 repeats the string "7" 2 times, giving "77".
But note that this isn't the number 77, it is a string.
That means,
+
@clcoding "77" is a sequence of two characters, '7' and '7'
So, "77" is not the same as the number 77.
"77" == 77
works out False.
So, False is what gets printed.
@clcoding Answer: False
Solution: Let us compare the values on the sides of ==.
The right hand side is 77.
The left hand side is x*2, and x is "7", a string.
"7"*2 repeats the string "7" 2 times, giving "77".
But note that this isn't the number 77, it is a string.
That means,
+
@PythonPr Answer: [5, 5, 5]
Solution: The value of x is [5].
Observe that it is a list with a single item, 5.
Now, what is x * 3?
How do you multiply a list with a number?
The answer is, python repeats the list.
So,
[5] * 3
is the items in the list [5], repeated 3 times.
+
@PythonPr Answer: [5, 5, 5]
Solution: The value of x is [5].
Observe that it is a list with a single item, 5.
Now, what is x * 3?
How do you multiply a list with a number?
The answer is, python repeats the list.
So,
[5] * 3
is the items in the list [5], repeated 3 times.
+
@PythonPr Answer: 11
Solution:
a is 5 and b is 2.
We have to evaluate
a + b * 3
ie
5 + 2 * 3
multiplication has higher precedence,
so 2 * 3 is worked out first
5 + 2 * 3
simplifies to
5 + 6
which is just
11
@PythonPr Answer: [3, 5, 8]
Solution: `sorted` is a built-in function.
it takes a list as an argument, and returns a sorted copy of it.
The sorting is in increasing order.
Therefore, sorted([8, 3, 5]) gives [3, 5, 8].
@PythonPr 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
+
@PythonPr {2, 1} has exactly the same items as {1, 2}.
They are specified with a different order, but a set doesn't preserve the order of items anyway.
Two sets are considered equal if they contain exactly the same items.
So, {1, 2} == {2, 1} gives True.
@PythonPr 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
+
@PythonPr Answer: 77
Solution: x is a string, consisting of one character, 7.
What does it mean to multiply a string by a number, like x*2?
In Python, it means repetition of the string.
x*2 repeats the string "7" twice, resulting in the string "77", which is printed as
77