Basic Operations<\/strong><\/p>\nArithmetic operations when applied on NumPy arrays, they are implemented element-wise.<\/p>\n
>>> a=np . array ( [ 20 , 30 , 40 , 50 ] )\r\n>>> b=np . arange ( 4 ) \r\n>>> c=a - b \r\n>>> c\r\narray ( [ 20 , 29 , 38 , 47 ] )\r\n>>> b**2\r\narray ( [ 0 , 1 , 4 , 9 ] )\r\n>>> a<39\r\narray ( [ True , True , False , False ] , dtype=bool )<\/pre>\nThe product operator * operates element-wise in NumPy arrays. The matrix product can be performed using the dot ( ) function or creating matrix objects (refer section 7.8).<\/p>\n
>>> a=np . array ( [ [ 1 , 1 ] ,\r\n. . . [ 0 , 1 ] ] )\r\n>>> b=np . array ( [ [ 2 , 0 ] ,\r\n. . . [ 3 , 4 ] ] )\r\n>>> a*b \r\narray ( [ [ 2 , 0 ] ,\r\n[ 0 , 4 ] ] )\r\n>>> np . dot ( a , b ) \r\narray ( [ [ 5 , 4 ] ,\r\n[ 3 , 4 ] ] )<\/pre>\nSome operations, such as +=, *=, etc., modifies an existing array, rather than creating a new array.<\/p>\n
>>> a=np . array ( [ [ 1 , 2 ] , # a is integer type\r\n. . . [ 3 , 4 ] ] )\r\n>>> b=np . array ( [ [ 1 . , 2 . ] , # b is float type\r\n. . . [ 3 . , 4 . ] ] )\r\n>>> a*=2 \r\n>>> a\r\narray ( [ [ 2 , 4 ] ,\r\n[ 6 , 8 ] ] )\r\n>>> b+=a \r\n>>> b\r\narray ( [ [ 3 . , 6 . ] ,\r\n[ 9 . , 12 . ] ] )\r\n>>> a+=b # b is converted to integer type\r\n>>> a\r\narray ( [ [ 5 , 10 ] ,\r\n[ 15 , 20 ] ] )<\/pre>\nWhen operating with arrays of different types, the type of the resulting array corresponds to the more general or precise one (a behavior known as “upcasting”).<\/p>\n
>>> a=np . array ( [ 1 . 1 , 2 . 2 , 3 . 3 ] )\r\n>>> a . dtype . name\r\n' float64 ' \r\n>>> b=np . array ( [ 4 , 5 , 6 ] )\r\n>>> b . dtype . name ' int32 '\r\n>>> c=a+b \r\n>>> c\r\narray 0( [ 5 . 1 , 7 . 2 , 9 . 3 ] )\r\n>>> c . dtype . name ' float64 '<\/pre>\nMany unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the ndarray class .<\/p>\n
>>> a=np . array ( [ [5 , 8 ] ,\r\n. . . [ 3 , 6 ] ] )\r\n>>> a . sum ( )\r\n22\r\n>>> a . min ( )\r\n3\r\n>>> a . max ( )\r\n8<\/pre>\nBy default, these operations apply to the array as though it were a list of numbers, regardless of its shape. However, by specifying the axis parameter you can apply an operation along the specified axis of an array:<\/p>\n
>>> a=np . arange ( 12 ) . reshape ( 3 , 4 )\r\n>>> a\r\narray ( [ [ 0 , 1 , 2 , 3 ] ,\r\n[ 4 , 5 , 6 , 7 ] ,\r\n[ 8 , 9 , 10 , 11 ] ] )\r\n>>> a.sum(axis=0) # Sum of each column\r\narray ( [12, 15, 18, 21] )\r\n>>> a.min(axis=1) # Minimum of each now\r\narray ( [ 0 , 4 , 8 ] )\r\n>>> a . cumsum ( axis=1 ) # Cumulative sum along each now\r\narray ( [ [ 0 , 1 , 3 , 6 ] ,\r\n[ 4 , 9 , 15 , 22 ] ,\r\n[ 8 , 17 , 27 , 38 ] ] )<\/pre>\n","protected":false},"excerpt":{"rendered":"In this Page, We are Providing Python Programming \u2013 Basic Operations. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Basic Operations Basic Operations Arithmetic operations when applied on NumPy arrays, they are implemented element-wise. >>> a=np . array ( [ 20 , 30 , 40 , 50 ] …<\/p>\n
Python Programming \u2013 Basic Operations<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming \u2013 Basic Operations - Python Programs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n