Data Types

Description

๐Ÿ“˜ Python Data Types - Category Wise

Category Data Type Description Example
๐Ÿงฎ Numeric Types int Integer numbers (positive, negative, zero) 10, -5, 0
๐Ÿงฎ Numeric Types float Floating-point numbers (decimals) 3.14, -0.99
๐Ÿงฎ Numeric Types complex Complex numbers with real and imaginary parts 2 + 3j
๐Ÿงต Text Type str Sequence of Unicode characters (strings) 'hello', "Python"
๐Ÿ“ฆ Sequence Types list Ordered, mutable, allows duplicate values [1, 2, 3], ['a', 'b']
๐Ÿ“ฆ Sequence Types tuple Ordered, immutable, allows duplicates (10, 20), (1,)
๐Ÿ“ฆ Sequence Types range Immutable sequence of numbers, often used in loops range(0, 5)
๐Ÿ”‘ Mapping Type dict Key-value pairs, mutable {'id': 1, 'name': 'Alice'}
๐Ÿ”˜ Set Types set Unordered, mutable collection of unique elements {1, 2, 3}
๐Ÿ”˜ Set Types frozenset Unordered, immutable set frozenset([1, 2, 3])
๐Ÿ”ข Boolean Type bool Represents True or False True, False
โšฐ๏ธ Binary Types bytes Immutable sequence of bytes b'hello'
โšฐ๏ธ Binary Types bytearray Mutable sequence of bytes bytearray([65, 66, 67])
โšฐ๏ธ Binary Types memoryview Memory view object of another binary object memoryview(b'abc')
๐Ÿง™ None Type NoneType Represents absence of value None

๐Ÿง  Data Type: int – Integer

๐Ÿ” What is int?

Used to represent whole numbers. No decimal points. Includes positive, negative numbers, and zero.

โœ… Real-Time Scenarios

Real-World Use Case Python Example
Age tracking age = 25
Employee salary salary = 45000
Product quantity qty = 5
Page number page = 12
Attendance count students_present = 30

โš™๏ธ Built-in Functions for int

Function Description Real-Time Use Case Example
int() Convert to integer Convert string to number int("45") → 45
abs() Absolute value Ignore negatives in balance abs(-100) → 100
pow() Exponentiation Power functions in finance pow(2, 3) → 8
bin() To binary Electronics representation bin(5) → '0b101'
hex() To hexadecimal Hex color codes, memory hex(255) → '0xff'
oct() To octal Low-level development oct(8) → '0o10'
round() Round number Invoice approximation round(99.6) → 100
type() Get data type Validation/debugging type(10) → <class 'int'>
# Real-Time Code Examples for int price_str = "150" price = int(price_str) print(price * 2) # 300 print(abs(-200)) # 200 print(pow(2, 5)) # 32 print(bin(10)) # 0b1010 print(hex(255)) # 0xff print(oct(10)) # 0o12 print(round(49.75)) # 50 print(type(100)) # <class 'int'>

๐Ÿง  Data Type: float – Floating-Point Number

๐Ÿ” What is float?

Used to represent numbers with decimal points. Useful when precision is needed like in prices, weight, or scientific data.

โœ… Real-Time Scenarios

Real-World Use Case Python Example
Product price price = 149.99
Bank balance balance = 10500.50
Student marks marks = 78.25
Temperature temp = 36.6
BMI tracking bmi = 22.45

โš™๏ธ Built-in Functions for float

Function Description Real-Time Use Case Example
float() Convert to float Price conversion float("99.99") → 99.99
round() Round to decimals Mark/price rounding round(78.456, 2) → 78.46
int() Convert to integer Drop decimal if needed int(12.9) → 12
format() Format float Invoice printing format(3.14159, ".2f") → '3.14'
is_integer() Check if decimal part is 0 Validation check (5.0).is_integer() → True
type() Get data type Validation/debugging type(5.5) → <class 'float'>
# Real-Time Code Examples for float price = float("149.99") print(price + 50) # 199.99 marks = 78.4567 print(round(marks, 2)) # 78.46 weight = 59.8 print(int(weight)) # 59 pi = 3.14159 print(format(pi, ".2f")) # '3.14' value = 7.0 print(value.is_integer()) # True x = 5.5 print(type(x)) # <class 'float'>

๐Ÿง  Data Type: str – String

๐Ÿ” What is str?

The str (string) type is used to represent text in Python. It's a sequence of characters enclosed in quotes, like "hello" or 'world'.

โœ… Real-Time Scenarios

Real-World Use Case Python Example
User name name = "Sairam"
Greeting message message = "Welcome back!"
Password input password = "abc@123"
Address or location address = "Hyderabad"
Email ID email = "hello@gmail.com"
Status or label status = "Pending"

โš™๏ธ Built-in Functions/Methods for str

Function/Method Description Real-Time Use Case Example
str() Convert value to string Print numbers with text str(25) → '25'
.lower() To lowercase Case-insensitive search "HELLO".lower()
.upper() To uppercase Header formatting "hello".upper()
.title() Capitalize each word Display names/titles "hello world".title()
.strip() Remove spaces Form input cleaning " hello ".strip()
.replace() Replace part of string Masking data "abc123".replace("123", "***")
.split() Split string to list Parse CSV text "a,b,c".split(",")
.join() Join list to string Format CSV line ",".join(["a","b"])
.find() Find index Search function "hello".find("l")
.startswith() Check start Prefix validation "abc123".startswith("abc")
.endswith() Check end File type validation "report.pdf".endswith(".pdf")
.count() Count substring Frequency analysis "banana".count("a")
len() Length of string Password validation len("hello")
type() Get data type Validation/debugging type("text")
# Real-Time Code Examples for str # Convert number to string num = 25 print("Age is " + str(num)) # Age is 25 # Formatting text name = "sAiRaM" print(name.lower()) # sairam print(name.upper()) # SAIRAM print(name.title()) # Sairam # Cleaning strings input_val = " hello world " print(input_val.strip()) # "hello world" # Replace data password = "user123" print(password.replace("123", "***")) # user*** # Split and join csv = "red,blue,green" colors = csv.split(",") print(colors) # ['red', 'blue', 'green'] data = ["id", "name", "email"] print(",".join(data)) # id,name,email # Search operations print("hello".find("e")) # 1 print("data.csv".endswith(".csv")) # True print("banana".count("a")) # 3 print(len("example")) # 7

๐Ÿง  Data Type: bool – Boolean

๐Ÿ” What is bool?

The bool data type in Python represents True or False. It is mainly used in logical operations and condition checking (e.g., if, while).

โœ… Real-Time Scenarios

Real-World Use Case Python Example
Is user logged in? is_logged_in = True
Is cart empty? cart_empty = False
Did payment succeed? payment_success = True
Is age greater than 18? is_adult = age > 18
Should email be sent? send_email = len(emails) > 0

โš™๏ธ Built-in Functions/Methods for bool

Function Description Real-Time Use Case Example
bool() Convert value to Boolean Check if field has value bool("abc") → True
bool("") → False
type() Get data type Debugging, validation type(True) → <class 'bool'>
any() True if at least one True Any condition passed any([0, False, 1]) → True
all() True if all are True Form field validations all([1, "yes", True]) → True
int() Convert to integer (True = 1, False = 0) Boolean math int(True) → 1
int(False) → 0
# Real-Time Code Examples for bool # Convert to Boolean print(bool("hello")) # True print(bool("")) # False print(bool(0)) # False print(bool(100)) # True # any() - One True is enough permissions = [False, False, True] print(any(permissions)) # True # all() - All must be True checks = [True, True, True] print(all(checks)) # True # Convert to int is_verified = True print(int(is_verified)) # 1 # Type checking print(type(False)) #