Data Types in Python

In Python, data types define the type of value a variable can hold. Python provides several built-in data types, categorized into different groups. Understanding these types is essential for effective programming.


1. Basic Data Types

1.1 Numeric Types

Python supports three main numeric types:

  • Integer (int): Whole numbers (e.g., 10, -5)
  • Floating-Point (float): Decimal numbers (e.g., 3.14, -2.5)
  • Complex (complex): Numbers with real and imaginary parts (e.g., 2 + 3j)

Example:

x = 10        # int
y = 3.14      # float
z = 2 + 3j    # complex
print(type(x), type(y), type(z))

Operations on Numeric Types:

# Arithmetic operations
print(10 + 5)  # Addition
print(10 - 5)  # Subtraction
print(10 * 5)  # Multiplication
print(10 / 2)  # Division
print(10 % 3)  # Modulus
print(10 ** 2) # Exponentiation

1.2 Boolean Type

  • Boolean (bool): Represents True or False

Example:

is_python_fun = True
print(type(is_python_fun))  # Output: <class 'bool'>

Booleans are often used in conditions and logical operations.

x = 10
y = 20
print(x > y)  # Output: False
print(x == y) # Output: False
print(x != y) # Output: True

2. Sequence Data Types

2.1 Strings (str)

A string is a sequence of characters enclosed in quotes.

Example:

text = "Hello, Python!"
print(type(text))

String Operations:

text = "Python"
print(text[0])    # Access first character
print(text[1:4])  # Substring (slicing)
print(len(text))  # Length of string
print(text.lower())  # Convert to lowercase
print(text.upper())  # Convert to uppercase
print(text.replace("P", "J"))  # Replace character

2.2 Lists (list)

A list is an ordered, mutable collection of items.

Example:

fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # Access first item
fruits.append("orange")  # Add item to list
print(fruits)

List Operations:

numbers = [1, 2, 3, 4, 5]
numbers.insert(2, 10)  # Insert 10 at index 2
numbers.remove(3)  # Remove 3 from the list
numbers.sort()  # Sort the list
print(numbers)

2.3 Tuples (tuple)

A tuple is an ordered, immutable collection of items.

Example:

coordinates = (10, 20)
print(coordinates[0])  # Access first item

Tuples are faster and safer than lists for immutable collections.


3. Set and Dictionary

3.1 Sets (set)

A set is an unordered collection of unique items.

Example:

numbers = {1, 2, 3, 3, 4}
print(numbers)  # Output: {1, 2, 3, 4} (removes duplicates)

Sets support mathematical operations like union, intersection, and difference.

A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A | B)  # Union
print(A & B)  # Intersection
print(A - B)  # Difference

3.2 Dictionaries (dict)

A dictionary stores key-value pairs.

Example:

student = {"name": "Alice", "age": 21}
print(student["name"])  # Output: Alice

Dictionary Operations:

student["grade"] = "A"  # Add a new key-value pair
del student["age"]  # Remove a key-value pair
print(student.keys())  # Get all keys
print(student.values())  # Get all values

4. Type Conversion

Python allows type conversion between compatible types.

Example:

x = 5   # int
y = float(x)  # Convert to float
z = str(x)  # Convert to string
print(type(y), type(z))

Some common conversions:

print(int("10"))  # Convert string to int
print(float("5.5"))  # Convert string to float
print(list("hello"))  # Convert string to list

5. Summary

✅ Learned about Python’s data types: numeric, boolean, sequences, sets, and dictionaries
✅ Explored operations and conversions between data types
✅ Practiced using lists, tuples, and dictionaries

SRIRAM
SRIRAM

Sriram is a seasoned Computer Science educator and mentor. He is UGC NET Qualified twice (2014 & 2019) and holds State Eligibility Test (SET) qualifications for both Andhra Pradesh (AP) and Telangana (TG). With years of experience teaching programming languages, he simplifies complex CS concepts for aspirants of UGC NET Computer Science, KVS, NVS, EMRS, and other competitive exams.

Leave a Reply

Your email address will not be published. Required fields are marked *