View Categories

Data Types

Data Types

Why do we use Data Types? #

In programming, a computer is like a very fast but literal-minded worker. If you give it two things and say “add these,” it needs to know if they are numbers (to do math) or text (to join them together).

Data Types tell the computer:

  1. How much memory to reserve.
  2. What operations are allowed (e.g., you can’t divide “Apple” by 5).
  3. How to treat the data internally.

1. Numeric Data (Numbers) #

These are used for any mathematical calculation.

  • int: Whole numbers.
  • float: Decimal points.
  • complex: Real and imaginary parts.
items = 10            # int
price = 15.5          # float
complex_val = 1 + 2j  # complex

print(f"Items: {items} | Type: {type(items)}")
print(f"Price: {price} | Type: {type(price)}")
print(f"Complex: {complex_val} | Type: {type(complex_val)}")

Output:

Items: 10 | Type: <class 'int'>
Price: 15.5 | Type: <class 'float'>
Complex: (1+2j) | Type: <class 'complex'>

2. Text Data (Strings) #

Strings are used to store characters, words, or sentences. They must be inside quotes.

brand = "AI Learner Tech"
location = 'Multan'

print(f"Brand: {brand}")
print(f"Type: {type(brand)}")

Output:

Brand: AI Learner Tech
Type: <class 'str'>

3. Sequence Data (List, Tuple, Range) #

These are used to store multiple items in a single variable.

  • List: Changeable (Mutable) collection.
  • Tuple: Fixed (Immutable) collection.
  • Range: A sequence of integers.
my_list = ["Python", "C++", "Java"]
my_tuple = (10, 20, 30)
my_range = range(5)

print("List:", my_list)
print("Tuple:", my_tuple)
print("Range as List:", list(my_range))

Output:

List: ['Python', 'C++', 'Java']
Tuple: (10, 20, 30)
Range as List: [0, 1, 2, 3, 4]

4. Mapped Data (Dictionary) #

A dictionary stores data in Key:Value pairs. It’s like an ID card where “Name” is the key and “Ali” is the value.

student = {"name": "Hamza", "grade": "A"}

print(student["name"])
print(f"Full Dictionary: {student}")

Output:

Hamza
Full Dictionary: {'name': 'Hamza', 'grade': 'A'}

5. Set Data #

Sets are for unique items. They automatically delete any duplicates you try to add.

Code Example:

numbers = {1, 2, 2, 3, 4, 4}

print("Set:", numbers)

Output:

Set: {1, 2, 3, 4}

6. Binary Data #

Used for low-level machine tasks, handling images, or raw file streams.

data_bytes = bytes("Hello", "utf-8")
data_array = bytearray([50, 100, 150])

print("Bytes:", data_bytes)
print("Bytearray:", data_array)

Output:

Bytes: b'Hello'
Bytearray: bytearray(b'2dk')

7. None Type #

Represents the absence of a value. It’s a placeholder.

x = None
print(f"Value of x: {x}")
print(f"Type of x: {type(x)}")

Output:

Value of x: None
Type of x: <class 'NoneType'>

Summary Table: Python Data Types #

CategoryTypeMutable (Can Change)?Best Used For
Numericint, floatNoMath, Counting, Prices
TextstrNoNames, Labels, Descriptions
SequencelistYesDynamic lists, shopping carts
SequencetupleNoFixed data (Coordinates, Constants)
MappeddictYesDatabase records, User profiles
SetsetYesFinding unique values
BinarybytesNoRaw files, image data

Practice Challenges #

  1. The Shopping List: Create a list of 3 items. Add a 4th item to it. Now, convert that list into a tuple and try to add a 5th item. Observe the error.
  2. The Unique Filter: You have a list of zip codes: [60000, 61000, 60000, 62000, 61000]. Use a set to find how many unique zip codes there are.
  3. The Identity Card: Create a dictionary representing a laptop. Include keys for brand (str), ram (int), and is_ssd (bool). Print only the ram value.

💬
AIRA (AI Research Assistant) Neural Learning Interface • Drag & Resize Enabled
×