1. Introduction
Type casting (or type conversion) is the process of converting a value from one data type to another, such as turning the string "25" into the integer 25. Python supports two kinds of type conversion: implicit conversion, which the interpreter performs automatically in certain expressions, and explicit conversion, where you call a built-in function to convert a value yourself.
Cricket analogy: When a scorer converts overs bowled (a decimal like 4.3) into a total ball count that's implicit conversion; when a captain manually asks the third umpire to review a decision, that's explicit — Python does the same converting '25' to 25 automatically or via int().
Type casting is essential whenever data arrives in the 'wrong' type for what you need to do with it — most commonly when reading user input (always a string) and needing to perform arithmetic on it, or when formatting numeric results as text for display.
Cricket analogy: When a scorer keys in a batsman's score as text from a manual sheet, it must be cast to an integer before totaling the innings score, just as Python casts an input() string before doing arithmetic on it.
2. Syntax
int("100") # str -> int
float("3.14") # str -> float
str(42) # int -> str
bool(0) # int -> bool
list("abc") # str -> list
# implicit conversion
result = 5 + 2.5 # int + float -> float3. Explanation
Implicit type conversion happens automatically when Python combines two compatible numeric types in an expression; for example, adding an int and a float produces a float because Python 'widens' the int to avoid losing precision. No data is lost and no function call is needed.
Cricket analogy: When a team's batting average (a float like 32.5) is added to a whole-number wicket count, Python automatically widens the int to a float so no precision is lost, like a scorer never rounding off a partial run.
Explicit type conversion requires calling a constructor function such as int(), float(), str(), list(), or bool() directly on a value. These functions attempt to reinterpret the input as the target type and raise an exception if the conversion is not meaningful.
Cricket analogy: Explicitly calling int() on a scoreboard string like '187' works fine, but calling int() on 'not out' raises an exception, just as a scorer can't force a non-numeric result into a run total.
bool() treats 0, 0.0, empty strings, empty collections, and None as falsy, and virtually everything else — including non-zero numbers and non-empty strings like "False" — as truthy.
Common gotcha: int("3.14") raises ValueError: invalid literal for int() with base 10: '3.14'. You cannot convert a string containing a decimal point directly to int — you must first convert to float and then to int, e.g. int(float("3.14")).
4. Example
a = 5
b = 2.5
c = a + b
print(c, type(c))
x = "100"
y = int(x)
print(y, type(y))
z = "3.14"
f = float(z)
print(f, type(f))
n = int(f)
print(n)
s = str(42)
print(s, type(s))
lst = list("abc")
print(lst)
print(int(True), int(False))5. Output
7.5 <class 'float'>
100 <class 'int'>
3.14 <class 'float'>
3
42 <class 'str'>
['a', 'b', 'c']
1 06. Key Takeaways
- Implicit conversion happens automatically for compatible numeric types (int + float -> float).
- Explicit conversion uses constructor functions: int(), float(), str(), list(), bool(), etc.
- int("3.14") fails directly — convert via float() first, then int() to truncate.
- Converting float to int truncates toward zero, it does not round.
- bool() follows Python's truthy/falsy rules for the source value.
- str() can convert virtually any object to its readable text representation.
Practice what you learned
1. What is the result of `5 + 2.5` in Python?
2. What happens when you run `int("3.14")`?
3. What does `int(7.9)` evaluate to?
4. What is `bool("False")` in Python?
5. Which explicit conversion correctly turns the string "3.14" into the integer 3?
6. What type results from implicitly adding an int and a float?
Was this page helpful?
You May Also Like
Data Types in Python
A tour of Python's built-in data types — numeric, sequence, mapping, set, boolean, and NoneType — and how mutability affects each.
Variables in Python
Learn how Python variables work as dynamically-typed name bindings, how to create and reassign them, and the rules for naming them correctly.
Input and Output in Python
How to read user input with input() and display output with print(), including formatting with f-strings, sep, end, and format().
try-except in Python
How to use try-except blocks to catch and handle exceptions gracefully, including catching multiple exception types.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics