***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Tuesday, June 15, 2021

Lists

A sequence is an object that holds multiple items of data, stored one after
the other. Operations can be performed on a sequence to examine and manipulate
the items stored in it.



Both lists and tuples
are sequences that can hold various types of data. 

The difference between lists
and tuples is simple: a list is mutable, which means that a program can change
its contents,  but a tuple is immutable,
which means that once it is created, its contents cannot be changed.

Lists



A list is an object that contains multiple data items. Lists are mutable,
which means that their contents can be changed during a program’s execution.
Lists are dynamic data structures, meaning that items may be added to them or
removed from them. Indexing, slicing, and various methods can be used to work
with lists in a program.



Each item that is
stored in a list is called an element.



Here is a statement
that creates a list of integers:



even_numbers = [2, 4,
6, 8, 10]



The items that are
enclosed in brackets and separated by commas are the list elements.



A list can hold items
of different types, as shown in the following example:



info = ['Ashraf', 27, 2650.87]



This statement creates
a list containing a string, an integer, and a floating-point number.



Python also has a
built-in list() function that can convert certain types of objects to lists.



Example
1:



numbers =
list(range(5))



When this statement executes,
the following things happen:




The range function is called with 5 passed as an argument. The function returns
an iterable containing the values 0, 1, 2, 3, 4.




The iterable is passed as an argument to the list() function. The list()
function returns the list [0, 1, 2, 3, 4].




The list [0, 1, 2, 3, 4] is assigned to the numbers variable.



Example
2:



numbers = list(range(1,
10, 2))



when three parameters
are passed as arguments to the range function, the first argument is the
starting value, the second argument is the ending limit, and the third argument
is the step value. This statement will assign the list [1, 3, 5, 7, 9] to the
numbers variable.



The Repetition Operator



The * symbol multiplies
two numbers. However, when the operand on the left side of the * symbol is a
sequence (such as a list) and the operand on the right side is an integer, it
becomes the repetition operator. The repetition operator makes multiple
copies of a list and joins them all together.



Here is the general
format:



list * n



In the general format, list
is a list and n is the number of copies to make.



Example:



>>> numbers =
[1, 2, 3] * 3



>>>
print(numbers)



[1, 2, 3, 1, 2, 3, 1,
2, 3]



Note: You cannot create
traditional arrays in Python because lists serve the same purpose and provide
many more built-in capabilities.



Iterating over a List
with the for Loop



Many of the same
programming techniques also apply to lists. For example, you can iterate over a
list with the for loop, as shown here:



numbers = [99, 100,
101, 102]



for n in numbers:



print(n)



If we run this code, it
will print:



99



100



101



102



 



Indexing



 Another
way that you can access the individual elements in a list is with an index.
Each element in a list has an index that specifies its position in the list.
Indexing starts at 0.
The index of the last element in a list
is 1 less than the number of elements in the list.



 Negative
indexes can be used with lists to identify element positions relative to the
end of the list. The Python interpreter adds negative indexes to the length of
the list to determine the element position. The index -1 identifies the last
element in a list, -2 identify the next to last element, and so forth.



The
following code shows an example:



my_list
= [10, 20, 30, 40]



print(my_list[−1],
my_list[−2], my_list[−3], my_list[−4])



In
this example, the print function will display:



40
30 20 10



An
IndexError exception will be raised if you use an invalid index with a list.



 



Concatenating
Lists



The
+ operator is used to concatenate two lists. Here is an example:



 



list1
= [1, 2, 3, 4]



list2
= [5, 6, 7, 8]



list3
= list1 + list2



 



After
this code executes, list1 and list2 remain unchanged, and list3 references the following
list:



[1,
2, 3, 4, 5, 6, 7, 8]



 



Note:
Keep
in mind that you can concatenate lists only with other lists. If you try to concatenate
a list with something that is not a list, an exception will be raised.



 


No comments:

Post a Comment