print "Hello World!"
print 2 + 2
2 + 2
print 2.1 + 2
2.1 + 2 == 4.0999999999999996
2L
2L + 2
2L/2
2L/2.0
complex(1,2)
1+2j
1 + 2j - 2j
(3.0*10.0 - 25.0)/5.0
print 3.085e18*1e6 # this is a Megaparsec in units of cm!
t = 1.0 # declare a variable t (time)
accel = 9.8 # acceleration in units of m/s^2
# distance travelled in time t seconds is 1/2 a*t**2
dist = 0.5*accel*t*t
print dist # this is the distance in meters
dist1 = accel*(t**2)/2
print dist1
dist2 = 0.5*accel*pow(t,2)
print dist2
Some Mathy Operators
print 6 / 5 ; print 9 / 5 # integer division returns the floor
6 % 5 # mod operator
1 << 2 ## shift: move the number 1 by two bits to the left
## that is make a new number 100 (base 2)
5 >> 1 ## shift: move the number 5 = 101 (base 2) one to
## to the right (10 = 2)
x = 2 ; y = 3 ## assign two variables on the same line!
x | y ## bitwise OR
x ^ y ## exclusive OR (10 ^ 11 = 01)
x & y ## bitwise AND
x = x ^ y ; print x
x += 3 ; print x
x /= 2.0
print x
Testing Relationships
dist1 = 4.9 ; dist = 4.9
dist1 == dist
dist < 10
dist <= 4.9
dist < (10 + 2j)
dist < -2.0
dist != 3.1415 # in the future you could use math.pi
0 == False
not False
0.0 == False
not (10.0 - 10.0)
not -1
not 3.1415
x = None # None is something special. Not true or false
print None == False
print None == True
print None == None
print False or True
print False and True
print type(1)
x = 2 ; print type(x)
type(2) == type(1)
print type(True)
print type(type(1))
print type(pow)
we can test whether something is a certain type with isinstance()
print isinstance(1,int)
print isinstance("spam",str)
print isinstance(1.212,int)
builtin-types: int
, bool
, str
, float
, complex
, long
....
x = "spam" ; type(x)
print "hello!\n...my sire."
"hello!\n...my sire."
"wah?!" == 'wah?!'
print "'wah?!' said the student"
print "\"wah?!\" said the student"
backslashes (\
) start special (escape) characters:
\n = newline (\r = return)
\t = tab
\a = bell
string literals are defined with double quotes or quotes. the outermost quote type cannot be used inside the string (unless it's escaped with a backslash)
# raw strings don't escape characters
print r'This is a raw string...newlines \r\n are ignored.'
# Triple quotes are real useful for multiple line strings
y = '''For score and seven minutes ago,
you folks all learned some basic mathy stuff with Python
and boy were you blown away!'''
print y
r
makes that string "raw"u
makes that string "unicode"http://docs.python.org/reference/lexical_analysis.html#string-literals
s = "spam" ; e = "eggs"
print s + e
print s + " and " + e
print "green " + e + " and\n " + s
print s*3 + e
print "*"*50
print "spam" is "good" ; print "spam" is "spam"
"spam" < "zoo"
"s" < "spam"
+
sign*
signprint 'I want' + 3 + ' eggs and no ' + s
print 'I want ' + str(3) + ' eggs and no ' + s
pi = 3.14159
print 'I want ' + str(pi) + ' eggs and no ' + s
print str(True) + ":" + ' I want ' + str(pi) + ' eggs and no ' + s
you must concatenate only strings, coercing ("casting")
other variable types to str
We can think of strings as arrays (although, unlike in C you never really need to deal with directly addressing character locations in memory)
s = 'spam'
len(s)
len("eggs\n")
len("")
print s[0]
print s[-1]
len()
gives us the length of an arrayx = 1
if x > 0:
print "yo"
else:
print "dude"
x = 1
if x > 0:
print "yo"
else:
print "dude"
One liners
print "yo" if x > 0 else "dude"
case statements can be constructed with
just a bunch of if
, elif
,...else
if x < 1:
print "t"
elif x > 100:
print "yo"
else:
print "dude"
ordering matters. The first block of True
in an if/elif gets executed then everything else does not.
x = "fried goldfish"
if x == "spam for dinner":
print "I will destroy the universe"
else:
# I'm fine with that. I'll do nothing
x = "fried goldfish"
if x == "spam for dinner":
print "I will destroy the universe"
else:
# I'm fine with that. I'll do nothing
pass
pass
is a "do nothing"/NOP statement