[go: up one dir, main page]

0% found this document useful (0 votes)
4 views12 pages

Chapter 6 pfe

The document provides an overview of lists in Python, including how to create, access, and modify them. It explains the differences between mutable lists and immutable strings, as well as how to iterate through lists and handle potential out-of-range errors. Additionally, it covers list operations such as appending, inserting, and finding elements within a list.

Uploaded by

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

Chapter 6 pfe

The document provides an overview of lists in Python, including how to create, access, and modify them. It explains the differences between mutable lists and immutable strings, as well as how to iterate through lists and handle potential out-of-range errors. Additionally, it covers list operations such as appending, inserting, and finding elements within a list.

Uploaded by

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

Lists

1
Creating Lists

values = [32, 54, 67.5, 29, 35, 80, 115, 44.5, 100, 65]

•The square brackets indicate that we are creating a list. The


items are stored in the order they are provided.

2
Accessing List Elements

• A list is a sequence of elements, each of which has an integer


position or index. To access a list element, you specify which
index you want to use. That is done with the subscript operator
([]) in the same way that you access individual characters in a
string. For example,

print(values[5]) # Prints the element at index 5

• This is not an accident. Both lists and strings are sequences,


and the [] operator can be used to access an element in any
sequence.

3
Mutable and Immutable

• There are two differences between lists and strings. Lists can
hold values of any type, whereas strings are sequences of
characters. Moreover, strings are immutable— you cannot
change the characters in the sequence. But lists are mutable.
You can replace one list element with another, like this:

values[5] = 87

Now the element at index 5 is filled with 87

4
out-of-range error or a
bounds error
• values[0], the first element
• values[1], the second element
• values[2], the third element
• ...
• values[9], the tenth element
• In this list, an index can be any integer ranging from 0 to 9.
• There is no values[10] in a list with ten elements – the index can
range from 0 to 9. To avoid out-of-range errors, you will want to
know how many elements are in a list.
• You can use the len function to obtain the length of the list; that
is, the number of elements:

numElements = len(values)

5
List Iteration

for i in range(10) :
print(i, values[i])
•The variable i iterates through the integer values 0 through 9,
which is appropriate because there is no element corresponding to
values[10].
•Instead of using the literal value 10 for the number of elements in
the list, it is a
•good idea to use the len function to create a more reusable loop:
for i in range(len(values)) :
print(i, values[i])
•If you don’t need the index values, you can iterate over the
individual elements using a for loop in the form:
for element in values :
print(element)

6
List References

• When you copy a list variable into another, both variables refer
to the same list. The second variable is an alias for the first
because both variables reference the same list.
scores = [10, 9, 7, 4, 5]
values = scores # Copying list reference
• You can modify the list through the following assignment
operation:
scores[3] = 10
print(values[3])

7
Self Check

1. Define a list of integers containing the first five prime numbers.


2. Assume that the list primes has been initialized as described
below. What does it contain after executing the following loop?
for i in range(2) :
primes[4 - i] = primes[i]
3. Assume that the list primes has been initialized as described
below. What does it contain after executing the following loop?
for i in range(5) :
primes[i] = primes[i] + 1
4. Given the definition
values = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] write statements to put the
integer 10 into the elements of the list values with the lowest and
the highest valid index.
5. Define a list containing two strings, "Yes", and "No".

8
Reverse Subscripts

• Python, unlike many other


languages, also allows you to use
negative subscripts when accessing
an element of a list. The negative
subscripts provide access to the list
elements in reverse order. ‘For
example, a subscript of –1 provides
access to the last element in the list:
last = values[-1]
print("The last element in the list
is", last)
• Similarly, values[-2] is the second-to-
last element. Note that values[-10] is
the first element.
• In general, the valid range of
negative subscripts is between -1
and -len(values).

9
List Operations
• In contrast, Python has a rich set of
operations that make list processing
quite convenient.

• Appending Element
• Here we start out with an empty list:
friends = [] # ---1
• A new element can be appended to the
end of the list with the append
method:
friends.append("Harry") 2# ---2
• The size, or length, of the list increases
after each call to the append method.
Any number of elements can be added
to a list:
friends.append("Emily") # ---3
friends.append("Bob")
friends.append("Cari")

10
List Operations

• Inserting an Element
• You have just seen how to add
a new element to the end of a
list using the list method
append. Sometimes, however,
a new element has to be
inserted at a specific position in
the list.
friends = ["Harry", "Emily",
"Bob", "Cari"] 1
• suppose we want to insert the
string "Cindy" into the list
following the first element,
which contains the string
"Harry". The statement
friends.insert(1, "Cindy") 2
• achieves this task
friends.insert(5, "Bill")

11
List Operations

• Finding an Element
• Let’s assume we have the following list declaration
friends = ["Harry", "Emily", "Bob", "Cari", "Emily"]
• If you simply want to know whether an element is present in a
list, use the in operator:
if "Cindy" in friends :
print("She's a friend")
• Often, you want to know the position at which an element
occurs.
n = friends.index("Emily") # Sets n to 1

12

You might also like