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
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: 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: 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: 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,
+
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.
+
Replying to @PythonPr
Answer: 3. 8
Solution: 8.9 has a decimal point, and hence it is considered a float number.
Float numbers are python's representation of Real numbers.
The int function converts float numbers into integers.
The conversion is done by rounding downwards.
So, int(8.9)
+
Replying to @PythonPr
Answer: A
Solution: a and b are sets.
The code computes
a ^ b
What is this operator ^?
The operator's action is called Symmetric Difference.
The diagram shows it the symmetric difference of A and B as shaded:
+
Replying to @PythonPr
Answer: A. HelloWorld
Solution: "Hello" + "World"
Here is an addition of two strings.
As we know, strings are sequences of characters.
How do you add two of these?
The answer is that the + operator does different things for different types.
Numbers get summed up. But,
+
Replying to @PythonPr
Answer: 6
Solution: sum() produces the sum of the items of this list.
Here it is 3+6+9=18
len() produces the length of the list.
Here it is 3 because there are 3 items.
The // gets the floor division (division that gives integer result by ignoring the remainder)
18//3 = 6
Replying to @PythonPr
Answer: A. Pn
Solution: x is a string "Python"
x[0] gets the leftmost character, 'P'.
x[-1] has a negative index.
It gets the rightmost character, 'n'.
When you sum these two, the characters get joined.
x[0]+x[-1]
is the same as
'P' + 'n'
ie, 'Pn'.
So,
Pn gets printed.
Replying to @CodingComputing @PythonPr
Common python mistake: Forgetting to convert user input to int (it remains string by default)
The comparisons would be string comparison, which will cause bugs later, so be aware!
I hope you found this answer useful.
Replying to @PythonPr
Answer: B. False
Solution: This is easy to get wrong!
"10" > "2" looks like a comparison of numbers, but look carefully.
"10" and "2" are strings. They are treated as sequences of characters.
How do you compare sequences of characters?
We do that by comparing
+
Replying to @PythonPr
Answer: 55
Solution: How does that happen?
That's because of data types.
Looking closely, it is not 5+5, it is "5"+"5".
The difference is that 5 is treated as number, but "5" is treated as a string, consisting of the digit '5'.
Addition of strings works differently from
+
Replying to @PythonPr
Answer: false
Solution: The condition of the if-block is:
'java' in list
Is this True or False?
Let us check whether 'java' appears in the list.
The list contains 'Java' but not 'java'.
Strings are case-sensitive, thus 'Java' and 'java' aren't equal.
So,
+
Replying to @CodingComputing @clcoding
But we have False as the index.
[1, 2][False]
For the purposes of arithmetic and indexing,
python treats booleans as integers.
True is considered 1, and False as 0.
So,
[1, 2][False]
is equivalent to
[1, 2][0]
that is, the index0 item, which is 1.
Replying to @clcoding
Answer: 1
Solution: Let's first understand what that mess of square bracket means.
[1, 2] is a list.
Anything that comes after it in square brackets is considered its index. Eg.
[1, 2][0] is the index0 item of that list, which is 1.
But here we don't have a number index
+
Replying to @PythonPr
Answer: A) 2
Solution: True and False are boolean values.
You may be familiar with `or`, `and`, `not` operations on these.
It may seem strange to add them with a +.
However, python allows that.
The way it handles arithmetic on Booleans is simple:
True = 1
False = 0
So
+
Replying to @PythonPr
Answer: tho
Solution: Recall that negative indexes count from the right side.
So, index (-1) refers to the last item,
(-2) refers to the second-last item,
and so on.
Thus, for the string "python", the slice -4:-1
starts at (-4), ie, 4th from right, that is 't'
and
+