0% found this document useful (0 votes)
47 views50 pages

Python Grade 9 Lesson Tuples and Lists

Tupple and lists pdf

Uploaded by

printerkey12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
47 views50 pages

Python Grade 9 Lesson Tuples and Lists

Tupple and lists pdf

Uploaded by

printerkey12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 50

Lesson 5: Tuples and Lists

• In this lesson, we will be exploring collection data types in Python. This time, we
will be looking at tuples and lists, which are sequences of objects.

Objectives
After studying this lesson, you should be able to:
• define tuples and lists;
• manipulate them using functions and methods; and
• explore the uses of these data types.
TUPLES
Tuples - are collections of objects separated by commas.
- are used to store multiple items in a single variable.
- are written using ROUND BRACKETS ( ) or PARENTHESES
In some ways, tuples are similar to Python lists in terms of indexing, nested
objects, and repetition, but the main difference between the two is that the
tuples are immutable, meaning you cannot modify their values once you have
specified them.

Tuple Items
• Tuple items are ordered, unchangeable, and allow duplicate values.
• Tuple items are indexed, the first item has index [0], the second item has
index [1] etc.
TUPLES
Specifying Tuples
• To specify a tuple, you enter each element and use a comma to separate them.

Example 1

OUTPUT:

Example 2

OUTPUT:

• You can either enclose the collection in parentheses or not.


TUPLES
Allow Duplicates
• Since tuples are indexed, they can have items with the same value:

Example 1

OUTPUT:

Tuple Length
• To determine how many items a tuple has, use the len() function:

Example 2

OUTPUT:
TUPLES
• To create a tuple with just one element, use a comma at the end of the
first (and only) element.
OUTPUT:

• Tuple items can be of any data type:

OUTPUT:
TUPLES
• You can also have tuples with elements of mixed data types.
OUTPUT:

• A tuple with strings, integers and boolean values:

OUTPUT:
TUPLES
Access Tuple Items
• You can reference a specific element by referencing its offset.
Example:

OUTPUT:
TUPLES
Range of Indexes
• You can specify a range of indexes by specifying where to start and
where to end the range.
When specifying a range, the return value will be a new tuple with the specified
items.
Example:

OUTPUT:

#Remember that the first item is position 0, and note that the item in position 5
is NOT included
TUPLES
Range of Indexes
• You can specify a range of indexes by specifying where to start and
where to end the range.
❑ By leaving out the start value, the range will start at the first
item:
Example:

OUTPUT:
TUPLES
Range of Indexes
• You can specify a range of indexes by specifying where to start and
where to end the range.
❑ By leaving out the end value, the range will go on to the end of
the tuple:
Example:

OUTPUT:
TUPLES
• One cannot add items to a tuple once it is created.
Example:
OUTPUT:

Python tuples are ordered and we can access their


elements using their index values. They are also immutable,
i.e., we cannot add, remove and change the elements once
declared in the tuple, so when we tried to add an element
at index 1, it generated the error.
TUPLES
You can assign the elements of a tuple into variables. This is also called
Unpacking a Tuple

Example: OUTPUT:
TUPLES
Creating Tuples Out of Other Types
Python has a tuple() function, which allows you to convert other
data types.

Example:

OUTPUT:
TUPLES
Tuple “Operations”
Like with strings, you can also use the + and * operators to manipulate tuple
values.
• Using the + operator combine two tuples.
Example:

OUTPUT:
TUPLES
Tuple “Operations”
Like with strings, you can also use the + and * operators to manipulate tuple
values
• Using the * operator duplicates the specified element.
Example:

OUTPUT:
TUPLES
Comparing Tuples
You can compare tuples using comparison operators. Comparison operators
check the values of objects and return a Boolean value (True or False).
➢ Let’s try using the == operator with a tuple OUTPUT:

This compares the element of each tuple with the other. So, since superhero[0] is
not equal in value to epic_hero[0] right from the start, the comparison fails, and
the output is False.
TUPLES
Here is a list of the commonly used operators.
Let’s say that variable x = 5 and y = 10.

Comparison
Operators:
LISTS
LISTS - are collections of objects separated by commas.
- are used to store multiple items in a single variable.
- are created using SQUARE BRACKETS [ ]

Unlike tuples, lists are mutable, meaning their values can be changed.

LIST ITEMS:
• List items are ordered, changeable, and allow duplicate values.
• List items are indexed, the first item has index [0], the second
item has index [1] etc.
• Since lists are indexed, list can have items with the same value.
LISTS
Specifying Lists
• To specify a list, you need to enclose elements using square brackets [] and
use the comma as the separator for the elements.

OUTPUT:
LISTS
Access LIST Items
• List items are indexed and can access them by referring to the index
number using offset.

Example:

OUTPUT:
LISTS
Range of Indexes
Example:

OUTPUT:
LISTS
Creating Lists Out of other Data Types
• You can also create lists out of other data types using the list() function
Example:
OUTPUT:
LISTS
• You can even create lists using data types that have offsets, like strings, for
instance.
Example:

OUTPUT:
LISTS
• Strings can be split using the split() method to create a list.
Example:

OUTPUT:
LISTS
• Inversely, you can create a string from a list using the join() method.

Example:

OUTPUT:
LISTS
Adding and Inserting Items
• Since lists are mutable, you can change their values in several ways. To start,
you can use the + and * operators on lists too
You can use the + to add two lists together. Just be sure to assign the
operation to a variable first.

Example:

OUTPUT:
LISTS
Adding and Inserting Items
• Instead of assigning a new variable name for the combined
sequence, you can also use an existing variable name.
Example:

OUTPUT:
LISTS
Adding and Inserting Items
• The extend() method works similarly. It takes on the additional list
as a parameter.
Example:

OUTPUT:
LISTS
• The * operator, like in tuples, multiplies the elements in the list
Example: OUTPUT:

❑ Comparing Lists
You can compare lists using comparison operators. Comparison operators
check the values of objects and return a Boolean value (True or False).
Example: OUTPUT:
LISTS
▪ You can add items to the end of the list using the append() method.
Example:

NOTE:
Remember that the append() method only adds a
single element to the end of the list. If you try to
append a list, the whole list will be appended as a
single element in another. If that is what you want
to achieve, it is better to use the + operator
OUTPUT: instead.
LISTS
▪ You can also insert an element within the list using the insert() method.
Example:

OUTPUT:
LISTS

Different Ways to delete/remove an element from the LIST


1. By using the del keyword and the element’s offset.
2. By using the remove() method and indicating the value of the element.
3. By using the pop() method. It takes on the index or offset of the element
to be popped as the parameter.
LISTS
Deleting LIST Values
1. You can delete an element from the list by using the del keyword and the
element’s offset.
Example:

OUTPUT:
LISTS
Deleting LIST Values Note:
2.) By using the remove() method and If there are similar values in
indicating the value of the element. the list, remove() only
deletes the first instance
Example:

OUTPUT:
LISTS
Deleting LIST Values Note:
3.) By using the pop() method. It takes Unlike del or remove(), the pop()
on the index or offset of the element method returns the element’s
to be popped as the parameter. value. Notice how the interpreter
Example: shows the value it removes

OUTPUT:
LISTS
Deleting ALL items in a LIST:
• You can delete all items in a list by using the clear() method.
Example:

OUTPUT:
LISTS
Finding Information
❑ You can “test” if a value is present in the list using the in keyword.
Using in returns a Boolean value.
Example:

OUTPUT:
LISTS
Finding Information
❑ You can also get the offset or index of an element by using the index()
method.
Example:

OUTPUT:
LISTS
Finding Information
❑ The len() function returns the number of elements in a list.

Example:

OUTPUT:
LISTS
Finding Information Example:
▪ You can also find the
number of times a
value appears in the
list using the count()
method. It takes on
the value you want
to be counted.

OUTPUT:
LISTS
Copying Values
• You can create copies of lists using the copy() method. This specifies a
new list object in the computer’s memory. It works best if all the
values of the list elements are immutable.

OUTPUT:
LISTS
• To copy the actual values from one list to another and not just the
reference, we can use Python’s copy module.

Modules extend Python’s capabilities. The copy module


gives us access to the deep copy operation.

• Let’s use deepcopy().


LISTS

OUTPUT:
LISTS
Reordering Elements
• Python provides two functions to reorder or sort elements in a
list.
❑ The sort() method rearranges the values of the list in itself.
❑ The sorted() function creates a sorted copy of a list
• This is helpful if you’d want to retain the list in its original
state. You may also need to assign the returned value of
the function to a variable for you to use it further.
LISTS
❑ The sort() method rearranges the values of the list in itself.
Example:

OUTPUT:

By default, the method arranges the values in ascending order. The method can take on
an argument. Using reverse = True will sort the list elements in descending order.
LISTS
By default, the sort method arranges the values in ascending order. The sort method
can also take on an argument. Using the reverse = True argument will sort the list
elements in descending order.
Example:

OUTPUT:
LISTS
❑ The sorted() function creates a sorted copy of a list
Example:

OUTPUT:
Tuples vs. Lists
If tuples and lists are both sequence structures, and lists appear
to allow you to do a lot of things with them, then why even use
tuples in the first place? There are unique situations when tuples
can provide an advantage:
▪ Tuples have fixed lengths, allowing them to use less space
in the memory.
▪ Tuple values are immutable, so you can’t change the values
by mistake.
▪ Tuples can be used as keys in dictionaries
END OF SLIDES

You might also like