Ipcswithpython.files.wordpress.com



Examples of Lists:>>> L=[10,20,30]>>> L[0]10>>> L[1]20>>> L[2]30Examples of Tuples:>>> T=(10,20,30)>>> T[0]10>>> T[1]20>>> T[2]30Use of Type Function on List and Tuple:>>> type(L)<class 'list'>>>> type(T)<class 'tuple'>Examples to show Lists values may be heterogeneous and Tuple values may be homogeneous:>>> L=[10,2.5,'hello'] >>> type(L) <class 'list'>>>> L[0] 10>>> L[1] 2.5>>> L[2] 'hello'>>> type(L[0]) <class 'int'>>>> type(L[1]) <class 'float'>>>> type(L[2]) <class 'str'>>>> T=(10,2.5,'hello') >>> T[0] 10>>> type(T[0]) <class 'int'>>>> T[1] 2.5>>> type(T[1]) <class 'float'>>>> T[2] 'hello'>>> type(T[2]) <class 'str'>Examples to show List size is mutable and Tuple size is immutable:>>> L.append(56) >>> L [10, 2.5, 'hello', 56]>>> T.append(56) Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> T.append(56)AttributeError: 'tuple' object has no attribute 'append'Examples to show List values are mutable and Tuple values are immutable:>>> L[0]="bye" >>> L ['bye', 2.5, 'hello', 56]>>> T (10, 2.5, 'hello')>>> T[0]="bye" Traceback (most recent call last): File "<pyshell#34>", line 1, in <module> T[0]="bye"TypeError: 'tuple' object does not support item assignmentIntroduction of Arrays: 1- D Array Creation and its indices and other examples:>>> import numpy as np >>> A=np.array([10,20,30]) >>> type(A) <class 'numpy.ndarray'>>>> A[0] 10>>> A[1] 20>>> A[2] 30>>> A=np.array([10,2.5,'Hello']) >>> A[0] '10'>>> A[1] '2.5'>>> A[2] 'Hello'Example to show the type of array using type () function:>>> A=np.array([10,20,30]) >>> type(A[0]) <class 'numpy.int32'>Example to show homogeneous type of data in an array:>>> type(A[1]) <class 'numpy.int32'>>>> type(A[2]) <class 'numpy.int32'>>>> A=np.array([10,2.5,'Hello']) >>> type(A[0]) <class 'numpy.str_'>>>> type(A[1]) <class 'numpy.str_'>>>> type(A[2]) <class 'numpy.str_'>Example to show that size of array is immutable:>>> A.append('23') Traceback (most recent call last): File "<pyshell#53>", line 1, in <module> A.append('23')AttributeError: 'numpy.ndarray' object has no attribute 'append'>>> A array(['10', '2.5', 'Hello'], dtype='<U32')>>> A[0]='2.3' >>> A array(['2.3', '2.5', 'Hello'], dtype='<U32')Examples to show use of arithmetic operators over two lists:>>> L1=[10,20,30] >>> L2=[1,2,3] >>> L1+L2 [10, 20, 30, 1, 2, 3]>>> L=L1+L2 >>> L [10, 20, 30, 1, 2, 3]>>> L=L1-L2 Traceback (most recent call last): File "<pyshell#63>", line 1, in <module> L=L1-L2TypeError: unsupported operand type(s) for -: 'list' and 'list'>>> L=L1*L2 Traceback (most recent call last): File "<pyshell#64>", line 1, in <module> L=L1*L2TypeError: can't multiply sequence by non-int of type 'list'>>> L=L1/L2 Traceback (most recent call last): File "<pyshell#65>", line 1, in <module> L=L1/L2TypeError: unsupported operand type(s) for /: 'list' and 'list'>>> L=L%L2 Traceback (most recent call last): File "<pyshell#66>", line 1, in <module> L=L%L2TypeError: unsupported operand type(s) for %: 'list' and 'list'>>> L=L1%L2 Traceback (most recent call last): File "<pyshell#67>", line 1, in <module> L=L1%L2TypeError: unsupported operand type(s) for %: 'list' and 'list'From the above mentioned examples we came to know that only + operator can be used over two lists in arithmetic operators.Examples to show the use of arithmetic operators over two Ararays:>>> A1=np.array([10,20,30]) >>> A2=np.array([1,2,3]) >>> A=A1+A2 >>> A array([11, 22, 33])>>> A=A1*A2 >>> A array([10, 40, 90])>>> A=A1/A2 >>> A array([10., 10., 10.])>>> A=A1%A2 >>> A array([0, 0, 0], dtype=int32)>>> A=A1-A2 >>> A array([ 9, 18, 27])>>> A1 array([10, 20, 30])>>> A2 array([1, 2, 3])Examples to show the use of numpy functions to do the arithmetic operations over two arrays:>>> A=np.add(A1,A2) >>> A array([11, 22, 33])>>> A=np.subtract(A1,A2) >>> A array([ 9, 18, 27])>>> A=np.multiply(A1,A2) >>> A array([10, 40, 90])>>> A=np.divide(A1,A2) >>> A array([10., 10., 10.])>>> A=np.remainder(A1,A2) >>> A array([0, 0, 0], dtype=int32)More Functions used to create arrays :Use of arrange () function:>>> A=np.arange(9) # arrange will generate [0,1,2,3,4,5,6,7,8]>>> A array([0, 1, 2, 3, 4, 5, 6, 7, 8])>>> A[0] 0>>> A[1] 1>>> A=np.arange(5,10) # arrange will generate [5,6,7,8,9]>>> A array([5, 6, 7, 8, 9])>>> A=np.arange(5,10,2) # arrange will generate [5,7,9] >>> A array([5, 7, 9])2. Use of linspace() function:>>> A=np.linspace(1,10,10) # here 1 – starting range value, 10- stop range value , 10 – total no of equally spaced elements >>> A array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])>>> A=np.linspace(1,10,20) # here 1-starting range value, 10-stop range value, 20- total no of equally spaced elements>>> A array([ 1. , 1.47368421, 1.94736842, 2.42105263, 2.89473684, 3.36842105, 3.84210526, 4.31578947, 4.78947368, 5.26315789, 5.73684211, 6.21052632, 6.68421053, 7.15789474, 7.63157895, 8.10526316, 8.57894737, 9.05263158, 9.52631579, 10. ])>>> A=np.linspace(1,10,30) >>> A array([ 1. , 1.31034483, 1.62068966, 1.93103448, 2.24137931, 2.55172414, 2.86206897, 3.17241379, 3.48275862, 3.79310345, 4.10344828, 4.4137931 , 4.72413793, 5.03448276, 5.34482759, 5.65517241, 5.96551724, 6.27586207, 6.5862069 , 6.89655172, 7.20689655, 7.51724138, 7.82758621, 8.13793103, 8.44827586, 8.75862069, 9.06896552, 9.37931034, 9.68965517, 10. ])>>> A=np.linspace(1,2,30) >>> A array([1. , 1.03448276, 1.06896552, 1.10344828, 1.13793103, 1.17241379, 1.20689655, 1.24137931, 1.27586207, 1.31034483, 1.34482759, 1.37931034, 1.4137931 , 1.44827586, 1.48275862, 1.51724138, 1.55172414, 1.5862069 , 1.62068966, 1.65517241, 1.68965517, 1.72413793, 1.75862069, 1.79310345, 1.82758621, 1.86206897, 1.89655172, 1.93103448, 1.96551724, 2. ])3. Use of ones() function: >>> A=np.ones(5) >>> A array([1., 1., 1., 1., 1.])>>> A=np.ones(5,dtype=np.int32) >>> A array([1, 1, 1, 1, 1])4. use of zeros() function:>>> A=np.zeros(5) >>> A array([0., 0., 0., 0., 0.])>>> A=np.zeros(5,dtype=np.int32) >>> A array([0, 0, 0, 0, 0])5. use of empty function():>>> A=np.empty(7) >>> A array([9.36532727e-280, 3.19125232e-241, 4.75336375e+180, 4.97455198e+180, 1.29750418e-311, 1.08646184e-311, 3.63964718e-310])5. use of random() function:>>> A=np.random.random(4) >>> A array([0.5053949 , 0.7524996 , 0.81245266, 0.57652249])>>> A=np.random.random(4) >>> A array([0.99944258, 0.6626761 , 0.19464538, 0.5944887 ])Examples of Array Slicing Example when the indices are +ve:>>> A=np.arange(10,40,3) >>> A array([10, 13, 16, 19, 22, 25, 28, 31, 34, 37])>>> A[0] # this accessing element, not array slicing10>>> A[4] # this is accessing element , not array slicing22>>> A[0:10:1] # this is array slicing, here 0- starting index, 10- stop position means index will be position -1, 1- increment in each steparray([10, 13, 16, 19, 22, 25, 28, 31, 34, 37])More examples of array slicing on +ve indices:>>> A[0:10:2] array([10, 16, 22, 28, 34])>>> A[2:10:2] array([16, 22, 28, 34])>>> A[1:10:2] array([13, 19, 25, 31, 37])>>> A[::] array([10, 13, 16, 19, 22, 25, 28, 31, 34, 37])>>> A[::2] array([10, 16, 22, 28, 34])Examples of array slicing when the indices are –ve:>>> A[::-1] #-1 denoted the array will be accessed from reverse order. array([37, 34, 31, 28, 25, 22, 19, 16, 13, 10])>>> A[-1:-5:-1] array([37, 34, 31, 28])>>> A[-1:-5:1] array([], dtype=int32)>>> A[-5:-1:1] array([25, 28, 31, 34])>>> A array([10, 13, 16, 19, 22, 25, 28, 31, 34, 37]) ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download