0% found this document useful (0 votes)
8 views5 pages

Lab Week 2

Lab assignment for subject uitm

Uploaded by

zz3906644
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Lab Week 2

Lab assignment for subject uitm

Uploaded by

zz3906644
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab Sheet #6: Python Tuples

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.

Accessing Tuple Elements

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.

Looping Through Tuples

You can use a for loop to iterate through the elements of a tuple.
Joining Tuples

You can concatenate two or more tuples to create a new tuple.

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.

Accessing Array Elements

You can access array elements using indexing, with the index star�ng at 0 for the first element.

Adding Elements to an Array

You can add elements to an array using the append() method or by concatena�ng two arrays using the +
operator.

Removing Elements from an Array

To remove elements from an array, you can use the pop() method to remove by index or the remove()
method to remove by value.

Looping Through Arrays

You can use a for loop to iterate through the elements of an array.

Copying Arrays

To copy an array, you can use slicing or the copy() method.


Reversing 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

You can concatenate two or more arrays to create a new array.

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.

You might also like