What is Type Casting
Type Casting ka matlab hai ek data type ko doosre data type mein convert karna. Python programming me calculations aur user inputs handling ke liye different data types me values transform ki jaati hain.
x = "100" x = int(x) print(x, type(x))
Common Type Casting Functions
int()— Converts value to Integerfloat()— Converts value to Floating-point numberstr()— Converts value to Stringbool()— Converts value to Boolean (True/False)list()— Converts iterable to Listtuple()— Converts iterable to Tupleset()— Converts iterable to Set
Integer Casting: int()
int() kisi value ko Integer (whole number) mein convert karta hai.
Syntax: int(value)
Example 1: Int to Float Conversion & Printing
age = 22 print(age) print(type(age)) age = float(age) print(age) print(type(age))
Example 2: Float to Integer
price = 99.99 print(price) price = int(price) print(price) print(type(price))
int() decimal value ko round nahi karta. Ye decimal part ko discard/remove kar deta hai: int(10.9) → 10, int(10.5) → 10, int(10.1) → 10.
Float Casting: float()
float() kisi value ko Floating Point Number (decimal number) mein convert karta hai.
Syntax: float(value)
Example 1: Integer to Float
num = 105 num = float(num) print(num) print(type(num))
Example 2: Float to String Conversion
price = 99.99 price = str(price) print(price) print(type(price))
Example 3: Direct Float Evaluation
x = 50 print(float(x))
String Casting: str()
str() kisi value ko String (text format) mein convert karta hai.
Syntax: str(value)
Example 1: Integer to String
age = 25 age = str(age) print(age) print(type(age))
Example 2: Float to Boolean Evaluation
price = 99.99 price = bool(price) print(price) print(type(price))
Example 3: Real-life Concatenation Use
age = 25
print("My age is " + str(age))Boolean Casting: bool()
bool() kisi value ko Boolean value mein convert karta hai. Boolean ke sirf 2 values hoti hain: True aur False.
Syntax: bool(value)
String to Boolean
name = "gagan" print(name) print(bool(name))
Zero vs Non-Zero Boolean Values
x = 0 print(bool(x))
| Expression | Result | Reason / Rule |
|---|---|---|
bool(0) | False | Zero value is False |
bool(1) | True | Positive non-zero is True |
bool(10) | True | Any non-zero integer is True |
bool(-5) | True | Negative non-zero integer is True |
bool("Hello") | True | Non-empty string is True |
bool("") | False | Empty string is False |
Collection Casting: list(), tuple(), set()
Iterables (strings, tuples, sets, lists) ko doosre collection types me convert karne ke functions:
1. list() Function
list() kisi iterable ko List mein convert karta hai. List square brackets [] use karti hai, multiple values store karti hai aur mutable hoti hai.
name = "Gagan"
result_str = list(name)
print(result_str)
numbers_tuple = (10, 20, 30)
result_tup = list(numbers_tuple)
print(result_tup)
numbers_set = {10, 20, 30}
result_set = list(numbers_set)
print(result_set)2. tuple() Function
tuple() kisi iterable ko Tuple mein convert karta hai. Tuple round brackets () use karta hai, ordered aur immutable hota hai.
numbers_list = [10, 20, 30]
result_list = tuple(numbers_list)
print(result_list)
name = "Gagan"
result_str = tuple(name)
print(result_str)
numbers_set = {10, 20, 30}
result_set = tuple(numbers_set)
print(result_set)3. set() Function
set() kisi iterable ko Set mein convert karta hai. Set duplicate values ko automatically remove kar deta hai.
numbers = [10, 20, 20, 30, 30, 30] result = set(numbers) print(result) banana_name = "banana" result_banana = set(banana_name) print(result_banana)
User Input + Type Casting
Python me input() function hamesha String data type return karta hai. Mathematical operations ke liye numerical conversion essential hota hai.
age = input("Enter your age: ")
print(type(age))Integer Input Conversion
age = int(input("Enter your age: "))
print(age)
print(type(age))Addition of Two User Inputs
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 + num2
print("Result:", result)Quick Revision & Summary
Quick lookup guide for type casting functions and string conversions:
| Function | Target Data Type | Behavior / Key Property |
|---|---|---|
int() | Integer | Removes decimal portion completely |
float() | Float | Adds decimal point (e.g. 50 → 50.0) |
str() | String | Converts any value to text format |
bool() | Boolean | 0 and empty strings → False; others → True |
list() | List | Converts iterable to mutable square bracket list |
tuple() | Tuple | Converts iterable to immutable round bracket tuple |
set() | Set | Converts iterable to unique unordered set |
Quick Conversion Examples
x = "100" print(int(x)) print(float(x)) print(str(x)) print(bool(x)) print(list(x)) print(tuple(x)) print(set(x))
Type Casting = Data Type ko Convert karna.
"100" → int("100") → 100 | 1 → bool(1) → True | "ABC" → list("ABC") → ['A', 'B', 'C']