#!/usr/bin/env python # coding: utf-8 # # Introduction To Python # This is a collection of various statements, features, etc. of IPython and the Python language. Much of this content is taken from other notebooks so I can't take credit for it, I just extracted the highlights I felt were most useful. # # Code cells are run by pressing shift-enter or using the play button in the toolbar. # In[1]: a = 10 # In[2]: print(a) # In[3]: import math # In[4]: x = math.cos(2 * math.pi) print(x) # Import the whole module into the current namespace instead. # In[5]: from math import * x = cos(2 * pi) print(x) # Several ways to look at documentation for a module. # In[6]: print(dir(math)) # In[7]: help(math.cos) # ### Variables # In[8]: x = 1.0 type(x) # In[9]: # dynamically typed x = 1 type(x) # ### Operators # In[10]: 1 + 2, 1 - 2, 1 * 2, 1 / 2 # In[11]: # integer division of float numbers 3.0 // 2.0 # In[12]: # power operator 2 ** 2 # In[13]: True and False # In[14]: not False # In[15]: True or False # In[16]: 2 > 1, 2 < 1, 2 > 2, 2 < 2, 2 >= 2, 2 <= 2 # In[17]: # equality [1,2] == [1,2] # ### Strings # In[18]: s = "Hello world" type(s) # In[19]: len(s) # In[20]: s2 = s.replace("world", "test") print(s2) # In[21]: s[0] # In[22]: s[0:5] # In[23]: s[6:] # In[24]: s[:] # In[25]: # define step size of 2 s[::2] # In[26]: # automatically adds a space print("str1", "str2", "str3") # In[27]: # C-style formatting print("value = %f" % 1.0) # In[28]: # alternative, more intuitive way of formatting a string s3 = 'value1 = {0}, value2 = {1}'.format(3.1415, 1.5) print(s3) # ### Lists # In[29]: l = [1,2,3,4] print(type(l)) print(l) # In[30]: print(l[1:3]) print(l[::2]) # In[31]: l[0] # In[32]: # don't have to be the same type l = [1, 'a', 1.0, 1-1j] print(l) # In[33]: start = 10 stop = 30 step = 2 range(start, stop, step) # consume the iterator created by range list(range(start, stop, step)) # In[34]: # create a new empty list l = [] # add an elements using `append` l.append("A") l.append("d") l.append("d") print(l) # In[35]: l[1:3] = ["b", "c"] print(l) # In[36]: 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) # In[37]: l.remove("A") print(l) # In[38]: del l[7] del l[6] print(l) # ### Tuples # In[39]: point = (10, 20) print(point, type(point)) # In[40]: # unpacking x, y = point print("x =", x) print("y =", y) # ### Dictionaries # In[41]: params = {"parameter1" : 1.0, "parameter2" : 2.0, "parameter3" : 3.0,} print(type(params)) print(params) # In[42]: 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"])) # ### Control Flow # In[43]: statement1 = False statement2 = False if statement1: print("statement1 is True") elif statement2: print("statement2 is True") else: print("statement1 and statement2 are False") # ### Loops # In[44]: for x in range(4): print(x) # In[45]: for word in ["scientific", "computing", "with", "python"]: print(word) # In[46]: for key, value in params.items(): print(key + " = " + str(value)) # In[47]: for idx, x in enumerate(range(-3,3)): print(idx, x) # In[48]: l1 = [x**2 for x in range(0,5)] print(l1) # In[49]: i = 0 while i < 5: print(i) i = i + 1 print("done") # ### Functions # In[50]: # include a docstring def func(s): """ Print a string 's' and tell how many characters it has """ print(s + " has " + str(len(s)) + " characters") # In[51]: help(func) # In[52]: func("test") # In[53]: def square(x): return x ** 2 # In[54]: square(5) # In[55]: # multiple return values def powers(x): return x ** 2, x ** 3, x ** 4 # In[56]: powers(5) # In[57]: x2, x3, x4 = powers(5) print(x3) # In[58]: f1 = lambda x: x**2 f1(5) # In[59]: map(lambda x: x**2, range(-3,4)) # In[60]: # convert iterator to list list(map(lambda x: x**2, range(-3,4))) # ### Classes # In[61]: class Point: def __init__(self, x, y): self.x = x self.y = y def translate(self, dx, dy): self.x += dx self.y += dy def __str__(self): return("Point at [%f, %f]" % (self.x, self.y)) # In[62]: p1 = Point(0, 0) print(p1) # In[63]: p2 = Point(1, 1) p1.translate(0.25, 1.5) print(p1) print(p2) # ### Exceptions # In[64]: try: print(test) except: print("Caught an expection") # In[65]: try: print(test) except Exception as e: print("Caught an exception: " + str(e))