· 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.
List slicing is an operation that extracts a subset of elements from an list and packages them as another list.
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.
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.
body of while
A loop becomes infinite loop if the condition given never becomes false. It keeps on
running. Such loops are called infinite loop.
n=int(input("enter the number"))
print("you entered:" , n)
Enter the number 10
Enter the number 12
Enter the number 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.
3. copy() method
>>>a is b
>>>a is b
>>> print(b) [1, 2, 3, 4, 5]
>>> a is b
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.
for i in range(0,len(a),1):
inside [11, 12, 13, 14, 15]
outside [11, 12, 13, 14, 15]
[30, 1, 2, 3, 4, 5]