Bahaviours of different data types in Python.
Behaviours of different data types in Python.
Behaviours of different data types:
The similar behaviour of one data type may perform differently on another. When we add two integers, resultantly we get another integer in return.
print(6+ 3)
9
When we add two strings together, we receive an error, as strings are non-numerical. When the strings get
Linked together, returning another
string.
print ('Summer' + 'Camp')
SummerCamp
When we add Boolean values, we expect another Boolean, as we aware of that By adding integers returns an integer, and when we add a strings returns a string. But ,when we add Boolean values we get an integer.
print(True + False)
1
This happens due to True and False are converted to the integers 1 and 0 in mathematical operations. The difference of behaviour in data types is not limited to addition.
print(6 * 6)
print('Try! * 3)
print(True * 2)
36
'Try! Try! Try!'
2
Comments
Post a Comment