Type Casting (Implicit vs Explicit)

Python mein Type Casting (jise Type Conversion bhi kaha jata hai) ka matlab hota hai ek data type ke value ko dusre data type mein badalna. Jaise kisi number ko text (string) mein badalna ya kisi decimal number (float) ko normal number (integer) mein badalna. Python mein type casting do tarah ki hoti hai:

1. Implicit Type Conversion (Automatic Conversion)

Implicit type conversion tab hota hai jab Python bina aapke kahe, apne aap ek data type ko dusre data type mein badal deta hai.

Python yeh kaam tabhi karta hai jab data ka nuksan (data loss) na ho raha ho. Python hamesha "chote" data type ko "bade" data type mein convert karta hai taaki accuracy bani rahe. niche Implicit เค•เคพ ek python code diya gaya hai .

# Integer value
num_int = 12  

# Float value
num_flo = 10.5  

# Dono ko add kiya
result = num_int + num_flo  

print("Result:", result)
print("Result ka Data Type:", type(result))
Code Output
Result: 22.5
Result ka Data Type: < class 'float' >

Upar ke example mein num_int ek integer tha. Lekin plus (+) karne ke baad jo result aaya, woh 22.5 (float) ban gaya. Python ne aisa isliye kiya kyunki agar woh result ko integer banata, toh .5 gayab ho jata, jisse aapka calculation galat ho jata. Is automatic process ko hi Implicit Conversion kehte hain.

2. Explicit Type Casting (Forced Conversion)

Explicit Type Casting tab hoti hai jab hum (developer) khud Python ko bolte hain ki "Is data type ko badal kar us data type mein convert karo." Iske liye hum Python ke inbuilt functions (jaise int(), float(), str()) ka istemal karte hain.

Iski zaroorat tab padti hai jab Python automatic conversion nahi kar pata. Jaise agar aap kisi text "10" ko number 5 ke sath jodna chahein, toh Python error de dega. Wahan aapko explicit casting karni padegi.Python mein explicit casting ke liye mukhyatah yeh functions use hote hain:

A. int() Function

Yeh kisi bhi float value ya valid numeric string ko integer mein badalta hai. Dhayan rahe, float ko int mein badalte waqt decimal ke baad ka hissa hat jata hai (round off nahi hota, sirf kat jata hai).

# Float ko int mein badalna
a = int(10.99)  # Output: 10 (decimal hat gaya)

# String ko int mein badalna
b = int("25")   # Output: 25

print(a, type(a))
print(b, type(b))

B. float() Function

Yeh kisi integer ya valid numeric string ko float (decimal number) mein badal deta hai. niche float() function kaa ek python code diya gaya hai.

# Int ko float mein badalna
x = float(5)     # Output: 5.0

# String ko float mein badalna
y = float("4.25") # Output: 4.25

print(x, type(x))
print(y, type(y))

C. str() Function

Yeh kisi bhi data type (number, float, list, etc.) ko string (text) mein badal deta hai. Jab aapko numbers ko text ke sath jodna ho, tab yeh bahut kaam aata hai.

age = 25
# Direct print("My age is " + age) likhne par error aayega
# Isliye str() ka use karenge:

message = "My age is " + str(age)
print(message) # Output: My age is 25

D. list(), tuple(), set() Functions

Aap inka use karke collection data types ko bhi aapas mein badal sakte hain.
1. list() Function : Yeh kisi bhi iterable (jaise tuple ya set) ko ek order-wise editable array ya list mein badal deta hai.
2. tuple() Function: Yeh list ya set ko tuple mein badalta hai, jisse uski values secure (read-only) ho jati hain aur change nahi ki ja sakteen.
3. set() Function: Yeh data ko unique collection mein badal deta hai, jisse saari duplicate values apne aap remove ho jati hain.

# 1. List ko Tuple mein badalna
my_list = [1, 2, 2, 3]
my_tuple = tuple(my_list)  # Output: (1, 2, 2, 3)

# 2. Tuple ko Set mein badalna (Set duplicate values ko automatic hata deta hai)
my_set = set(my_tuple)     # Output: {1, 2, 3}

# 3. Set ko wapas List mein badalna
final_list = list(my_set)  # Output: [1, 2, 3]
Feature Implicit Type Conversion Explicit Type Casting
Performed By Done automatically by the Python interpreter. Done manually by the developer using built-in functions.
Data Loss No data loss occurs (Python safely upgrades data types). Data loss can occur (e.g., losing decimals when casting float to int).
Errors Completely safe; never causes runtime errors or exceptions. Can cause errors (like ValueError) if the conversion is invalid.