What are Data Types
Data Types in Python are used to define the type of data stored in a variable. They tell Python what kind of value a variable contains, such as a number, text, list, dictionary, or boolean value.
Variables store different kinds of data, and operations on variables depend on their assigned data type.
Python is dynamically typed. You do not need to explicitly declare a variable's data type; Python detects it automatically when a value is assigned.
Primary Data Types Categories
Python offers several built-in data types grouped into primary categories:
1. Numeric Types
- int: Integer numbers (e.g.
10,-848561456). - float: Decimal numbers (e.g.
9.5,75.6). - complex: Numbers with real and imaginary parts (e.g.
5j,3 + 4j, or created viacomplex(3, 4)).
2. Boolean Type
- bool: Represents boolean truth values:
TrueorFalse.
number = -848561456 percentage = 75.6 is_active = True num1 = 5j num2 = 3 + 4j num3 = complex(3, 4) print(number, percentage, is_active, num2)
Mutable vs Immutable Data Types
Python data types memory mutability basis par do categories me split hote hain: Mutable and Immutable.
| Point | Mutable | Immutable |
|---|---|---|
| Meaning | Value ko change kar sakte hain | Value ko change nahi kar sakte |
| After creation | Object ke andar changes possible | Object ke andar changes possible nahi |
| Memory | Same object ko modify kiya ja sakta hai | Change karne par new object create hota hai |
| Examples | list, dict, set | int, float, string, tuple, bool |
| Code Example | my_list = [1, 2, 3] → my_list[0] = 10 | x = "Hello" → string characters directly change nahi honge |
| Change possible? | Yes | No |
Immutable objects like Strings or Tuples cannot be modified in-place. Modifying a string variable rebinds the variable to a newly created object in memory.
Identifying Data Types with type()
Python provides the built-in type() function to inspect the exact data type of any variable at runtime.
name = "gagan" age = "22" number = 22 salary = 2220.5 print(type(name)) print(type(age)) print(type(number)) print(type(salary))
gender = "male" print(gender, type(gender))
Sequences, Maps & Sets
Python provides specialized collection data types to manage multiple elements:
Sequence Data Types
- str: String of characters, e.g.
"Hello". - list: Ordered mutable collection, e.g.
[1, 2, 3, "sumit"]. - tuple: Ordered immutable collection, e.g.
(1, 2, 3).
Mapping Data Type
- dict: Key-value pairs dictionary, e.g.
{"name": "John", "age": 25}(where"name"is key,"John"is value).
Set Data Type
- set: Unordered collection of unique items, e.g.
{2, 5, 9, 9, 9, 7}.
dict_data = {
"name": "gagan",
"course": "Python"
}
print(dict_data, type(dict_data))
s = {2, 5, 9, 9, 9, 7}
print(s, type(s))
my_list = [10, 20, 30, 40, "gagan", "sumit"]
print(my_list, type(my_list))Notice how {2, 5, 9, 9, 9, 7} automatically removes duplicate elements when created, resulting in {2, 5, 7, 9}.
Practice Questions & Solutions
Practice questions covering variables, input functions, math operations, and data types:
1. Print Name, Age, and Favourite Food
name = "Gagan"
age = 21
fav_food = "rajma chawal"
print("name", name)
print("Age", age)
print("fav_food", fav_food)2. Swap Values using Temporary Variable
x = 10
y = 20
print("x", x)
print("y", y)
temp = x
x = y
y = temp
print("x", x)
print("y", y)3. Calculate Area from User Input
width = int(input("enter the width = "))
height = int(input("enter the height = "))
area = width * height
print("area", area)4. Welcome Message with User Input
name = input("enter your name: ")
age = int(input("enter your age: "))
print("Welcome", name, "! You are", age, "Years Old")5. Total Cost of Purchased Items
item1 = 50.5
item2 = 25
item3 = 30
total = item1 + item2 + item3
print("total", total)Mini Project: Hotel Management System
An interactive command-line application demonstrating variable storage, user inputs, data type casting, conditional branching, loops, and bill calculations.
print("===== HOTEL MANAGEMENT SYSTEM =====")
while True:
print("\n1. Book Room")
print("2. Order Food")
print("3. Check Room Price")
print("4. Exit")
choice = int(input("\nEnter your choice: "))
if choice == 1:
name = input("Enter your name: ")
room = int(input("Enter room number: "))
print("Room", room, "booked successfully for", name)
elif choice == 2:
print("\n===== FOOD MENU =====")
print("1. Pizza - ₹250")
print("2. Burger - ₹150")
print("3. Pasta - ₹200")
print("4. Coffee - ₹100")
food = int(input("Choose your food: "))
quantity = int(input("Enter quantity: "))
if food == 1:
price = 250
food_name = "Pizza"
elif food == 2:
price = 150
food_name = "Burger"
elif food == 3:
price = 200
food_name = "Pasta"
elif food == 4:
price = 100
food_name = "Coffee"
else:
print("Invalid food choice!")
continue
total = price * quantity
print("\nFood:", food_name)
print("Quantity:", quantity)
print("Total:", total)
if total >= 1000:
discount = total * 20 / 100
print("20% Discount Applied")
elif total >= 500:
discount = total * 10 / 100
print("10% Discount Applied")
else:
discount = 0
print("No Discount")
final_bill = total - discount
print("Discount:", discount)
print("Final Bill:", final_bill)
elif choice == 3:
print("\n===== ROOM PRICES =====")
print("1. Single Room - ₹1500")
print("2. Double Room - ₹2500")
print("3. Deluxe Room - ₹4000")
room_type = int(input("Choose room type: "))
if room_type == 1:
print("Single Room = ₹1500 per night")
elif room_type == 2:
print("Double Room = ₹2500 per night")
elif room_type == 3:
print("Deluxe Room = ₹4000 per night")
else:
print("Invalid room choice!")
elif choice == 4:
print("\nThank you for visiting our hotel!")
break
else:
print("\nInvalid choice!")This project integrates variable assignments, data type conversions, conditional logic (if-elif-else), loops (while True), arithmetic operations for pricing/discounts, and formatted outputs.