Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Exercises

01

Beginner

  1. Create a variable farmer_name (String) with a name of your choice.

  2. Create a variable age (Integer).

  3. Create a variable is_land_owner (Boolean).

  4. Print them all using print().

02

Beginner

  1. Create two variables: plot_area_sqm and production_kg. Assign 5000 and 2500 as their respective values.

  2. Compute for the yield (in tons) per hectare and assign the value to a new variable yield_tons_per_ha.

    • Hint: 1 Hectare = 10,000 sqm. 1 Ton = 1,000 kg.

  3. Print the result.

03

Intermediate

  1. Using the formula in Exercise 02, create a function that takes plot_area_sqm and production_kg as parameters.

  2. Return a dictionary as output with the following keys:

    • plot_area_sqm

    • production_kg

    • yield_tons_per_ha

04

Beginner

  1. Import the random library.

  2. Use random.randint(50, 100) to simulate a harvest weight in kilograms.

  3. Print: “Today’s harvest is [X] kg.”

05

Intermediate

  1. Import the datetime library.

  2. Create a function called date_to_census that takes one argument, census_date, which should be a future date. The function will return how many days from today the census will be.

  3. Test the function with census_date = May 1, 2026.

06

Advanced

  1. You have the following list of census dates: dates = [“2025.05.01”, “2026.03.11”, “2025.08.15”, “2026.01.11”, “2025.12.25”, “2025.11.20”, “2026.03.02”, “2026.05.01”]

  2. Create a function process_dates that takes a list of date strings as input.

  3. For each date string, check if the date is in the future by comparing its difference from today’s date with zero. If it’s positive, return an output indicating how many days until the census; otherwise, raise an exception and print a warning message “Invalid Date.”

  4. The return value should be a dict with the following keys:

    • census_date: date of census

    • days_to_go: # of days until census or -1 if “Invalid Date”

  5. The funtion should continue running for the entire list.

07

Beginner

  1. Create an estimated_revenue function that takes the following parameters:

    • tons_harvested

    • price_per_kg

  2. Test it with tons_harvested = 0.5 and price_per_kg = 100

08

Advanced

  1. You have the following list of crops and their prices:

    • corn: 13.00

    • palay: 16.50

    • potato: 14.25

    • tomato: 22.00

    • bell pepper: 30.00

  2. You also have the following table:

NameCropTons HarvestedYear
JuanCorn1002022
MariaPalay1202022
JosePotato902023
JuanTomato1102023
PedroBell Pepper1302023
PedroCorn952023
KikoPalay1252024
MariaPotato1052024
JoseTomato1152025
KikoBell Pepper1352025
  1. How much is the estimated revenue of each individual?

  2. How much is the estimated revenue per crop?

09

Intermediate

  1. You have a list of dates with different formats.

  2. Convert all dates to YYYY-MM-DD format and save to a new list.

  3. Hint: Use the parser method in the dateutil library

10

Advanced

  1. You have a list of dates with different formats.

  2. Use regular expressions to find dates that follow the YYYY-MM-DD format

11

Intermediate

  1. Initialize tank_level = 0 and max_capacity = 50.

  2. Define a pump_speed variable to increment tank_level in each iteration.

  3. Use a while loop with the following conditions:

    • The first time tank_level >= max_capacity/2, print “Halfway there!” and continue.

    • If tank_level == max_capacity, break.

  4. After the loop, print the final level, including “overflow by X” if exceeded.

Try with different values of pump_speed.

12

Advanced

  1. Initialize tank_level = 0 and max_capacity = 50.

  2. Define a pump_speed variable to increment tank_level in each iteration.

  3. Use a while loop with the following conditions:

    • Print milestones (10%, 20%, ..., 100%) as the tank level approaches capacity

    • The first time tank_level >= 25, print “Halfway there!” and continue.

    • If tank_level == max_capacity, break.

  4. After the loop, print the final level, including “overflow by X” if exceeded.

Try with different values of pump_speed.

13

Advanced

  1. Create a Livestock class with attributes for species and count.

  2. Add methods to Livestock for checking herd status and string representation.

  3. Create a Farmer class with attributes for name and livestock inventory.

  4. Implement methods in Farmer for adding and removing livestock from the farmer’s inventory.

    • The add_livestock() method allows farmers to add Livestock objects to their inventory.

    • The remove_livestock() method enables farmers to remove specific types of livestock from their inventory.

  5. Update __str__() method in both classes to provide human-readable output.

    • Livestock prints the registered herd.

    • Farmer prints his name and his registered herd.

  6. Write example usage code to demonstrate each class’s functionality.

14

Intermediate

  1. Create a file yields.txt with these lines:

    10.5
    8.2
    Missing
    5.0

    Plus points for using Python to create the file.

  2. Write a script to read the file and sum the numbers.

  3. Use try-except to ignore the “Missing” line without crashing. Print “Skipped line” when it happens.

15

Advanced

  1. Download the file named “brgy_population.txt”

  2. Read the file using with open(...)

  3. Loop through each line:

    • Clean the lines by removing the whitespaces

    • Ignore the lines that start with #

    • Split the line to get the population number

  4. Calculate and print the total population.

Sample Data

# Data from NCR (2020)
NCR|   Barangay 143 | 1401
NCR|Barangay 144| 880
   NCR| Barangay 145| 1029
# End of Report

16

Intermediate

  1. List: farm_sizes = [1.2, 0.0, 3.5, -1.0, 5.0].

  2. Use filter to keep only positive numbers (> 0).

  3. Use map on the result to calculate the potential production (multiply by 4.0 tons/ha).

  4. Print the final estimates.

  5. Rewrite your solution using a single List Comprehension instead of map/filter.

  6. Which do you prefer?