print "Hello World!"
Hello World!
print 2 + 2
4
2 + 2
4
print 2.1 + 2
4.1
2.1 + 2 == 4.0999999999999996
True
2L
2L
2L + 2
4L
2L/2
1L
2L/2.0
1.0
complex(1,2)
(1+2j)
1+2j
(1+2j)
1 + 2j - 2j
(1+0j)
(3.0*10.0 - 25.0)/5.0
1.0
print 3.085e18*1e6 # this is a Megaparsec in units of cm!
3.085e+24
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
4.9
dist1 = accel*(t**2)/2
print dist1
4.9
dist2 = 0.5*accel*pow(t,2)
print dist2
4.9
Some Mathy Operators
print 6 / 5 ; print 9 / 5 # integer division returns the floor
1 1
6 % 5 # mod operator
1
1 << 2 ## shift: move the number 1 by two bits to the left
## that is make a new number 100 (base 2)
4
5 >> 1 ## shift: move the number 5 = 101 (base 2) one to
## to the right (10 = 2)
2
x = 2 ; y = 3 ## assign two variables on the same line!
x | y ## bitwise OR
3
x ^ y ## exclusive OR (10 ^ 11 = 01)
1
x & y ## bitwise AND
2
x = x ^ y ; print x
1
x += 3 ; print x
4
x /= 2.0
print x
2.0
Testing Relationships
dist1 = 4.9 ; dist = 4.9
dist1 == dist
True
dist < 10
True
dist <= 4.9
True
dist < (10 + 2j)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/jbloom/Classes/python-bootcamp/DataFiles_and_Notebooks/01_BasicTraining/<ipython-input-31-8db11a0aca8e> in <module>() ----> 1 dist < (10 + 2j) TypeError: no ordering relation is defined for complex numbers
dist < -2.0
False
dist != 3.1415 # in the future you could use math.pi
True
0 == False
True
not False
True
0.0 == False
True
not (10.0 - 10.0)
True
not -1
False
not 3.1415
False
x = None # None is something special. Not true or false
print None == False
print None == True
print None == None
False False True
print False or True
print False and True
True False
print type(1)
<type 'int'>
x = 2 ; print type(x)
<type 'int'>
type(2) == type(1)
True
print type(True)
<type 'bool'>
print type(type(1))
<type 'type'>
print type(pow)
<type 'builtin_function_or_method'>
we can test whether something is a certain type with isinstance()
print isinstance(1,int)
print isinstance("spam",str)
print isinstance(1.212,int)
True True False
builtin-types: int
, bool
, str
, float
, complex
, long
....
x = "spam" ; type(x)
str
print "hello!\n...my sire."
hello! ...my sire.
"hello!\n...my sire."
'hello!\n...my sire.'
"wah?!" == 'wah?!'
True
print "'wah?!' said the student"
'wah?!' said the student
print "\"wah?!\" said the student"
"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.'
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
For score and seven minutes ago, you folks all learned some basic mathy stuff with Python and boy were you blown away!
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
spameggs
print s + " and " + e
spam and eggs
print "green " + e + " and\n " + s
green eggs and spam
print s*3 + e
spamspamspameggs
print "*"*50
**************************************************
print "spam" is "good" ; print "spam" is "spam"
False True
"spam" < "zoo"
True
"s" < "spam"
True
+
sign*
signprint 'I want' + 3 + ' eggs and no ' + s
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /Users/jbloom/Classes/python-bootcamp/DataFiles_and_Notebooks/01_BasicTraining/<ipython-input-65-91d89321074a> in <module>() ----> 1 print 'I want' + 3 + ' eggs and no ' + s TypeError: cannot concatenate 'str' and 'int' objects
print 'I want ' + str(3) + ' eggs and no ' + s
I want 3 eggs and no spam
pi = 3.14159
print 'I want ' + str(pi) + ' eggs and no ' + s
I want 3.14159 eggs and no spam
print str(True) + ":" + ' I want ' + str(pi) + ' eggs and no ' + s
True: I want 3.14159 eggs and no spam
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)
4
len("eggs\n")
len("")
0
print s[0]
print s[-1]
s m
len()
gives us the length of an arrayx = 1
if x > 0:
print "yo"
else:
print "dude"
yo
x = 1
if x > 0:
print "yo"
else:
print "dude"
yo
One liners
print "yo" if x > 0 else "dude"
yo
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"
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
IndentationError: expected an indented block
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