Project 1
Assignment A
Python basics
This assignment is designed to help you start building a foundation in Python programming, a skill that will be invaluable as we explore computational techniques and their applications in biology. Whether you are new to programming or already have some experience, this assignment will provide the opportunity to practice essential coding skills and gain confidence in writing Python code.
Remember that programming is a skill that improves with practice, so take your time, experiment with different approaches, and don't hesitate to seek help from the teaching team if you encounter difficulties. Happy coding!
Submitting to Gradescope ¶
To submit your assignment, follow these steps:
- Open your coding environment (e.g., Jupyter Notebook, VS Code, PyCharm, or any text editor you’re using).
-
Ensure your code is complete, well-commented, and saved in a
.py
file format. For example, name your file something likeassignment4.py
.- If you're working in Jupyter Notebook, you can export your file as a Python script by selecting File > Download as > Python (.py) .
- Double-check that the file contains all your completed functions and is ready for grading.
- Log in to Gradescope and navigate to the appropriate course and assignment.
- Click on the "Submit" button for the assignment.
-
Upload your
.py
file by dragging it into the submission area or using the file picker. - Wait for the confirmation that your file has been successfully uploaded. You may also see an automated output check or feedback after submission (if enabled).
You have unlimited submissions before the deadline.
Kaggle learn ¶
To begin, please complete the following interactive lessons on Kaggle Learn .
- For Intro to Programming , please complete the Arithmetic and Variables , Functions , Data Types , and Conditions and Conditional Statements .
- For Python , please complete Hello, Python , Functions and Getting Help , and Booleans and Conditionals .
You only need to complete the Tutorials for each aforementioned lessons. (Doing the Lesson Exercises can be helpful, but is not strictly required.)
Q01 ¶
Change
"Your message here!"
to use a different message.
For instance, you might like to change it to something like:
print("Good morning!")
.
Or, you might like to see what happens if you write something like
print("3+4")
.
Does it return 7, or does it just think of
"3+4"
as just another message?
Make sure that your message is enclosed in quotation marks (
"
), and the message itself does not use quotation marks.
For instance, this will throw an error:
print("She said "great job" and gave me a high-five!")
because the message contains quotation marks.
Feel free to try out multiple messages!
print("Your message here!")
Q02 ¶
A comment in Python has a pound sign (
#
) in front of it, which tells Python to ignore the text after it.
Putting a pound sign in front of a line of code will make Python ignore that code. For instance, this line would be ignored by Python, and nothing would appear in the output:
# print(1+2)
Removing the pound sign will make it so that you can run the code again. When we remove the pound sign in front of a line of code, we call this uncommenting .
Comment out the line
print("Every Villain Is Lemons")
and uncomment
print("Please, print me.")
.
This should result in
"Please, print me."
being printed.
# print("Please, print me.")
print("Every Villain Is Lemons")
Q03 ¶
In the tutorial, you defined several variables to calculate the total number of seconds in a year. Run the next code cell to do the calculation here.
# Create variables
num_years = 4
days_per_year = 365
hours_per_day = 24
mins_per_hour = 60
secs_per_min = 60
# Calculate number of seconds in four years
total_secs = None
Use the next code cell to:
-
Define a variable
births_per_min
and set it to 250.
births_per_min = None
Now, compute how many babies are born in one year using
births_per_min
and store it in the variable
births_per_year
.
Q04 ¶
You are trying to figure out what your letter grade in your class would be given a percentage. Suppose that the class is using the following scale:
- A: 90.0 to 100.0,
- B: 80.0 to 89.9,
- C: 70.0 to 79.9,
- D: 60.0 to 69.9,
- F: 0.0 to 59.9.
Write a function called
calculate_grade
that returns your letter grade given any
percentage
.
For example, to return
"A"
you would write
if percentage >= 90.0:
return "A"
def calculate_grade(percentage):
"""Determine the letter grade based on the given percentage.
Args:
percentage: The percentage score for which to calculate the grade.
This value should be between 0.0 and 100.0.
Returns:
The letter grade corresponding to the percentage:
"""
return None
Example test cases and expected outputs:
print(calculate_grade(95.0)) # Output: "A" (95.0 is between 90.0 and 100.0)
print(calculate_grade(82.5)) # Output: "B" (82.5 is between 80.0 and 89.9)
print(calculate_grade(74.0)) # Output: "C" (74.0 is between 70.0 and 79.9)
print(calculate_grade(90.0)) # Output: "A" (90.0 is the minimum for an "A")
print(calculate_grade(0.0)) # Output: "F" (0.0 is the lowest possible score)
Q05 ¶
Imagine you're managing a fruit stand and need to calculate the total cost for a customer's purchase. At your stand, each apple costs $0.50, and each banana costs $0.30. Your goal is to write a program that calculates the total cost based on how many apples and bananas the customer wants to buy.
Write a function named
calculate_total_cost
that:
-
Takes two arguments:
num_apples
(an integer) andnum_bananas
(an integer). - Calculates the total cost by multiplying the number of apples by their price and the number of bananas by their price.
- Returns the total cost as a float.
def calculate_total_cost(num_apples: int, num_bananas: int) -> float:
"""Calculate the total cost of apples and bananas.
Args:
num_apples: Number of apples.
num_bananas: Number of bananas.
Returns:
Total cost of the purchase.
"""
return None
Example test cases and expected outputs:
print(calculate_total_cost(4, 5)) # Output: 3.5
print(calculate_total_cost(0, 10)) # Output: 3.0
print(calculate_total_cost(7, 0)) # Output: 3.5
print(calculate_total_cost(3, 2)) # Output: 2.1
print(calculate_total_cost(1, 1)) # Output: 0.8
Q06 ¶
In this task, you’ll create a mathematical utility function to perform a simple computation. The function will take two numbers as input, square the first number, add the second number, and return the result.
Write a function named
square_and_add
that:
-
Accepts two arguments:
x
andy
(both integers or floats). -
Squares the value of
x
. -
Adds
y
to the squared value. - Returns the computed result.
def square_and_add(x: float, y: float) -> float:
"""Square the first number and add the second number.
Args:
x: The number to square.
y: The number to add.
Returns:
The result of squaring x and adding y.
"""
return None
Here are some example outputs.
print(square_and_add(3, 5)) # Output: 14
print(square_and_add(-2, 10)) # Output: 14
print(square_and_add(0, 7)) # Output: 7
print(square_and_add(1.5, 2)) # Output: 4.25
print(square_and_add(4, -3)) # Output: 13
Q07 ¶
Imagine you're writing a program to greet people based on the time of day.
The program will ask for the current hour in a 24-hour format and provide an appropriate greeting:
"Good morning!"
,
"Good afternoon!"
, or
"Good evening!"
.
Write a function named
greet_by_time
that:
-
Takes one argument,
hour
, representing the time in 24-hour format as an integer (0–23). -
Returns a string greeting:
-
"Good morning!"
if the hour is between 0 and 11. -
"Good afternoon!"
if the hour is between 12 and 18. -
"Good evening!"
if the hour is between 19 and 23. -
"Invalid time!"
if the hour is not in the valid range.
-
def greet_by_time(hour: float) -> str:
"""Provide a greeting based on the time of day.
Args:
hour: The current hour in 24-hour format.
Returns:
A greeting message.
"""
return None
Here are some example outputs.
print(greet_by_time(2)) # Output: "Good morning!"
print(greet_by_time(22)) # Output: "Good evening!"
print(greet_by_time(-1)) # Output: "Invalid time!"