Let’s start from the middle of this expression. '3' * 2
evaluates to '33'
. This is because multiplication of a str
object by a int
object results in concatenating string with itself n
times (providing you are multiplying by n
).
int('33')
is just 33
as an integer. When you feed int
class a string it will try to convert it to a number, and will raise ValueError
if it cannot convert (like int('xfew')
). int
has an optional second argument base
, that specifies the numeric base in with string is written. For instance int('ff', 16)
returns 255
. base
is by default equal to 10
.
Operator //
is an operator of integer division. Therefore 33 // 11
is equal to 3
. Note that 33 / 11
is equal to 3.0
, a float
. Of course after converting it to int
this will also be just 3
.
int(3)
does really nothing. Just returns 3
. Concluding spam[int(int('3' * 2) // 11)]
evals to spam[3]
:
spam = ['a', 'b', 'c', 'd']
spam[int(int('3' * 2) // 11)] # returns 'd'
solved List question from the boot automating the boring stuff [closed]