Type Conversion in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Type Conversion in Python

Can someone please give me the "promotion of types" charts for datatypes in python which can be used in type conversion to know the priority of datatypes.

11th May 2019, 6:29 PM
harshit
harshit - avatar
6 Answers
+ 10
Type changing is quite easy in Python. There is a class for each type of builtin objects in Python. You can use their class names like functions to convert an object as parameter to the wanted type. str(x) => string int(x) => integer float(x) => float bool(x) => boolean list(x) => list If you dont remember the name of the class of an object, you can use: type(y)(x) There are also some special rules between type conversions, if you want to know.
11th May 2019, 7:04 PM
Seb TheS
Seb TheS - avatar
+ 3
Implicitly? Like in C? Python is strongly typed, you have to convert explicitly (or I don't understand yet what you mean). For general operator precedence look here: https://docs.python.org/3/reference/expressions.html
11th May 2019, 7:32 PM
HonFu
HonFu - avatar
+ 3
Yes, I think the string has the highest priority. Any object can be converted in to a string You can convert string in to floats and integers. float("5.8") => 5.8 float("5") => 5.0 int("5") => 5 int("5.8") => Error You can convert floats to integers and viceversa. int(5.999) => 5 float(6) => 6.0 Converting strings into list types will make a list of each character from the list, but viceversa will only give the list representation as a string. list("Hello") => ["H", "e", "l", "l", "o"] str(["H", "e", "l", "l", "o") => "[\"H\", \"e\", \"l\", \"l\", \"o\"]" You can convert any object in to booleans too, empty values are False, rest are True. bool("True") => True bool("False") => True bool("") => False bool(0) => False bool(-100) => True bool([]) => False
12th May 2019, 8:30 AM
Seb TheS
Seb TheS - avatar
+ 2
Int and float are sort of an 'exception', but trying to be precise: Types behave in the way they are defined by their interface (methods, operator overloads etc.) - the famous 'duck-typing. So if some sort of 'auto'-conversion takes place, it is because the types were designed to behave that way - and you can do that with your own types too. So for example you can define what exactly happens when you add type A with type B and then just that will happen. As far as the built-in behaviour goes, cases like that seem to be very limited. if a str is gonna be an int or a float, you have to explicitly say so. It's not like for example in C++ where the compiler automatically tries to use one of the many constructors to convert anything to anything if anyhow possible unless you explicitly prevent it.
12th May 2019, 9:22 AM
HonFu
HonFu - avatar
+ 1
HonFu I thought you just said in another post, that in Python int divided by int would implicitly cast the result into a float if the result was not an integer. So there is implicit conversion and you do not have to explicitly convert everything in Python. Or are you talking about something else here? When the conversion results in loss of precision. My latest understanding of implicit conversion is that it will always happen where precision must be maintained, choosing the type that preserves absolut precision. If that is not possible an error occurs.
12th May 2019, 9:07 AM
Michael U.
Michael U. - avatar
0
Yes, but actually I want to know the order/priority of datatypes in python. EXAMPLE: string has higher priority than integer thus we can't implicitly typecast a string variable to integer variable
11th May 2019, 7:20 PM
harshit
harshit - avatar