Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

MODULE 3: Python Data Types

This is Module 2, where we explore how Python stores and organizes information. Understanding data types is the foundation for building any complex application.

1. Numbers (int, float, complex)

Python supports three distinct numerical types:

  • int: Whole numbers (e.g., 10, -5).
  • float: Decimal numbers (e.g., 10.5, 3.14).
  • complex: Numbers with a real and imaginary part.

Example:

Python

x = 10          # int
y = 3.14        # float
z = 2 + 3j      # complex (j represents √-1)

2. Strings (Indexing, Slicing, Methods)

Strings are sequences of characters wrapped in quotes.

  • Indexing: Access a single character (starts at 0).
  • Slicing: Get a “slice” of the string using [start:stop:step].
  • Methods: Built-in functions like .upper(), .lower(), and .replace().

Example:

Python

text = "Python"
print(text[0])       # 'P'
print(text[0:2])     # 'Py' (Up to but not including index 2)
print(text.upper())  # 'PYTHON'

3. Lists (Operations, Methods)

Lists are ordered, mutable (changeable) collections that can hold different data types.

Example:

Python

fruits = ["apple", "banana"]
fruits.append("cherry")   # Adds to the end
fruits[0] = "blueberry"   # Changes "apple" to "blueberry"
print(fruits)             # ['blueberry', 'banana', 'cherry']

4. Tuples

Tuples are like lists, but they are immutable. Once created, you cannot change them. They are used for data that shouldn’t change, like coordinates or configuration settings.

Example:

Python

point = (10, 20)
# point[0] = 15  # This would cause an Error!

5. Sets

Sets are unordered collections of unique elements. They are great for removing duplicates.

Example:

Python

nums = {1, 2, 2, 3, 4}
print(nums)  # Output: {1, 2, 3, 4} (Duplicates removed)

6. Dictionaries

Dictionaries store data in Key-Value pairs. Think of a real dictionary: the word is the “key” and the definition is the “value.”

Example:

Python

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

7. Mutable vs. Immutable Objects

This is a critical concept in Python. It defines whether an object’s value can be changed after it is created.

CategoryData Types
Mutable (Can change)List, Dictionary, Set
Immutable (Cannot change)Int, Float, String, Tuple

8. Memory Management Basics

Python manages memory automatically using a Garbage Collector.

  • Reference Counting: Python keeps track of how many variables point to an object. When the count hits zero, the memory is freed.
  • Private Heap: All objects and data structures are stored in a private heap. The programmer doesn’t need to manually allocate memory like in C++.

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥