Hello I will show some type conversions and casting processes.
First of all, I have to state that conversion processes in python are separated as follows.
1-) Implicit Type Conversion
2-) Explicit Type Conversion
It is generally divided into 2. What is the difference between them?
In Implicit Type conversion, type conversion occurs as a result of an arithmetic operation. The most obvious example is when you add a float number and an int number, the result will be float. In other words, we leave it to the python compiler this casting progress.
In Explicit Type conversion, if we force this change. Specifically, we set to specify the type of variable.
1-) Implicit Type Conversion Example
my_int_num = 102 my_float_num = 100.1 result = my_float_num + my_int_num print(type(result)) #<class 'float'> print(result) #202.1
As you can see, the python compiler took care of this process for us.
The result was auto casting. So what would happen if we instead converted a float with string variable or string with int variable to an operation? in this case, we would get an error.
my_str_num = "102" my_int_num = 100 result = my_str_num + my_int_num print(result) #TypeError: can only concatenate str (not "int") to str
2-) Explicit Type Conversion Example
my_int_num = 102 my_float_num = 100.1 result = int(my_float_num) + my_int_num print(type(result)) # <class 'int'> print(result) # 202