Don't wanna be here? Send us removal request.
Text
•List is one of the most frequently used and very versatile data type used in Python.
•A list can store a collection of data of any size.
•Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type.
•The values in a list are called elements or sometimes items.
•In many other programming languages, you would use a type called an array to store a sequence of data. An array has a fixed size.
•A Python list’s size is flexible. It can grow and shrink on demand.
•Python list is enclosed between square([ ]) brackets and elements are stored in the index basis with starting index 0.
•In Python, lists are mutable i.e., Python will not create a new list if we modify an element of the list.
Creating Lists
•A list is created by placing all the items (elements) inside a square bracket [ ], separated by commas.
•It can have any number of items and they may be of different types (integer, float, string etc.).
•Similar to string indices, list indices start at 0, and lists can be sliced, concatenated and so on.
•list can be created by two methods:
–Without using the constructor of the list class
–By using the constructor of the list class
📷
Accessing elements from a list
There are various ways in which we can access the elements of a list.
List Index
We can use the index operator [] to access an item in a list. In Python, indices start at 0. So, a list having 5 elements will have an index from 0 to 4.
Trying to access indexes other than these will raise an IndexError. The index must be an integer. We can't use float or other types, this will result in TypeError.
Nested lists are accessed using nested indexing.
# List indexing
my_list = ['p', 'r', 'o', 'b', 'e']
# Output: p
print(my_list[0])
# Output: o
print(my_list[2])
# Output: e
print(my_list[4])
# Nested List
n_list = ["Happy", [2, 0, 1, 5]]
# Nested indexing
print(n_list[0][1])
print(n_list[1][3])
# Error! Only integer can be used for indexing
print(my_list[4.0])
Output
p
o
e
a
5
Traceback (most recent call last):
File "<string>", line 21, in <module>
TypeError: list indices must be integers or slices, not float
Negative indexing
Python allows negative indexing for its sequences. The index of -1 refers to the last item, -2 to the second last item and so on.
# Negative indexing in lists
my_list = ['p','r','o','b','e']
print(my_list[-1])
print(my_list[-5])
When we run the above program, we will get the following output:
e
p
📷
List operations
The most widely used list operations in Python are:
1. append()
The append() method is used to add elements at the end of the list. This method can only add a single element at a time. To add multiple elements, the append() method can be used inside a loop.
Code:
myList.append(4)
myList.append(5)
myList.append(6)
for i in range(7, 9):
myList.append(i)
print(myList)
Output:
List operations
The most widely used list operations in Python are:
1. append()
The append() method is used to add elements at the end of the list. This method can only add a single element at a time. To add multiple elements, the append() method can be used inside a loop.
Code:
myList.append(4)
myList.append(5)
myList.append(6)
for i in range(7, 9):
myList.append(i)
print(myList)
2. extend()
The extend() method is used to add more than one element at the end of the list. Although it can add more than one element unlike append(), it adds them at the end of the list like append().
Code:
myList.extend([4, 5, 6])
for i in range(7, 9):
myList.append(i)
print(myList)
3. insert()
The insert() method can add an element at a given position in the list. Thus, unlike append(), it can add elements at any position but like append(), it can add only one element at a time. This method takes two arguments. The first argument specifies the position and the second argument specifies the element to be inserted.
Code:
myList.insert(3, 4)
myList.insert(4, 5)
myList.insert(5, 6)
print(myList)
4. remove()
The remove() method is used to remove an element from the list. In the case of multiple occurrences of the same element, only the first occurrence is removed.
Code:
myList.remove('makes learning fun!')
myList.insert(4, 'makes')
myList.insert(5, 'learning')
myList.insert(6, 'so much fun!')
print(myList)
6. Slice
The Slice operation is used to print a section of the list. The Slice operation returns a specific range of elements. It does not modify the original list.
Code:
print(myList[:4]) # prints from beginning to end index
print(myList[2:]) # prints from start index to end of list
print(myList[2:4]) # prints from start index to end index
print(myList[:]) # prints from beginning to end of list
7. Reverse()
The reverse() operation is used to reverse the elements of the list. This method modifies the original list. To reverse a list without modifying the original one, we use the slice operation with negative indices. Specifying negative indices iterates the list from the rear end to the front end of the list.
Code:
print(myList[::-1]) # does not modify the original list
myList.reverse() # modifies the original list
print(myList)
8. len()
The len() method returns the length of the list, i.e. the number of elements in the list.
Code:
print(len(myList))
9. min() & max()
The min() method returns the minimum value in the list. The max() method returns the maximum value in the list. Both the methods accept only homogeneous lists, i.e. list having elements of similar type.
Code:
print(min(myList))
Code:
print(min([1, 2, 3]))
print(max([1, 2, 3]))
10. count()
The function count() returns the number of occurrences of a given element in the list.
Code:
print(myList.count(3))
Bottom of Form
Top of Form
11. Concatenate
The Concatenate operation is used to merge two lists and return a single list. The + sign is used to perform the concatenation. Note that the individual lists are not modified, and a new combined list is returned.
Code:
yourList = [4, 5, 'Python', 'is fun!'] print(myList+yourList)
12. Multiply
Python also allows multiplying the list n times. The resultant list is the original list iterated ntimes.
Code:
print(myList*2)
13. index()
The index() method returns the position of the first occurrence of the given element. It takes two optional parameters – the begin index and the end index. These parameters define the start and end position of the search area on the list. When supplied, the element is searched only in the sub-list bound by the begin and end indices. When not supplied, the element is searched in the whole list.
Code:
print(myList.index('EduCBA')) # searches in the whole list
print(myList.index('EduCBA', 0, 2)) # searches from 0th to 2ndposition
14. sort()
The sort method sorts the list in ascending order. This operation can only be performed on homogeneous lists, i.e. lists having elements of similar type.
Code:
yourList = [4, 2, 6, 5, 0, 1] yourList.sort()
print(yourList)
15. clear()
This function erases all the elements from the list and empties it.
Code:
myList.sort()
print(myList)
0 notes
Text
The Zen of Python


0 notes
Text
“Don't be pushed around by the fears in your mind. Be led by the dreams in your heart.” ― Roy T. Bennett,
0 notes
Text
Lesson 1
Python is a not a new language, originated around 1989's, paved its way to become most preferable language for Data Scientists.
If you are wondering to start learning this language,the first step you should take is go to following link and download latest version of the Python framework.
During installation ,always select the option of adding environment variables, so that you don't manually have to set PATH,which could be a gruesome process for naive or new users.
Don't forget to compare the Python2 and Python3,for the sake of accessibility.
If you are working on 32-bit system ,download same configuration Python software.
Happy learning!!!!!! See you in next lesson...............
"Don’t practice until you get it right. Practice until you can’t get it wrong."
0 notes
Photo

Follow for latest technological lessons.
0 notes