Type Casting with Python
Type Casting with Python
Built-in Types
Every programming language comes with built-in data types. These help us to handle different types of data including numerical data and textual data.
According to the official Python Standard Library documentation, Python has provided us many different Built-in Types. In this article we will only be discussing the Numeric Types “-int and float-” and the Text Sequence Type “-str-”.
Type Conversion
Type conversion -as the name implies- is converting data of one type to another. This either happens implicitly or can be done explicitly.
Implicit type conversion is done by Python to convert smaller types to larger types.
int < float < str
Explicit Type Conversion (Type Casting) is done by the developer to convert larger types to smaller types.
str > float > int
Why Type Conversion?
Let’s understand how each of these two works by creating a simple calculator program.
In order to read user input, Python uses input() function. The function returns the user input value as a str type value. Following example shows how this causes an issue when we want to add numbers.
number1 = input("Enter 1st number: ")
number2 = input("Enter 2nd number: ")
print(f"{number1} + {number2} = {number1 + number2}")
print(f"type of number1: {type(number1)}")
print(f"type of number2: {type(number2)}")
Explicit Type Conversion
This is where we have to explicitly convert type to fix this issue. Explicit type conversion is done to prevent run time errors (exceptions).
We can use the int() constructor to convert str values to int values. Modify your code as follows and run your code.
number1 = input("Enter 1st number: ")
number2 = input("Enter 2nd number: ")
number1 = int(number1)
number2 = int(number2)
print(f"{number1} + {number2} = {number1 + number2}")
print(f"type of number1: {type(number1)}")
print(f"type of number2: {type(number2)}")
You may reduce your keystrokes by using conversion in the same line with input function.
number1 = int(input("Enter 1st number: "))
number2 = int(input("Enter 2nd number: "))
Alternatively, you may choose to keep the original input as of str type and cast the operand values when you use them in a calculation. However, this might not be a good approach if you have to use input values in multiple calculations.
number1 = input("Enter 1st number: ")
number2 = input("Enter 2nd number: ")
print(f"{number1} + {number2} = {int(number1) + int(number2)}")
print(f"type of number1: {type(number1)}")
print(f"type of number2: {type(number2)}")
Note: if the user inputs fractional values, casting to int type throws a run time error (exception). We have to use float() constructor instead of int() to avoid this.
Implicit Type Conversion
Python will implicitly convert smaller data types to higher data types to prevent data loss.
Following code shows that Python implicitly converts division answer to a float value even though input values are of int type. This is done to prevent losing remainder value incase user inputs a dividend which is not divisible by the divisor without a remainder.
number1 = int(input("Enter 1st number: "))
number2 = int(input("Enter 2nd number: "))
answer = number1 / number2
print(f"{number1} / {number2} = {answer}")
print(f"type of number1: {type(number1)}")
print(f"type of number2: {type(number2)}")
print(f"type of answer: {type(answer)}")
Simple Calculator
Now that we know how to implement type casting, let’s implement out complete calculator program with casting input values to float type, so that we can input both integer and fractional values. Additionally, let’s use the round() function to round our answers to two decimal places.
number1 = float(input("Enter 1st number: "))
number2 = float(input("Enter 2nd number: "))
add = round(number1 + number2, 2)
subtract = round(number1 - number2, 2)
multiply = round(number1 * number2, 2)
divide = round(number1 / number2, 2)
print(f"{number1} + {number2} = {add}")
print(f"{number1} - {number2} = {subtract}")
print(f"{number1} * {number2} = {multiply}")
print(f"{number1} / {number2} = {divide}")
Comments
Post a Comment