Skip to main content

Type Conversion (Casting)

Following our exploration of Fundamental Data Types: Booleans (True, False), this article covers type conversion, also known as casting. We'll learn how to convert variables from one data type to another.


📚 Prerequisites

A basic understanding of Python's fundamental data types (integers, floats, strings, and booleans).


🎯 Article Outline: What You'll Master

In this article, you will learn:

  • What type conversion is and why it's useful.
  • How to convert between different number types.
  • How to convert other data types to strings.
  • How to convert other data types to booleans.

🧠 Section 1: What is Type Conversion?

Type conversion, or casting, is the process of converting a variable from one data type to another. This is often necessary when you want to perform operations that require a specific data type. For example, you can't add a string and an integer together without first converting one of them.

Python has two types of casting:

  • Implicit Type Casting: Python automatically converts one data type to another. This usually happens when you're working with numbers.
  • Explicit Type Casting: You manually convert a data type using built-in functions like int(), float(), str(), and bool().

💻 Section 2: Converting Between Number Types

You can easily convert between integers and floats using the int() and float() functions.

my_float = 3.14
my_integer = int(my_float) # my_integer is now 3 (the decimal part is truncated)
print(my_integer)

my_integer = 10
my_float = float(my_integer) # my_float is now 10.0
print(my_float)

🛠️ Section 3: Converting to Strings

You can convert almost any data type to a string using the str() function. This is especially useful when you want to concatenate a number with a string.

age = 30
message = "I am " + str(age) + " years old."
print(message)

🔬 Section 4: Converting to Booleans

You can convert any value to its boolean equivalent using the bool() function. This is useful for checking if a value is "truthy" or "falsy".

print(bool(0))      # Output: False
print(bool(1)) # Output: True
print(bool("")) # Output: False
print(bool("Hello")) # Output: True
print(bool([])) # Output: False
print(bool([1, 2])) # Output: True

💡 Conclusion & Key Takeaways

You've now learned how to convert between different data types in Python. This is a fundamental skill that you'll use all the time in your programming journey.

Let's summarize the key takeaways:

  • Type Conversion: The process of converting a variable from one data type to another.
  • int(), float(), str(), bool(): The built-in functions for explicit type conversion.
  • Implicit vs. Explicit: Python can sometimes convert types automatically, but you often need to do it manually.

Challenge Yourself: Write a script that takes a number as input (which will be a string), converts it to an integer, and then prints the square of the number.


➡️ Next Steps

In the next article, we'll learn about "Understanding None".

Happy coding!


Glossary (Python Terms)

  • Type Conversion (Casting): The process of converting a variable from one data type to another.
  • Implicit Type Casting: Automatic type conversion by the Python interpreter.
  • Explicit Type Casting: Manual type conversion using built-in functions.

Further Reading (Python Resources)