L19 worksheet ¶
The
print()
function is used to display output on the screen.
Here's a simple example:
print("Computational biology is the best major!")
print("Welcome to Python programming!")
print("Welcome", "to", "Python", "programming!")
print(42)
print(3.14)
print("The answer is:", 42)
print("Sometimes I want to\n\n\nbe dramatic.")
Comments are used to explain what your code does. They are ignored by Python when the code is run.
# This is a single-line comment
print("Comments are ignored by Python.")
"""
This is a multi-line comment.
It can span multiple lines.
"""
print("Use comments to explain your code!")
🏅 Exercises
1.1 In the cell below, print your name, age, and favorite color on one line. For example
Alex, 22, Purple
1.2 In the cell below, print the same information but only one on each line. For example,
Alex
22
Purple
1.3
In the cell below, print the same text as before but using only one
print
statement.
Arithmetic operations ¶
In this section, you will learn how to:
- Perform basic arithmetic operations using Python.
- Understand operator precedence.
Relevant material: Arithmetic
Python supports the following arithmetic operators:
Operator | Description | Example |
---|---|---|
+
|
Addition |
2 + 3
→
5
|
-
|
Subtraction |
7 - 4
→
3
|
*
|
Multiplication |
3 * 4
→
12
|
/
|
Division |
8 / 2
→
4.0
|
//
|
Floor Division |
7 // 3
→
2
|
%
|
Modulus (remainder) |
7 % 3
→
1
|
**
|
Exponentiation |
2 ** 3
→
8
|
# Addition
print(5 + 3)
# Subtraction
print(10 - 7)
# Multiplication
print(4 * 6)
# Division
print(9 / 3)
# Floor Division
print(7 // 2)
# Modulus
print(8 % 3)
# Exponentiation
print(2**4)
When multiple operators are used in a single expression, Python follows operator precedence (order of operations):
-
Parentheses
()
-
Exponentiation
**
-
Multiplication
*
, Division/
, Floor Division//
, Modulus%
-
Addition
+
, Subtraction-
# Parentheses first
print((2 + 3) * 4)
# Exponentiation before multiplication
print(2**3 * 4)
# Left-to-right for operators with same precedence
print(10 / 2 * 3)
🏅 Exercises
2.1
Evaluate the expression $\frac{(3 + 5)^{2}}{2 \cdot 4}$.
(Hint: the answer is
8.0
.)
Variables ¶
In this section, you will learn:
- How to define and use variables in Python.
- Rules for naming variables.
- The importance of variables in programming.
Relevant material: variables
Variables are containers for storing data values. Think of them as labels for information you want to use in your program.
# Defining variables
name = "Alice"
age = 25
pi = 3.141592
# Using variables
print(name)
print(age)
print(pi)
Alice 25 3.141592
name, age, pi = "Bob", 111, 3.141_592
print(name, age, pi)
name = "Bill"
print(name, age, pi)
Bob 111 3.141592 bill 111 3.141592
Python automatically assigns a type to a variable based on the value you assign.
Type | Description | Example |
---|---|---|
int
|
Integer (whole number) |
age = 25
|
float
|
Decimal number |
pi = 3.14
|
str
|
String (text) |
name = "Bob"
|
bool
|
Boolean (True/False) |
is_valid = True
|
Related material: Data types
x = 10
print(type(x))
y = 3.14
print(type(y))
z = "Hello"
print(type(z))
🏅 Exercises
3.1
: Using the variables
length
,
height
, and
width
, compute the volume of a box with dimensions 10, 5, and 3, respectively.
3.2
: Imagine drawing a line from one corner of the rectangular box to the opposite corner, creating a right triangle with sides representing the length, width, and height; the diagonal is the hypotenuse of this triangle with length $\sqrt{l^2 + h^2 + w^2}$.
Calculate this diagonal length using the same
length
,
height
, and
width
variables below.
3.3
: Change the
length
,
height
, and
width
variables defined in
3.1
to be 8.32, 5.433, and 12.1 and recompute the diagonal using the same cell in
3.2
.
Conditionals ¶
In this section, you will learn:
-
How to use
if
,elif
, andelse
statements to control the flow of a program. - How to write programs that make decisions based on conditions.
Related material: conditionals
Conditional statements allow a program to execute different blocks of code based on certain conditions.
if condition:
# Code to execute if condition is True
elif another_condition:
# Code to execute if another_condition is True
else:
# Code to execute if none of the conditions are True
if ¶
The
if
statement checks if a condition is
True
.
If it is, the code block inside it runs.
age = 20
if age >= 18:
print("You are an adult!")
if-else ¶
The
else
block runs if the condition in the
if
statement is
False
.
temperature = 15 # Celsius
if temperature > 20:
print("It's warm outside.")
else:
print("It's cold outside.")
if-elif-else ¶
The
elif
(short for "else if") allows for multiple conditions to be checked.
grade = 85
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
else:
print("F")
Logical operators ¶
You can combine conditions using logical operators:
-
and
: All conditions must beTrue
. -
or
: At least one condition must beTrue
. -
not
: Negates the condition.
# Using 'and'
age = 25
if age > 18 and age < 30:
print("You are a young adult.")
# Using 'or'
day = "Saturday"
if day == "Saturday" or day == "Sunday":
print("It's the weekend!")
# Using 'not'
is_raining = False
if not is_raining:
print("You don't need an umbrella.")
🏅 Exercises
4.1 Write a program that checks if a number is positive, negative, or zero.
4.2 Write a program that determines whether the year is a leap year or not using the following rules:
- A year is a leap year if it is divisible by 4.
- However, if the year is divisible by 100, it is not a leap year unless it is also divisible by 400.
Print an appropriate message based on the result.
Collections ¶
In this section, you will learn:
- What collections are and why they are useful.
- How to use lists, tuples, and dictionaries in Python.
Related material: Collections
Collections are data structures that allow you to group multiple values together. Python provides several types of collections, such as:
- Lists: Ordered, changeable, and allow duplicate values.
- Tuples: Ordered, unchangeable, and allow duplicate values.
- Dictionaries: Unordered, changeable, and store data in key-value pairs.
Lists ¶
A list is a collection that is ordered and changeable.
Lists are defined using square brackets
[]
.
Related material: Lists
# Creating a list
fruits = ["apple", "banana", "cherry"]
# Accessing items by index
print(fruits[0])
# Modifying a list
fruits[1] = "blueberry"
print(fruits)
# Adding items to a list
fruits.append("orange")
print(fruits)
# Removing items from a list
fruits.remove("cherry")
print(fruits)
# Creating a tuple
coordinates = (4, 5)
# Accessing items by index
print(coordinates[0])
# Tuples are immutable, so you cannot modify them
coordinates[0] = 10
Dictionaries ¶
A dictionary is a collection that stores data in key-value pairs.
Dictionaries are defined using curly braces
{}
.
Related material: Dictionary
# Creating a dictionary
person = {"name": "Alice", "age": 25, "city": "New York"}
# Accessing values by key
print(person["name"])
person["age"] = 26
print(person)
# Adding a new key-value pair
person["profession"] = "Engineer"
print(person)
# Removing a key-value pair
del person["city"]
print(person)
🏅 Exercises
5.1 In this exercise, you'll work with a list of your favorite movies, making modifications to practice managing and updating data. Start by creating a list of your favorite movies, including at least a few titles that you enjoy. Once your initial list is ready, add a new movie to it, expanding your collection. Next, update the list by replacing the second movie with a different title of your choice. Finally, remove the last movie from the list, leaving you with a revised selection of your favorites. This exercise will help you practice creating, updating, and modifying lists step by step.
5.2 In this exercise, you'll work with tuples, which are immutable sequences in Python. Begin by creating a tuple containing three numbers of your choice. Once the tuple is defined, print the first number and the last number in the tuple. This will help you practice accessing elements in a tuple by their position.
5.3
-
Create a dictionary to represent a shopping cart with the following items and their quantities:
-
"apple": 3
-
"banana": 2
-
"milk": 1
-
-
Update the quantity of
"apple"
to5
. -
Add a new item to the dictionary:
"bread"
with a quantity of2
. -
Remove the item
"banana"
from the dictionary. - Print the updated shopping cart to display the final contents and their quantities.
Loops ¶
In this section, you will learn:
-
How to use
for
andwhile
loops to repeat actions. -
How to use the
range()
function to control loops.
Related material: Loops
Loops allow you to repeat a block of code multiple times. Python supports two main types of loops:
-
for
loops: Used to iterate over a sequence (e.g., a list or a range of numbers). -
while
loops: Used to repeat code as long as a condition isTrue
.
for ¶
A
for
loop iterates over a sequence (like a list or string) and executes a block of code for each item.
# Iterating over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# Iterating over a string
for char in "Python":
print(char)
range() ¶
The
range()
function generates a sequence of numbers, often used in loops.
for i in range(5):
print(i)
for i in range(1, 6):
print(i)
for i in range(0, 10, 2):
print(i)
for i in range(5):
print("Yay!")
while ¶
A
while
loop keeps executing as long as a condition is
True
.
count = 0
while count < 5:
print(count)
count += 1
Breaking out ¶
You can use
break
to exit a loop and
continue
to skip the rest of the current iteration.
for i in range(10):
if i == 5:
break
print(i)
for i in range(5):
if i == 2:
continue
print(i)
🏅 Exercises
6.1
: Write a
for
loop to print even numbers between 0 and 20.
6.2
: Write a
while
loop to print numbers from 5 down to 1.
6.3
: Write a program that uses a
for
loop to calculate the factorial of the number (e.g.,
5! = 5 * 4 * 3 * 2 * 1
).
Example output:
The factorial of 5 is: 120
Activities ¶
For the remainder of our class, we will work on activities to practice our new programming knowledge.
GC content calculator ¶
In this activity, you will write a Python function that calculates the
GC content
of a DNA sequence using a
for
loop.
GC content is the percentage of bases in a DNA sequence that are either Guanine (
G
) or Cytosine (
C
).
-
Define a function named
compute_gc_content
that:- Takes a single string as a parameter (representing a DNA sequence).
-
Iterates over the characters in the sequence using a
for
loop. -
Counts the occurrences of
G
andC
bases (case-insensitive). - Computes the GC content as a percentage of the total sequence length.
- Returns the GC content as a float rounded to two decimal places.
-
Your function should:
- Ignore lowercase vs. uppercase differences (e.g., treat "G" and "g" the same).
-
Handle cases where the input string is empty by returning
0.0
.
Example 1:
# Input
dna_sequence = "AGCTATAG"
result = compute_gc_content(dna_sequence)
# Output
print(result) # Output: 37.50
Example 2:
# Input
dna_sequence = "GGGCCC"
result = compute_gc_content(dna_sequence)
# Output
print(result) # Output: 100.00
Example 3:
# Input
dna_sequence = ""
result = compute_gc_content(dna_sequence)
# Output
print(result) # Output: 0.0
Hints
-
Use a
for
loop to iterate through each character in the DNA sequence. -
Use an accumulator variable to count the number of
G
andC
bases. -
Use the
len()
function to calculate the total length of the DNA sequence.
For a bonus challenge, modify your function to:
-
Skip invalid characters (anything other than
A
,T
,G
, orC
) and continue processing. - Print a warning if invalid characters are detected.
Test your function with sequences like:
"AGCTXTAGGZ"
and
"123AGCT!"
Counting base frequencies ¶
In this activity, you will write a Python function that calculates the frequency of each base (A, T, G, C) in a DNA sequence. This activity builds on your understanding of loops and adds a layer of complexity by introducing dictionaries.
-
Define a function named
count_base_frequencies
that:- Takes a single string as a parameter (representing a DNA sequence).
-
Uses a dictionary to store the counts for each base (
A
,T
,G
, andC
). -
Iterates over the DNA sequence using a
for
loop and updates the dictionary counts. - Returns the dictionary with base frequencies.
-
Your function should:
- Ignore case sensitivity (e.g., treat "G" and "g" the same).
- Handle cases where the input string contains invalid characters by skipping them.
- Handle an empty string by returning an empty dictionary.
Example 1:
# Input
dna_sequence = "AGCTAGCTAGCTA"
result = count_base_frequencies(dna_sequence)
# Output
print(result) # Output: {'A': 4, 'T': 3, 'G': 3, 'C': 3}
Example 2:
# Input
dna_sequence = "GGGCCC"
result = count_base_frequencies(dna_sequence)
# Output
print(result) # Output: {'A': 0, 'T': 0, 'G': 3, 'C': 3}
For a bonus challenge, modify your function to calculate and return the frequency of invalid characters in the sequence.
-
Example Input:
"AGCTXTAGGZ"
-
Example Output:
{ 'A': 2, 'T': 2, 'G': 3, 'C': 1, 'Invalid': 2 }
Computing the Average Phred Score ¶
In this activity, you will:
-
Load a FASTQ file using Python's
open()
function. -
Use the
readlines()
method to process the contents of the file. - Calculate the average Phred score for the quality scores in the FASTQ file.
A FASTQ file contains sequencing reads and their quality scores. Each read spans four lines:
-
Line 1
: Sequence identifier (starts with
@
). - Line 2 : DNA sequence.
-
Line 3
: Separator line (starts with
+
). - Line 4 : Quality scores (one ASCII character per base).
Here is an example of two FASTQ reads.
@SEQ_ID
GATTACA
+
IIIIIII
@SEQ_ID2
CCGGAAT
+
JJJJJJJ
Quality scores in the FASTQ file are encoded as ASCII characters.
To calculate the Phred score for a character, subtract
33
from its ASCII value.
phred_score = ord(character) - 33
Instructions
-
Write a function
compute_average_phred
that:- Takes the name of a FASTQ file as input.
- Reads the file line by line.
- Extracts quality score lines (every 4th line starting from the 4th line).
- Converts each character in the quality score lines to its Phred score.
- Computes and returns the average Phred score across all characters.
-
Your function should handle:
-
Empty files by returning
0.0
. -
Files with no quality score lines by returning
0.0
.
-
Empty files by returning
Note: I want you to use Google to figure out how to open and read a file in Python.
Specifically, you should use the keyword
with
in your code.
You can use this example FASTQ .