Introduction
In Python programming, we often need to store and manage multiple values together. For example, a student management system may need to store student names, a shopping application may need to store product names, and an employee management system may need to store employee details.
Creating a separate variable for every value can make a program difficult to manage.
Python provides two useful data structures for storing multiple values:
- Lists
- Tuples
Lists and tuples allow us to organize multiple values inside a single variable. They make Python programs easier to write, read, and maintain.
In this Day 6 Python OJT tutorial by Webitya, we will learn how to create lists and tuples, access their items, update list values, add and remove items, use important list methods, perform slicing, and process data using loops.
This tutorial is designed for beginners who want to strengthen their Python programming fundamentals through practical examples.
Table of Contents
- What are Lists and Tuples?
- Creating a List in Python
- Accessing List Items Using Indexing
- Updating, Adding, and Removing List Items
- Important List Methods
- List Slicing in Python
- Looping Through a List
- Tuples in Python
- Accessing Tuple Items
- Tuple Immutability
- Important Tuple Methods
- Creating a Tuple with One Item
- Lists vs Tuples
- Practical Python Examples
- Common Beginner Mistakes
- Key Takeaways
- 20 Python Practice Questions
1. What are Lists and Tuples?
What is a List?
A list is a collection of multiple items stored in a single variable.
In Python, lists are created using square brackets [].
Example:
fruits = ["Apple", "Banana", "Mango"] print(fruits)
Output:
['Apple', 'Banana', 'Mango']
Here, the variable fruits stores three different values.
Instead of creating three separate variables, we can store all the values inside one list.
What is a Tuple?
A tuple is also a collection of multiple items stored in a single variable.
In Python, tuples are created using parentheses ().
Example:
colors = ("Red", "Green", "Blue")
print(colors)
Output:
('Red', 'Green', 'Blue')
A tuple stores multiple values just like a list, but its items cannot be changed after the tuple is created.
List and Tuple Examples
# List
fruits = ["Apple", "Banana", "Mango"]
# Tuple
colors = ("Red", "Green", "Blue")
Both lists and tuples can store:
- Strings
- Integers
- Floating-point numbers
- Boolean values
- Different data types together
Example:
student = ["Rahul", 21, 85.5, True] print(student)
Output:
['Rahul', 21, 85.5, True]
2. Creating a List in Python
A list can be created by placing multiple values inside square brackets.
Example:
students = ["Rahul", "Aman", "Priya", "Riya"] print(students)
Output:
['Rahul', 'Aman', 'Priya', 'Riya']
Creating a List of Numbers
numbers = [10, 20, 30, 40, 50] print(numbers)
Output:
[10, 20, 30, 40, 50]
Creating an Empty List
An empty list does not contain any items.
data = [] print(data)
Output:
[]
We can add items to this list later using list methods.
Creating a List with Different Data Types
student = ["Rahul", 21, 85.5, True] print(student)
Output:
['Rahul', 21, 85.5, True]
A list can contain different data types. However, in many real-world applications, lists are commonly used to store related values.
3. Accessing List Items Using Indexing
Indexing means accessing an individual item from a list using its position.
Python uses zero-based indexing.
This means the first item has index 0, the second item has index 1, and the third item has index 2.
Example:
students = ["Rahul", "Aman", "Priya", "Riya"] print(students[0]) print(students[1]) print(students[2]) print(students[3])
Output:
Rahul Aman Priya Riya
Understanding List Indexing
| Item | Index |
| Rahul | 0 |
| Aman | 1 |
| Priya | 2 |
| Riya | 3 |
Remember that Python indexing starts from zero, not one.
Negative Indexing
Python also supports negative indexing.
Negative indexing allows us to access items from the end of a list.
Example:
students = ["Rahul", "Aman", "Priya", "Riya"] print(students[-1]) print(students[-2])
Output:
Riya Priya
Explanation:
- -1 accesses the last item.
- -2 accesses the second-last item.
- -3 accesses the third-last item.
Negative indexing is useful when we want to access the last item without calculating the list length.
4. Updating, Adding, and Removing List Items
Lists are mutable.
Mutable means that we can change the values of a list after creating it.
Updating a List Item
We can update an item by using its index.
Example:
fruits = ["Apple", "Banana", "Mango"] fruits[1] = "Orange" print(fruits)
Output:
['Apple', 'Orange', 'Mango']
Here, Banana is replaced with Orange because index 1 refers to Banana.
Adding Items Using append()
The append() method adds a new item at the end of a list.
Example:
fruits = ["Apple", "Banana"]
fruits.append("Mango")
print(fruits)
Output:
['Apple', 'Banana', 'Mango']
The new item is added at the end of the existing list.
Adding Items Using insert()
The insert() method adds an item at a specific position.
Syntax:
list_name.insert(index, item)
Example:
fruits = ["Apple", "Banana", "Mango"] fruits.insert(1, "Orange") print(fruits)
Output:
['Apple', 'Orange', 'Banana', 'Mango']
Here, Orange is inserted at index 1.
The existing items move to the right to make space for the new item.
Removing Items Using remove()
The remove() method removes an item by its value.
Example:
fruits = ["Apple", "Banana", "Mango"]
fruits.remove("Banana")
print(fruits)
Output:
['Apple', 'Mango']
The value Banana is removed from the list.
Removing Items Using pop()
The pop() method removes an item using its index.
Example:
fruits = ["Apple", "Banana", "Mango"] fruits.pop(0) print(fruits)
Output:
['Banana', 'Mango']
Here, the item at index 0, which is Apple, is removed.
If we use pop() without an index, it removes and returns the last item.
Example:
fruits = ["Apple", "Banana", "Mango"] removed_item = fruits.pop() print(removed_item) print(fruits)
Output:
Mango ['Apple', 'Banana']
5. Important List Methods in Python
Python provides several useful methods for working with lists.
len() – Count the Number of Items
The len() function returns the total number of items in a list.
Example:
numbers = [10, 20, 30, 40] print(len(numbers))
Output:
4
There are four items in the list.
sort() – Sort List Items
The sort() method arranges list items in ascending order by default.
Example:
numbers = [40, 10, 30, 20] numbers.sort() print(numbers)
Output:
[10, 20, 30, 40]
We can also sort numbers in descending order.
numbers = [40, 10, 30, 20] numbers.sort(reverse=True) print(numbers)
Output:
[40, 30, 20, 10]
reverse() – Reverse the List
The reverse() method reverses the current order of items.
Example:
numbers = [10, 20, 30, 40] numbers.reverse() print(numbers)
Output:
[40, 30, 20, 10]
Note that reverse() does not sort the list. It simply reverses the existing order.
count() – Count a Particular Value
The count() method returns how many times a value appears in a list.
Example:
numbers = [10, 20, 10, 30, 10] print(numbers.count(10))
Output:
3
The number 10 appears three times.
index() – Find the Position of an Item
The index() method returns the index of the first occurrence of a value.
Example:
fruits = ["Apple", "Banana", "Mango"]
print(fruits.index("Banana"))
Output:
1
Banana is located at index 1.
clear() – Remove All Items
The clear() method removes all items from a list.
Example:
numbers = [10, 20, 30] numbers.clear() print(numbers)
Output:
[]
The list becomes empty.
Quick Reference of List Methods
| Method | Purpose |
| append() | Add an item at the end |
| insert() | Add an item at a specific position |
| remove() | Remove an item by value |
| pop() | Remove an item by index |
| sort() | Sort items |
| reverse() | Reverse item order |
| count() | Count occurrences |
| index() | Find an item's position |
| clear() | Remove all items |
6. List Slicing in Python
List slicing allows us to access a specific part of a list.
Instead of accessing only one item, we can access multiple items using slicing.
Slicing Syntax
list_name[start:stop]
The start index is included, but the stop index is excluded.
Example:
numbers = [10, 20, 30, 40, 50] print(numbers[1:4])
Output:
[20, 30, 40]
Explanation:
- Start index is 1.
- Stop index is 4.
- Index 4 is not included.
Slicing from the Beginning
If we do not specify the start index, Python starts from the beginning.
Example:
numbers = [10, 20, 30, 40, 50] print(numbers[:3])
Output:
[10, 20, 30]
Slicing from a Specific Index to the End
If we do not specify the stop index, Python continues until the end of the list.
Example:
numbers = [10, 20, 30, 40, 50] print(numbers[2:])
Output:
[30, 40, 50]
Reversing a List Using Slicing
We can use [::-1] to create a reversed copy of a list.
Example:
numbers = [10, 20, 30, 40, 50] print(numbers[::-1])
Output:
[50, 40, 30, 20, 10]
Unlike the reverse() method, slicing creates a new list and does not modify the original list.
7. Looping Through a List
A loop allows us to process each item in a list one by one.
Python provides a for loop that is commonly used to iterate through lists.
Example:
students = ["Rahul", "Aman", "Priya"]
for student in students:
print(student)
Output:
Rahul Aman Priya
Understanding the Code
The for loop takes one item from the list at a time.
- First, student becomes Rahul.
- Next, student becomes Aman.
- Finally, student becomes Priya.
The print() statement executes for every item.
Practical Example – Display Student Names
students = ["Rahul", "Aman", "Priya", "Riya"]
for name in students:
print("Student Name:", name)
Output:
Student Name: Rahul Student Name: Aman Student Name: Priya Student Name: Riya
This approach is useful when displaying student records, product names, employee lists, or other collections of data.
8. Tuples in Python
A tuple is a collection of multiple values stored in parentheses ().
Example:
student = ("Rahul", 21, "Ranchi")
print(student)
Output:
('Rahul', 21, 'Ranchi')
Here, the tuple contains three values:
- Rahul – String
- 21 – Integer
- Ranchi – String
Creating a Tuple with Different Data Types
data = ("Rahul", 21, 85.5, True)
print(data)
Output:
('Rahul', 21, 85.5, True)
Tuples can store strings, integers, floating-point numbers, Boolean values, and other Python objects.
Creating an Empty Tuple
data = () print(data)
Output:
()
9. Accessing Tuple Items
Tuples use indexing just like lists.
Indexing starts from zero.
Example:
student = ("Rahul", 21, "Ranchi")
print(student[0])
print(student[1])
print(student[2])
Output:
Rahul 21 Ranchi
We can also use negative indexing.
student = ("Rahul", 21, "Ranchi")
print(student[-1])
Output:
Ranchi
The index -1 accesses the last item.
10. Tuple Immutability
Tuples are immutable.
Immutable means that we cannot change, add, or remove individual items after creating a tuple.
Example:
numbers = (10, 20, 30) numbers[1] = 50 print(numbers)
Output:
TypeError
Python generates a TypeError because tuple items cannot be modified.
Why Are Tuples Useful?
Tuples are useful when we want to keep a group of values fixed.
For example, a location may be represented using latitude and longitude.
location = (23.3441, 85.3096) print(location)
Output:
(23.3441, 85.3096)
A tuple can be a suitable choice when the values should remain unchanged.
11. Important Tuple Methods
Tuples have fewer methods than lists because they cannot be modified.
count() – Count a Value
The count() method returns the number of times a value appears.
Example:
numbers = (10, 20, 30, 20, 40) print(numbers.count(20))
Output:
2
index() – Find the Position
The index() method returns the position of the first occurrence of a value.
Example:
numbers = (10, 20, 30, 20, 40) print(numbers.index(30))
Output:
2
len() – Count Tuple Items
The len() function returns the total number of items.
Example:
numbers = (10, 20, 30, 20, 40) print(len(numbers))
Output:
5
12. Creating a Tuple with One Item
When creating a tuple with only one item, we must include a comma.
Correct example:
number = (10,) print(type(number))
Output:
<class 'tuple'>
Without the comma:
number = (10) print(type(number))
Output:
<class 'int'>
Python treats (10) as a normal integer because parentheses alone do not create a tuple.
Remember: A single-item tuple requires a comma.
13. Lists vs Tuples – Key Differences
Lists and tuples are both used to store multiple values, but they have different characteristics.
| Feature | List | Tuple |
| Brackets | [] | () |
| Mutable | Yes | No |
| Add items | Yes | No |
| Remove items | Yes | No |
| Update items | Yes | No |
| Methods | Many | Limited |
| Use case | Changing data | Fixed data |
When Should You Use a List?
Use a list when your data may change.
Examples:
- Shopping list
- Student attendance
- Product inventory
- Employee records
- Daily task list
When Should You Use a Tuple?
Use a tuple when you want to keep a collection of values fixed.
Examples:
- Coordinates
- Fixed configuration values
- Days of the week
- RGB color values
- Fixed student information
Both lists and tuples are important Python data structures. Choosing the right one depends on how you need to manage your data.
14. Practical Python Examples
Practical Example 1 – Shopping List Using a List
In a shopping application, users may add or remove products from their shopping list.
shopping = ["Milk", "Bread", "Eggs"]
shopping.append("Butter")
shopping.remove("Bread")
print(shopping)
Output:
['Milk', 'Eggs', 'Butter']
Explanation:
- We created a shopping list.
- We added Butter using append().
- We removed Bread using remove().
- We printed the updated list.
A list is suitable because shopping items can change.
Practical Example 2 – Student Information Using a Tuple
A tuple can store fixed student information.
student = ("Rahul", 21, "BSc IT")
print("Name:", student[0])
print("Age:", student[1])
print("Course:", student[2])
Output:
Name: Rahul Age: 21 Course: BSc IT
Explanation:
- Index 0 contains the student's name.
- Index 1 contains the student's age.
- Index 2 contains the course name.
A tuple is suitable when we want to keep this group of values unchanged.
Practical Example 3 – Find the Total of Numbers in a List
numbers = [10, 20, 30, 40, 50]
total = 0
for number in numbers:
total = total + number
print("Total:", total)
Output:
Total: 150
This example demonstrates how a list and a loop can be used together to process numerical data.
15. Common Beginner Mistakes
Mistake 1 – Using the Wrong Index
Incorrect:
fruits = ["Apple", "Banana", "Mango"] print(fruits[3])
This produces an IndexError because the valid indexes are 0, 1, and 2.
Correct:
print(fruits[2])
Output:
Mango
Mistake 2 – Updating a Tuple
Incorrect:
numbers = (10, 20, 30) numbers[0] = 50
This produces a TypeError because tuples are immutable.
Mistake 3 – Forgetting the Comma in a Single-Item Tuple
Incorrect:
number = (10)
Correct:
number = (10,)
Mistake 4 – Confusing remove() and pop()
remove() deletes an item by its value.
fruits.remove("Apple")
pop() deletes an item by its index.
fruits.pop(0)
Mistake 5 – Confusing sort() and reverse()
sort() arranges items in order.
reverse() reverses the current order.
These methods perform different operations.
16. Key Takeaways
After completing this Python Lists and Tuples tutorial, you should understand:
- Lists store multiple values using square brackets.
- Tuples store multiple values using parentheses.
- Python indexing starts from zero.
- Negative indexing accesses items from the end.
- Lists are mutable.
- Tuples are immutable.
- The append() method adds items at the end.
- The insert() method adds items at a specific position.
- The remove() method removes an item by value.
- The pop() method removes an item by index.
- List slicing helps access a specific part of a list.
- A for loop can process each item in a list.
- Tuples provide methods such as count() and index().
- A single-item tuple requires a comma.
17. Python Lists and Tuples – 20 Practice Questions
Practice these questions to strengthen your understanding of Python lists and tuples.
Basic List Questions
- Create a list containing five student names and print the first and last name.
- Create a list of five numbers and update the second item to 100.
- Create a list of fruits and add two new fruits using append() and insert().
- Create a list of five items and remove one item using remove() and another using pop().
- Create a list of numbers and sort them in ascending order.
List Methods and Indexing
- Create a list of numbers and reverse the list using reverse().
- Count how many times the number 10 appears in a list.
- Find the index of the value Mango in a list of fruits.
- Create a list of numbers and remove all items using clear().
- Use list slicing to print the first three items from a list.
Slicing and Looping
- Use slicing to print items from index 2 to index 5.
- Create a list of numbers and print the list in reverse order using slicing.
- Write a for loop to print every item from a list of student names.
Tuple Practice
- Create a tuple containing your name, age, and city. Print each item separately.
- Create a tuple of numbers and find its length using len().
- Create a tuple and count how many times a particular value appears using count().
- Find the index of a value in a tuple using index().
- Try updating an item in a tuple and observe the error. Explain why the error occurs.
- Create a tuple with one item and check its data type using type().
Real-World Practice
- Create a shopping list using a list and a student record using a tuple. Explain why you selected each data structure.



