class: center, middle # Introduction to Python --- # Python [Python](http://python.org) is one of many scripting languages. Others include [Perl](http://www.perl.org), [Ruby](http://www.ruby-lang.org/), and even the Bash/Shell programming we've been talking about. It is a script because we write the code in a text file and then run it directly with an interpreter. Before when we ran a script in bash/shell we would do ```shell $ bash myscript.sh ``` To run code for python we will do ```shell $ python myscript.py ``` --- #A first script Here's a text file that is called 'hello.py' and contains the following ```python print("Hello World!") ``` ```shell $ python hello.py hello world $ python hello.py > message.txt # can redirect the output $ cat message.txt hello world ``` --- #The Python interpreter The Python interpreter is a program we run, giving it a file or script which specifies the actions for it to take. One can also just run the interpreter on the command line and interact with it directly. Just execute the python command on the command line. ```python $ python Python 2.7.10 (default, Oct 3 2015, 13:37:56) [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print "hello world" hello world ``` --- #Some math with the interpreter ```python >>> 3+10 13 >>> 33/13 2 >>> 33/13.0 2.5384615384615383 >>> 2**10 # 2^10 1024 >>> (909+951) / 2.0 # Parenthees make a difference 930 >>> 909+951/2.0 1384 ``` --- #Modules: for Math Python has the concept of modules or libraries that contain routines we can re-use. We will spend more time on the types of modules that exist (the list if vast!). Here we will use the [math](https://docs.python.org/2/library/math.html) module which definese many routines. ```python >>> sqrt(9) Traceback (most recent call last): File "
", line 1, in
NameError: name 'sqrt' is not defined >>> import math >>> math.sqrt(9) 3 >>> math.pow(2,10) 1024.0 >>> 33/13.0 2.5384615384615383 >>> math.ceil(33/13.0) # round up 3.0 >>> math.floor(33/13.0) # round down 2.0 >>> round(2.2) # round works as you expect for rounding (<.5) 2.0 >>> round(2.6) # round works as you expect for rounding (>=.5) 3.0 ``` --- # Other math capabilities The % operator is modulus - it means return the remainder after dividing by the number. It is useful to see if something divides evenly into a number ```python >>> 10 / 3 3 >>> 10 % 3 # 10 / 3 is 3 with remainder of 1 1 >>> 10 / 2 5 >>> 10 % 2 # 10 / 2 is 5 with a remainder of 0 0 ``` This can be useful to see if something is "Modulo" something (or Mod) -- really useful when looking at sequence data and checking to see if it will translate -- is the length/value mod 3 == 0 - means it is divisible perfectly by 3. --- #Strings Quotes can define the different special characters and use of strings ```python >>> 'break dance' 'break dance' >>> "break dance" 'break dance' >>> "don't break dance" "don't break dance" >>> 'don\'t break dance' "don't break dance" >>> ('concatenate ' 'these ' 'together') 'concatenate these together' ``` --- # Variables Data can be assigned to variables. Variables don't exist until they are declared. The = is used for assignment. Some modifiers allow for modifying variable in place * += add to the value of the variable * -=, /=, *= are other ```python >>> d = 0 # declare the variable d >>> print "d is " + d d is 0 >>> print "e is " + e Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'e' is not defined >>> d=8*7 >>> print d 56 >>> d += 100 >>> print d 156 ``` --- # String functions Documented well in the python [string](https://docs.python.org/2/library/string.html?highlight=string) library code. * string.find(substring,[,starting[,end]]) Find a substring within a string ```python >>> str = "Jumping cow over the moon" >>> str.find("cow") 8 >>> str.find("o") # start searching from beginning 9 >>> str.find("o",12) # start searching after character 12 22 ``` * string.split(separator, max_split) Split a string into a list based on a separator (great for column delimited data!). max_splits specifies if it should stop after N separators are found ```python >>> str = "Jumping cow over the moon" >>> str.split(" ") ['Jumping', 'cow', 'over', 'the', 'moon'] >>> str.split(" ",2) ['Jumping', 'cow', 'over the moon'] ``` --- #substring - extracting parts of a larger string You can query the string with the [ ] operator to get a subset of the string ```python >>> msg="I am a golden god!" >>> msg[:4] # everything before position 4 'I am' >>> msg[7:13] # get a middle part from 7-15 'golden' >>> msg[14:] # get from 14 to the end 'god!' ``` --- #Lists For collecting enumerated items ```python >>> l = [ 2, 3, 5] >>> l[0] # lists start at 0 2 >>> l[1:3] 3,5 >>> l.append(10) >>> print l [2, 3, 5, 10] ``` The sum() function will summate a list, len() will report how long it is. max() will report the largest number in a list ```python >>> sum(l) 20 >>> len(l) 4 >>> max(l) 10 ``` --- #Lists other tools Lists can be sorted ```python >>> l = [10,3,17,4] >>> l.sort() >>> l [3, 4, 10, 17] >>> ls = ['zf','fz','no','apple'] >>> ls.sort() >>> print ls ['apple', 'fz', 'no', 'zf'] ``` List comprehension -- we will do more on this when we cover loops ```python squares = [] for x in range(10): squares.append(x**2) print squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] # can also use this syntax to build it up squares = [x**2 for x in range(10)] ``` --- #Using split to parse strings Starting with strings containing several parts which encode information you want. ```text Symbol:NASDAQ=AAPL;Date:2015-10-01;Performance:Open=111.29,Close=109.56 Symbol:NASDAQ=MSFT;Date:2015-10-01;Performance:Open=46.65,Close=46.53 Symbol:NYSE=MMM;Date:2015-10-01;Performance:Open=149.0,Close=148.64 ``` What if we want to extract the 'AAPL','MSFT','MMM' and the Opening share price? Let's use split for this as one way to do it. ```python >>> q = "Symbol:NASDAQ=AAPL;Date:2015-10-01;Performance:Open=111.29,Close=109.56" >>> types = q.split(";") >>> print types ['Symbol:NASDAQ=AAPL', 'Date:2015-10-01', 'Performance:Open=111.29,Close=109.56'] >>> symbolset = types[0].split(":") >>> print symbolset ['Symbol', 'NASDAQ=AAPL'] >>> symbol = symbolset[1].split("=") >>> symbol ['NASDAQ', 'AAPL'] >>> print symbol[1] 'AAPL' ``` --- #Documentation and Comments Comment lines start with a '#' There are also othe self-documentation feature to make it easy to generate readable documention for web or other sites. These are flanked with three double quotes like ```python """Documentation here""" # can also be on multiple lines """This message could be concise, on multiple lines, and later I could tell you more about my program or function""" ``` --- #Print information out print can accomodate a lot of types of data but it doesn't know how to mix types that well ```python >>> l = [7, 8, 9] >>> print l [7, 8, 9] >>> print "the list is " + l TypeError: cannot concatenate 'str' and 'list' objects >>> print "the list is " + str(l) the list is [7, 8, 9] >>> print "the first item in the list is " + l[0] TypeError: cannot concatenate 'str' and 'int' objects >>> print "the first item in the list is " + str(l[0]) the first item in the list is 7 # use the repr function to also print out the represenetation of #an object >>> print "the square of 8 is " + repr(8**2) the square of 8 is 64 ``` --- #repr function to display an object/string/number ```python # use repr to print out literally the string >>> hello = 'hello world\n' >>> print hello hello world >>> print repr(hello) 'hello world\n' ``` --- #More fancy printing Can use formatting to print some things out like these important [numbers and phrase](http://genius.com/Beastie-boys-get-it-together-lyrics). ```python >>> n = [1, 2, 'oh my god'] >>> print n [1, 2, 'oh my god'] # print things out with fancier printing >>> print n[0].rjust(5), n[1].rjust(5), n[2].ljust(10) AttributeError: 'int' object has no attribute 'rjust' >>> print repr(n[0]).rjust(5), repr(n[1]).rjust(5), n[2].ljust(10) 1 2 oh my god >>> print repr(n[0]).ljust(5), repr(n[1]).rjust(5), n[2].ljust(10) 1 2 oh my god >>> print repr(n[0]).ljust(5), repr(n[1]).rjust(5), n[2].rjust(10) 1 2 oh my god >>> print repr(n[0]).rjust(5), repr(n[1]).rjust(5), n[2].rjust(12) 1 2 oh my god >>> print repr(n[0]).rjust(5), repr(n[1]).rjust(5), n[2].ljust(12) 1 2 oh my god ``` --- #Print out with placeholders ```python >>> a = 15 >>> b = 41 >>> print "a + b = ", a+b a + b = 56 >>> print "{} + {} = {}".format(a,b,a+b) 15 + 41 = 56 >>> print "{} ({}) + {} ({}) = {}".format("a",a,"b",b,a+b) a (15) + b (41) = 56 ``` --- #Problems to try in class Using command line python (e.g. start it up without any arguments) 1. Determine what 2146 divided by 13 is. Show with floating point. 2. assign a string to a variable, determine how long the string is, print out the first 2 letters of the string 3. create a list from the string "a-b-cc-de-fgh" where the '-' separate the words. print out the length of list. 4. Make a list mixed with words and numbers, sort it. How does it behave? 5. Calculate the mean of a list of numbers (4, 8, 15, 16, 23, 42) --- #Logic with if statements & importance of indentation if / elif / else are used to apply logic. This plus loops is the essence of programming. Note this requires you indent your code to indicate the level of the if statement ```python x=10 if x > 0: print "x is positive: ", x elif x < 0: print "x is negative: ", x else: print "x is 0: ", x ``` --- #Loops Iterate through lists or based on logic. Here we count up from 1 to 10 ```python for n in range(1,11): print "n is ", n if n % 2 == 0: print "n is even" #iterate over a range based on a list a = ['Mary', 'had', 'a', 'little', 'lamb'] for i in range(len(a)): print i, a[i] ``` ```python n is 1 n is 2 n is even n is 3 n is 4 n is even ... 0 Mary 1 had 2 a 3 little 4 lamb ``` --- #Loops can also count backwards ```python for n in range(10,-1,-1): print n 10 9 8 7 6 5 4 3 2 1 0 ``` --- #Loops over plus lists ```python stocks = [ 'Symbol:NASDAQ=AAPL;Date:2015-10-01;Performance:Open=111.29,Close=109.56', 'Symbol:NASDAQ=MSFT;Date:2015-10-01;Performance:Open=46.65,Close=46.53', 'Symbol:NYSE=MMM;Date:2015-10-01;Performance:Open=149.0,Close=148.64'] for ticker in stocks: print ticker print # empty line for ticker in stocks: parts = ticker.split(";") symbolset = parts[0].split(":") symbol = symbolset[1].split("=") print symbol[1] ``` produces ```python Symbol:NASDAQ=AAPL;Date:2015-10-01;Performance:Open=111.29,Close=109.56 Symbol:NASDAQ=MSFT;Date:2015-10-01;Performance:Open=46.65,Close=46.53 Symbol:NYSE=MMM;Date:2015-10-01;Performance:Open=149.0,Close=148.64 AAPL MSFT MMM ``` --- #Iterate over a string Can use for loops on strings as well ```python dna="AAGATCAGGAC" for c in dna: print "DNA base is " + c ```