str(bool(1) + float(10)/float(2)) here, str() does not convert to string… where as str() function normally converts all numeric into string…?
Course: Full Speed Python - Learn Interactively
Lesson: Dictionaries - Full Speed Python
str(bool(1) + float(10)/float(2)) here, str() does not convert to string… where as str() function normally converts all numeric into string…?
Course: Full Speed Python - Learn Interactively
Lesson: Dictionaries - Full Speed Python
Hi Fatih!
When we add a boolean to an integer, it performs addition operation on them by considering True = 1
and False = 0
in python. Here in the following statement:
str(bool(1) + float(10)/float(2))
Boolean i.e., True
will be added as a 1
to 5
which will generate 6
and finally convert it to string by str
method. Moreover you cal also verify its data type using type
method.
Hope it helps. If you still have any queries please let us know.
Thank you!
Hi Asma, thank you for your reply…
I’ve investigated that the output
print (str(bool(1) + float(10)/float(2)))
and
(str(bool(1) + float(10)/float(2))
seems different.
The output of >>> print (str(bool(1) + float(10)/float(2))) is 6.0 where as;
the output of >>> str(bool(1) + float(10)/float(2)) is ‘6.0’
So, when it is written inside print() function, the output seems like integer whereas without print() function it seems as string which made me understand wrong.
Shouldn’t it seem produce the same result? i.e. ‘6.0’ like a strings seems?