%run talktools.py print("Hello World!") print(2 + 2) 2 + 2 print(2.1 + 2) print(4.0999999999999995) 2.1 + 2 == 4.0999999999999995 4 * 2 4 / 2 5 / 2 # Note this is different in Python 2.x!! 5 // 2 5 % 2 # modulus (remainder after division) 5 ** 2 # or you can use the pow() function pow(5, 2) print(2 + 2) 3 + 3 print(1 + 1) # easy arithmetic 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) x, y = 4, 50 print(x) print(y) x, y = y, x # easy swap! print(x) print(y) x = 4 x += 8 # same as x = x + 8 print(x) x *= 0.2 # x is upgraded to a float! print(x) x %= 1 print(x) bin(14) # print binary representation bin(13) 14 & 13 bin(14 & 13) 14 | 13 bin(14 | 13) 2 < 4 3 >= 3 5 == 4 5 != 4 5 < 2 + 4j x = 4 y = 6 print(2 < x <= 4) print(2 < y <= 4) # This allows strange/confusion expressions # don't do things like this! 8 > x <= 5 0.1 + 0.2 == 0.3 # this is a string formatting command (we'll cover this later) # it says to print 20 places after the decimal print("{0:.20f}".format(0.1 + 0.2)) print("{0:.20f}".format(0.3)) x = 4 (x > 2) and (x < 10) (x <= 4) or not (x > 10) 0 == False not False not 0 not -1 print(None) # None is a special object print(None == True) print(None == False) print(None == None) print(bool(None)) x = 1 y = 1 x is y x = 1111 y = 1111 print(x is y) x = None print(x is None) print(type(1)) x = 2 print(type(x)) type(2) == type(1) print(type(True)) print(type(None)) print(type(type(1))) print(type(pow)) print(isinstance(1, int)) print(isinstance("spam", str)) print(isinstance(1.212, int)) 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") # raw strings (marked with r) 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) 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" == "good"); print("spam" == "spam") "spam" < "zoo" "s" < "spam" print('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) # Note that raw_input does not work in IPython notebook version < 1.0 # You can always do this from a file or from the command line, though faren = input("enter a temperature (in Fahrenheit): ") print(faren) cent = (faren - 32) / 1.8 cent = (float(faren) - 32) / 1.8 print(cent) # Or in one line: faren = float(input("enter a temperature (in Fahrenheit): ")) (faren - 32) / 1.8 s = 'spam' len(s) len("eggs\n") len("") print(s[0]) print(s[-1]) s[0:1] s[1:4] s[-2:] s[0:100] # if the slice goes past the end, no complaints! s[0:4:2] s[::2] s[::-1] x = 1 if x > 0: print("yo") else: print("dude") "yo" if x > 0 else "dude" x = 1 y = 0 while y < 10: print("yo" if x > 0 else "dude") x *= -1 y += 1 # Could also do this with a break statement x = 1 y = 0 while True: print("yo" if x > 0 else "dude") x *= -1 y += 1 if y >= 10: break if x < 1: print("t") elif x > 100: print("yo") elif x == 42: print("bingo") else: print("dude") 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 %%file number_game.py # The above "magic" command saves the contents # of the current cell to file. We'll see more of these later x = 0 max_tries = 10 count = 0 while True: x_new = int(input("Enter a new number: ")) if x_new > x: print(" -> it's bigger than the last!") elif x_new < x: print(" -> it's smaller than the last!") else: print(" -> no change! I'll exit now") break x = x_new count += 1 if count > max_tries: print("too many tries...") break %run number_game.py # this magic command runs the given file