Python Language Overview

BCH 519
Spring 2019

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

Running Python programs

Run python program from the Linux command line:

$ python myprog.py

Python versions

  • Python 2 vs Python 3
  • Python 2.x is legacy use 3
  • Default python version on vortex is 2.7.5
  • To use python 3 run:
$ module list
No modules loaded
$ python -V
Python 2.7.5

$ module load python/py37-anaconda-2018.12
 Anaconda Python 3.7 version 2018.12 has been loaded. 
$ python -V
Python 3.7.1

Python Documentation

Code to all exercises

  • In course project directory on vortex

/projects/academic/courses/bch519/spring19/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

  • Python programs are text files
  • File names (typically) end in .py
  • print displays the given text (without quotations)
  • The text inside quotes is called a string
  • 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

  • # Comments are important. They help yourself and others understand your code
  • # Anything after the # is ignored by python
  • # 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

  • Python can do basic math
  • Order of operations: (3 * 4) + (6 / 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("Gene {0} has {1} sequences".format(gene, seq_count))
print("The average number of sequences is: {}".format(avg))
# Only works in Python >3.6
print(f"There's a total of {total} sequences")

Then run in the terminal by typing:

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

Exercise 4: What we learned

  • Variable is a named reference to a value
  • Variables can be printed in human-readable form using format
  • Values are converted to strings. {} are placholders of variables
  • A number inside {0} refers to the position of the variable passed to the format function
  • https://docs.python.org/3/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))

Exercise 5: Output

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

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

Exercise 6: Printing Strings

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

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)

Exercise 6: Output

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

  • Use a backslash \ to escape special characters
  • "\t" prints a TAB character "\n" prints a newline
  • """ triple-quote defines strings with many lines of text`
  • "{0} {1} {2} {1}" can re-use placeholders in format string`
  • 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

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

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")

Exercise 7: Output

Then run in the terminal by typing:

$ python ex7.py
Five is greater than four
B

Example 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

  • Conditional constructs allow execution of code given some condition is true/false
  • Join two or more boolean expressions with and/or logic operators
  • Spacing is important!
  • 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

Exercise 9: Loops

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

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)

Exercise 9: Output

Then 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

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

Executing Python programs

To run a Python program from the Linux command line:

$ python myprogram.py

Alternatively, ensure the first line of your script contains:

#!/usr/bin/env python

Make your script executable and run:

$ chmod +x myprogram.py
$ ./myprogram.py

Homework #2

Due: 2019-02-19 09:00:00