# -*- coding: utf-8 -*- """ Created on Sun Jun 8 20:38:04 2014 @author: Babar """ # Solutions to exercises for Tutorial # 2 import numpy as np #Evaluate the square root of 10.1*10.2 + 15.3*15.4 np.sqrt(10.1*10.2 + 15.3*15.4) #What happens when you add two string variables together? print "What happens when "+"you add two string variables together?" #Create a numpy integer array of values from 0 to 100, incrementing in steps of 2 res = np.arange(0, 100, 2) print np.min(res) print np.max(res) #Create a numpy floating-point array of 101 elements that range in value from 0.0 to 1.0 res = np.arange(101, dtype=np.float) / 100. print np.min(res) print np.max(res) print len(res) #Find the minimum and maximum value of the resulting array in previous exercise. #See above #Find the sum total of all elements of the resulting array. print np.sum(res) #What is the length of the array 'a' created by the following numpy command: # a = np.zeros( int(np.sqrt(np.pi*100)+3.) )? a = np.zeros( int(np.sqrt(np.pi*100)+3.) ) print len(a) #What is the data type of the array 'a' in the previous exercise? type(a) a.__class__ #Create an array 'x' of size 20 with floating point values between 1 and 20. #Create a second array 'y' of size 20 with floating point values between 1 and 20. #These represent the (x,y) coordinate values of 20 objects on a Cartesian grid. #Calculate the distance between a point at (2.5,7.8) and all (x,y) pair objects, #and store the resulting distance values in a variable 'dist'. x = np.arange(1, 20, dtype=np.float) y = np.arange(1, 20, dtype=np.float) # Note: You can either have an array of size 21 with range 1...20, or you can # have an array of size 20 with range 1...19 #Create a 2-Dimensional numpy array of size 15 columns X 17 rows filled with zeros. #Set the middle column to 10. Set the last five rows to 20. nrows = 17 ncols = 15 myArray = np.zeros((nrows, ncols)) print myArray.shape # # middle column has index value of 7 # 0...6 (7 elements before it), and # 8...14 (7 elements after it) # Colon (:) means all # [:,7] means all rows and column = 7 myArray[:,7] = 10 # last 5 rows are referenced as -5: myArray[-5:,:] = 20 # Of course, this is also valid myArray[12:17,:] = 20