A Minimal Bookdown Book
1
Introduction
1.1
Part 1 - Language Basics
1.2
Part 2 - Turning Ideas into Code
1.3
Part 3 - Biological Problems
2
PART 1 - LANGUAGE BASICS
3
Simple Commands and Data Structures
3.1
How to Run Python Programs?
3.2
Printing on the Screen
3.2.1
Commas in ‘print’ Statements
3.2.2
Comments
3.2.3
Summary of rules
3.3
Mathematical Operations
3.4
Variables
3.4.1
Integer and Float Variables
3.4.2
Printing a Variable
3.4.3
Boolean Variables
3.4.4
String Variables
3.5
Python is not Algebra
3.5.1
i=i+a
3.5.2
Integer Division
3.5.3
Exchanging numbers
3.5.4
Addition of strings
3.6
Lists
3.6.1
Keyword ‘in’
3.6.2
Keyword - del
3.7
Keywords are Special Words
3.7.1
Special words cannot be used as names
3.8
Built-in Functions
3.8.1
Function - range()
3.8.2
Function - len()
3.8.3
Function - float()
3.8.4
Function - int()
3.8.5
Function - str()
3.8.6
References
4
Flow of Code
4.1
Rule 1 - Top to Bottom
4.2
Rule 2 - Right to Left
4.3
Rule 3 - Comments
4.4
Rule 4 - Variables
4.5
Changing the Standard Flow
5
Loops
5.1
Expansion of ‘for’ Loops
5.1.1
Expand -
5.2
What will this code do?
5.2.1
Hint
5.2.2
Solution
5.3
What will this code do?
5.3.1
Answer -
5.4
Explain the outputs of the following two codes.
5.4.1
Solution
5.5
Using ‘for’ over String
5.6
Keywords ‘break’ and ‘continue’
5.7
Summary
6
Conditional Statements
6.1
if, else, elif
6.2
Loop (‘while’)
6.3
Keywords ‘break’ and ‘continue’
6.4
Summary
7
Creating New Functions
7.1
Code Flow with Functions
7.2
Providing Default Parameter
7.3
Write a function to compute Fibonacci numbers.
7.4
Recursive Functions
7.5
Importing Functions from a File
8
Dictionary
8.1
Dictionary vs List
8.2
Keyword -
in
8.3
Keyword -
del
8.4
Using ‘for’ over a Dictionary
9
PART 2 - TURNING IDEAS INTO CODE
10
Simple Problems
10.1
Sum of Integers
10.1.1
Step 1
10.1.2
Step 2
10.1.3
Step 3
10.2
Sum of Odd Numbers
10.2.1
Step 1
10.2.2
Step 2
10.2.3
Step 3
10.3
Sum of Squares
10.3.1
Step 1
10.3.2
Step 2
10.3.3
Step 3
10.4
Finding Highest Number from a List
10.4.1
STEP 1
10.4.2
STEP 2, 3
10.5
Shortest Nucleotide Sequence
10.6
Find Position of the Shortest Sequence
10.7
Exercises
10.7.1
Sum of Powers of Three
10.7.2
Sum of Powers of Two
10.7.3
Factorial
11
Complex Problem - Rotate a List
11.1
Problem Solving Strategy
11.2
Solving the Rotation Problem
11.2.1
STEP 1. Convert into a small problem
11.2.2
STEP 2. Solve on paper
11.2.3
STEP 3. Convert the box solution to Python
11.2.4
STEP 4. Convert into Loop
11.2.5
STEP 5. Use Range
11.2.6
STEP 6. Generalization
11.3
Strategies for Difficult Problems
11.3.1
Develop tests
11.3.2
Develop an algorithm
11.3.3
Choose a data structure
11.3.4
Break big problem into small problems
11.3.5
Debug Faulty Code
11.3.6
Make Code Run Faster
12
Reversing a list of numbers
12.1
STEP 1. Convert into a small problem
12.2
STEP 2. Solve on Paper
12.3
STEP 3. Write Python Code without Loop
12.4
STEP 4. Use Loop
12.5
STEP 5. Use range
12.6
STEP 6. Generalize
12.7
Exercises
13
Using ‘while’ Loop
13.1
STEP 1 - Go Small
13.2
STEP 2 - Use Loop
13.3
STEP 3 - Use Range
13.4
Using “break” to Leave Loop Early
13.5
Using “while” Loop
13.6
Example 2. Check whether a number can be expressed as sum of squares
14
Common Errors and Logical Flaws
14.1
Common Error Messages
14.1.1
NameError
14.1.2
SyntaxError
14.1.3
IndentationError
14.1.4
IndexError/KeyError
14.1.5
TypeError
14.2
Common Logical Flaws
14.2.1
Using Loops Incorrectly
14.2.2
Exchanging Numbers
14.2.3
Infinite Loop in ‘while’
14.2.4
List of strings versus list as a string
14.2.5
Confusing Between readline() vs readlines()
14.2.6
Not Removing Newline after Reading Text from a File
14.2.7
Mixing up Between [] vs ()
14.2.8
Mixing up Between the Left and Right Sides of Expressions
14.2.9
String-related Errors
14.2.10
Function-related Errors
14.2.11
Dictionary-related Errors
14.3
Debugging Concept
14.4
Additional References
15
PART 3 - BIOLOGICAL PROBLEMS
15.1
Problem 1 - Count Nucleotides
15.2
Problem 2 - Reverse Complement
15.2.1
Reverse complement
15.3
Problem 3 - Palindromes
15.3.1
Palindrome in Biology
15.4
Problem 4 - Translation
16
Biology Problems
16.1
Counting Nucleotides
16.2
Reverse Complement
16.3
Translating Nucleotide Sequences to into Protein Sequences
16.4
Computing GC-content of a genome
16.5
Counting K-mers in the E. coli Genome
17
Using Dictionary on Biological Data
17.1
Problem - Counting of nucleotides
17.1.1
STEP 1 - Simplify the Problem
17.1.2
STEP 2 - Replace with Loop
17.2
Problem - Counting of nucleotide pairs and triplets
17.2.1
Introducing Dictionary
17.2.2
Solving Previous Counting Problem Using Dictionary
17.2.3
Solving The Problem of Counting Pairs
17.3
Exercise
18
Input/Output
18.1
Setting up an IDE
18.1.1
Code
18.2
Function - readline()
18.3
Function - rstrip()
18.4
Function - readlines()
18.5
Reading from a file
18.6
Writing in a file
18.6.1
Example
19
Reading FASTA Files
19.1
Data structure
19.2
Python code to read the FASTA file
19.3
Describing How to Code (Based on Code for Reading a FASTA File)
19.4
Step 1. Reading each line
19.5
Step 2. Processing the ID
19.6
Step 3. Processing the non-ID lines from FASTA file
19.7
Step 4. Data structure
19.8
Step 5. For loop and final code
19.9
Line by line analysis
19.10
General guidelines
19.10.1
line 1
19.10.2
line 2
19.10.3
line 3
19.10.4
line 4
19.10.5
lines 5,6
19.10.6
lines 7, 8
19.10.7
line 9
19.10.8
line 10
19.10.9
line 11,12
19.10.10
line 13
19.11
Debugging
20
Python vs R
20.1
Sum of integers
20.1.1
Python Solution
20.1.2
R Solution
20.2
More Complex Sum
20.2.1
Python Solution
20.2.2
R Solution
20.3
Compute the sum
1*100 + 2*99+ ....+ 100*1
20.3.1
Python Solution
20.3.2
R Solution
20.4
How many of
(1*100, 2*99, 3*98, ..., 100*1)
are greater than 2000?
20.4.1
Python Solution
20.4.2
R Solution
20.5
What is the sum of members of
(1*100, 2*99, 3*98, ..., 100*1)
greater than 2000?
20.5.1
Python Solution
20.5.2
R Solution
20.6
Rotate a list of numbers
20.6.1
Python Solution
20.6.2
R Solution
20.7
Check Whether a Number is Prime
20.7.1
Python Solution
20.7.2
R Solution
21
Appendix
21.1
Printing on the Screen
21.1.1
Commas in ‘print’ Statements
21.1.2
Summary of rules
21.2
Comments
21.2.1
Summary of rules
21.3
Mathematical Operations
21.4
Numbers and Boolean Variables
21.4.1
Printing a Variable
21.4.2
Integers and Decimal Numbers
21.4.3
Boolean Variables
21.4.4
Exercises
21.5
Lists
21.6
Built-in Functions
21.6.1
Function - range()
21.6.2
Function - len()
21.6.3
Function - float()
21.6.4
Function - int()
21.7
List-related Functions
21.7.1
Function - sort()
21.7.2
Function - index()
21.7.3
Function - append()
21.7.4
Function - remove()
21.8
Standard Flow of Code
21.8.1
Changing the standard flow
21.9
Conditional Statements
21.9.1
Summary of rules
21.9.2
Exercises
21.10
‘for’ Loops
21.10.1
Expansion of ‘for’ Loops
21.10.2
Keywords ‘break’ and ‘continue’
21.10.3
Summary
21.10.4
Exercises
21.11
‘while’ Loops
21.11.1
Keywords ‘break’ and ‘continue’
21.11.2
Summary
21.12
Creating New Functions
21.12.1
Code Flow with Functions
21.12.2
Default Parameter
Published with bookdown
Python for Biology
Chapter 9
PART 2 - TURNING IDEAS INTO CODE