Python Casting
🐍 Python Casting (Type Conversion)
In Python, casting means converting a value from one data type to another.
For example, converting a string "10" to an integer 10.
Python has built-in functions to cast data types:
| Function | Converts to |
|---|---|
int() | Integer (whole number) |
float() | Decimal number |
str() | String |
✅ 1. Casting to Integer — int()
Use int() when you want to convert values into whole numbers.
⚠️ Note: You cannot convert strings containing letters:
✅ 2. Casting to Float — float()
Use float() to convert values into decimal numbers.
✅ 3. Casting to String — str()
Use str() to convert values into text format.
📌 Examples of Casting in Real Life Programs
Example 1: Input Conversion
Example 2: Mathematical Calculation with Casting
🔁 Mixed Conversion Example
🧠 Summary Table
| Data | int() | float() | str() |
|---|---|---|---|
"10" | ✔ | ✔ | ✔ |
"10.5" | ❌ | ✔ | ✔ |
10 | ✔ | ✔ | ✔ |
10.5 | ✔ (→10) | ✔ | ✔ |
"abc" | ❌ | ❌ | ✔ |
