Python Casting
Python casting means converting a value from one data type to another.
It is mainly used when you want to specify a variable type explicitly instead of relying on automatic type detection.
Casting is extremely important while:
- Taking user input
- Performing calculations
- Cleaning and validating data
Specify a Variable Type
Python allows you to specify a variable’s data type by using built-in casting functions.
Common casting functions:
int()float()str()bool()
python
x = int(10)
y = float(10)
z = str(10)
print(x, y, z)
python
a = int("25")
b = float("3.14")
c = str(True)
print(a, b, c)
Casting Integer (int)
Converts a value into an integer.
python
x = int(9.9)
print(x)
python
y = int("100")
print(y)
python
z = int(True)
print(z)
Hidden Fact
True becomes 1 and False becomes 0 when cast to int.
Casting Float (float)
Converts a value into a floating-point number.
python
x = float(10)
print(x)
python
y = float("5.5")
print(y)
python
z = float(False)
print(z)
Casting String (str)
Converts a value into a string.
python
x = str(100)
print(x)
python
y = str(3.14)
print(y)
python
z = str(None)
print(z)
Casting Boolean (bool)
Converts a value into
True or False.python
print(bool(1))
python
print(bool(0))
python
print(bool(""))
python
print(bool("Python"))
Important Rule
0,None,"",[],{}→False- Everything else →
True
Casting Collections (Less Known but Powerful)
Many students don’t know you can cast between collection types.
List Casting
python
x = list("Python")
print(x)
python
y = list((1, 2, 3))
print(y)
python
z = list(range(5))
print(z)
Tuple Casting
python
x = tuple([1, 2, 3])
print(x)
python
y = tuple("Hello")
print(y)
python
z = tuple(range(3))
print(z)
Set Casting (Unique Values Only)
python
x = set([1, 2, 2, 3])
print(x)
python
y = set("Python")
print(y)
python
z = set((10, 20, 20, 30))
print(z)
Casting with type() (Advanced Insight)
Many students don’t know you can check casting results dynamically.
python
x = int("50")
print(type(x))
python
y = float("5.5")
print(type(y))
python
z = bool("False")
print(type(z))
User Input Casting (Very Important)
User input is always a string, even if you enter a number.
python
age = int(input("Enter age: "))
print(age)
python
price = float(input("Enter price: "))
print(price)
python
name = str(input("Enter name: "))
print(name)
Common Casting Errors (Hidden Traps)
python
# int("Python") # Error
# float("Ten") # Error
Correct way:
python
num = "10"
result = int(num)
print(result)
New Concept Many Students Don’t Know
eval() for Dynamic Casting (Use Carefully)
eval() converts a string into a Python expression.python
x = eval("10 + 5")
print(x)
python
y = eval("3.14")
print(type(y))
python
z = eval("[1, 2, 3]")
print(type(z))
Security Warning
Never use eval() on user input. It can execute harmful code.
Summary
- Casting specifies variable data type explicitly
- Use
int(),float(),str(),bool()commonly - Collections can also be cast
- Boolean casting follows strict rules
- User input always needs casting
eval()is powerful but dangerous
Exercise
- Convert a string number to integer
- Convert a word into a list
- Convert
Trueto integer and float - Use
eval()safely with a numeric expression