Computer Science, asked by ritukumari18972, 5 hours ago

WAP to accept 10 items in a list and check if the sequence of items in the list remains the same when we traverse it from the Right-hand side

Answers

Answered by kanchanbti
0

Answer:

A list is an ordered collection of values. The values that make up a list are called its elements, or its items. We will use the term element or item to mean the same thing. Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can be of any type. Lists and strings — and other collections that maintain the order of their items — are called sequences.

11.1. List values

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

1

2

ps = [10, 20, 30, 40]

qs = ["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 (amazingly) another list:

1

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

A list within another list is said to be nested.

Finally, a list with no elements is called an empty lis

17

Any expression evaluating to an integer can be used as an index:

>>> numbers[9-8]

5

>>> numbers[1.0]

Traceback (most recent call last):

File "<interactive input>", line 1, in <module>

TypeError: list indices must be integers, not float

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

>>> numbers[2]

Traceback (most recent call last):

File "<interactive input>",

11.3. List length

The function len returns the length of a list, which is equal to the number of its elements. If you are going to use an integer index to access the list, 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:

1

2

3

4

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

for i in range(len(horsemen)):

print(horsemen[i])

The last time the body of the loop is executed, i is len(horsemen) - 1, which is the index of the last element. (But the version without the index looks even better now!)

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

>>> len(["car makers", 1, ["Ford", "Toyota", "BMW"], [1, 2, 3]])

4

11.4. List membership

in and not in are Boolean operators that test membership in a sequence. We used them previously with strings, but they also work with lists and other sequences:

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

>>> "pestilence" in horsemen

True

>>> "debauchery" in horsemen

False

>>> "debauchery" not in horsemen

True

Using this produces a more elegant version of the nested loop program we previously used to count the number of students doing Computer Science in the section Nested Loops for Nested Data

Similarly, the * operator repeats a list

11.6. List slices

11.7. Lists are mutable

11.9. Objects and references

11.10. Aliasing

Similar questions