python elements:
*keywords
*symbols
*topics
*modules
variables:
* case sensitive
*start with a letter not a number
*long names are ok
types and operators:
* int
*float
*long
*complex a = (3 + 4j) #type (a)
*airthmetic operators
*assignment operators
*comparison operators: < > = >= == !=
*some have overload abilities +
casts:* int()
*float()
*long()
*hex() #string representation
*oct() #string rep
*str() # for printig numbers and strings
Built in constants:
*Trus
*False
Indenting counts:
* indent 4 spaces or a tab
*: at end of line indicates start of code block
*requires next line to be indented
*
*code block ends with an outdent
*check your indents!
*
Program structure
*loops
*conditionals, controls
*functions
*
IDLE:
* syntex color coded
*statement completion
*written in python tkinter gui module???
*
dir()
*gives you a list of the variables in the interpreter
*
*
Comments:
in line text after # is ignored
text within triple quotes
""" this is a multiline
comment
indentation needs to conform """
STRINGS:
sequence of characters such as s = "abcdefg"
indexed wih [] starting at [0]
s[-1] refers to last character in string
* negative indexing starts at last character
*use s[p:q] for string slicing
*s[3:] evaluated as "defg"
*s[:3] eevaluated as "abc" (up to but not 3)
*s[1:-2] evaluated as "bcde" (up to but not including -2)
VERY HANDY!
STR Concatenation
* first = "John"
*last = "Cleese"
*
*full = first + " " + last
*sp = " "
*full = first + sp + last
"+" operator is Operand Aware
*if both sides are strings, python will concatenate
*if they are numbrs it will add them
*if you mix: you get an error
*
*
*
*
immutable string:
* if you want to change a character in a string, assign the new string to the same variable
*
*
automatic memory management
*21st century!
*
*
printing:
_ = " " # string variable names _
print ("hello" + _ + "there")
pi = 3.14159
print ('The answer is ' + str(pi)) # cast float to string in order to print
Conditionals
elif = else if
if a>0:
elif a<0:
else:
rint column titles
right align degree values
limit radians to 7 character
str is a class
*see documentation + read up!
*
*
math module
*import math
*
math.sqrt(x)
math.sin(x)
math.cos(x)
use math.pi for defined constant
use math.cos(radian) to compute cosine
print cosine in 3rd column
*align cosine to decimal point
*
import sys
sys.path.append("/u/efeibush/spline")
import cubic.py # import your own code
Collections -> arrays in other languages
List [] # ordered sequence of stuff
*looks like an array in c
*indexed from [0]
*last index is length [-1]
*
*it is though a class
*with its own methods
*.append()
*.sort()
*
*magic slice operator :
*magic iter() function actually __iter__()
tuple () # n-tuple, immutable
Declare a List
*x = [14,23, 34 ,42, 50, 59]
*x.append(66)
*
List methods
*append
*extend
*insert
*remove
*sort
*reverse
*index
*count
*
*cList = aList + bList #concatenate lists
*
ragne() function returns a list
range(stop)
range(start, stop)
range(start,stop,incr)
returns list of integres up to but not including stop
built in function
dir(__builtins__)
looping with range
*for in
*for i in range(10):
*for i in dayList:
*
numpy.linspace
import numpy
a = numpy.linspace(1., 2.,11)
for i in a:
look up list techniques
store a list of radians and a list of cosines
print the lists
use a range loop instead of while
import numpy (if you get into large arrays)
ndarray class
princeton.edu/~freibush
delete keyword
del a[3]
unpack a list into variables
name = ["abe", "lincoln"]
*
first, last = name
command line arguments
*import sys
*print (sys.argv)
*sys,argv is a list
*sys.argv[0] has the name of the python lfile
*
*
shell scripting
much better text handling than csh and bsh and shell independent
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*