Coding Computing Coach (@CodingComputing) header
Coding Computing Coach (@CodingComputing) profile picture

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
Photo by Coding Computing Coach (@CodingComputing)Photo by Coding Computing Coach (@CodingComputing)Photo by Coding Computing Coach (@CodingComputing)Photo by Coding Computing Coach (@CodingComputing)Photo by Coding Computing Coach (@CodingComputing)Photo by Coding Computing Coach (@CodingComputing)Photo by Coding Computing Coach (@CodingComputing)Photo by Coding Computing Coach (@CodingComputing)Photo by Coding Computing Coach (@CodingComputing)Photo by Coding Computing Coach (@CodingComputing)
🚨 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…
1
21
5,771
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, +
1
1
2
161
Coding Computing Coach retweeted
Python Quiz: What is the Output?
28
11
1
120
12,193
Python Quiz: What is the Output?
28
11
1
120
12,193
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, +
1
1
2
161
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.
48
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
1
1
118
What will be the output of the following Python code? print(2**-1)
11%0
63%0.5
21%-2
5%-1
708 votes • Final results
12
2
40
21,283
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
1
1
118
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, +
2
317
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 results
16
10
1
59
21,331
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 results
16
10
1
59
21,331
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, +
1
1
3
625
"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.
82
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. +
1
1
3
378
Python Quiz: Comment your answer 💯
22
5
2
73
9,686
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. +
1
1
3
378
So, the single item 5 is repeated 3 times. [5] * 3 gives [5, 5, 5]. That is what gets printed.
56
Python Quiz: What is the Output? a = 5 b = 2 c = a + b * 3 print (c)
76%11
3%15
14%21
7%17
422 votes • Final results
5
1
51
7,582
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
2
185
Python Quiz: What is the Output? x = [8, 3, 5] print(sorted(x))
82%[3, 5, 8]
8%[8, 3, 5]
3%[5, 3, 8]
7%[3, 8, 5]
441 votes • Final results
7
2
38
7,281
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].
1
109
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 +
1
1
3
444
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 results
9
6
37
11,676
Python Quiz: What is the Output? print({1, 2} == {2, 1})
45%True
38%False
12%Error
5%None
624 votes • Final results
9
6
37
11,676
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 +
1
1
3
444
{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.
61
Python Quiz: What is the Output? x = " 7 " print ( x*2 )
36%14
13%49
46%77
5%72
1,152 votes • Final results
12
5
1
53
13,465
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
228