Friday 26 February 2016

, ,

Practical System Biology Part 2: Python Introduction


Nah, bagi teman-teman yang baru pertama kali belajar programming, kita akan menggunakan buku karya Langtangen, “A primer on scientific programming with Python (3rd edition)”. Coba download buku ini dari perpustakaan kampusmu, atau bisa juga didowload dari github sang author (thanks Langtangen!): 
http://hplgit.github.io/primer.html/doc/pub/half/book.pdf.

Untuk belajar, cara paling cepat dan efektif adalah dengan mencoba mengerjakan beberapa exercise yang ada di buku. Kita akan menggunakan chapter 1, 2, 3, dan 5 untuk memahami dasar-dasar Python.

Sebagai panduan belajar, teman-teman bisa mencoba mengikuti saran berikut:

Chapter 1

Bacaan untuk chapter 1:

  • 1.1 Pengenalan Python
  • 1.2: istilah-istilah di computer science
  • 1.3: pembagian dengan integer dan obyek di Python
  • 1.4.1: menggunakan modul Python

Latihan untuk chapter 1:

Exercises 1.6 

  • Let p be a bank’s interest rate in percent per year. An initial amount A has then grown to:
  • after n years. Make a program for computing how much money 1000 euros have grown to after three years with 5 percent interest rate.
  • Solution to 1.6

Exercises 1.10

  • The bell-shaped Gaussian function, 
  • is one of the most widely used functions in science and technology. The parameters m and s > 0 are prescribed real numbers. Make a program for evaluating this function when m = 0, s = 2, and x = 1. Verify the program’s result by comparing with hand calculations on a calculator.
  • Solution to 1.10

Chapter 2

Bacaan untuk chapter 2:

  • 2.1: while loops
  • 2.2: lists dan for loops
  • 2.3.1 - 2.3.5: contoh list dan loops
  • 2.4: nested lists
  • 2.5: tuples
  • 2.6.2: contoh aplikasi
  • 2.6.3: cara mencari informasi dalam Python

Latihan untuk chapter 2:

Exercise 2.10

  • We want to generate x coordinates between 1 and 2 with spacing 0.01. The coordinates are given by the formula xi = 1 + ih, where h = 0.01 and i runs over integers 0, 1, . . . , 100. Compute the xi values and store them in a list (use a for loop, and append each new xi value to a list, which is empty initially).
  • Solution to 2.10

Exercise 2.12 

  • The following code is supposed to compute the sum function:
  • s = 0; k = 1; M = 100
  • while k < M:
    • s += 1/k
  • print s
  • This program does not work correctly. What are the three errors? (If you try to run the program, nothing will happen on the screen. Type Ctrl+c, i.e., hold down the Control (Ctrl) key and then type the c key, to stop the program.) Write a correct program.
  • Hint. There are two basic ways to find errors in a program: (1) read the program carefully and think about the consequences of each statement, (2) print out intermediate results and compare with hand calculations. First, try method 1 and find as many errors as you can. Thereafter, try method 2 for M = 3 and compare the evolution of s with your own hand calculations.
  • Solution to 2.12

Exercise 2.13

  • Rewrite the corrected version of the program in Exercise 2.12 using a for loop over k values instead of a while loop.
  • Solution to 2.13

Chapter 3

Bacaan untuk chapter 3:

  • 3.1.1 - 3.1.6: functions
  • 3.1.10: the main program
  • 3.2: branching and if statements
  • 3.3.1: using functions

Latihan untuk chapter 3:

Exercise 3.1


  • The the formula above is for converting Fahrenheit degrees to Celsius reads.
  • Write a function C(F) that implements this formula. To verify the implementation of C(F), you can convert a Celsius temperature to Fahrenheit and then back to Celsius again using the F(C) function from Chapter 3.1.1 and the C(F) function. That is, you can check that the boolean expression c == C(F(c)) is True for any temperature c (you should, however, be careful with comparing real numbers with ==, see Exercise 2.24).
  • Solution to 3.1

Exercise 3.4 

  • The standard Python function called sum takes a list as argument and computes the sum of the elements in the list:
  • >>> sum([1,3,5,-5])
  • 4
  • Implement your own version of sum. 
  • Solution to 3.4

Exercise 3.14 

  • Make a Python function gauss(x, m=0, s=1) for computing the Gaussian function 
  • Call gauss(x) and print out the result for x ∈ [−5, 5] (say for 11 uniformly spaced x values). 
  • Solution to 3.14

Exercise 3.23

  • Given a list a, the max function in Python’s standard library computes the largest element in a: max(a). Similarly, min(a) returns the smallest element in a. The purpose of this exercise is to write your own max and min function. Use the following technique: Initialize a variable max_elem by the first element in the list, then visit all the remaining elements (a[1:]), compare each element to max_elem, and if greater, make max_elem refer to that element. Use a similar technique to compute the minimum element. Collect the two pieces of code in functions.
  • Solution to 3.23

Exercise 3.34

  • Write a function count_pairs(dna, pair) that returns the number of occurrences of a pair of characters (pair) in a DNA string (dna). For example, calling the function with dna as ’ACTGCTATCCATT’ and pair as ’AT’ will return 2. 
  • Solution to 3.34

Chapter 5

Bacaan untuk chapter 5:

  • 5.3.1 - 5.3.2: plotting data
  • 5.6.1: arrays
  • 5.6.4: accessing elements of arrays
  • 5.6.7: manipulating the shape of arrays
  • 5.7.1 - 5.7.2: arrays with more than one dimension
Nah, setelah mencoba latihan-latihan diatas, teman-teman pasti sudah lebih familiar dengan Python.

Loh, ga ada latihan untuk Chapter 5 nih??

Jangan khawatir, di postingan selanjutnya, kita akan mencoba belajar membuat plot dengan menggunakan contoh Fluorescence In Situ Hybridization (FISH).



0 comments :

Post a Comment