ASSIGNMENT
Submitted to : Ms. Anza Jabbar
Submitted by : Syeda Rutab Aziz
Registration no : COSC232101044
Class : BSCS 4A
Course : AI LAB
Date Submitted : 25 - 11 - 2024
Python Lists and Tuples
Introduction
Python provides several data types to store collections of data. The most common are Lists and Tuples.
Both are used to store collections of items, but they have some key differences.
Lists
Definition
Lists are mutable, ordered collections of items. They can contain elements of different
types.
Characteristics
Mutable: You can change, add, or remove items after the list has been created.
Ordered: The items have a defined order, and that order will not change unless you
explicitly do so.
Syntax
# Creating a list
my_list = [1, 2, 3, 'apple', 'banana']
Common Operations
Accessing Elements: Use indexing to access list elements.
print(my_list[0]) # Output: 1
Modifying Elements: Assign a new value to a specific index.
my_list[1] = 'orange'
Adding Elements: Use append() to add an item at the end.
my_list.append('grape')
Removing Elements: Use remove() to remove a specific item.
my_list.remove('banana')
Operations
Concatenation: Combine two lists using the + operator.
list1 = [1, 2, 3]
list2 = [4, 5]
combined_list = list1 + list2
print(combined_list) # Output: [1, 2, 3, 4, 5]
Repetition: Repeat elements using the * operator.
list1 = [1, 2, 3]
repeated_list = list1 * 3
print(repeated_list) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
Membership: Check if an item exists using the in keyword.
my_list = [1, 2, 3, 'apple', 'banana']
if 'apple' in my_list:
print('Apple is in the list') # Output: Apple is in the list
Length: Use len() to get the number of items.
my_list = [1, 2, 3, 'apple', 'banana']
length = len(my_list)
print(length) # Output: 5
Slicing: Access a subset of the list.
my_list = [1, 2, 3, 'apple', 'banana']
sublist = my_list[1:3]
print(sublist) # Output: [2, 3]
Example
fruits = ['apple', 'banana', 'cherry']
fruits.append('date')
print(fruits) # Output: ['apple', 'banana', 'cherry', 'date']
Tuples
Definition
Tuples are immutable, ordered collections of items. They can also contain elements of
different types.
Characteristics
Immutable: Once a tuple is created, you cannot change its elements.
Ordered: The items have a defined order, similar to lists.
Syntax
# Creating a tuple
my_tuple = (1, 2, 3, 'apple', 'banana')
Common Operations
Accessing Elements: Use indexing to access tuple elements.
print(my_tuple[0]) # Output: 1
Immutability: You cannot modify elements, but you can concatenate tuples.
my_tuple = (1, 2, 3, 'apple', 'banana')
new_tuple = my_tuple + ('cherry',)
Operations
Concatenation: Combine two tuples using the + operator.
tuple1 = (1, 2, 3)
tuple2 = (4, 5)
combined_tuple = tuple1 + tuple2
print(combined_tuple) # Output: (1, 2, 3, 4, 5)
Repetition: Repeat elements using the * operator.
tuple1 = (1, 2, 3)
repeated_tuple = tuple1 * 3
print(repeated_tuple) # Output: (1, 2, 3, 1, 2, 3, 1, 2, 3)
Membership: Check if an item exists using the in keyword.
my_tuple = (1, 2, 3, 'apple', 'banana')
if 'apple' in my_tuple:
print('Apple is in the tuple') # Output: Apple is in the tuple
Length: Use len() to get the number of items.
my_tuple = (1, 2, 3, 'apple', 'banana')
length = len(my_tuple)
print(length) # Output: 5
Slicing: Access a subset of the tuple.
my_tuple = (1, 2, 3, 'apple', 'banana')
subtuple = my_tuple[1:3]
print(subtuple) # Output: (2, 3)
Example
colors = ('red', 'green', 'blue')
print(colors[1]) # Output: green
Key Differences
Feature List Tuple
Mutability Mutable (changeable) Immutable (unchangeable)
Syntax Square brackets [] Parentheses ()
Performance Slower due to mutability Faster due to immutability
Use Cases Dynamic collections Fixed collections
Conclusion
Lists and tuples are fundamental data structures in Python. Choose lists when you need a mutable
collection of items, and tuples when you need an immutable collection. Understanding these differences
will help you make efficient decisions in your Python programming.