Lab Week 2
Lab Week 2
What is a Tuple?
A tuple is an ordered, immutable collec�on of elements enclosed in parentheses (). Tuples are similar to
lists but cannot be modified a�er crea�on. This immutability makes tuples suitable for situa�ons where
you don't want the data to change.
You can access tuple elements using indexing, just like in lists. The index starts from 0 for the first
element.
Upda�ng Tuples
Tuples are immutable, meaning you can't change their elements once created. However, you can create
a new tuple with the desired changes.
To update a tuple, you can create a new one based on the exis�ng tuple.
Unpacking Tuples
You can assign the elements of a tuple to mul�ple variables in one line using tuple unpacking.
You can use a for loop to iterate through the elements of a tuple.
Joining Tuples
Tuple Methods
1. count()
The count() method returns the number of �mes a specified element appears in the tuple.
2. index()
The index() method returns the index of the first occurrence of a specified element in the tuple.
Conclusion
Tuples are a fundamental data structure in Python. They are useful for storing and managing data when
you don't want it to change. You can access, unpack, loop through, join, and use tuple methods to work
with tuples effec�vely. Remember that tuples are immutable, so if you need to modify them, you'll create
new tuples based on the original ones.
Lab Sheet #7: Python Array
What is an Array?
An array is a data structure that holds a collec�on of elements of the same data type. In Python, arrays
can be implemented using lists or the array module. We'll focus on lists in this tutorial, which are more
versa�le and commonly used.
You can access array elements using indexing, with the index star�ng at 0 for the first element.
You can add elements to an array using the append() method or by concatena�ng two arrays using the +
operator.
To remove elements from an array, you can use the pop() method to remove by index or the remove()
method to remove by value.
You can use a for loop to iterate through the elements of an array.
Copying Arrays
You can reverse the order of elements in an array using the reverse() method.
Sor�ng Arrays
You can sort the elements of an array using the sort() method.
Joining Arrays
Array Methods
1. count()
The count() method returns the number of �mes a specified element appears in the array.
2. index()
The index() method returns the index of the first occurrence of a specified element in the array.
Conclusion
Arrays, implemented using lists in Python, are versa�le and widely used data structures for holding
collec�ons of elements. You can access, add, remove, loop through, copy, reverse, and sort arrays, as well
as use array methods for specific tasks. Understanding these opera�ons is fundamental for working with
arrays in Python.