Every value in Python has a data type. The type defines the operations that can be done on the data and the structure in which you want the data to be stored.
Data conversion in Python can happen in two ways:
· Explicit type conversion: either you tell the compiler to convert a data type to some other type explicitly, or
· Implicit conversion: the compiler understands this by itself and does it for you.
Python Implicit Data Type Conversion
Implicit conversion or coercion is when data type conversion takes place either during compilation or during run time and is handled directly by Python for you.
Example
a_int = 1 |
In the example, an int value a_int was added to a float value b_float, and the result was automatically converted to a float value c_sum without you having to tell the compiler. This is the implicit data conversion. The conversion from float to integer is not done because then the compiler will need to remove the fractional part leading to the loss of information.
Python Explicit Data Type Conversion
Explicit conversion also known as type casting is when data type conversion takes place because you clearly defined it in your program. You basically force an expression to be of a specific type. The general form of an explicit data type conversion is as follows:
(required_data_type)(expression)
With explicit data type conversion, there is a risk of information loss since you’re forcing an expression to be of a specific type.
Integers and floats are data types that deal with numbers. To convert integer to float, use the float() function in Python. Similarly, if you want to convert a float to an integer, you can use the int() function.
a_int = 3 a_float = 3.3 c_sum = a_float + b_float |
Let’s try to execute below example:
quantity = 23 |
Above program gives error as we can not concatenate integer and string
TypeError: can only concatenate str (not “int") to str
We will discuss about different types of errors and how t handle them in chapter 9 (Exception and Assertion).
To fix this error, we need to convert the integer value into string type before concatenating. str() function explicitly converts any datatype into string. Modified program would look like:
quantity = 23 |
The number systems refer to the number of symbols or characters used to represent any numerical value. The number system that typically used everyday is called decimal. In the decimal system, you use ten different symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. With these ten symbols, you can represent any quantity. Binary, hexadecimal, and octal refer to different number systems.
When you run out of symbols, you go to the next digit placement. In the decimal system, to represent one higher than 9, you use 10 meaning one unit of ten and zero units of one. However, it is different in other number systems. For example, when you consider a binary system which only uses two symbols: 0 and 1, when you run out of symbols, you need to go to the next digit placement. So this is how you will count in binary: 0, 1, 10, 11, 100, 101 and so on.
The example below shows how numbers are converted from one number system to another. Refer the comments in the code for better understanding.
# ####################### # #######################
|