Casting in Python refers to converting a variable from one data type to another. This is a common operation when you need to perform different operations on data types or ensure compatibility with functions that require specific types.
Casting to Integer
You can cast other types to integers using the int() function. This is useful when you have numeric values in string format or floating-point numbers that need to be converted to integers.
Python
# Casting a string to an integer
num_str = "123"
num_int = int(num_str)
print(num_int) # Output: 123
# Casting a float to an integer
num_float = 12.99
num_int = int(num_float)
print(num_int) # Output: 12
Casting to Float
To convert other data types to floats, use the float() function. This is useful for performing arithmetic operations that require decimal precision.
Python
# Casting a string to a float
num_str = "123.45"
num_float = float(num_str)
print(num_float) # Output: 123.45
# Casting an integer to a float
num_int = 123
num_float = float(num_int)
print(num_float) # Output: 123.0
Casting to String
You can convert other types to strings using the str() function. This is particularly useful when you need to concatenate numbers with strings or print variables in a readable format.
Python
# Casting an integer to a string
num_int = 123
num_str = str(num_int)
print(num_str) # Output: "123"
# Casting a float to a string
num_float = 12.34
num_str = str(num_float)
print(num_str) # Output: "12.34"
Casting to Boolean
The bool() function converts values to Boolean (True or False). This is helpful for condition checks and logical operations.
Casting may raise errors if the conversion is not possible. For example, trying to cast a string with non-numeric characters to an integer will result in a ValueError. Always ensure that the data being casted is compatible with the target type to avoid such errors.
Casting in Python
Python me casting ka matlab ek variable ko ek data type se doosre data type me convert karna hota hai. Ye tab common hai jab aapko alag-alag operations perform karne hote hain ya aise functions ke saath kaam karna hota hai jo specific types ke sath compatible hote hain.
Casting to Integer
Aap int() function ka use karke doosre types ko integers me cast kar sakte hain. Ye tab useful hota hai jab aapke paas string format me numeric values hoti hain ya floating-point numbers ko integers me convert karna hota hai.
Python
# Ek string ko integer me cast karna
num_str = "123"
num_int = int(num_str)
print(num_int) # Output: 123
# Ek float ko integer me cast karna
num_float = 12.99
num_int = int(num_float)
print(num_int) # Output: 12
Casting to Float
Doosre data types ko floats me convert karne ke liye float() function ka use karein. Ye un arithmetic operations ke liye useful hai jisme decimal precision ki zaroorat hoti hai.
Python
# Ek string ko float me cast karna
num_str = "123.45"
num_float = float(num_str)
print(num_float) # Output: 123.45
# Ek integer ko float me cast karna
num_int = 123
num_float = float(num_int)
print(num_float) # Output: 123.0
Casting to String
Aap str() function ka use karke doosre types ko strings me convert kar sakte hain. Ye tab khaas taur par useful hai jab aapko numbers ko strings ke sath concatenate karna hota hai ya variables ko readable format me print karna hota hai.
Python
# Ek integer ko string me cast karna
num_int = 123
num_str = str(num_int)
print(num_str) # Output: "123"
# Ek float ko string me cast karna
num_float = 12.34
num_str = str(num_float)
print(num_str) # Output: "12.34"
Casting to Boolean
bool() function values ko Boolean (True ya False) me convert karta hai. Ye condition checks aur logical operations ke liye madadgar hai.
Agar conversion possible nahi hai to casting errors raise kar sakta hai. Udaharan ke liye, agar aap ek string jo non-numeric characters contain karti hai usse integer me cast karte hain to ValueError aayega. Hamesha ye ensure karein ki jo data aap cast kar rahe hain wo target type ke sath compatible ho taaki aise errors avoid ho sakein.
Import Links
Here are some useful import links for further reading: