Session Overview

  • Collections
  • List
  • Tuple
  • Set
  • Dictionary

Collections

Collections are data types used to store multiple values in a single variable. They help us organize and manage data efficiently.

Python provides four commonly used collection types:

Collection Ordered Allows Duplicates Mutable
List Yes Yes Yes
Tuple Yes Yes No
Set No No Yes
Dictionary Yes Keys: No Yes
Mutable vs Immutable
  • Mutable: Data can be modified after creation. Example: List, Set, Dictionary.
  • Immutable: Data cannot be modified after creation. Example: Tuple.
# Mutable Example (List)

students = ["Shree", "Vinayak"]

students.append("Rohit")

print(students)
Output
['Shree', 'Vinayak', 'Rohit']
# Immutable Example (Tuple)

students = ("Shree", "Vinayak")

students[0] = "Rohit"
Result
TypeError:
'tuple' object does not support item assignment

List

A List is an ordered collection that can store multiple values. Lists are mutable, which means their contents can be modified.

Creating a List
students = ["Shree", "Vinayak", "Rohit"]

print(students)
Output
['Shree', 'Vinayak', 'Rohit']
Accessing List Elements
students = ["Shree", "Vinayak", "Rohit"]

print(students[0])
print(students[1])
Output
Shree
Vinayak
Adding Item using append()
students = ["Shree", "Vinayak"]

students.append("Rohit")

print(students)
Output
['Shree', 'Vinayak', 'Rohit']
Looping Through a List
students = ["Shree", "Vinayak", "Rohit"]

for student in students:
    print(student)
Output
Shree
Vinayak
Rohit

Tuple

A Tuple is an ordered collection used to store multiple values. Unlike Lists, Tuples are immutable, which means their values cannot be changed after creation.

Creating a Tuple
students = ("Shree", "Vinayak", "Rohit")

print(students)
Output
('Shree', 'Vinayak', 'Rohit')
Accessing Tuple Elements
students = ("Shree", "Vinayak", "Rohit")

print(students[0])
print(students[1])
Output
Shree
Vinayak
Tuple is Immutable
students = ("Shree", "Vinayak", "Rohit")

students[0] = "Ram"
Result
TypeError:
'tuple' object does not support item assignment

Note: Use Tuple when data should not be modified after creation.

Set

A Set is an unordered collection of unique values. Duplicate values are automatically removed.

Creating a Set
numbers = {10, 20, 30, 40}

print(numbers)
Output
{10, 20, 30, 40}
Duplicate Values are Removed
numbers = {10, 20, 20, 30, 30, 40}

print(numbers)
Output
{10, 20, 30, 40}
Adding an Item using add()
numbers = {10, 20, 30}

numbers.add(40)

print(numbers)
Output
{10, 20, 30, 40}
Looping Through a Set
numbers = {10, 20, 30}

for num in numbers:
    print(num)
Output
10
20
30

Note: Since Sets are unordered, the output order may vary.

Dictionary

A Dictionary stores data in key-value pairs. Each value is accessed using its corresponding key.

Creating a Dictionary
student = {
    "name": "Shree",
    "city": "Mumbai",
    "age": 22
}

print(student)
Output
{'name': 'Shree', 'city': 'Mumbai', 'age': 22}
Accessing Values
student = {
    "name": "Shree",
    "city": "Mumbai"
}

print(student["name"])
print(student["city"])
Output
Shree
Mumbai
Adding a New Key
student = {
    "name": "Shree"
}

student["city"] = "Mumbai"

print(student)
Output
{'name': 'Shree', 'city': 'Mumbai'}
Looping Through a Dictionary
student = {
    "name": "Shree",
    "city": "Mumbai"
}

for key, value in student.items():
    print(key, value)
Output
name Shree
city Mumbai

Note: Dictionaries are heavily used in Python and Django for storing structured data using key-value pairs.

Quick Revision

List
  • Ordered
  • Mutable
  • Allows Duplicates
Tuple
  • Ordered
  • Immutable
  • Allows Duplicates
Set
  • Unique Values
  • Mutable
  • Unordered
Dictionary
  • Key-Value Pairs
  • Mutable
  • Ordered
Mutable
  • Can Be Modified
  • List
  • Set, Dictionary
Immutable
  • Cannot Be Modified
  • Tuple
  • Fixed Data

Assignment

  1. Create a List of 5 student names and display all names using a loop.
  2. Create a Tuple containing City Names and display the first and last city.
  3. Create a Set with duplicate numbers and display the final Set.
  4. Create a Dictionary containing Name, City and Age of a student and display all values.
  5. Create a Dictionary of an employee and display all key-value pairs using a loop.