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 :
mylist = ["Lion", "Tiger", "Elephant"] print(mylist[1])Output :
mylist = ["Lion", "Tiger", "Elephant"] print(mylist[-1])Output :
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"] for x in mylist: print(x)Output :
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"] print(mylist[2:5])Output :
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"] print(mylist[:3])Output :
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"] print(mylist[3:])Output :
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"] print(mylist[-4:-2])Output :
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"] print(mylist) #Before the value change mylist[1] = "Panda" print(mylist) #After the value changeOutput :
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"] print(mylist) #Before adding a new element mylist.append("Panda") print(mylist) #After adding a new elementOutput :
mylist = ["Lion", "Tiger", "Elephant","Zebra","Kangaroo"] print(len(mylist))Output :
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 listOutput :
n1 = int(input("Enter No. of Rows : "))n2 = int(input("Enter No. of Columns : "))print("For Matrix 1 : ")m1 = []i=0while i<n1:j=0tlist=[]while j<n2:n = int(input("Enter value : "))tlist.append(n)j = j+1m1.append(tlist)i = i+1print(m1)print("For Matrix 2 : ")m2 = []i=0while i<n1:j=0tlist=[]while j<n2:n = int(input("Enter value : "))tlist.append(n)j = j+1m2.append(tlist)i = i+1print(m2)m3 = list()print("Sum of Matrix M1 and M2")i=0while i<n1:j=0tlist=[]while j<n2:n = m1[i][j] + m2[i][j]tlist.append(n)j = j+1m3.append(tlist)i = i+1print(m3)m4 = list()print("Multiplication of Matrix M1 and M2")i=0
while i<n1:j=0tlist=[]while j<n2:k=0n=0while k<n2:n += m1[i][k] * m2[k][j]k = k+1tlist.append(n)j = j+1m4.append(tlist)i = i+1print(m4)
import numpy as nparr1 = np.array([[1, 2],[3, 4]])arr2 = np.array([[1, 2],[3, 4]])
arr_result = np.dot(arr1, arr2)print(arr_result)
import tensorflow as tfA1 = tf.constant([[1, 2],[3, 4]])B1 = tf.constant([[1, 2],[3, 4]])C1 = tf.matmul(A1,B1)print(C1)