Conversion of Python Data Types
When we are processing data, some data types are not what we want. What should we do?
1. Data type conversion: type to be converted (data)
- Convert num01 to an integer: int (num01)
- To convert num01 to a floating point: float (num01)
- To convert num01 to a string: str (num01)
- Convert nun01 to boolean: bool (num01)
For example:
Print (int ("12345") + 1)
Print (float ("12.345") + 1.187)
Print (str (123) +"456")
Print (bool (1 ))#As long as it is not converted from zeroTrue, 0Is False
Print (int (123.456 ))#Convert to a value after the decimal point
Print (int ("Sandy"))#Cannot be converted, not a basic number requirement
Note: During data type conversion, not all conversions can be successful and exception handling is required.
2. Convert numeric values into characters
For example:
Print (ord ("X"))
Print (chr (88 ))
Iii. hexadecimal conversion
For example:
Print (hex (200 ))
Print (oct (0, 200 ))
Print (bin (200 ))
Demo: