#4 Python Tutorial for Beginners

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

Let's get started.

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.

Example :

#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 :

[12234556]
45
[14234556]
[34'Ps_Inf0'23.34False]


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 :

[12234945]


List function

  • sort() method : sort() method is used to sort the list.
Example :

# sort the list using sort() method.
number = [12 , 945 , 23 , 78 , 15]
number.sort()

#Print the list using print() function.
print(number)

Output :

[91215234578]

  • reverse() method : reverse() method is used to reverse the list.
Example :

# 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)

Output :

[3045942312]
['Orange''Apple''Cherry']

  • append() method : append() method is used to add the value at end of the list.
Example :

# append() method.
# add the value at the end of the list.

number = [12 , 945 , 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 :

[12945237815309]
['Cherry''Apple''Orange''Mango']

  • insert() method : insert() method is used to insert value at specific index in the list.
Example :

# 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 :

[122342894530]
['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 :

[122344530]
['Cherry''Orange']

  • remove() method : remove() method is used to remove specific value.
Example :

# 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 :

[12494530]
['Apple''Orange']


Tuples

  • A Tuples is immutable.
  • It means you cannot change the value after creating of tuples.
Example :

# 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 :

(1223456730)
45


Tuples function

  • count() method : count() method is used in tuples to find the number of items present in tuples.
Example :

# 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.
Example :

# 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.  🙏


Post a Comment

0 Comments