Python Language Overview

BCH 519
Spring 2017

Andrew E. Bruno
aebruno2@buffalo.edu

Course Goals

  • Learn python to formulate solutions to bioinformatics problems

Book

What is Python?

  • High-level, general purpose, dynamic programming language
  • Created by Guido van Rossum in 1989
  • Why is it called python?

When he began implementing Python, Guido van Rossum was also
reading the published scripts from "Monty Python's Flying Circus",
a BBC comedy series from the 1970s. Van Rossum thought he needed a
name that was short, unique, and slightly mysterious, so he decided
to call the language Python.
        
-- General Python FAQ

Programming in Python

What is programming?

Running Python programs

To run a Python program from the Linux command line:


$ python myprog.py
    

Python Documentation

Code to all exercises

/projects/academic/courses/bch519/spring16/exercises

Basic Syntax Overview

Exercise 1: First Python Program

Type the following code into a text file named ex1.py

print "Hello World"
print "Just what do you think you're doing, Dave?" 
print 'Seq1: "hg19" ATCGCTGCAT'

Then run in the terminal by typing:

$ python ex1.py
Hello World
Just what do you think you're doing, Dave?
Seq1: "hg19" ATCGCTGCAT

What do errors look like?

Edit ex1.py and remove the first 'p' in print on the first line


rint "Hello World"
    

Then re-run in the terminal by typing:

$ python ex1.py
  File "ex1.py", line 1
    rint "Hello World"
                     ^
SyntaxError: invalid syntax

Exercise 1: What we learned

  1. Python programs are text files (typically) ending in .py
  2. print displays the given text (without quotations)
  3. The text inside quotes is called a string
  4. Python reports errors if the syntax is not correct

Exercise 2: Comments

Type the following code into a text file named ex2.py

# This is a comment, here to explain what the code is doing
# Anything after the # is ignored by python

print "Hello # World"  # comments can be here

#
# HAL: It can only be attributable to human error.
#
print "Just what do you think you're doing, Dave?"

Then run in the terminal by typing:

$ python ex2.py
Hello # World
Just what do you think you're doing, Dave?

Exercise 2: What we learned

  1. # Comments are important. They help yourself and others understand your code
  2. # Anything after the # is ignored by python
  3. # Good practice to always comment your code!

Exercise 3: Numbers

Type the following code into a text file named ex3.py

print 0 + 1 + 1 + 2 + 3 + 5 + 8
print 22 % 5
print 3 * 4 + 6 / 3

Then run in the terminal by typing:

$ python ex3.py
20
2
14

Exercise 3: What we learned

  1. Python can do basic math
  2. Order of operations: (3 * 4) + (6 / 3)
  3. % = modulo operator

Exercise 4: Variables

Type the following code into a text file named ex4.py

seq_count = 10
gene = "NM_130786"
total = seq_count * 50
avg = total / seq_count
print "On gene {0} there are {1} sequences".format(gene, seq_count)
print "There's a total of", total, " sequences"
print "The average number of sequences is: {}".format(avg)

Then run in the terminal by typing:

$ python ex4.py
On gene NM_130786 there are 10 sequences
There's a total of 500  sequences
The average number of sequences is: 50

Exercise 4: What we learned

  1. Variable is a named reference to a value
  2. Variables can be printed in human-readable form using format
  3. Values are converted to strings. {} are placholders of variables
  4. A number inside {0} refers to the position of the variable passed to the format function
  5. http://docs.python.org/2/tutorial/inputoutput.html

Exercise 5: Strings

Type the following code into a text file named ex5.py

first = 'Marty'
last = "McFly"
gigawatts = 1.21

full_name = first + ' ' + last
greeting = "Hello {} {}".format(first, last)

print full_name
print greeting
print 'Achtung %s, %s gigawatts!' % (first, gigawatts)
print '{0:.3f} gigawatts'.format(gigawatts)
print "First: {0} Last: {1}".format(first, last)

Then run in the terminal by typing:

$ python ex5.py
Marty McFly
Hello Marty McFly
Achtung Marty, 1.21 gigawatts!
1.210 gigawatts
First: Marty Last: McFly

Exercise 5: What we learned

  1. Double quotes or single quotes may be used around literal strings
  2. the + operator concatenates strings together
  3. str.format can be used to concatenate strings
  4. % operator can be used for string formatting
  5. {0:.3f} an optional ':' and format specifier can follow the index. This formats to 3 decimal places

Exercise 6: More Strings and Printing

Type the following code into a text file named ex6.pl

chrom = 'chrom1'
start = 1002
end = 1012
print "chrom\tstart\tend"
print "{}\t{}\t{}".format(chrom, start, end)
print "{0}:{1}-{2}, start = {1}".format(chrom, start, end)

print 'What\'s the Gene\'s \\ fasta'
fasta = """>NM_130786
GGGCCTCATT
GCTGCAGACG
"""
print fasta

substr = chrom[1:4]  # extract substring
print substr

Then run in the terminal by typing:

$ python ex6.py
chrom   start   end
chr1    1002    1012
chr1:1002-1012, start = 1002
What's the Gene's \ fasta
>NM_130786
GGGCCTCATT
GCTGCAGACG

hro

Exercise 6: What we learned

  1. Use a backslash \ to escape special characters
  2. "\t" prints a TAB character "\n" prints a newline
  3. """ triple-quote defines strings with many lines of text
  4. "{0} {1} {2} {1}" can re-use placeholders in format string
  5. chrom[1:4] extracts a substring, starting from index 1 up to index 4 (not including)
         index: 0 1 2 3 4 5
        string: c h r o m 1 
                  * * *
    sub-string: hro
    

Control Structures

Conditionals

  • A control structure controls the flow of a program
    • if = choice of whether to execute a block of code
    • if-elif-else = multiple block choices
    • for loops = repeat a block multiple times
    • while loops = repeat a block multiple times
  • Uses boolean expressions as conditions

Basic structure of the if statement

if boolean_condition:
    code_block 
elif: other_condition:
    code_block
else:
    code_block

Exercise 7: Conditionals

if 5 > 4:
    print "Five is greater than four"

if "Hello" == "HELLO":
    print "This will not print"

grade = 83.5;
if grade < 50:
    print "D"
elif grade < 75:
    print "C"
elif grade < 90:
    print "B"
else:
    print "A"

Run in the terminal by typing:

$ python ex7.py
Five is greater than four
B

Example flow control

Flow control

Boolean Logic

p q p and q p or q
T T T T
T F F T
F T F T
F F F F

Exercise 8: Boolean Operators

Join two or more boolean expressions with and/or logic

a = 10
b = 5
c = 7

print a > b and a > c  # TRUE
print c > a and c > b  # FALSE
print a > b and b > c  # FALSE
print c > a or c > b   # TRUE 
print a < c or b > c   # FALSE

Exercise 7 & 8: What we learned

  1. conditional constructs allow execution of code given some condition is true/false
  2. can join two or more boolean expressions with and/or logic operators
  3. code under the if needs to be indented 4 spaces

Python builtin operators

Arithmetic/BooleanComparison operators
+addition==equality
-subtraction!=inequality
*multiplication<strictly less than
/division>strictly greater than
**exponentiation<=less than or equal
%modulo>=greater than or equal
andandisobject identity
ororis notnegated object identity
notnot 

Loops

Allows execution of the same code a number of times

Basic structure of looping constructs

while boolean_condition:
    code_block

for i in range(0,10):
    code_block

for i in ['X','Y','Z']:
    code_block

Example flow control

Flow control

Exercise 9: Loops

x = 0
while x != 5:
    print x
    x = x + 1

message = "Hello"
for char in message:
    print char

chroms = ['chr1', 'chr2', 'chr3']
for c in chroms:
    if c == 'chr2':
        continue

    print c

Run in the terminal by typing:

$ python ex9.py
0
1
2
3
4
H
e
l
l
o
chr1
chr3

Exercise 10: Nesting Loops

  • Loops can also be nested
  • For example, looping through rows/cols of a matrix
for i in range(0, 5):
    for j in range(0, 5):
        print "row: {} col: {}".format(i,j)

Exercise 9 & 10: What we learned

  1. loop constructs allow execution of code multiple times
  2. code under the for needs to be indented 4 spaces
  3. for loops over a list of values (iterable)
  4. continue command starts next iteration of the loop
  5. Loops can be nested
  6. strings are iterable

Executing Python programs

To run a Python program from the Linux command line:


$ python ex5.py
    

Alternatively, ensure the first line of your script contains:

#!/usr/bin/env python

Make your script executable and run:


$ chmod +x ex5.pl
$ ./ex5.py
    

Homework #2

  • Due: 2017-02-21 09:00:00