Python Casting: Beginner Python tutorials 5 | Better4Code
Python Casting: Python is one of the most popular programming languages in use today. It is a general-purpose language that is used for web development, data analysis, machine learning, and more. One of the key features of Python is its dynamic typing system, which means that the data type of a variable is inferred at runtime. However, sometimes it is necessary to convert data from one type to another, and this is where casting comes in.
Python Casting refers to the process of converting a value from one data type to another. Python supports several types of casting, including explicit and implicit casting. Explicit casting, also known as type casting, is when a programmer specifies the type of data to convert to. Implicit casting, also known as type coercion, is when Python automatically converts the data to the appropriate type based on the context.
In this article, we will explore Python casting in more detail, including its types and examples.
Types of Python Casting
There are three main types of casting in Python: int(), float(), and str(). The int() function is used to convert a value to an integer, the float() function is used to convert a value to a floating-point number, and the str() function is used to convert a value to a string.
Let’s look at some examples of each type of casting.
Int Casting
To convert a value to an integer, we use the int() function. Here is an example:
x = "123"
y = int(x)
print(y)
Output:
123
In this example, we convert the string “123” to an integer using the int() function. The output is 123.
Float Casting
To convert a value to a floating-point number, we use the float() function. Here is an example:
x = "3.14"
y = float(x)
print(y)
Output:
3.14
In this example, we convert the string “3.14” to a floating-point number using the float() function. The output is 3.14.
Str Casting
To convert a value to a string, we use the str() function. Here is an example:
x = 123
y = str(x)
print(y)
Output:
"123"
In this example, we convert the integer 123 to a string using the str() function. The output is the string “123”.
Implicit Casting
Python also supports implicit casting, where the data type is automatically converted based on the context. For example, if we add an integer and a floating-point number, Python will automatically convert the integer to a floating-point number to perform the calculation. Here is an example:
x = 10
y = 3.14
z = x + y
print(z)
Output:
13.14
In this example, we add an integer and a floating-point number. Python automatically converts the integer to a floating-point number before performing the calculation.
Conclusion
Casting is an important concept in Python that allows us to convert data from one type to another. In this article, we explored the three main types of casting in Python: int(), float(), and str(). We also looked at an example of implicit casting. With this knowledge, you can easily manipulate data of different types in Python.