Computer Science, asked by intelligent5191, 7 months ago

Lists are mutable. It means that the contents of the list can be changed after it has been created. Given a list1 of colors, 1. Write a command to change/override the fourth element of list1 as BLACK 2. Write a command to display the first three elements of the list1. 3. Write a command to find the total elements in the list1. list1 = ['Red','Green','Blue','Orange', 'pink', 'Purple','Magenta','Yellow']

Answers

Answered by ishuraj25122005
1

Answer:

hii

Explanation:

list is an ordered set of values, where each value is identified by an index. The values that make up a list are called its elements. Lists are similar to strings, which are ordered sets of characters, except that the elements of a list can have any type. Lists and strings — and other things that behave like ordered sets — are called sequences.

9.1. List values

There are several ways to create a new list; the simplest is to enclose the elements in square brackets ( [ and ]):

[10, 20, 30, 40]

["spam", "bungee", "swallow"]

The first example is a list of four integers. The second is a list of three strings. The elements of a list don’t have to be the same type. The following list contains a string, a float, an integer, and (mirabile dictu) another list:

["hello", 2.0, 5, [10, 20]]

A list within another list is said to be nested.

Finally, there is a special list that contains no elements. It is called the empty list, and is denoted [].

Like numeric 0 values and the empty string, the empty list is false in a boolean expression:

>>> if []:

... print 'This is true.'

... else:

... print 'This is false.'

...

This is false.

>>>

With all these ways to create lists, it would be disappointing if we couldn’t assign list values to variables or pass lists as parameters to functions. We can:

>>> vocabulary = ["ameliorate", "castigate", "defenestrate"]

>>> numbers = [17, 123]

>>> empty = []

>>> print vocabulary, numbers, empty

['ameliorate', 'castigate', 'defenestrate'] [17, 123] []

9.2. Accessing elements

The syntax for accessing the elements of a list is the same as the syntax for accessing the characters of a string—the bracket operator ( [] – not to be confused with an empty list). The expression inside the brackets specifies the index. Remember that the indices start at 0:

>>> print numbers[0]

17

Any integer expression can be used as an index:

>>> numbers[9-8]

123

>>> numbers[1.0]

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

TypeError: list indices must be integers

If you try to read or write an element that does not exist, you get a runtime error:

>>> numbers[2]

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IndexError: list index out of range

If an index has a negative value, it counts backward from the end of the list:

>>> numbers[-1]

123

>>> numbers[-2]

17

>>> numbers[-3]

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IndexError: list index out of range

numbers[-1] is the last element of the list, numbers[-2] is the second to last, and numbers[-3] doesn’t exist.

It is common to use a loop variable as a list index.

horsemen = ["war", "famine", "pestilence", "death"]

i = 0

while i < 4:

print horsemen[i]

i += 1

This while loop counts from 0 to 4. When the loop variable i is 4, the condition fails and the loop terminates. So the body of the loop is only executed when i is 0, 1, 2, and 3.

Each time through the loop, the variable i is used as an index into the list, printing the i-eth element. This pattern of computation is called a list traversal.

9.3. List length

The function len returns the length of a list, which is equal to the number of its elements. It is a good idea to use this value as the upper bound of a loop instead of a constant. That way, if the size of the list changes, you won’t have to go through the program changing all the loops; they will work correctly for any size list:

horsemen = ["war", "famine", "pestilence", "death"]

i = 0

num = len(horsemen)

while i < num:

print horsemen[i]

i += 1

The last time the body of the loop is executed, i is len(horsemen) - 1, which is the index of the last element. When i is equal to len(horsemen), the condition fails and the body is not executed, which is a good thing, because len(horsemen) is not a legal index.

Although a list can contain another list, the nested list still counts as a single element. The length of this list is 4:

['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]

9.4. List membership

in is a boolean operator that tests membership in a sequence. We used it previously with strings, but it also works with lists and other sequences:

>>> horsemen = ['war', 'famine', 'pestilence', 'death']

>>> 'pestilence' in horsemen

True

>>> 'debauchery' in horsemen

False

Since pestilence is a member of the horsemen list, the in operator returns True. Since debauchery is not in the list, in returns False.

We can use the not in combination with in to test whether an element is not a member of a list:

>>> 'debauchery' not in horsemen

True

Similar questions