Course Content

Python Lists And List Functions

Lists :

Python lists are containers used to store a list of values of any data type. In simple words, we can say that a list is a collection of elements from any data type E.g.

list1 = ['harry', 'ram', 'Aakash', 'shyam', 5, 4.85]

The above list contains strings, an integer, and even an element of type float. A list can contain any kind of data i.e. it’s not mandatory to form a list of only one data type. The list can contain any kind of data in it.

Do you remember we saw indexing in strings? List elements can also be accessed by using Indices i.e. first element of the list has 0 index and the second element has 1 as its index and so on.

Note: If you put an index which isn’t in the list i.e. that index is not there in list then you will get an error. i.e. if a list named list1 contains 4 elements, list1[4] will throw an error because the list index starts from 0 and goes upto (index-1) or 3.
Have a look at the examples below:

List Methods :

Here is the list of list methods in Python. These methods can be used in any python list to produce the desired output.

List Slicing :

List slices, like string slices, returns a part of a list extracted out of it. Let me explain, you can use indices to get elements and create list slices as per the following format :

seq = list1[start_index:stop_index]

Just like we saw in strings, slicing will go from a start index to stop_index-1. It means the seq list which is a slice of list1 contains elements from the specified start_index to specified (stop_index – 1).

List Methods:

There are a lot of list methods that make our life easy while using lists in python. Lets have a look at few of them below:

 

Tuples in Python:

A tuple is an immutable data type in Python. A tuple in python is a collection of elements enclosed in () (parentheses). Tuple once defined can’t be changed i.e. its elements or values can’t be altered or manipulated.

Note: To create a tuple of one element it is necessary to put a comma  ‘,’ after that one element like this tup=(1,) because if we have only 1 element inside the parenthesis, the python interpreter will interpret it as a single entity which is why it’s important to use a ‘,’ after the element while creating tuples of a single element.

Swapping of two numbers

Python provides a very handy way of swapping two numbers like below:

Code file as described in the video

grocery = ["Harpic", "vim bar", "deodrant", "Bhindi",
           "Lollypop", 56]
# print(grocery[5])
numbers = [2, 7, 9, 11, 3]
# numbers.remove(9)
# numbers.pop()
# numbers.sort()
# numbers = []
# numbers.reverse()
# numbers.append(1)
# numbers.append(72)
# numbers.append(5)
# numbers.insert(2, 67)
# print(numbers)
# 3, 11, 9, 7, 2
# print(numbers)
# numbers[1] = 98
# print(numbers)
# Mutable - can change
# Immutable - cannot change
# tp = (1,)
# print(tp)
a= 1
b = 8
a, b = b,a
# temp = a
# a = b
# b = temp
print(a, b)

Comments(10)

Raheel123 1 year, 2 months ago
Sir you have a lot of likes 
ahmedraza 7 months, 1 week ago
nice
anubhavs921 6 months, 3 weeks ago
Very Nice tutorial and better explain of lists.......thanku sir
abcd 4 months, 1 week ago
good
sanayasingh 6 months ago
cosmeticitem= ["serum","facewash","lip bam","face mask","facesheet","Toner"]
cosmeticitem.sort()
print(cosmeticitem)
 output= ['Toner', 'face mask', 'facesheet', 'facewash', 'lip bam', 'serum']
can you explain why toner comes first in output ?
A.Basit 6 months ago
gr8 job...thks...
Vivek-Mishra 4 months ago
Full Notes and Source Code ::::::::::} https://github.com/Optimized-World/Complete-Python-Tutorial-and-Notes |::::::::::::| Thanks, Harry Bhai for Awesome Tutorials
slakhera3 1 month, 1 week ago
Hi sir, can you please explain a bit more about tuple. It was not very clear to me.
aarav007 2 weeks, 4 days ago
Very nice datatypes
aarav007 2 weeks, 4 days ago
Very nice datatypes Harry you teach very well it is very nice to learn python.

Resources

  1. File - Download here
  2. File - Download here
  3. File - Download here
  4. File - Download here
  5. File - Download here
  6. File - Download here

Course Announcements

Any Course related announcements will be posted here