Introduction
In Python programming, data is often stored in collections. Sometimes we need to store multiple values inside a single variable. Python provides powerful data structures for this purpose called Lists and Tuples.
Lists and tuples are widely used in Artificial Intelligence, Machine Learning, Data Science, Web Development, and Automation projects.
For example, a Data Scientist may store multiple customer records inside a list, while an AI model may store fixed configuration values inside a tuple.
In this tutorial, you will learn Lists and Tuples in detail, including syntax, indexing, slicing, updating data, methods, real-world applications, and practical examples.
Learning Objectives
- Understand Python Lists.
- Understand Python Tuples.
- Differentiate between Lists and Tuples.
- Perform indexing and slicing operations.
- Use common list methods.
- Apply lists and tuples in real-world examples.
What is a List in Python?
A list is a collection data type used to store multiple items inside a single variable.
Lists are:
- Ordered
- Mutable (changeable)
- Allow duplicate values
Lists are created using square brackets [].
Example:
fruits = ["Apple","Banana","Orange"]
Here, the list stores three fruit names.
Why Lists are Important
Lists are important because they allow programmers to manage groups of related data efficiently.
Instead of creating separate variables:
student1 = "John"
student2 = "Alice"
student3 = "David"
We can use a list:
students = ["John","Alice","David"]
This approach makes code cleaner and easier to manage.
Creating Lists
Lists can store different data types.
Example:
data = ["Python", 100, True, 75.5]
Output:
['Python',100,True,75.5]
Accessing List Elements
Python uses indexing to access list items.
Indexing starts from 0.
Example:
colors = ["Red","Blue","Green"]
print(colors[0])
Output:
Red
Here:
- Red → index 0
- Blue → index 1
- Green → index 2
Negative Indexing
Python also supports negative indexing.
Negative indexing starts from the end.
Example:
colors = ["Red","Blue","Green"]
print(colors[-1])
Output:
Green
List Slicing
Slicing extracts multiple elements from a list.
Syntax:
list[start:end]
Example:
numbers = [10,20,30,40,50]
print(numbers[1:4])
Output:
[20,30,40]
Updating List Elements
Lists are mutable, meaning their values can be modified.
Example:
fruits = ["Apple","Banana","Orange"]
fruits[1] = "Mango"
print(fruits)
Output:
['Apple','Mango','Orange']
Adding Elements to a List
append() Method
Adds an item to the end of the list.
numbers = [1,2,3]
numbers.append(4)
print(numbers)
Output:
[1,2,3,4]
insert() Method
Inserts a value at a specific position.
numbers = [10,20,30]
numbers.insert(1,15)
print(numbers)
Output:
[10,15,20,30]
Removing List Elements
remove() Method
fruits = ["Apple","Banana","Orange"]
fruits.remove("Banana")
print(fruits)
Output:
['Apple','Orange']
pop() Method
Removes an element using index number.
numbers = [1,2,3]
numbers.pop(1)
print(numbers)
Output:
[1,3]
Useful List Methods
| Method | Description |
|---|---|
| append() | Add item |
| insert() | Insert item |
| remove() | Remove item |
| pop() | Remove by index |
| sort() | Sort list |
| reverse() | Reverse list |
What is a Tuple in Python?
A tuple is another collection data type used to store multiple values.
Tuples are:
- Ordered
- Immutable (cannot be changed)
- Allow duplicate values
Tuples are created using parentheses ().
Example:
colors = ("Red","Blue","Green")
Why Tuples are Important
Tuples are useful when data should remain constant and should not be modified accidentally.
Examples:
- Coordinates
- Configuration settings
- Fixed AI parameters
- Database records
Accessing Tuple Elements
Tuple indexing works similarly to lists.
languages = ("Python","Java","C++")
print(languages[0])
Output:
Python
Tuple Slicing
Tuples also support slicing.
numbers = (10,20,30,40,50)
print(numbers[1:4])
Output:
(20,30,40)
Difference Between Lists and Tuples
| Feature | List | Tuple |
|---|---|---|
| Symbol | [] | () |
| Mutable | Yes | No |
| Performance | Slightly Slower | Faster |
| Modification | Allowed | Not Allowed |
Real-World Examples
Student Marks Example
marks = [75,80,90,95]
print(marks)
GPS Coordinates Example
location = (28.6139,77.2090)
print(location)
Python Example
fruits = ["Apple","Banana","Orange"]
fruits.append("Mango")
print(fruits)
colors = ("Red","Blue","Green")
print(colors[1])
Output:
['Apple','Banana','Orange','Mango']
Blue
Interview Questions
1. What is a list in Python?
A list is an ordered, mutable collection used to store multiple values.
2. What is a tuple?
A tuple is an ordered, immutable collection.
3. Which brackets are used for lists?
Square brackets [] are used.
4. Which method adds an item to a list?
append() method.
Assignment
- Create a list containing five student names.
- Add one new name using append().
- Remove one name using remove().
- Create a tuple containing five cities.
- Perform indexing and slicing operations.
Quiz
Q1. Which brackets create a list?
- A. ()
- B. {}
- C. []
- D. <>
Answer: C. []
Q2. Which data type is immutable?
- A. List
- B. Tuple
- C. Dictionary
- D. String
Answer: B. Tuple
Summary
In this tutorial, you learned Lists and Tuples in Python. Lists are mutable collections used for storing multiple values, while tuples are immutable collections designed for fixed data.
You explored indexing, slicing, updating, list methods, tuple operations, and real-world examples.
Lists and tuples are important concepts for Python programming, Data Science, and Artificial Intelligence development.
Next Tutorial
Tutorial 14: Dictionaries and Sets
“`
