📦 What are Variables and Data Types?
Variables are containers used to store data values in computer memory. In Python, variables are created the moment you assign a value using the assignment operator (=).
Data types classify the specific kind of value a variable holds (such as numbers, text, or collections), determining what operations can be performed on them.
1. Variable Declaration & Dynamic Typing
Python is dynamically typed, meaning you do not need to explicitly declare data types when creating variables.
Python
# Variable assignment
company_name = "Nikita-Infotech"
employee_count = 25
rating = 4.85
print(company_name, type(company_name))
print(employee_count, type(employee_count))
print(rating, type(rating))
Output:
Nikita-Infotech <class 'str'>
25 <class 'int'>
4.85 <class 'float'>
Real-Time Example 1: Integer Data Type (int)
Integers represent whole numbers (positive, negative, or zero) without decimals, used for counts and IDs.
Python
stock_quantity = 150
items_sold = 35
remaining_stock = stock_quantity - items_sold
print(f"Initial Stock: {stock_quantity}")
print(f"Items Sold: {items_sold}")
print(f"Remaining Inventory Count: {remaining_stock}")
Output:
Initial Stock: 150
Items Sold: 35
Remaining Inventory Count: 115
Real-Time Example 2: Float Data Type (float)
Floats represent numbers containing decimal points, vital for currency, measurements, and percentages.
Python
item_price = 499.99
gst_rate = 0.18
tax_amount = item_price * gst_rate
total_price = item_price + tax_amount
print(f"Base Price: ₹{item_price}")
print(f"Calculated Tax: ₹{tax_amount:.2f}")
print(f"Total Invoice Amount: ₹{total_price:.2f}")
Output:
Base Price: ₹499.99
Calculated Tax: ₹90.00
Total Invoice Amount: ₹589.99
Real-Time Example 3: String Data Type (str)
Strings represent text enclosed within single or double quotes, used for names, emails, and messages.
Python
customer_name = "Rahul Sharma"
customer_email = "rahul.sharma@gmail.com"
# String concatenation and methods
welcome_msg = f"Hello, {customer_name}! Your email is {customer_email.lower()}."
print(welcome_msg)
Output:
Hello, Rahul Sharma! Your email is rahul.sharma@gmail.com.
Real-Time Example 4: Boolean Data Type (bool)
Booleans represent truth values: either
True or False, critical for conditional logic and flags.
Python
is_logged_in = True
has_premium_access = False
if is_logged_in and not has_premium_access:
print("User is logged in on Standard Plan. Upgrade prompt displayed.")
else:
print("Access status requires verification.")
Output:
User is logged in on Standard Plan. Upgrade prompt displayed.
Real-Time Example 5: List Data Type (list)
Lists are ordered, mutable collections enclosed in square brackets
[] that can hold duplicate items.
Python
shopping_cart = ["Mouse", "Keyboard", "Laptop Sleeve"]
shopping_cart.append("USB-C Hub")
print("Cart Items:", shopping_cart)
print("First item in cart:", shopping_cart[0])
Output:
Cart Items: ['Mouse', 'Keyboard', 'Laptop Sleeve', 'USB-C Hub']
First item in cart: Mouse
Real-Time Example 6: Tuple Data Type (tuple)
Tuples are ordered, immutable (read-only) collections enclosed in parentheses
().
Python
# Fixed GPS coordinates for an office branch
office_coordinates = (18.5204, 73.8567)
print(f"Latitude: {office_coordinates[0]}")
print(f"Longitude: {office_coordinates[1]}")
Output:
Latitude: 18.5204
Longitude: 73.8567
Real-Time Example 7: Set Data Type (set)
Sets are unordered collections of unique elements enclosed in curly braces
{} that automatically remove duplicates.
Python
# Unique visitor IP tracking
visitor_ips = {"192.168.1.10", "10.0.0.5", "192.168.1.10", "172.16.0.4"}
print("Unique Visitor IPs:", visitor_ips)
print("Total Unique Visitors:", len(visitor_ips))
Output:
Unique Visitor IPs: {'192.168.1.10', '10.0.0.5', '172.16.0.4'}
Total Unique Visitors: 3
Real-Time Example 8: Dictionary Data Type (dict)
Dictionaries store data as key-value pairs enclosed in curly braces
{}, optimized for rapid lookups.
Python
user_profile = {
"username": "nikita_dev",
"role": "Full-Stack Developer",
"active_status": True
}
print(f"User Profile -> Name: {user_profile['username']} | Role: {user_profile['role']}")
Output:
User Profile -> Name: nikita_dev | Role: Full-Stack Developer
Real-Time Example 9: Type Casting & Conversion
Convert data types explicitly using built-in functions like
int(), str(), and float().
Python
# User input string representation of a price
input_price_str = "1500"
input_qty_str = "3"
# Explicit Type Casting
total_cost = int(input_price_str) * int(input_qty_str)
print("Calculated Cost:", total_cost, type(total_cost))
Output:
Calculated Cost: 4500 <class 'int'>