{
"metadata": {
"name": "",
"signature": "sha256:893b08422a810351790f27c072914742da3cb5fc24e1b29db4c060396708ae60"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Py4Life](https://raw.githubusercontent.com/Py4Life/TAU2015/gh-pages/img/Py4Life-logo-small.png)](http://py4life.github.io/TAU2015/)\n",
"## Lecture 1 - 11.3.2015\n",
"### Last update: 11.3.2015\n",
"### Tel-Aviv University / 0411-3122 / Spring 2015"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## My first python script"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\"Hello ATGC!\""
],
"language": "python",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## What can we do with python?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Use python as a calculator\n",
"We can peform simple calculations\n",
"![calc](http://upload.wikimedia.org/wikipedia/commons/8/81/Casio_fx-991MS_scientific_calculator_1aa.jpg)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"4"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"4+5"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"(4+2-11)*3"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from math import exp\n",
"exp(-9)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from math import sin,pi\n",
"sin(pi/3)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Print stuff to the screen\n",
"We can just print text"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(\"Welcome to python for life-science course!\")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Or we can print text along with some calculation"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(\"The product of 7 and 8 is\",7*8)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Class exercise 1A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1) Print to the screen the following sentences: \n",
"\"I'm a genius!\"\n",
"\"7 + 6 = \" RESULT \n",
"\"my name is \" YOUR NAME "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Your code here:\n",
"print(\"I'm a genius!\")\n",
"print(\"7 + 6 =\",7+6)\n",
"print(\"My name is Inigo Montoya\")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variables\n",
"We can store _values_ in the computer's memory, instead of just calculating/printing them. Values are stored within _variables_. \n",
"A variable always has:\n",
"- A name\n",
"- A value\n",
"- A defined type (number,text,etc.)\n",
"\n",
"To insert a _value_ into a _variable_, we use _asignments_, specifically using the '=' sign. "
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = 5"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once a variable has been declared, we can use it to get its value."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(a)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a + 7"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can assign new variables"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"b = a * 2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a + b"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can assign a new value to an existing variable, overwriting the previous value."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(\"a is\",a)\n",
"a = 8\n",
"print(\"and now a is\",a)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What happens to b???"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(b)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can determine a variable's type using the type() command.\n",
"There are integers (int type)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(a)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Strings (text) - we'll talk more about strings next time."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq = 'ATGCGTATAGCAGATACAGt'\n",
"type(seq)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"floating point (real numbers)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"pi = 3.14159265359\n",
"type(pi)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and another thing, called boolean variables, which get either _True_ or _False_. We'll come back to these soon."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"booly = True\n",
"type(booly)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Some notes about variable names__:\n",
"* Make them meaningful! Instead of using _x_, _y_, _a_, _b_ etc, choose names that will resemble the meaning of the variable, such as _sequence_length_, _sum_of_numbers_ and so on.\n",
"* You can choose any name, but you can't include spaces, special characters and words that have special meaning in python (for example _print_).\n",
"* If you need to give long variable names, the convention is either to use underscores, or to start each word with a capitall letter - first_sequence_length or firstSequenceLength. We will use underscores throughout the course."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Comments\n",
"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."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(\"This will be printed\")\n",
"# print(\"This will not be printed\")\n",
"print(\"Another example\") # of a comment "
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Operators\n",
"Operators allow us to perform basic actions on variables\n",
"### Arithmetic operators\n",
"Used on numbers (integers and floating point). We've already seen some.\n",
"![arit](https://raw.githubusercontent.com/yoavram/Py4Life/master/lec1_images/arit_operators.jpg)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 = 8\n",
"num2 = 5"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 + num2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 - num2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 * num2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 / num2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 // num2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 % num2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 ** num2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Class exercise 1B"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1) Define two variables: a and b and give them numeric values of your choice. Then assume these are the lengths of the edges of a right triangle, and use numeric operators to calculate the length of the hypotenuse (\u05d9\u05ea\u05e8). Print out the result. \n",
"Reminder: $c^2 = a^2 + b^2$ \n",
"* Hint: to calculate the squared root of c, use c\\*\\*0.5"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Define a and b\n",
"a = 10\n",
"b = 4.7\n",
"\n",
"# calculate hypotenuse\n",
"c = (a**2 + b**2)**0.5\n",
"\n",
"# print result\n",
"print(c)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Comparisson operators\n",
"These operators are used to compare numbers and strings. They always return boolean values, i.e. True or False.\n",
"![comp](https://raw.githubusercontent.com/yoavram/Py4Life/master/lec1_images/comp_operators.jpg)\n",
"This is pretty straightforward for integers:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 == num2 # Note: '==', not '='"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 == 8"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 > num2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num2 > num1"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 != num2 "
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num2 < 5"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num2 <= 5"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For strings, '<' and '>' operators are based on alphabetical order."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plant = 'Arabidopsis thaliana'\n",
"mammal = 'Mus musculus'\n",
"plant == mammal"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plant > mammal"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plant <= mammal"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Logical operators\n",
"These operators work on _booleans_ rather than integers or strings. Logical operators always return booleans.\n",
"There are three logical operators:\n",
"![logic](https://raw.githubusercontent.com/yoavram/Py4Life/master/lec1_images/logic_operators.jpg)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"(num1 > num2) and (num1 != num2)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"(num1 != num2) and (num1 < num1)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"(num1 != num2) or (num1 < num1)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"num1 == 3 or num2 == 10 or num2 > 7"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"not (num1 > num2)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"not (num1 < num2)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"boolean = (num1 > num2)\n",
"type(boolean)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"(boolean) and num2 == 5"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also think of logical operators as 2X2 matrices, or alternatively - Venn diagrams.\n",
"![logic_venn](https://raw.githubusercontent.com/yoavram/Py4Life/master/lec1_images/logic_venn.jpg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Branching - IF statements\n",
"So far we've seen (small) programs that just start running, and finish when all commands are performed.\n",
"But sometimes we want to perform certain commands only _if_ a condition is met. For this we use _if statements_:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"if num1 > num2:\n",
" print('Yes')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"if num1 < num2:\n",
" print('Yes')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice the colon and the indented block. The syntax is always: \n",
"__if__ _condition_: \n",
" indented commands \n",
"\n",
"Only commands within the indented block are conditional. Other commands will be executed, no matter if the condition is met or not.\n",
"![if flow](https://raw.githubusercontent.com/yoavram/Py4Life/master/lec1_images/if_flow.jpg)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"if num1 > num2:\n",
" print('Yes')\n",
" print('Another operation will follow')\n",
" num1 = 10\n",
"print(num1)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__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."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 15\n",
"y = 8\n",
"if (x > 10 and y < 10) or x * y == 56:\n",
" print('Yes')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 9\n",
"if (x > 10 and y < 10) or x * y == 56:\n",
" print('Yes')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 7\n",
"if (x > 10 and y < 10) or x * y == 56:\n",
" print('Yes')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's write a program that checks if a number is devisible by 17. Remember the Modulus operator...?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 442\n",
"if x % 17 == 0:\n",
" print('Number is devisible by 17!')\n",
"print('End of program.')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can add _else_ statements to perform commands in case the condition is __not__ met, or in other words, if the boolean is False.\n",
"![if else flow](https://raw.githubusercontent.com/yoavram/Py4Life/master/lec1_images/if_else_flow.jpg)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 586\n",
"if x % 17 == 0:\n",
" print('Number is devisible by 17!')\n",
"else:\n",
" print('Number is not devisible by 17!')\n",
"print('End of program.')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = 586\n",
"if x % 17 == 0:\n",
" print('Number is devisible by 17!')\n",
"elif x % 2 == 0:\n",
" print('Number is not devisible by 17, but is even!')\n",
"else:\n",
" print('Number is not devisible by 17, and is odd!')\n",
"print('End of program.')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Class exercise 1C"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1) A leap year is a year that has 366 days (adding February 29th). A year is a leap year if it is divisible by 400, or divisible by 4 but not by 100. E.g., 2012 and 2000 are leap years, but 1900 isn't. Test a year of your choice by using an appropriate _if - else_ statement and print the result."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Choose a year\n",
"year = 1492\n",
"\n",
"# test year\n",
"if (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)):\n",
" print(year,\"is a leap year\")\n",
"else:\n",
" print(year,\"is not a leap year\")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## While loops - continuos If"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One thing computers are very good at (and most humans not) is doing repetitive jobs. \n",
"We use _while_ loops to do something again and again, as long as a condition is met. \n",
"![while](http://www.tutorialspoint.com/images/python_while_loop.jpg)\n",
"The syntax is very similar to that of _if_ statements."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from random import randint\n",
"random_num = randint(1,100)\n",
"while random_num <= 90: # condition\n",
" print(random_num) # indented block\n",
" random_num = randint(1,100)\n",
"print ('Found a number greater than 90!', random_num)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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... \n",
"For example, here we change the value by obtaining a new random number.\n",
"![inf-loop](http://i.imgur.com/4QHKb.jpg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's count how many times it takes to get a random number greater than 90. We'll use a counter variable."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from random import randint\n",
"counter = 1\n",
"random_num = randint(1,100)\n",
"while random_num <= 90: # condition\n",
" print(random_num) # indented block\n",
" random_num = randint(1,100)\n",
" counter = counter + 1 # what's happening here?\n",
"print ('Found a number greater than 90!', random_num, '. It took',counter,'tries.')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Class exercise 1D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1) The Collatz Conjecture (also known as the 3n+1 conjecture) is the conjecture that the following process is finite for every natural number:\n",
"```\n",
"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.\n",
"```\n",
"\n",
"Complete the code below to check if the Collatz conjecture is true for a number of your choice. Print every step of the process."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"m = 555 # integer to apply the conjecture on\n",
"\n",
"n = m\n",
"while n != 1:\n",
" print(n, end=\", \")\n",
" # if n is even\n",
" if n % 2 == 0:\n",
" n = n // 2\n",
" # if n is odd\n",
" else:\n",
" n = 3 * n + 1\n",
"print(1) # 1 was not printed\n",
"print(m, \"is OK\")"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![CollatzXKCD](http://imgs.xkcd.com/comics/collatz_conjecture.png)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fin\n",
"This notebook is part of the _Python Programming for Life Sciences Graduate Students_ course given in Tel-Aviv University, Spring 2015.\n",
"\n",
"Part of this notebook was adapted from the [Lists and Loops](http://pythonforbiologists.com/index.php/introduction-to-python-for-biologists/lists-and-loops/) chapter in Martin Jones's _Python for Biologists_ book.\n",
"\n",
"The notebook was written using [Python](http://pytho.org/) 3.4.1 and [IPython](http://ipython.org/) 2.1.0 (download from [PyZo](http://www.pyzo.org/downloads.html)).\n",
"\n",
"The code is available at https://github.com/Py4Life/TAU2015/blob/master/lecture1.ipynb.\n",
"\n",
"The notebook can be viewed online at http://nbviewer.ipython.org/github/Py4Life/TAU2015/blob/master/lecture1.ipynb.\n",
"\n",
"The notebook is also available as a PDF at https://github.com/Py4Life/TAU2015/blob/master/lecture1.pdf?raw=true.\n",
"\n",
"This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.\n",
"\n",
"![Python logo](https://www.python.org/static/community_logos/python-logo.png)"
]
}
],
"metadata": {}
}
]
}