Hello Guys ,
In previous tutorial you learned about String , String slicing , and String functions in python.
In this tutorial you will learn below points:
- List
- List slicing
- List function
- Tuples
- Tuples function
List
- List are containers to store a set of values of any data type.
- It is mutable means you can change the value after creation of list.
- We can also make a list of different types of things.
#Lists
#Create a list using [] brackets.
number = [12 , 23 , 45 , 56]
#Print the list using print() function
print(number)
#Value Access using index in list.
print(number[2])
#Change the value using index.
number[0] = 14
print(number)
# We can also create a list with items of different types.
types = [34 , "Ps_Inf0" , 23.34 , False]
print(types)
Output :
[12, 23, 45, 56]
45
[14, 23, 45, 56]
[34, 'Ps_Inf0', 23.34, False]
List slicing
- List in python can be sliced for a getting a particular value.
Example :
# List Slicing
no = [12 , 23 ,4 , 9 , 45 , 30 , 56]
print(no[0:5])
Output :
[12, 23, 4, 9, 45]
List function
- sort() method : sort() method is used to sort the list.
# sort the list using sort() method.
number = [12 , 9, 45 , 23 , 78 , 15]
number.sort()
#Print the list using print() function.
print(number)
Output :
[9, 12, 15, 23, 45, 78]
- reverse() method : reverse() method is used to reverse the list.
# reverse the list using reverse() method.
no = [12 , 23 ,4 ,9 , 45 , 30]
no.reverse()
#Print the list using print() function.
print(no)
# We can also reverse the String type list.
fruits = ["Cherry" , "Apple" , "Orange"]
fruits.reverse()
print(fruits)
[30, 45, 9, 4, 23, 12]
['Orange', 'Apple', 'Cherry']
- append() method : append() method is used to add the value at end of the list.
# append() method.
# add the value at the end of the list.
number = [12 , 9, 45 , 23 , 78 , 15]
number.append(309)
#Print the list using print() function.
print(number)
# We can also add the value end of the String type list.
fruits = ["Cherry" , "Apple" , "Orange"]
fruits.append("Mango")
print(fruits)
Output :
[12, 9, 45, 23, 78, 15, 309]
['Cherry', 'Apple', 'Orange', 'Mango']
- insert() method : insert() method is used to insert value at specific index in the list.
# insert() method
'''
We can also insert value at specific index
in list using insert() method.
'''
no = [12 , 23 ,4 ,9 , 45 , 30]
no.insert(3 , 28)
#Print the list using print() function.
print(no)
'''
We can also insert value at specific index
of the string type list.
'''
fruits = ["Cherry" , "Apple" , "Orange"]
fruits.insert(1 , "Mango")
print(fruits)
Output :
[12, 23, 4, 28, 9, 45, 30]
['Cherry', 'Mango', 'Apple', 'Orange']
- pop() method : pop() method is used to remove value from specific index.
Example :
# pop() method
'''
We can also remove value from specific index
in list using pop() method.
'''
no = [12 , 23 ,4 ,9 , 45 , 30]
no.pop(3)
#Print the list using print() function.
print(no)
'''
We can also remove value from specific index
of the string type list.
'''
fruits = ["Cherry" , "Apple" , "Orange"]
fruits.pop(1)
print(fruits)
Output :
[12, 23, 4, 45, 30]
['Cherry', 'Orange']
- remove() method : remove() method is used to remove specific value.
# remove() method.
'''
We can also remove specific value
from list using remove() method.
'''
no = [12 , 23 ,4 ,9 , 45 , 30]
no.remove(23)
#Print the list using print() function.
print(no)
'''
We can also remove specific value
from the string type list.
'''
fruits = ["Cherry" , "Apple" , "Orange"]
fruits.remove("Cherry")
print(fruits)
Output :
[12, 4, 9, 45, 30]
['Apple', 'Orange']
Tuples
- A Tuples is immutable.
- It means you cannot change the value after creating of tuples.
# Create tuple using () brackets.
tuple = (12 ,23 , 45 ,67 ,30)
# Print the tuple value using print() method.
print(tuple)
# Access tuple value using index
print(tuple[2])
Output :
(12, 23, 45, 67, 30)
45
Tuples function
- count() method : count() method is used in tuples to find the number of items present in tuples.
# count() method
'''
count() method used in tuples to find the number
of items present in tuples.
'''
no = (12 , 23 ,4 , 9 , 45 , 30 , 56 , 9 ,34 ,9)
print(no.count(9))
'''
We can also remove specific value
from the string type list.
'''
fruits = ("Cherry" , "Apple" , "Orange", "Apple")
print(fruits.count("Apple"))
Output :
3
2
- index() method : index() method is used find the index for given number.
# index() method
'''
index() method used to find
index for given element.
'''
no = (12 , 23 ,4 ,9 , 45 , 30)
print(no.index(4))
'''
We can also find index for given element
from the string type list.
'''
fruits = ("Cherry" , "Apple" , "Orange")
print(fruits.index("Apple"))
Output :
2
1
If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support. 🙏
0 Comments