import this ls scripts/hello-world*.py cat scripts/hello-world.py !python scripts/hello-world.py cat scripts/hello-world-broken.py !python scripts/hello-world-broken.py cat scripts/hello-world-in-arabic.py !python scripts/hello-world-in-arabic.py # IPython supports utf-8 by default print "مرحبا" %%timeit x = range(10000) sum(x) %run scripts/hello-world.py import math import math x = math.cos(2 * math.pi) print x from math import * x = cos(2 * pi) print x from math import cos, pi x = cos(2 * pi) print x from math import cos as cos1 import math as ma x = cos1(2 * ma.pi) print x import math print dir(math) help(math.log) math.log? log(10) # %load solutions/getting_help.py # variable assignments x = 1.0 my_variable = 12.2 type(x) x = 1 type(x) print(y) # integers x = 1 type(x) # float x = 1.0 type(x) # boolean b1 = True b2 = False type(b1) # complex numbers: note the use of `j` to specify the imaginary part x = 1.0 - 1.0j type(x) print(x) print(x.real, x.imag) import types # print all types defined in the `types` module print(dir(types)) x = 1.0 # check if the variable x is a float type(x) is float # check if the variable x is an int type(x) is int isinstance(x, float) x = 1.5 print(x, type(x)) x = int(x) print(x, type(x)) z = complex(x) print(z, type(z)) x = float(z) 1 + 2, 1 - 2, 1 * 2, 1 / 2 1.0 + 2.0, 1.0 - 2.0, 1.0 * 2.0, 1.0 / 2.0 # Integer division of float numbers 3.0 // 2.0 # Note! The power operators in python isn't ^, but ** 2 ** 2 True and False not False True or False 2 > 1, 2 < 1 2 > 2, 2 < 2 2 >= 2, 2 <= 2 # equality [1,2] == [1,2] # inequality 1 != 1 # objects identical? l1 = [1,2] l2 = l1 l1 is l2 a = 15 b = 5 c = 2.2 # expression evaluation here val = ??? print(val) # %load solutions/operators.py a = 15 b = 5 c = 2.2 # expression evaluation here val = (float(5*(a>b)) + float(2*(a<(b*4))))/float(a) * c print(val) s = "Hello world" type(s) # length of the string: the number of characters len(s) # replace a substring in a string with somethign else s2 = s.replace("world", "test") print(s2) s[0] s[0:5] s[:5] s[6:] s[:] s[:-2] s[-1], s[-2] s[::1] s[::2] print(dir(str)) import math print 'The value of %5s is approximately %5.3f.' % ('PI', math.pi) print('{0} and {1}'.format('spam', 'eggs')) print('{1} and {0}'.format('spam', 'eggs')) print( 'This {food} is {adjective}.'.format(food='spam', adjective='absolutely horrible') ) print( 'The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred', other='Georg') ) import math print( 'The value of PI is approximately {0:.3f}.'.format(math.pi) ) text = "Hello world!" # %load solutions/strings.py l = [1,2,3,4] print(type(l)) print(l) print(l) print(l[1:3]) print(l[::2]) l = [1, 'a', 1.0, 1-1j] print(l) nested_list = [1, [2, [3, [4, [5]]]]] print(nested_list) nl = [1, [2, 3, 4], [5, [6, 7, 8]]] print(nl) print(nl[0]) print(nl[1][1]) print(nl[2][1][2]) start = 10 stop = 30 step = 2 range(start, stop, step) # convert a string to a list by type casting: s2 = list(s) print s2 # sorting lists s2.sort() print(s2) # create a new empty list l = [] # add an elements using `append` l.append("A") l.append("d") l.append("d") print(l) l[1:3] = ["d", "d"] print(l) del l[0] print(l) l1 = [1, 2, 3] + [4, 5, 6] print(l1) l2 = [1, 2, 3] * 2 print(l2) # %load solutions/lists.py params = {"parameter1" : 1.0, "parameter2" : 2.0, "parameter3" : 3.0, 1: 4.0, (5, 'ho'): 'hi'} print(type(params)) print(params) print("parameter1 = " + str(params["parameter1"])) print("parameter2 = " + str(params["parameter2"])) print("parameter3 = " + str(params["parameter3"])) params["parameter1"] = "A" params["parameter2"] = "B" # add a new entry params["parameter4"] = "D" print("parameter1 = " + str(params["parameter1"])) print("parameter2 = " + str(params["parameter2"])) print("parameter3 = " + str(params["parameter3"])) print("parameter4 = " + str(params["parameter4"])) print("'key 1' = " + str(params[1])) print("'key (5, 'ho')' = " + str(params[(5, 'ho')])) del params["parameter2"] print(params) # %load solutions/dicts.py statement1 = False statement2 = False if statement1: print("statement1 is True") elif statement2: print("statement2 is True") else: print("statement1 and statement2 are False") statement1 = False if statement1: print("printed if statement1 is True") print("still inside the if block") if statement1: print("printed if statement1 is True") print("now outside the if block") # a compact way for using the if statement a = 2 if statement1 else 4 print("a= ", a) name = 'john' if name in ['jed', 'john']: print("We have Jed or John") num = 1 if num in [1, 2]: print("We have 1 or 2") for x in [1,2,3]: print(x) for x in range(4): # by default range start at 0 print(x) for x in range(-3,3): print(x) for word in ["scientific", "computing", "with", "python"]: print(word) for key, value in params.items(): print(key + " = " + str(value)) for idx, x in enumerate(range(-3,3)): print(idx, x) i = 0 while i < 5: print(i) i = i + 1 print("done") words = ["Aerial", "Affect", "Agile", "Agriculture", "Animal", "Attract", "Audubon", "Backyard", "Barrier", "Beak", "Bill", "Birdbath", "Branch", "Breed", "Buzzard", "The", "On", "Upper", "Not", "What", "Linked", "Up", "In", "A", "lol"] # %load solutions/control_flow.py def func0(): print("test") func0() def func1(s): """ Print a string 's' and tell how many characters it has """ print(s + " has " + str(len(s)) + " characters") return 1, 2, 3 help(func1) func1("test") def powers(x): """ Return a few powers of x. """ return x ** 2, x ** 3, x ** 4 powers(3) x2, x3, _ = powers(3) print(x3) def myfunc(x, p=2, debug=False): if debug: print("evaluating myfunc for x = " + str(x) + " using exponent p = " + str(p)) return x**p myfunc(5) myfunc(5, debug=True) myfunc(p=3, debug=True, x=7)