Day 4 of the Ultimate Learning Challenge : Mastering the Basics of Python

Table of contents

Hellooo, welcome back to day 4 of mastering the basics of Python. Today, In this blog we’ll take a look at Lists and Tuples in Python.
Python Lists
In Python, lists are one of the most commonly used and versatile built-in data structures. They allow you to store multiple items in a single variable, and these items can be of any data type—strings, integers, floats, even other lists. List are
Ordered
Mutable
Iterable collection of elements.
Syntax:
pythonCopyEditmy_list = [1, 2, 3, 4, 5]
You can also create an empty list like this:
pythonCopyEditempty_list = []
Or using the list()
constructor:
pythonCopyEditanother_empty_list = list()
Accessing List Elements
Use indexing to access individual elements. Index starts at 0
.
pythonCopyEditfruits = ['apple', 'banana', 'cherry']
print(fruits[0]) # apple
You can also use negative indexing:
pythonCopyEditprint(fruits[-1]) # cherry
Slicing Lists
You can extract sublists using slicing:
pythonCopyEditnumbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40]
print(numbers[:3]) # [10, 20, 30]
print(numbers[::2]) # [10, 30, 50]
Modifying Lists
Changing elements:
pythonCopyEditmy_list = [1, 2, 3]
my_list[1] = 20
print(my_list) # [1, 20, 3]
Adding elements:
pythonCopyEditmy_list.append(4) # Adds to end
my_list.insert(1, 100) # Inserts at index 1
Removing elements:
pythonCopyEditmy_list.remove(100) # Removes by value
my_list.pop() # Removes last item
del my_list[0] # Removes by index
Common List Methods
Method | Description |
append(x) | Adds x to the end of the list |
insert(i, x) | Inserts x at index i |
remove(x) | Removes first occurrence of x |
pop(i) | Removes item at index i (default last) |
index(x) | Returns the index of x |
count(x) | Returns count of x in list |
reverse() | Reverses the list in-place |
sort() | Sorts the list in-place (ascending) |
copy() | Returns a shallow copy of the list |
clear() | Removes all items from the list |
Looping Through Lists
Using a for
loop:
pythonCopyEditfor fruit in fruits:
print(fruit)
Using enumerate()
:
pythonCopyEditfor index, value in enumerate(fruits):
print(index, value)
List Comprehensions
Create new lists in a clean and efficient way:
pythonCopyEditsquares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
With condition:
pythonCopyEditevens = [x for x in range(10) if x % 2 == 0]
Nesting Lists
Lists can contain other lists:
pythonCopyEditmatrix = [[1, 2], [3, 4], [5, 6]]
print(matrix[1][0]) # 3
Copying Lists
Using =
copies the reference, not the list:
pythonCopyEdita = [1, 2, 3]
b = a
b[0] = 100
print(a) # [100, 2, 3]
Use .copy()
or list()
to avoid this:
pythonCopyEditc = a.copy()
c[0] = 999
print(a) # [100, 2, 3]
print(c) # [999, 2, 3]
Built-in Functions with Lists
pythonCopyEditlen(my_list) # Number of elements
sum(my_list) # Sum of all numeric elements
min(my_list) # Smallest value
max(my_list) # Largest value
sorted(my_list) # Returns a new sorted list
Python Tuples
Tuples are used to store multiple items in a single variable. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets. Tuples are
Ordered
Immutable
Allow duplicates
Syntax:
my_tuple = (1, 2, 3)
Tuple Properties
Property | Description |
Ordered | Elements have a defined order |
Immutable | Cannot be modified after creation |
Heterogeneous | Can contain different data types |
Indexable | Supports positive and negative indexing |
Creating Tuples
# Empty tuple
t1 = ()
# Tuple with integers
t2 = (1, 2, 3)
# Mixed data types
t3 = ("Python", 3.14, True)
# Single-element tuple (don’t forget the comma!)
t4 = (42,)
Accessing Tuple Elements
You can use indexing:
t = ("Mango", "Apple", "Cherry")
print(t[0]) # Mango
print(t[-1]) # Cherry
Slicing:
print(t[0:2]) # ('Mango', 'Apple')
Basic Operations on Tuples
Concatenate using +
a = (1, 2)
b = (3, 4)
print(a + b) # (1, 2, 3, 4)
Repeat using *
t = ("Python",)
print(t * 3) # ('Python', 'Python', 'Python')
Check Membership using in
t = (1, 2, 3)
print(2 in t) # True
print(5 in t) # False
Find Length using len()
t = ("a", "b", "c")
print(len(t)) # 3
Nested Tuples
Tuples can contain other tuples:
nested = ((1, 2), (3, 4), (5, 6))
Accessing Elements in Nested Tuples
nested = ((1, 2), (3, 4), (5, 6))
print(nested[1]) # (3, 4)
print(nested[1][0]) # 3
Immutable Tuples
You cannot change a tuple after it's created:
t = (1, 2, 3)
t[0] = 100 # Error: 'tuple' object does not support item assignment
Inserting Elements in a Tuple
Tuples do not support direct insertion. You can convert to a list, insert, then convert back:
t = (1, 2, 3)
temp = list(t)
temp.insert(1, 100)
t = tuple(temp)
print(t) # (1, 100, 2, 3)
Modifying Elements of a Tuple
Same approach — convert to a list:
t = (1, 2, 3)
temp = list(t)
temp[0] = 42
t = tuple(temp)
print(t) # (42, 2, 3)
Deleting Elements from a Tuple
You cannot delete individual elements, but you can delete the entire tuple:
t = (1, 2, 3)
del t
To "remove" an element, use list conversion:
t = (1, 2, 3)
temp = list(t)
temp.remove(2)
t = tuple(temp)
print(t) # (1, 3)
:> To be Continued…
Happy Learning !
Thanks for reading :)
Subscribe to my newsletter
Read articles from Ashika Arjun directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
