Lesson 01
What is Type Casting?
Learn why Python converts one possible type into another, and which five fundamental casting functions you will use.
Type casting is the process of converting one possible type of value into another type of value.
You already met this idea when keyboard input returned a string. Before multiplication, that string had to become a number.
What you'll learn
- Convert one value type into another
- Understand why
input()and APIs usually return text - Use the five fundamental casting functions
- Recognize which conversions succeed and which fail
Why do we convert types?
Python is dynamically typed, but every value has one type at a time.
| Situation | What happens |
|---|---|
| Keyboard input | "3.5" → 3.5 using float() |
| Whole-number math | 12.99 → 12 using int() |
| Conditions | 100 → True using bool() |
| Display text | 100 → "100" using str() |
Five fundamental casting functions
| Function | Converts into |
|---|---|
int() |
Integer numbers |
float() |
Decimal numbers |
bool() |
True or False |
complex() |
Complex numbers |
str() |
Text / strings |
General syntax
new_value = int(value)
new_value = float(value)
new_value = bool(value)
new_value = complex(value)
new_value = str(value)The argument must be a possible source type. Later lessons show exactly which combinations work.
Interactive playground
Change the input and instantly see which conversions succeed.
The value is treated as a string, the same way input() works.
Current Python type: str
Conversion results
int()
Success
12 (int)
float()
Success
12.0 (float)
bool()
Always succeeds
True (bool)
In Python, any non-empty string becomes True.
complex()
Success
(12+0j) (complex)
str()
Always succeeds
"12" (str)
Try these values:
Possible vs not possible
Not every conversion is allowed.
| Result | Example |
|---|---|
| Possible | int(12.34) → 12 |
| Possible | float("12.34") → 12.34 |
| TypeError | int(2+3j) |
| ValueError | int("12.34") |
Remember the difference
- TypeError: The value’s type cannot be converted.
- ValueError: The type is valid, but the content is invalid.
This module focuses on those five converters. Collection conversions (list(), tuple(), set(), dict()) come later.
Real Python example
text = "12"
number = int(text)
print(number)
print(type(number))12
<class 'int'>The original string is not modified. Python creates a new integer object.
Practice exercises
- Print
type("10")andtype(int("10")). - Predict whether
float("10")works, then run it. - Test
int("10.5")and observe theValueError.
Quick knowledge check
-
What is type casting?
Converting one possible type of value into another type of value. -
Name the five fundamental converters.
int(),float(),bool(),complex(),str() -
Does every conversion succeed?
No. Invalid conversions raise eitherTypeErrororValueError.
Summary
- Type casting converts one value type into another.
- Python has five fundamental casting functions.
- Success depends on the source type and, for strings, the text content.
- The next lesson explores
int()in depth with every valid and invalid conversion.
Loading playground…