Oct-04-2019, 05:44 AM
Hi!
Well, this is a question that arose in some other threads that I was participating in, and I wanted to give it a bit more of a thought, and to ask for some insights from others.
The process of converting one data-type into another data-type is called casting, so in theory, with the str(), float(), and int() functions, we can transform a numeric value string into a float or into an integer, and in a similar way, a float into a string or an integer, or an integer into a string or a float.
So now, I'm going to show the inputs and outputs of some of these transformations:
1) a numeric value, naked or wrapped in quotes, in the format of a whole number or in the format of a float, can be transformed into a string with no problems.
2) a numeric value, naked or wrapped in quotes, in the format of a whole number or in the format of a float, can be transformed into a float with no problems.
3) a numeric value, naked or wrapped in quotes, in the format of a whole number or in the format of a float, can be transformed into an integer with no problems, EXCEPT WHEN IT'S A FLOAT WRAPPED IN QUOTES, THAT PRODUCES AN ERROR.
It seems that this is a contradiction on the casting of data types, or an inconsistency in Python programming language itself.
Do you have an explanation for that behaviour?
All insights are welcome!!!
All the best,
Well, this is a question that arose in some other threads that I was participating in, and I wanted to give it a bit more of a thought, and to ask for some insights from others.
The process of converting one data-type into another data-type is called casting, so in theory, with the str(), float(), and int() functions, we can transform a numeric value string into a float or into an integer, and in a similar way, a float into a string or an integer, or an integer into a string or a float.
So now, I'm going to show the inputs and outputs of some of these transformations:
Output:>>> str(4)
'4'
>>> str('4')
'4'
>>> str(4.5)
'4.5'
>>> str('4.5')
'4.5'
>>> float(4)
4.0
>>> float('4')
4.0
>>> float(4.5)
4.5
>>> float('4.5')
4.5
>>> int(4)
4
>>> int('4')
4
>>> int(4.5)
4
>>> int('4.5')
Error:Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
int('4.5')
ValueError: invalid literal for int() with base 10: '4.5'
where we can see that:1) a numeric value, naked or wrapped in quotes, in the format of a whole number or in the format of a float, can be transformed into a string with no problems.
2) a numeric value, naked or wrapped in quotes, in the format of a whole number or in the format of a float, can be transformed into a float with no problems.
3) a numeric value, naked or wrapped in quotes, in the format of a whole number or in the format of a float, can be transformed into an integer with no problems, EXCEPT WHEN IT'S A FLOAT WRAPPED IN QUOTES, THAT PRODUCES AN ERROR.
It seems that this is a contradiction on the casting of data types, or an inconsistency in Python programming language itself.
Do you have an explanation for that behaviour?
All insights are welcome!!!
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs