#Week 2 Examples import numpy as np #Useful numpy functions: a = np.arange(1,101) # counting numbers 1-100 b = np.arange(1,101,2) #same but every second number c = np.linspace(0,1,100) #array of 100 subdivisions between 0 and 1 d = np.array([1,2,3,...]) #manually defined array f = np.loadtxt('Filename.txt') #Syntax for loading a file g = 3*np.exp(7*a) #defining an exponential h = np.sin(a) #defining a sine i = np.zeros(20) #array of 20 zeros j = np.ones(20) #array of 20 ones array_of_25s = j*25 #array of 20 25's list1 = [1.,2,3,4,5,6,76] #a random list arr_list = np.array(list1) #casting list as array print np.mean(b) print np.max(a) print np.min(f) multi = np.concatenate((a,b,c)) #join arrays end to end split = np.split(a,[15,25,40]) #split arrays at indicated indices #Commenting code: #Single line comments can be written by a "#" #Anything on the line AFTER the hash will not be read x = np.arange(5) #x will be created, its just these words that arent read #If you need a comment to last several lines, try this: ''' Triple quotes allow us to write as many lines as we want in a comment and none of it will be read by the compilor. Further, if you have a block of code you want to "turn off" for debugging purposes, you can enclose it in triple quotes ''' ''' Commenting is more than just defining each line of code with a description of what it does (which alone would be actually be unhelpful and time consuming). Commenting starts with variable names. Like above, the names aa, b, c, gg, arr_1, etc. are all terrible names for variables- they tell you nothing about what the variable is! Better names are orbital_periods, planet_masses, time, stack, etc. The first step in decoding your code later or sharing it is making sure the variable names make sense and can be followed by other people. Sometimes commenting a variable declaration is warranted (if it's really important), but usually you reserve comments for things like loops that do a specific task, or documenting the functions you write.