+ 1
Multiplying different data types
So in Python, we cannot multiply a string with an int but we are allowed to multiply an int with a float? Why?
2 Respostas
+ 3
We can multiple a string with an int:
Print('a'*2)----> aa
Your answer: because float and int are both numerical values so we can use different generators for this two types
0
You can actually multiply a string by an int in python. I think you wanted to ask why we can not multiply a string by a float, and why that results in an error while we can multiply an int by a float without any problems.
While this is mostly a feature of the language, to make text processing easier using Python, these three hints might help you understand why that is not possible, while the other is possible:
1- multiplying a string by a float simply (if not mathematically) makes no sense! how would you define 2 and a half times (2.5) of the character 'a'?
2- the fundamental design of a 'float' variable and an 'int' variable are fundamentally different. While multiplying a character by an 'int' will simply result in 'int' times of that character, multiplying a character by a float however, as manageable as it maybe bitwise, it results in an unmanageable or undefined character which might have no representative on the Unicode table for example.
3- Multiplication of an int and a float are mathematically and computationally valid! Also while the multiplication of a string and an int is a(n Easter egg :) ) gift by Python, multiplication of a string and a float is mathematically invalid.