0% found this document useful (0 votes)
44 views37 pages

5 Data Types - Lists

The document discusses various techniques for working with lists in Python including defining and creating lists, accessing and modifying list elements, slicing lists, copying lists, nesting lists, and using common list methods and functions to process list data such as len(), min(), max(), sum(), enumerate(), and random.choice(). Lists in Python allow storing ordered sequences of mutable data that can be efficiently accessed, modified, and iterated over using indexes and slices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views37 pages

5 Data Types - Lists

The document discusses various techniques for working with lists in Python including defining and creating lists, accessing and modifying list elements, slicing lists, copying lists, nesting lists, and using common list methods and functions to process list data such as len(), min(), max(), sum(), enumerate(), and random.choice(). Lists in Python allow storing ordered sequences of mutable data that can be efficiently accessed, modified, and iterated over using indexes and slices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

5 Data Types-Lists

(Midterm Test)

Instructor: Raed Karim


Email: [email protected]
Course: PROG12583

1
Data Types-Lists
Objectives

At the end of this module, students will be able to:


• Define and create List to store data.
• Process and access data stored in Lists.
• Write and implement List methods and functions.
• Solve problems that requires data stored in Lists.

2
Working with Lists
Part - 1

3
Data Types- Lists
Working with Lists
• A List contains a collection of items (data).
• Lists are ordered (indexed) sequences of arbitrary data (items) that is mutable.
• Sequence means that a list stores items in the sequence in which they are added to the list,
lists are a type of data structure known as a sequence.
• Mutable means that the length of the sequence can be changed and elements can be
substituted.
• Lists can be of any data, of one type or mixed type, and can be written explicitly.
• The basic format is a square-bracket-enclosed [ ], comma-separated list of arbitrary data.

4
Data Types- Lists
Working with Lists

• You can also create an empty list. Then, you can add items to this list later on.
• In some languages (like Java), this structure is referred to as an array and the items
in the array are referred to as elements.
• Just like strings, to refer to (and to get) an item in a list, you use an index starting at 0. The
second item has an index of 1, and so on.
• You can also get items starting from the end of the list by using a negative integer as the
index. Then, the last item in a list has an index of -1, the second to last item has an index of
-2, and so on.
• You can get an item in a list and assign its value to a variable.

5
Data Types- Lists
Working with Lists

• You can use the repetition operator (*) to help create a list. For example, you create a list
with one item, then you use the repetition operator to repeat that item as many times as you
want.
• In the same way, you can add an item to a list. Here, you code the name of the list and the
index that accesses the item on the left of the assignment operator (=). Then, you code the
data that you want to assign to the item on the right side of the assignment operator.
• If you attempt to access an item that doesn’t exist in the list, you’ll get an IndexError that
indicates that the index is out of range.

6
Data Types- Lists
Working with Lists: Examples

7
Data Types- Lists
Working with Lists: Examples

8
Data Types- Lists
Working with Lists: slicing lists

• You can slice a list to get a subset of the original list.


• If you only want to supply start and end arguments, you only need to code one colon.
• If you want to start slicing at the beginning of the list, you can omit the start argument. Or,
if you want to continue slicing to the end of the list, you can omit the end argument.
• If you include the step argument, it specifies which items in the list should be included. If,
for example, you want to get every other item in the list, you can supply a step argument of
2. Or, if you want to reverse the items, you can supply a step argument of -1.

9
Data Types- Lists
Working with Lists: Examples for slicing and concatenating lists

10
Data Types- Lists
Working with Lists: copying lists

• The assignment operator makes a shallow copy of a list, so both list variables refer to the
same list. Since a list is a mutable type, this causes both variables to refer to the same list.
As a result, if you use one variable to change the list, those changes are also available to the
other variable.
• By contrast, the deepcopy() function of the copy module makes a deep copy of the list, so
the list variables refer to two different lists.
• You have to import the copy module in order to use the deepcopy() function. As a result,
you can use one variable to change items in one list, and you can use the other variable to
change the items in the other list.

11
Data Types- Lists
Working with Lists: Examples for copying lists

12
Data Types- Lists
Working with Lists: creating a list of lists

• To create a list of lists, you code another list within each item of a first list. This takes what
you learned about lists and applies it to a list of lists.
• To refer to the items in a list of lists, you use two indexes. If necessary, you can also use
negative indexes to work from the end of each list.
• This can also be referred to as a two-dimensional list, and you can think of the data as
columns within rows.

13
Data Types- Lists
Working with Lists: Examples of creating a list of lists

14
Data Types- Lists
Working with Lists: Examples of creating a list of lists

15
Data Types- Lists

Working with Lists: Methods for modifying a list

• You can use the list methods to add and remove


the items in a list.
• You can add items to a list and remove items from
a list. To do that, you can use some of the
methods that are available for a list. These are
summarized in the table shown to the right.

16
Data Types- Lists

Working with Lists: Examples for list methods

17
Data Types- Lists

Working with Lists: Examples for list methods

18
Data Types- Lists

Working with Lists: Methods for modifying a list

• What is the difference between remove() and


pop()?
 Remove() takes the item itself as an argument to
remove; however, pop() takes an index of an item
as an argument to remove.
 pop() returns the item removed so you can
assign to a variable; however, remove() doesn’t
return.

19
Data Types- Lists

Working with Lists: Methods for modifying a


list

• There are three other methods: count(),


reverse() and sort().
• The sort() method requires a second argument
if the list contains strings with mixed cases.
That’s because uppercase letters come before
lowercase letters when strings are compared.

20
Data Types- Lists

Working with Lists: Methods for modifying a list

21
Data Types- Lists

Working with Lists: Methods for modifying a list

• There is another useful method: extend().


• You can use this method to extend a list to
include items of another list.

22
Data Types- Lists

Working with Lists: Functions for processing


list items

• You can use the built-in len() function to get


the length of the list, which is the number of
items in the list.
• If, for example, there are 10 items in a list,
the indexes range from 0 through 9, but the
len() function returns 10.

23
Data Types- Lists

Working with Lists: Functions for


processing list items

• You can use the min() function to get


the lowest value in the list and the
max() function to get the highest value.
• You use the sum() function to get the
sum of all the values in the list. You can
include a value for the optional start
argument. This adds the start value to
the total that’s returned by sum().

24
Data Types- Lists

Working with Lists: Functions for


processing list items

• You can use the enumerate() function to


returns all the items with their
corresponding indexes.

25
Data Types- Lists

Working with Lists: Functions for processing list items

• Later, we will learn about functions. At this point, we


want to know how to use some useful functions of
Module random to process list items.
• The random module provides functions for
generating random numbers. One of its uses is for
game development.
• You can use the choice() function to get a randomly
selected item from a list. Here, choice is used as the
variable name to hold the returned randomly
selected item.
• You can use the shuffle() function to shuffle the
numbers in a list.
26
Data Types- Lists

Working with Lists: Functions for processing list items

27
Data Types- Lists

Working with Lists: Functions for


processing list items

• You can use the in keyword to search for


an item in a list. This is one way to make
sure that an item is in a list before you
process it. In this example, the item is
removed after the code makes sure it’s in
the list.

28
Data Types- Lists

Working with Lists: Functions for


processing list items

• In the same way, you can use the not in


keyword to check if an item does not
exist in a list. In this example, the item
is appended to the list after the code
makes sure it’s not in the list.

29
Data Types- Lists

Working with Lists: Functions for


processing list items
Where n is the number
of elements; the first
• There is a built-in function range(), that can be range(n)
element is index 0, and
used to automatically generate regular last n-1
arithmetic sequences. start at n and stop
range(n, m)
• The range() is useful when you need to at m-1
generate a list sequence. Begin, increment
range(begin, end, step)
by step, stop at end-1
• The range() syntax is shown to the right:

30
Data Types- Lists

Working with Lists: Functions for


processing list items
Where n is the number
of elements; the first
• There is a built-in function range(), that can be range(n)
element is index 0, and
used to automatically generate regular last n-1
arithmetic sequences. start at n and stop
range(n, m)
• The range() is useful when you need to at m-1
generate a list sequence. Begin, increment
range(begin, end, step)
by step, stop at end-1
• The range() syntax is shown to the right:

31
Data Types- Lists

Working with Lists: Functions for


processing list items

32
Data Types- Lists

Working with Lists:Do it yourself !


• Ask the user to enter first name, last name and a student id, all in one input statement.
• Create an empty list, then append the entered information into the list using append() one at a time.
• Last, print the list.

33
33
Data Types- Lists

Working with Lists: Do it yourself !


• Ask the user to enter 4 sales amounts as float values, all in one input statement separated with ( , ).
• Append them into a list.
• Find the minimum, maximum, the sum of the list of the sales.
• Sort the list then reverse it.
• Last, count the number of times an amount occurs in a list.
• Print out all outputs in the above questions.

34
34
Data Types- Lists

Working with Lists: Do it yourself !


• Create a list of 3 subjects you take in your school.
• Create another list of the 3 grades of the subjects. A sample of the extended list:
• Extend the first list to include the second one. ['math', 'python', 'network', 90, 93, 88]
• Check for a specific subject if it is in the list, if so, remove it with its grade. If the subject is not in the
list, append it with its grade into the list so it still looks consistent- the subjects followed by the
grades.
• Calculate the average of the grades. In a new list, append or extend it to include the subjects and
the average value.
• Get the average value from the list and check for it to print the corresponding grading letter (e.g., A,
B, C, etc.) – follow the example in the last week lesson.

35
35
Data Types- Lists

Working with Lists: Do it yourself !


• Create a list of three lists of 2 float numbers in each.
• Access the second list.
• Compare the two values of that list.
• If they are equal, append ‘neutral’ to the list, if the first value is smaller than the second, append
‘safe’; otherwise, append ‘risky’.
• Access the appended value in the previous step and print out to the screen.

36
36
References
Material has been taken from (can be accessed through Sheridan's Library Services) :

• Starting Out with Python (ISBN-13: 9780136912330 ):


• https://www.pearson.com/en-ca/subject-catalog/p/starting-out-with-python/
P200000003356/9780136912330

• Murach’s Python 2nd Edition programming, 2nd Edition :


• https://bookshelf.vitalsource.com/reader/books/9781943872756/pages/recent

• Introducing Python, Lubanovic, B., O'Reily Media, 2nd Edition, 2019


• https://www.oreilly.com/library/view/introducing-python-2nd/
9781492051374/
37

You might also like