Python Programming

Tutorial No. 7 : Operations : List Data Type

About List Data Type and Operations with List

A list is a collection which is ordered and changeable. In Python, lists are always written within square brackets.
1. Create a Simple List : Following statements will create a mylist and will display the elements of list

mylist = ["Lion", "Tiger", "Elephant"]
print(mylist)
Output :
['Lion', 'Tiger', 'Elephant']

2. Access the List : You access the list item by specifying the index number.Following code will display the Tiger as List elements are referred from index 0.
mylist = ["Lion", "Tiger", "Elephant"]
print(mylist[1])
Output :
Tiger

3. Negative Indexing : You access the list item by specifying the negative index number.Following code will display the Elephant as List elements are referred from right side and first element value is Elephant.
mylist = ["Lion", "Tiger", "Elephant"]
print(mylist[-1])
Output :
Elephant

4. Access all the elements of the list :
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"]
for x in mylist:
  print(x)
Output :
Lion
Tiger
Elephant
Zebra
Kangaroo

5. Range as Indexes : You can specify a range as indexes by specifying the starting element and where ending element. When specifying a range, the return value will be a new list with the specified items.
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"]
print(mylist[2:5])
Output :
['Elephant', 'Zebra', 'Kangaroo']
In above example,it will start from index 2 ie from Elephant and will end with index 4 and display ['Elephant', 'Zebra', 'Kangaroo'].If you skip the start range then it will start from index 0 and will end with end value-1 and if you skip the end range then it will start from the index of start range and will end with last element.
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"]
print(mylist[:3])
Output :
['Lion', 'Tiger', 'Elephant']
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"]
print(mylist[3:])
Output :
['Zebra', 'Kangaroo']
You can also specify the negative indexes if you want to start the from end of list
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"]
print(mylist[-4:-2])
Output :
['Tiger', 'Elephant']

6. Change the value of element of the list : You can change the value of any element of the list by specifying the index value. If I want to change the value of second element to Panda.I can do with the help of following code.
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"]
print(mylist) #Before the value change
mylist[1] = "Panda"
print(mylist) #After the value change
Output :
['Lion', 'Tiger', 'Elephant', 'Zebra', 'Kangaroo']
['Lion', 'Panda', 'Elephant', 'Zebra', 'Kangaroo']

7. Insert new element in the list : You can add new element in the list by using append() method. In the following code Panda willbe added as a new element of the list
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"]
print(mylist) #Before adding  a new element
mylist.append("Panda")
print(mylist) #After adding a new element
Output :
['Lion', 'Tiger', 'Elephant', 'Zebra', 'Kangaroo']
['Lion', 'Tiger', 'Elephant', 'Zebra', 'Kangaroo', 'Panda']

8. No. of Elements in the List : You can count total no. elements available in List by uisng len() method
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"]
print(len(mylist))
Output :
5

9. Delete from the list : You can delete particual element or all the elements from the list.
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"]
print(mylist) #Before deleting Lion from the list
mylist.remove("Lion")
print(mylist) #After deleting Lion from the list
mylist.pop(2)
print(mylist) #After deleting Third  element from the list
del mylist[1]
print(mylist) #After deleting second  element from the list
mylist.clear()
print(mylist) #After removing all the elements from the list
del mylist
print(mylist) #After removing all the elements and also deleting the list
Output :
['Lion', 'Tiger', 'Elephant', 'Zebra', 'Kangaroo']
['Tiger', 'Elephant', 'Zebra', 'Kangaroo']
['Tiger', 'Elephant', 'Kangaroo']
['Tiger', 'Kangaroo']
[]
Traceback (most recent call last):
File "c:/Users/Dhaval Mehta/Desktop/temp.py", line 12, in <module>
print(mylist) #After removing all the elements and also deleting the list
NameError: name 'mylist' is not defined

Matrix Addition and Multiplication

n1 = int(input("Enter No. of Rows  : "))
n2 = int(input("Enter No. of Columns : "))
print("For Matrix 1 : ")
m1 = []
i=0
while i<n1:
    j=0
    tlist=[]
    while j<n2:
        n = int(input("Enter value  : "))
        tlist.append(n)
        j = j+1
    m1.append(tlist)
    i = i+1
print(m1)
print("For Matrix 2 : ")
m2 = []
i=0
while i<n1:
    j=0
    tlist=[]
    while j<n2:
        n = int(input("Enter value  : "))
        tlist.append(n)
        j = j+1
    m2.append(tlist)
    i = i+1
print(m2)
m3 = list()
print("Sum of Matrix M1  and M2")
i=0
while i<n1:
    j=0
    tlist=[]
    while j<n2:
        n = m1[i][j] + m2[i][j]
        tlist.append(n)
        j = j+1
    m3.append(tlist)
    i = i+1
print(m3)
m4 = list()
print("Multiplication of Matrix M1  and M2")
i=0

while i<n1:
    j=0
    tlist=[]
    while j<n2:
         k=0
         n=0
         while k<n2:
            n +m1[i][k] * m2[k][j]
            
            k = k+1
         tlist.append(n)
         j = j+1
    m4.append(tlist)
    i = i+1
print(m4)

Matrix Addition and Multiplication suing Numpy and Tensorflow

import numpy as np
arr1 = np.array([[12],
                 [34]])
arr2 = np.array([[12],
                 [34]])

arr_result = np.dot(arr1, arr2)
print(arr_result)

import tensorflow as tf
A1 = tf.constant([[12],
                 [34]])
B1 = tf.constant([[12],
                 [34]])
C1 = tf.matmul(A1,B1)
print(C1)