
Making Python simple for you, by exploring the fundamentals. Tips and explanations to become code-literate in this AI age. Building @PythonResources
Maximize your coding skills 👇
Joined May 2019
- Tweets13.6K
- Following104
- Followers8.9K
- Likes9K
🚨 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…
Coding Computing Coach retweeted
Replying to @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,
+
Replying to @CodingComputing @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.
Replying to @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,
+
Coding Computing Coach retweeted
Replying to @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
Replying to @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
Comparing numbers and strings
Replying to @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,
+
Coding Computing Coach retweeted
What will be the output of the following Python code?
x = "7"
print(x * 2 == 77)
27%True
42%False
7%77
24%Error
1,240 votes • Final resultsReplying to @CodingComputing @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.
Replying to @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,
+
Coding Computing Coach retweeted
Replying to @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.
+
Replying to @CodingComputing @PythonPr
So, the single item 5 is repeated 3 times.
[5] * 3 gives [5, 5, 5].
That is what gets printed.
Replying to @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.
+
Replying to @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
Replying to @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].
Coding Computing Coach retweeted
Replying to @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
+
Coding Computing Coach retweeted
Python Quiz: What is the Output?
print({1, 2} == {2, 1})
45%True
38%False
12%Error
5%None
624 votes • Final resultsReplying to @CodingComputing @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.
Replying to @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
+
Replying to @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



