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
- Likes8.9K
🚨 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…
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
Coding Computing Coach retweeted
Replying to @PythonPr
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.
Replying to @PythonPr
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.
Coding Computing Coach retweeted
Replying to @RealBenjizo
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.
Coding Computing Coach retweeted
You want to exit a for loop; which statement do you use?
2%continue
81%break
2%yield
15%return
106 votes • Final resultsReplying to @RealBenjizo
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.
Coding Computing Coach retweeted
Replying to @PythonPr
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,
+
Coding Computing Coach retweeted
Only 1% of coders get this right on the first try!
What's your answer?
Replying to @CodingComputing @PythonPr
Finally, i takes the value 2.
print(i*2)
prints
4
So, the net output is
0
2
4
and that's what gets printed.
Replying to @CodingComputing @PythonPr
the variable i will take on these 3 values one-by-one.
So, for the first iteration, the value of i is 0.
The body of the loop is then executed, ie,
print(i*2)
as the current value of i is 0, i*2 is also 0, and that gets printed.
Next i becomes 1.
print(i*2)
prints
2
+
Replying to @PythonPr
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,
+
Coding Computing Coach retweeted
Replying to @PythonPr
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.
+