What is a list and how to declare it?
A list in Python is a collection of items (called elements) that are ordered,
mutable (can be changed), and allow duplicates.
It can store different data types such as numbers, strings, or even other
lists.
✅ How to declare a list:
You declare a list by placing elements inside square brackets [ ], separated
by commas.
# Empty list
my_list = []
# List of numbers
numbers = [1, 2, 3, 4, 5]
# List of strings
fruits = ["apple", "banana", "cherry"]
# Mixed data types
mixed = [10, "hello", 3.14, True]
# Nested list (list inside a list)
nested = [[1, 2], [3, 4]]
👉 Example usage:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
print(fruits[1]) # Output: banana
Indexing and slicing in lists.
Indexing in Lists
Indexing means accessing individual elements of a list using their position
(index number).
Indexing starts from 0 (first element).
You can also use negative indexing (last element = -1, second last = -
2, etc.).
fruits = ["apple", "banana", "cherry", "mango"]
# Positive indexing
print(fruits[0]) # apple (first element)
print(fruits[2]) # cherry (third element)
# Negative indexing
print(fruits[-1]) # mango (last element)
print(fruits[-2]) # cherry (second last element)
🔹 Slicing in Lists
Slicing means taking a portion (sub-list) from the list using a start:stop:step
format.
👉 Syntax:
list[start : stop : step]
start → index to begin (default = 0)
stop → index to end (exclusive)
step → jump between elements (default = 1)
Example:
numbers = [10, 20, 30, 40, 50, 60]
# Basic slicing
print(numbers[1:4]) # [20, 30, 40] (from index 1 to
3)
# Leaving start/stop empty
print(numbers[:3]) # [10, 20, 30] (from start to
index 2)
print(numbers[3:]) # [40, 50, 60] (from index 3 to
end)
# Using step
print(numbers[::2]) # [10, 30, 50] (every 2nd
element)
print(numbers[::-1]) # [60, 50, 40, 30, 20, 10]
(reversed list)
✨ In short:
Indexing = pick one element.
Slicing = pick a range of elements.
List functions: append, extend, insert, pop, remove,
clear — what they do and their syntax.
1. append()
What it does: Adds a single element at the end of the list.
Syntax:
list.append(element)
Example:
numbers = [1, 2, 3]
numbers.append(4)
print(numbers) # [1, 2, 3, 4]
2. extend()
What it does: Adds multiple elements (from another list, tuple, or
iterable) to the end of the list.
Syntax:
list.extend(iterable)
Example:
numbers = [1, 2, 3]
numbers.extend([4, 5])
print(numbers) # [1, 2, 3, 4, 5]
3. insert()
What it does: Inserts an element at a specific index in the list.
Syntax:
list.insert(index, element)
Example:
numbers = [1, 3, 4]
numbers.insert(1, 2)
print(numbers) # [1, 2, 3, 4]
4. pop()
What it does: Removes and returns an element from the list. By
default, removes the last element, but you can specify an index.
Syntax:
list.pop([index])
Example:
numbers = [10, 20, 30, 40]
numbers.pop() # removes 40
numbers.pop(1) # removes 20
print(numbers) # [10, 30]
5. remove()
What it does: Removes the first occurrence of a given element. If the
element is not found, it raises an error.
Syntax:
list.remove(element)
Example:
numbers = [1, 2, 3, 2, 4]
numbers.remove(2) # removes first 2
print(numbers) # [1, 3, 2, 4]
6. clear()
What it does: Removes all elements from the list (empties the list).
Syntax:
list.clear()
Example:
numbers = [1, 2, 3]
numbers.clear()
print(numbers) # []
Programs to show output of list manipulations.
Program: List Manipulations
# Initial list
numbers = [10, 20, 30]
print("Original List:", numbers)
# 1. append()
numbers.append(40)
print("After append(40):", numbers)
# 2. extend()
numbers.extend([50, 60])
print("After extend([50, 60]):", numbers)
# 3. insert()
numbers.insert(2, 25)
print("After insert(2, 25):", numbers)
# 4. pop() without index (removes last element)
removed = numbers.pop()
print("After pop():", numbers, "| Removed:", removed)
# 5. pop() with index
removed = numbers.pop(1)
print("After pop(1):", numbers, "| Removed:",
removed)
# 6. remove() (removes first occurrence of value)
numbers.remove(25)
print("After remove(25):", numbers)
# 7. clear() (removes all elements)
numbers.clear()
print("After clear():", numbers)
Dictionary in Python and its defination
Definition
A dictionary in Python is a collection of key–value pairs.
Each key is unique and is used to access its corresponding value.
Dictionaries are mutable (you can change them).
They are written inside curly braces {}, with keys and values
separated by a colon :.
General Syntax
dictionary_name = {
key1: value1,
key2: value2,
key3: value3,
...
}
Example
student = {
"name": "Alice",
"roll_no": 101,
"marks": 85
}
print(student)
Output:
{'name': 'Alice', 'roll_no': 101, 'marks': 85
How to declare a dictionary with key-value pairs?
Declaring a Dictionary
A dictionary is declared inside curly braces {}, with each key and value
separated by a colon :.
Multiple pairs are separated by commas ,.
Syntax
dictionary_name = {
key1: value1,
key2: value2,
key3: value3
}
Examples
1. Simple dictionary
student = {
"name": "Alice",
"roll_no": 101,
"marks": 85
}
print(student)
Output:
{'name': 'Alice', 'roll_no': 101, 'marks': 85}
2. Dictionary with numbers as keys
squares = {
1: 1,
2: 4,
3: 9,
4: 16
}
print(squares)
Output:
{1: 1, 2: 4, 3: 9, 4: 16}
3. Mixed types in values
info = {
"name": "Bob",
"age": 20,
"subjects": ["Math", "Science", "English"]
}
print(info)
Output:
{'name': 'Bob', 'age': 20, 'subjects': ['Math',
'Science', 'English']}
Characteristics of a dictionary
Characteristics of a Dictionary
1. Unordered
o Dictionaries do not store items in a fixed order (before Python
3.7).
o From Python 3.7 onwards, dictionaries preserve insertion
order, but logically they are still treated as unordered
collections.
2. Mutable
o You can add, update, or remove key–value pairs after creating
a dictionary.
3. Key–Value Pairs
o Each element is stored as a pair:
o key: value
o Example: "name": "Alice"
4. Unique Keys
o Dictionary keys must be unique.
o If the same key is repeated, the last value overwrites the
previous one.
5. Keys must be Immutable
o Valid keys: numbers, strings, tuples (immutable types).
o Invalid keys: lists, sets, or other dictionaries (mutable types).
6. Values can be of Any Type
o Values can be numbers, strings, lists, tuples, or even other
dictionaries.
7. Dynamic Size
o Dictionaries can grow or shrink as you add or remove items.
8. Efficient Lookup
o Accessing values by key is very fast compared to searching in a
list.
✅ Example:
student = {
"name": "Alice",
"age": 18,
"subjects": ["Math", "Science"]
}
print(student)
Output:
{'name': 'Alice', 'age': 18, 'subjects': ['Math',
'Science']}
Programs to access values using loops.
🔹 1. Loop through keys and get values
student = {"name": "Alice", "age": 18, "marks": 90}
for key in student:
print(key, ":", student[key])
Output:
name : Alice
age : 18
marks : 90
🔹 2. Loop through values only
student = {"name": "Alice", "age": 18, "marks": 90}
for value in student.values():
print(value)
Output:
Alice
18
90
🔹 3. Loop through key–value pairs
student = {"name": "Alice", "age": 18, "marks": 90}
for key, value in student.items():
print(key, "->", value)
Output:
name -> Alice
age -> 18
marks -> 90
🔹 4. Access values using while loop with iterator
student = {"name": "Alice", "age": 18, "marks": 90}
it = iter(student.items())
while True:
try:
key, value = next(it)
print(key, "=", value)
except StopIteration:
break
Output:
name = Alice
age = 18
marks = 90