← Back to Home

Python Data Types

By Instructor

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.

This comprehensive guide covers Python data types, categories, mutable vs immutable data types, identifying types with type(), sequence & mapping collections, practice questions, and a Hotel Management System mini project.

By Instructor7 chaptersPython 3.xBeginner to Advanced
datatypes.py
name = "Gagan"
age = 21
skills = ["Python", "Data Types"]

print(f"{name}: {type(skills)}")
python datatypes.py → Gagan: <class 'list'>
Chapter 1

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.

Key Concept

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.

Chapter 2

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 via complex(3, 4)).

2. Boolean Type

  • bool: Represents boolean truth values: True or False.
numeric_and_bool.py
number = -848561456
percentage = 75.6
is_active = True
num1 = 5j
num2 = 3 + 4j
num3 = complex(3, 4)

print(number, percentage, is_active, num2)
Chapter 3

Mutable vs Immutable Data Types

Python data types memory mutability basis par do categories me split hote hain: Mutable and Immutable.

PointMutableImmutable
MeaningValue ko change kar sakte hainValue ko change nahi kar sakte
After creationObject ke andar changes possibleObject ke andar changes possible nahi
MemorySame object ko modify kiya ja sakta haiChange karne par new object create hota hai
Exampleslist, dict, setint, float, string, tuple, bool
Code Examplemy_list = [1, 2, 3] → my_list[0] = 10x = "Hello" → string characters directly change nahi honge
Change possible?YesNo
Important Distinction

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.

Chapter 4

Identifying Data Types with type()

Python provides the built-in type() function to inspect the exact data type of any variable at runtime.

identify_types.py
name = "gagan"
age = "22"
number = 22
salary = 2220.5

print(type(name))
print(type(age))
print(type(number))
print(type(salary))
gender_type.py
gender = "male"
print(gender, type(gender))
Chapter 5

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}.
collections_demo.py
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))
Set Automatic Duplicate Removal

Notice how {2, 5, 9, 9, 9, 7} automatically removes duplicate elements when created, resulting in {2, 5, 7, 9}.

Chapter 6

Practice Questions & Solutions

Practice questions covering variables, input functions, math operations, and data types:

1. Print Name, Age, and Favourite Food

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

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

sol_03.py
width = int(input("enter the width = "))
height = int(input("enter the height = "))
area = width * height

print("area", area)

4. Welcome Message with User Input

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

sol_05.py
item1 = 50.5
item2 = 25
item3 = 30

total = item1 + item2 + item3
print("total", total)
Chapter 7

Mini Project: Hotel Management System

An interactive command-line application demonstrating variable storage, user inputs, data type casting, conditional branching, loops, and bill calculations.

hotel_management.py
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!")
Project Summary

This project integrates variable assignments, data type conversions, conditional logic (if-elif-else), loops (while True), arithmetic operations for pricing/discounts, and formatted outputs.