# introduction.py ''' Gives a basic introduction into python. ''' # # Prelude # # Python code is interpreted. # Python code runs on multiple platforms, given the right libraries are installed. # Output print("Hello World!") # # Variables # # Variables/Objects are not declared and typed dynamically. a = 5 b = 7 c = a + b d = "hello" print(c) print(hello) # Variable names are case sensitive. # Must start with _ or a letter, (not a number!) A = 1 c = A + a print(c) a = True b = False print(a+b) print(a*b) # There are more binary operations, like XOR, etc. # Types and conversion. type(a) float(a) # Complex type. (5j + 3) * (5j - 3) # A few operators: 2+2 2-1 3*2 5/4 # integer division in Python 2.* 5./4 # float division in Python 2.* 5//4 # integer division 2**3 # power 5 % 4 # remainder of 5/4 # A few functions: abs(a) pow(a, b) c = 5j + 3 c.conjugate() # # Strings # my_string = "chicken kidney pie" print(my_string) anotheshoer_string = 'beautiful blue raincoat' print(another_string) # Special characters and commands can be used using the backslash '\' print('Don\'t worry,\nbe happy.') # Some String operations. print("It is a shoe! It is a shoe!\n" "It\'s a sandal!") # String *slicing*. my_string[0] my_string[3] my_string[-1] my_string[2:4] my_string[::2] len(my_string) # # Functions. # # Functions return values and du something. a = abs(-5) # Get help (IPython): abs? abs?? # # Try autocompletion with tab (IPython). # # # Lists. # sequence = [1, 2, 5, 10, 20, 300] sequence[0] sequence[-1] sequence[-2] sequence[2:-3] sequence + [1, 2] sequence.append(500) sequence[-1] = [-1, -2, -3] sequence.append('what a twist!') sequence.extend([1, 2]) # Find the first occurence of this element in the list. sequence.index(1) sequence.pop() sequence.remove(1) sequence.reverse() sequence.sort() # # sets # # Sets are un-ordered collection of elements. a = {0, 1, 4, 2, 1} # Set operations: b = set([0, 1, 3, 2, 2, 1]) a.intersection(b) a.union(b) a.issubset(b) a - b b - a # # Including libraries. # import numpy as np import pylab as plt np.pi np.sin(3) # # Script files. # # Create a flie myScript.py. # We can execude file using. execfile('myScript.py') # OR: runfile('myScript.py') # OR (Python 3): exec(open("myScript.py").read()) # Or we can call it from the command line. python myScript.py # Comments can be added with the hash symbol. # Identation matters!!!!!! # Always 4 spaces!: # # Control Flow. # if x < 0: print("x is negative") elif x == 0: print("x = 0") else: print("x is positive") # Boolean algebra. if (x > 0) + (x < 0): print("x is not 0") if (x > 0) or (x < 0): print("x is not 0") if (x > 0) * (x < 1): print("x is in the open interval (0, 1)") if (x > 0) and (x < 1): print("x is in the open interval (0, 1)") # Loops my_list = [0, 1, 2, 3] for i in my_list: print(i) my_list = [10, 20, 3, 4] for i, j in enumerate(my_list): print(i, j) for i in range(10): print(i) for i in range(5, 10): print(i) for i in range(10): print(i) if i == 5: break for i in range(10): if i == 5: continue print(i) i = 0 while i < 5: print(i) i += 1 # Do nothing. if a > 0: print("a is positive") else: pass # Python requires at least one command in command blocks.