Lists
· List is an ordered sequence of items. Values in the list are called elements / items.
· It can be written as a list of comma-separated items (values) between square brackets[ ].
· Items in the lists can be of different data types.
1. Indexing
2. Slicing
3. Concatenation
4. Repetitions
5. Updating
6. Membership
7. Comparison
List slicing is an operation that extracts a subset of elements from an list and packages them as another list.
Listname[start:stop]
Listname[start:stop:steps]
v default start value is 0
v default stop value is n-1
v [:] this will print the entire list
v [2:2] this will create a empty slice
v Methods used in lists are used to manipulate the data quickly.
v These methods work only on lists.
v They do not work on the other sequence types that are not mutable, that is, the values they contain cannot be changed, added, or deleted.
syntax:
list name.method name( element/index/list)
1. For loop
2. While loop
3. Infinite loop
1. List using For Loop:
v The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects.
v Iterating over a sequence is called traversal.
v Loop continues until we reach the last item in the sequence.
v The body of for loop is separated from the rest of the code using indentation.
for val in sequence:
2. List using While loop
v The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
v When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
Syntax:
while (condition):
body of while
a=[1,2,3,4,5]
i=0
sum=0
while i<len(a):
sum=sum+a[i]
i=i+1
print(sum)
15
A loop becomes infinite loop if the condition given never becomes false. It keeps on
running. Such loops are called infinite loop.
a=1
while (a==1):
n=int(input("enter the number"))
print("you entered:" , n)
Enter the number 10
you entered:10
Enter the number 12
you entered:12
Enter the number 16
you entered:16
v Lists are mutable. (can be changed)
v Mutability is the ability for certain types of data to be changed without entirely recreating it.
v An item can be changed in a list by accessing it directly as part of the assignment statement.
v Using the indexing operator (square brackets[ ]) on the left side of an assignment, one of the list items can be updated.
v Creating a copy of a list is called aliasing. When you create a copy both list will be having same memory location. changes in one list will affect another list.
v Alaising refers to having different names for same list values.
v In this a single list object is created and modified using the subscript operator.
v When the first element of the list named “a” is replaced, the first element of the list named “b” is also replaced.
v This type of change is what is known as a side effect. This happens because after the assignment b=a, the variables a andb refer to the exact same list object.
v They are aliases for the same object. This phenomenon is known as aliasing.
v To prevent aliasing, a new object can be created and the contents of the original can be copied which is called cloning.
v To avoid the disadvantages of copying we are using cloning. creating a copy of a same list of elements with two different memory locations is called cloning.
v Changes in one list will not affect locations of aother list.
v Cloning is a process of making a copy of the list without modifying the original list.
1. Slicing
2. list()method
3. copy() method
>>>a=[1,2,3,4,5]
>>>b=a[:]
>>>print(b)
[1,2,3,4,5]
>>>a is b
False
>>>a=[1,2,3,4,5]
>>>b=list
>>>print(b)
[1,2,3,4,5]
>>>a is b
false
>>>a[0]=100
>>>print(a)
>>>a=[100,2,3,4,5]
>>>print(b)
>>>b=[1,2,3,4,5]
a=[1,2,3,4,5]
>>>b=a.copy()
>>> print(b) [1, 2, 3, 4, 5]
>>> a is b
False
v In python, arguments are passed by reference.
v If any changes are done in the parameter which refers within the function, then the changes also reflects back in the calling function.
v When a list to a function is passed, the function gets a reference to the list.
v Passing a list as an argument actually passes a reference to the list, not a copy of the list.
Since lists are mutable, changes made to the elements referenced by the parameter change the same list that the argument is referencing.
def remove(a):
a.remove(1)
a=[1,2,3,4,5]
remove(a)
print(a)
[2,3,4,5]
def inside(a):
for i in range(0,len(a),1):
a[i]=a[i]+10
print(“inside”,a)
a=[1,2,3,4,5]
inside(a)
print(“outside”,a)
inside [11, 12, 13, 14, 15]
outside [11, 12, 13, 14, 15]
def insert(a):
a.insert(0,30)
a=[1,2,3,4,5]
insert(a)
print(a)
[30, 1, 2, 3, 4, 5]
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.