In the last session, we discussed the Python Function. Now, we will look at Python's Different Data Types and examples. The Data types are the most important topic in Python.

Let’s start,

Definition of List in Python:

  1. Introduction to Lists

Code
  
    ##check the type 
#input
list=[]
print(type(list))

#output
<class 'list'>
  1. Create the simple List using string and numbers

Code
  
    #Input
names=["Ram","Raja","Rani",1,2,3,4,5]
print(names)

#Output
['Ram', 'Raja', 'Rani', 1, 2, 3, 4, 5]
  1. Create the simple List using string, numbers, float and Boolean values

Code
  
    #input
mixed_list =[5,"Hello",3.14,True]
print(mixed_list)

#output
[5, 'Hello', 3.14, True]
  1. Accessing or Slicing the List of Elements

Code
  
    #input
fruits=["apple", "banana", "cherry", "kiwi", "Orange"]
print(fruits[0])
print(fruits[1])
print(fruits[4])
print(fruits[1:])
print(fruits[:1])
print(fruits[1:3])
print(fruits[-1])
Print(fruits[::-1]

#output
apple
banana
Orange
['banana', 'cherry', 'kiwi', 'Orange']
['apple']
['banana', 'cherry']
Orange
['Orange', 'kiwi', 'cherry', 'banana', 'apple']
  1. Adding the list

Code
  
    ## Adding Method List
#input-1 
fruits.append("pomegranate") 
print(fruits)

#Output-1
['apple', 'banana', 'cherry', 'kiwi', 'Orange', 'pomegranate']


##insert method
#input-2
fruits.insert(1,"watermelon")
print(fruits)

#output-2
['apple', 'watermelon', 'banana', 'cherry', 'kiwi', 'Orange', 'pomegranate']

##Remove method - particular element
#input-3
fruits.remove("banana")
print(fruits)

#output-3
['apple', 'watermelon', 'watermelon', 'cherry', 'kiwi', 'Orange', 'pomegranate']

##Remove Method - last element
#input-4
popped_fruits=fruits.pop()
print("The Removed Element is", popped_fruits)
print(fruits)

#output-5
The Removed Element is pomegranate
['apple', 'watermelon', 'watermelon', 'cherry', 'kiwi', 'Orange']
  1. Modifying the List

Code
  
    #input
fruits=["apple","banana","cherry","kiwi","Orange"]
fruits[1]="watermelon"
print(fruits)

#Output
['apple', 'watermelon', 'cherry', 'kiwi', 'Orange']
  1. Checking the Index Method of List

Code
  
    #input
fruits=["apple", "banana", "cherry", "kiwi", "Orange"]
index=fruits.index("cherry")
print(index)

#output
3
  1. Sorts the list in ascending order

Code
  
    #input
fruits = ['apple', 'watermelon', 'banana', 'cherry', 'kiwi', 'orange']
fruits.sort()
print(fruits)

#output
['apple', 'banana', 'cherry', 'kiwi', 'orange', 'watermelon']
  1. Reverse the List

Code
  
    #input
fruits = ['apple', 'watermelon', 'banana', 'cherry', 'kiwi', 'orange']
fruits.reverse()
print(fruits[::-1])
print(fruits)

#output
['orange', 'kiwi', 'cherry', 'banana', 'watermelon', 'apple']
  1. Clear the List

Code
  
    #input
fruits = ['apple', 'watermelon', 'banana', 'cherry', 'kiwi', 'orange']
fruits.clear() 
print(fruits)

#output
[ ]
  1. Iterating the List using For loop

Code
  
    #input
Numbers_List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in Numbers_List:
    print(number)

#output
1
2
3
4
5
6
7
8
9
10
  1. Iterating the List with index using For loop - enumerate is the special key word for identify the Index

Code
  
    #input
Numbers_List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for index,number in enumerate(Numbers_List):
    print(index,number)

#output
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

14 . List Comprehension Method

Code
  
    ##Normal Method
#Input-1
list=[]
for x in range(10):
    list.append(x**2)
print(list)

#output-1
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

## List Comprehension Method
#input-2
[x**2 for x in range(10)]

#output-3
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

## List Comprehension Method
#input-2
[x for x in range(0,20,2)]

#output-3
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
  1. Merging Two Lists Using extend( ) keyword in Python

Code
  
    #input
fruits1 = ["apple", "banana", "cherry", "kiwi", "Orange"]
fruits2 = ["Guva", "pomegranate"]
fruits1.extend(fruits2)
print(fruits1)

#output
['apple', 'banana', 'cherry', 'kiwi', 'Orange', 'Guva', 'pomegranate']

#Note : The extend() method adds each element of fruits2 to the end of fruits1.
  1. Merging Two Lists Using Zip ( ) Keyword in Python

Code
  
    #input
fruits = ["apple", "banana", "cherry", "kiwi", "Orange"]
price = [180, 90, 170, 160, 110]

fruit_prices = list(zip(fruits, price))
print(fruit_prices)

#output
[('apple', 180), ('banana', 90), ('cherry', 170), ('kiwi', 160), ('Orange', 110)]

Now, We have covered list operations with various examples and methods. In the next article, we will dive into tuple operations in Python.