""" Produced by Andrew Jason Penner Sept 31 2007 """ #!/usr/bin/python """ This is a python comment line it is always begun with a set of three " marks. It is likewise ended with three " marks Also note that the general convention is to leave the comment/uncomment marks at the left of the screen, but this is not necessary, except for readability """ """ Printing a line in python has a definite C or C++ feel to it """ print 'This is the way python prints to screen' print 'To print a few lines one must include a \\n','\n','and it will act as a carriage return','\n' """ We can start with the main data types that python can handle float (single precision) integer (again single precision) long (double precision floats and integer) complex (x+iy can be floating point or integer, however calculations done on complex numbers will automatically type-cast as a float) The variables are automatically defined appropriately """ str1='Hello' str2='World' R=1.6 D=3.1415926535897932384 I=2 comI=1+2j # they use j instead of i, likely created by an engineer comR=0.5+1.5j print 'The values used in this example are\n' print 'R =',R print 'D =',D print 'I =',I print 'comI =',comI print 'comR =',comR print 'str1 =',str1 print 'str2 =',str2,'\n' """ We can extract the real and imaginary part of a complex number quite easily """ print 'The real part of comI is',comI.real print 'The imaginary part of comI is', comI.imag print 'The real part of comR is',comR.real print 'The imaginary part of comR is', comR.imag """ We can also convert between floats and integer, and vice-versa. This is usually known as typecasting """ print 'The integer form of the variable R is', int(R) print 'The integer form of the variable D is', int(D) print 'The floating point form of the variable I is', float(I) """ Handling complex numbers is different, since a type-casting will not work """ #float(comI) # Returns errors and if uncommented will crash the program """ Note that the output of abs(comI) would be 2 if it did not autotypecast """ print 'The magnitude of the complex number comI is',abs(comI) # sqrt(comI.real^2 + comI.imag^2) print 'The magnitude of the complex number comR is',abs(comR),'\n' """ Also basic mathematics is as expected in this program """ prod=R*D quot=D/I sum=R+D diff=R-D neg=-R c=complex(R,D) cc=c.conjugate() divmod(D,R) print 'This is the product of R and D', prod print 'This is the quotient of D and I', quot print 'This is the sum of D and R', sum print 'This is the difference of R and D', diff print 'This is the negative of R', neg print 'This is a complex number of the form R+Dj', c print 'This is the conjugate of the last complex number',cc print 'This is the divmod of D and R', divmod(D,R) """ There are 2 ways to take a value to a power. The first listed here is the typical C or C++ method using a function called pow, the first argument is the base, the second is the power. The second method is more along the lines of FORTRAN where I suppose the laguage was developed before the invention of the ^ symbol in ASCII. (Speculation) power1=pow(R,I) power2=R**I """ power1=pow(R,I) power2=R**I print 'This is R to the power of I using the C style power function', power1 print 'This is R to the power of I using the FORTRAN style power function', power2 """ The above is another example if auto typecasting, otherwise div would have returned 1 """ rem=D%I flr1=D//I flr2=R//I print '\nSome less common mathematical function are\n' print 'Remainder x%y which for x=D, and y=I', rem print 'Floored quotient of x and y, for x=D, and y=I',flr1 print 'Floored quotient of x and y, for x=R, and y=I',flr2 """ Note that the floored quotient is also known as integer division In this method the result is not likely of type integer, but the display on the screen is. """ rnd=round(quot,2) print '\nThis is the rounding function for python as applied to the dividend',rnd """ This pretty much sums up all the possible functions that are inherent to python for number manipulation. You of course are free to define your own mathematical modules, but we will cross that bridge later """ """ Now we move onto strings. This is a personal favourite, I am not sure why """ con=str1+str2 con2=str1+' '+str2 multi=str1*3 print 'We can append strings together by simply adding them like numbers', con print 'We can also add undefined strings in on the fly by instering them between \' marks', con2 print 'Another interesting feature is the way to concatonate multiple strings',multi """ Remember that there are special characters in programming languages which are usually interpreted before execution such as quotes, brackets, and slash marks, to display these characters we need to escape them the same as you did in gnuplot and other scripts. """ """ To print usage messages one can do the following """ print """ Usage: This is a usage statement. It can contain multiple lines It can also produce special characters ',\," without needing to be escaped """ """ Another useful feature of strings is the ability to strip out portions of a string """ print "This is the middle three characters in str1",str1[1:4] print "This is the first two characters in str1",str1[:2] print "This is everything but the first two characters in str1",str1[2:] """ Strings in python cannot be altered and attempt to perform """ #str1[2] = 'f' # Returns an error if uncommented and executed """ Indicies can be negative, the first character in a string has index 0, and thus the last character can be accessed using index -1 """ print "This is the last character in the string str1",str1[-1] print "This is the number of characters in str1", len(str1) """ There is a lot more one can do with strings but it is not worth mentioning on a first pass at python """ print '\n' """ Finally onto lists. Lists have a similar behaviour as strings. If you are familiar with C, FORTRAN, etc... you can think of lists as arrays. """ list = ['string',123,1.444] print 'This is the content of my list ',list print 'This is the last element of the list',list[-1] print 'This is the last two elements of the list',list[1:] print 'This is the first element of the list repeated 3 time',list[0]*3 """ We can change the contents of a list rather easily """ list[1] = list[1] - 23 print 'The second element of the old list has been changed and now the list is',list list[1:1] = ['added','between','element','0','and','1'] print 'Now I have added an element to the list',list list[1:7] = [] print 'Now I have removed that addendum',list list[:0] = list print 'Now I add a copy of the list to the beginning of the list',list print 'The length of the list is now',len(list),'I know this by using the len() command' list1 = [1,2,3] list2 = [list1,5,6] list[:] = [] print 'Now I cleared the list',list print "I have now created two more lists for the sake of examples",list1,list2 print "Note that the first element of list2 is actually a list, so we have created a list of lists (a nested list)" print "calling a particular element of the nested list we use a double index method", list2[0][2] print "However if you get the indicies messed up it will return an error" #list2[1][2] # This will return an error if uncommented and executed """ As before there is a lot more than can be done to manipulate the lists, but this will give you a good idea as to what you can do with them """ """ This concludes the first part of the examples leading you to become a python programmer """