"Hello ATGC!"
We can peform simple calculations
4
4+5
(4+2-11)*3
from math import exp
exp(-9)
from math import sin,pi
sin(pi/3)
We can just print text
print("Welcome to python for life-science course!")
Or we can print text along with some calculation
print("The product of 7 and 8 is",7*8)
"I'm a genius!"
"7 + 6 = " RESULT
"my name is " YOUR NAME
# Your code here:
print("I'm a genius!")
print("7 + 6 =",7+6)
print("My name is Inigo Montoya")
We can store values in the computer's memory, instead of just calculating/printing them. Values are stored within variables.
A variable always has:
To insert a value into a variable, we use asignments, specifically using the '=' sign.
a = 5
Once a variable has been declared, we can use it to get its value.
print(a)
a + 7
We can assign new variables
b = a * 2
a + b
We can assign a new value to an existing variable, overwriting the previous value.
print("a is",a)
a = 8
print("and now a is",a)
What happens to b???
print(b)
We can determine a variable's type using the type() command. There are integers (int type)
type(a)
Strings (text) - we'll talk more about strings next time.
seq = 'ATGCGTATAGCAGATACAGt'
type(seq)
floating point (real numbers)
pi = 3.14159265359
type(pi)
and another thing, called boolean variables, which get either True or False. We'll come back to these soon.
booly = True
type(booly)
Some notes about variable names:
We can add explanatory text to our code to make it more readable. We do that by simply adding a '#' in the beginning of a comment.
print("This will be printed")
# print("This will not be printed")
print("Another example") # of a comment
num1 = 8
num2 = 5
num1 + num2
num1 - num2
num1 * num2
num1 / num2
num1 // num2
num1 % num2
num1 ** num2
Reminder: $c^2 = a^2 + b^2$
# Define a and b
a = 10
b = 4.7
# calculate hypotenuse
c = (a**2 + b**2)**0.5
# print result
print(c)
These operators are used to compare numbers and strings. They always return boolean values, i.e. True or False. This is pretty straightforward for integers:
num1 == num2 # Note: '==', not '='
num1 == 8
num1 > num2
num2 > num1
num1 != num2
num2 < 5
num2 <= 5
For strings, '<' and '>' operators are based on alphabetical order.
plant = 'Arabidopsis thaliana'
mammal = 'Mus musculus'
plant == mammal
plant > mammal
plant <= mammal
These operators work on booleans rather than integers or strings. Logical operators always return booleans. There are three logical operators:
(num1 > num2) and (num1 != num2)
(num1 != num2) and (num1 < num1)
(num1 != num2) or (num1 < num1)
num1 == 3 or num2 == 10 or num2 > 7
not (num1 > num2)
not (num1 < num2)
boolean = (num1 > num2)
type(boolean)
(boolean) and num2 == 5
We can also think of logical operators as 2X2 matrices, or alternatively - Venn diagrams.
So far we've seen (small) programs that just start running, and finish when all commands are performed. But sometimes we want to perform certain commands only if a condition is met. For this we use if statements:
if num1 > num2:
print('Yes')
if num1 < num2:
print('Yes')
Notice the colon and the indented block. The syntax is always:
if condition:
indented commands
Only commands within the indented block are conditional. Other commands will be executed, no matter if the condition is met or not.
if num1 > num2:
print('Yes')
print('Another operation will follow')
num1 = 10
print(num1)
Note: the condition expression always returns a boolean, and the indented commands only occur if the boolean has a True value. Therefore, we can use logical operators to create more complex conditions.
x = 15
y = 8
if (x > 10 and y < 10) or x * y == 56:
print('Yes')
x = 9
if (x > 10 and y < 10) or x * y == 56:
print('Yes')
x = 7
if (x > 10 and y < 10) or x * y == 56:
print('Yes')
Let's write a program that checks if a number is devisible by 17. Remember the Modulus operator...?
x = 442
if x % 17 == 0:
print('Number is devisible by 17!')
print('End of program.')
We can add else statements to perform commands in case the condition is not met, or in other words, if the boolean is False.
x = 586
if x % 17 == 0:
print('Number is devisible by 17!')
else:
print('Number is not devisible by 17!')
print('End of program.')
Things get even more interesting when using elif statements, where multiple conditions are tested one by one. Once a condition is met, the corresponding indented commands are performed. If none of the conditions is True, the else block (if exists) is executed.
x = 586
if x % 17 == 0:
print('Number is devisible by 17!')
elif x % 2 == 0:
print('Number is not devisible by 17, but is even!')
else:
print('Number is not devisible by 17, and is odd!')
print('End of program.')
# Choose a year
year = 1492
# test year
if (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)):
print(year,"is a leap year")
else:
print(year,"is not a leap year")
One thing computers are very good at (and most humans not) is doing repetitive jobs.
We use while loops to do something again and again, as long as a condition is met.
The syntax is very similar to that of if statements.
from random import randint
random_num = randint(1,100)
while random_num <= 90: # condition
print(random_num) # indented block
random_num = randint(1,100)
print ('Found a number greater than 90!', random_num)
When using a while loop, always make sure that you change the value of the variable tested in the condition. Otherwise, the condition will always be True
and you will find yourself in an infinite loop...
For example, here we change the value by obtaining a new random number.
Now let's count how many times it takes to get a random number greater than 90. We'll use a counter variable.
from random import randint
counter = 1
random_num = randint(1,100)
while random_num <= 90: # condition
print(random_num) # indented block
random_num = randint(1,100)
counter = counter + 1 # what's happening here?
print ('Found a number greater than 90!', random_num, '. It took',counter,'tries.')
If the number n is even divide it by two (n/2), if it is odd multiply it by 3 and add 1 (3n+1). Repeat this process until you get the number 1.
Complete the code below to check if the Collatz conjecture is true for a number of your choice. Print every step of the process.
m = 555 # integer to apply the conjecture on
n = m
while n != 1:
print(n, end=", ")
# if n is even
if n % 2 == 0:
n = n // 2
# if n is odd
else:
n = 3 * n + 1
print(1) # 1 was not printed
print(m, "is OK")
This notebook is part of the Python Programming for Life Sciences Graduate Students course given in Tel-Aviv University, Spring 2015.
Part of this notebook was adapted from the Lists and Loops chapter in Martin Jones's Python for Biologists book.
The notebook was written using Python 3.4.1 and IPython 2.1.0 (download from PyZo).
The code is available at https://github.com/Py4Life/TAU2015/blob/master/lecture1.ipynb.
The notebook can be viewed online at http://nbviewer.ipython.org/github/Py4Life/TAU2015/blob/master/lecture1.ipynb.
The notebook is also available as a PDF at https://github.com/Py4Life/TAU2015/blob/master/lecture1.pdf?raw=true.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.