← Back to Home

Type Casting in Python

By Instructor

Type Casting ka matlab hai ek data type ko doosre data type mein convert karna. Program execution me explicit and implicit type conversions dynamic calculations ke liye essential hote hain.

This comprehensive guide covers int(), float(), str(), bool(), list(), tuple(), set(), user input type casting, and quick conversion tables.

By Instructor8 chaptersPython 3.xBeginner to Advanced
typecasting.py
x = "100"
num = int(x)

print(f"Value: {num}, Type: {type(num)}")
python typecasting.py → Value: 100, Type: <class 'int'>
Chapter 1

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.

basic_conversion.py
x = "100"
x = int(x)
print(x, type(x))

Common Type Casting Functions

  • int() — Converts value to Integer
  • float() — Converts value to Floating-point number
  • str() — Converts value to String
  • bool() — Converts value to Boolean (True/False)
  • list() — Converts iterable to List
  • tuple() — Converts iterable to Tuple
  • set() — Converts iterable to Set
Chapter 2

Integer Casting: int()

int() kisi value ko Integer (whole number) mein convert karta hai.

Syntax: int(value)

Example 1: Int to Float Conversion & Printing

example_int.py
age = 22
print(age)
print(type(age))

age = float(age)
print(age)
print(type(age))

Example 2: Float to Integer

float_to_int.py
price = 99.99
print(price)

price = int(price)
print(price)
print(type(price))
Important Note on Truncation

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.

Chapter 3

Float Casting: float()

float() kisi value ko Floating Point Number (decimal number) mein convert karta hai.

Syntax: float(value)

Example 1: Integer to Float

int_to_float.py
num = 105
num = float(num)
print(num)
print(type(num))

Example 2: Float to String Conversion

float_to_str.py
price = 99.99
price = str(price)
print(price)
print(type(price))

Example 3: Direct Float Evaluation

direct_float.py
x = 50
print(float(x))
Chapter 4

String Casting: str()

str() kisi value ko String (text format) mein convert karta hai.

Syntax: str(value)

Example 1: Integer to String

int_to_str.py
age = 25
age = str(age)

print(age)
print(type(age))

Example 2: Float to Boolean Evaluation

float_to_bool.py
price = 99.99
price = bool(price)

print(price)
print(type(price))

Example 3: Real-life Concatenation Use

concatenation.py
age = 25
print("My age is " + str(age))
Chapter 5

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

str_bool.py
name = "gagan"
print(name)
print(bool(name))

Zero vs Non-Zero Boolean Values

zero_bool.py
x = 0
print(bool(x))
ExpressionResultReason / Rule
bool(0)FalseZero value is False
bool(1)TruePositive non-zero is True
bool(10)TrueAny non-zero integer is True
bool(-5)TrueNegative non-zero integer is True
bool("Hello")TrueNon-empty string is True
bool("")FalseEmpty string is False
Chapter 6

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.

list_conversions.py
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.

tuple_conversions.py
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.

set_conversions.py
numbers = [10, 20, 20, 30, 30, 30]
result = set(numbers)
print(result)

banana_name = "banana"
result_banana = set(banana_name)
print(result_banana)
Chapter 7

User Input + Type Casting

Python me input() function hamesha String data type return karta hai. Mathematical operations ke liye numerical conversion essential hota hai.

user_age_str.py
age = input("Enter your age: ")
print(type(age))

Integer Input Conversion

user_age_int.py
age = int(input("Enter your age: "))
print(age)
print(type(age))

Addition of Two User Inputs

addition_user_input.py
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

result = num1 + num2
print("Result:", result)
Chapter 8

Quick Revision & Summary

Quick lookup guide for type casting functions and string conversions:

FunctionTarget Data TypeBehavior / Key Property
int()IntegerRemoves decimal portion completely
float()FloatAdds decimal point (e.g. 50 → 50.0)
str()StringConverts any value to text format
bool()Boolean0 and empty strings → False; others → True
list()ListConverts iterable to mutable square bracket list
tuple()TupleConverts iterable to immutable round bracket tuple
set()SetConverts iterable to unique unordered set

Quick Conversion Examples

quick_examples.py
x = "100"

print(int(x))
print(float(x))
print(str(x))
print(bool(x))
print(list(x))
print(tuple(x))
print(set(x))
Memory Trick

Type Casting = Data Type ko Convert karna.
"100" → int("100") → 100 | 1 → bool(1) → True | "ABC" → list("ABC") → ['A', 'B', 'C']