🚨 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: A
Solution: x.insert(2, 0) modifies x.
It inserts the item 0 at index 2.
What happens to the current items at index 2 and beyond in x?
They get shifted to the right.
So, x gets modified to
[5, 6, 0, 7, 8]
that's what is printed, hence Option A is correct.
@PythonDvz Strings in python are immutable.
That means, once a string object is created, it cannot be modified.
Here we are creating the string and trying to make a change to it.
But that gives an error because strings aren't mutable.
@PythonPr Answer: 17
Solution: x is 2 and y is 3.
z is assigned the expression
x**y + y**x
ie
3**2 + 2**3
There are 2 operations here: ** that denotes exponentiation, and + for addition.
** has higher precedence, so it's performed first.
3**2 is 9
2**3 is 8
9+8 is 17, that's printed.
@PythonDvz NameError.
We are trying to access x, but we have never defined what x is.
Since python doesn't know what x is, it puts up an error. Specifically a NameError.
@RealBenjizo Answer: B
Solution: The function is the tricky part, let's zoom in on that.
The function call passes the argument d to modify_dict.
But modify_dict doesn't "modify" d.
Rather, it gives the name d to something else (dictionary with grandma).
And, that changed d is just a
+
@RealBenjizo Answer: B
Solution: The function is the tricky part, let's zoom in on that.
The function call passes the argument d to modify_dict.
But modify_dict doesn't "modify" d.
Rather, it gives the name d to something else (dictionary with grandma).
And, that changed d is just a
+
@RealBenjizo Answer: a. Error
Solution: x is a tuple.
Tuples are immutable, ie, you cannot change it after it is created.
In the function call to func, the argument is the tuple.
However, func tries to modify the index 0 value of this tuple.
This gives error because tuples are immutable.
@RealBenjizo Answer: a. Error
Solution: x is a tuple.
Tuples are immutable, ie, you cannot change it after it is created.
In the function call to func, the argument is the tuple.
However, func tries to modify the index 0 value of this tuple.
This gives error because tuples are immutable.
@akshaymarch7 Writing code with correct syntax is not the bottleneck.
The understanding the requirements and formulating a sound plan is more important than ever.
Indexing starts from 0 for the leftmost item.
The more you increase, the rightwards you go.
BUT there is a negative indexing feature.
The negative index starts from -1 from the rightmost item.
The more you decrease, the leftwards you go.
@PythonPr Answer: 3
Solution: As we know, indexing starts from 0, from the leftmost item.
However, python provides this feature of negative index, which makes it easy to obtain items from the right.
a[-1] refers to the last (rightmost) item of the list a.
Thus, here it is 3.
@PythonPr False.
`a` is a list.
tuple(a) produces a tuple from `a`.
So, it has the same items, in the same order.
But within a tuple as the container.
As the containers don't match (list vs tuple),
the comparison gives False.
@PythonDvz Answer: C
a is a str & b is an int.
It seems impossible to multiply them using the * operator.
But the * operator does different things for different operand types.
For str and int, the * operator repeats the str, int times.
'5' * 3 repeats the string thrice, ie, "555"
@PythonDvz Answer: C
a is a str & b is an int.
It seems impossible to multiply them using the * operator.
But the * operator does different things for different operand types.
For str and int, the * operator repeats the str, int times.
'5' * 3 repeats the string thrice, ie, "555"