Introduction
Setup
Operators
variable
String
Number
Dtype & Comment
Lists
Tuples
Sets
Dictionaries
if else statement
For Loop
While Loop
iterator & iterable
Pre-Built functions
Function
Decorators
generators
Types of errors
Exceptions Handling
OOP
Regular expression
Create module
OS module
Date module
Random module
Math
Statistics module
Exercise 1
Exercise 2
Exercise 3
Learn Data Structure & Algorithm
Learn Numpy
Learn Pandas
Learn Matplotlib
Learn Seaborn
Learn Statistics
Learn Math
Learn MATLAB
Learn Machine learning
Learn Github
Learn OpenCV
Learn Deep Learning
Learn MySQL
Learn MongoDB
Learn Web scraping
Learn Excel
Learn Power BI
Learn Tableau
Learn Docker
Learn Hadoop
In python, list is used to store multiple collections of data in one variable. You can store too many values in a list and perform a lot of work on it. You can store string, number, or both at the same time in a list. You can use a capital letter and a small letter in a list. List items are ordered and can have duplicate values. You can access or get a particular item using the index number.
We write list inside a third bracket. So at first give a third bracket[].
To write a string use single or double quotation ('', ").
To write numbers, don't need to use quotations.
Give comma between two values.
If we write the number inside quotation then it will act as a string and we won't be able to perform
mathematical work on that list element. If we write numbers and letters or words together then you must give a
quotation.
Index means the position number of each value which are stored in the list. In python, the index starts from 0. If there are 5 values in the list then its first index is 0 and last the index =(n-1) =(5-1)=4. Index numbers are used in various ways. It's a very important thing. You can get list items using an index number.
To check whether a value is in the list or not, "in" is used.
Example:To add value to the list append() function is used. This function add a new value at the end of the list.
Example:To find the length of the list len() function is used. By using len() function you can get the last index number of the list. By finding the last index number you can say that how many values are present in the list.
Example:To add value to a list in a specific position, insert() function is used. To do this, first pass the index number where we want to add, then give a comma and then pass the value.
Example:To remove any value from the list, remove() function is used. To remove value pass the value in the function bracket.
Example:To sort the data in ascending order, sort() function is used. To sort in descending order, pass a parameter reverse=true in the bracket of sort function.
Example:1To reverse the the list elements, reverse() function is used.
Example:See one thing, here in the example, main list element 3 is present in index number 2. But in the output it is in 1 index position. So you can say that you changed the order. This means that you reversed position of the values.
pop() function
To remove a value from end of the list, pop() function is used. If we use pop function one time then it will remove one value from end .Every time we do that, the value of total will decrease from the end .
Example:To find the index number of any value of a list, the index() function is used.
Example:To see that a selected value how many times present in the list, the count function is used. Pass a value( present in the list) in the function and then it will show that how many times the value present in the list.
Example:To copy a list copy() function is used.
Example:To remove all the elements present in a list at once, the clear function is used.
Example:By index number, we can get values from the list. Negative indexing means starting from the end of the list. The working process is same as what we saw in the string slicing.
Example:1How to Change value of list using index number?
Example:1You can join multiple lists together and can create a new list
Example:
You will understand this concept better after learning about loops and conditions. I will recommend you to
learn about loops and conditions before learning list comprehension. You can learn about loops and conditions
from the next lecture. List comprehension helps us to create a shorter syntax when we want to create a new
list based on the values of an existing list.
Basic syntax: newlist = [expression for item in iterable if condition == True]
Let's see an example:
Based on a list of numbers, create a new list. This new list will contain only the even numbers from the first
list. Without list comprehension, you have to write a for statement with a conditional test inside.
In comprehension bracket using the if condition, we found all the even number from the first list and then using for loop we took those values in the x variable, one by one and then print the value of x. Now if we compare both examples then we can understand that list comprehension makes the code shorter.
Example:2What we did here, we do square of even numbers present in the list and odd number as negative odd.What we did here, we do square of even numbers present in list and odd number as negative odd.