import antigravity import this script_dir = '../scripts/' ls $script_dir"hello-world"*.py cat $script_dir"hello-world.py" !python $script_dir"hello-world.py" !ls $script_dir cat $script_dir"hello-world-in-portuguese.py" !python $script_dir"hello-world-in-portuguese.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 numpy.linalg import inv from scipy.sparse.linalg import inv as sparseinv import math print(dir(math)) help(math.log) log(10) log(10, 2) help(math) import sys import keyword print sys.version print '' print(keyword.kwlist) # 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) y = bool(z.real) print(z.real, " -> ", y, type(y)) y = bool(z.imag) print(z.imag, " -> ", y, type(y)) 1 + 2, 1 - 2, 1 * 2, 1 / 2 1.0 + 2.0, 1.0 - 2.0, 1.0 * 2.0, 1.0 / 2.0 3.0 // 2.0 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] # objects identical? l1 = l2 = [1,2] l1 is l2 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[::1] s[::2] print("str1" + "str2" + "str3") # strings added with + are concatenated without space print("value = %f" % 1.0) s2 = "value1 = %.2f value2 = %d" % (3.1415, 1.5) print(s2) s3 = 'value1 = {0}, value2 = {1}'.format(3.1415, 1.5) print(s3) l = [1,2,3,4] print(type(l)) print(l) print(l) print(l[1:3]) print(l[::2]) l[0] l = [1, 'a', 1.0, 1-1j] print(l) start = 10 stop = 30 step = 2 range(start, stop, step) list(range(-10, 10)) s # convert a string to a list: s2 = list(s) 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] = "p" l[2] = "p" print(l) l[1:3] = ["d", "d"] print(l) l.insert(0, "i") l.insert(1, "n") l.insert(2, "s") l.insert(3, "e") l.insert(4, "r") l.insert(5, "t") print(l) l.remove("A") print(l) del l[7] del l[6] print(l) point = (10, 20) print(point, type(point)) point = 10, 20 print(point, type(point)) x, y = point print("x =", x) print("y =", y) point[0] = 20 params = {"parameter1" : 1.0, "parameter2" : 2.0, "parameter3" : 3.0,} 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"])) statement1 = False statement2 = False if statement1: print("statement1 is True") elif statement2: print("statement2 is True") else: print("statement1 and statement2 are False") statement1 = statement2 = True if statement1: if statement2: print("both statement1 and statement2 are True") # Bad indentation! if statement1: if statement2: print("both statement1 and statement2 are True") # this line is not properly indented 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") 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) l1 = [x**2 for x in range(0,5)] print(l1) i = 0 while i < 5: print(i) i = i + 1 print("done") 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") help(func1) func1("test") def square(x): """ Return the square of x. """ return x ** 2 square(4) def powers(x): """ Return a few powers of x. """ return x ** 2, x ** 3, x ** 4 powers(3) x2, x3, x4 = 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) f1 = lambda x: x**2 # is equivalent to def f2(x): return x**2 f1(2), f2(2) # map is a built-in python function map(lambda x: x**2, range(-3,4)) class Point: """ Simple class for representing a point in a Cartesian coordinate system. """ def __init__(self, x, y): """ Create a new Point at x, y. """ self.x = x self.y = y def translate(self, dx, dy): """ Translate the point by dx and dy in the x and y direction. """ self.x += dx self.y += dy def __str__(self): return("Point at [%f, %f]" % (self.x, self.y)) p1 = Point(0, 0) # this will invoke the __init__ method in the Point class print(p1) # this will invoke the __str__ method p2 = Point(1, 1) p1.translate(0.25, 1.5) print(p1) print(p2) %%file mymodule.py """ Example of a python module. Contains a variable called my_variable, a function called my_function, and a class called MyClass. """ my_variable = 0 def my_function(): """ Example function """ return my_variable class MyClass: """ Example class. """ def __init__(self): self.variable = my_variable def set_variable(self, new_value): """ Set self.variable to a new value """ self.variable = new_value def get_variable(self): return self.variable import mymodule help(mymodule) mymodule.my_variable mymodule.my_function() my_class = mymodule.MyClass() my_class.set_variable(10) my_class.get_variable() reload(mymodule) %reload_ext version_information %version_information math %who from IPython.core.display import HTML def css_styling(): styles = open("./styles/custom.css", "r").read() return HTML(styles) css_styling()