Exercises
your_name = ""01¶
Beginner
Create a variable
farmer_name(String) with a name of your choice.Create a variable
age(Integer).Create a variable
is_land_owner(Boolean).Print them all using
print().
# Write your solution here02¶
Beginner
Create two variables:
plot_area_sqmandproduction_kg. Assign 5000 and 2500 as their respective values.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.
Print the result.
# Write your solution here03¶
Intermediate
Using the formula in Exercise 02, create a function that takes
plot_area_sqmandproduction_kgas parameters.Return a dictionary as output with the following keys:
plot_area_sqmproduction_kgyield_tons_per_ha
# Write your solution here04¶
Beginner
Import the
randomlibrary.Use
random.randint(50, 100)to simulate a harvest weight in kilograms.Print: “Today’s harvest is [X] kg.”
# Write your solution here05¶
Intermediate
Import the
datetimelibrary.Create a function called
date_to_censusthat takes one argument,census_date, which should be a future date. The function will return how many days from today the census will be.Test the function with
census_date= May 1, 2026.
# Write your solution here06¶
Advanced
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”]
Create a function
process_datesthat takes a list of date strings as input.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.”
The return value should be a dict with the following keys:
census_date: date of censusdays_to_go: # of days until census or -1 if “Invalid Date”
The funtion should continue running for the entire list.
# Write your solution here07¶
Beginner
Create an
estimated_revenuefunction that takes the following parameters:tons_harvestedprice_per_kg
Test it with
tons_harvested= 0.5 andprice_per_kg= 100
# Write your solution here08¶
Advanced
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
You also have the following table:
| Name | Crop | Tons Harvested | Year |
|---|---|---|---|
| Juan | Corn | 100 | 2022 |
| Maria | Palay | 120 | 2022 |
| Jose | Potato | 90 | 2023 |
| Juan | Tomato | 110 | 2023 |
| Pedro | Bell Pepper | 130 | 2023 |
| Pedro | Corn | 95 | 2023 |
| Kiko | Palay | 125 | 2024 |
| Maria | Potato | 105 | 2024 |
| Jose | Tomato | 115 | 2025 |
| Kiko | Bell Pepper | 135 | 2025 |
How much is the estimated revenue of each individual?
How much is the estimated revenue per crop?
# Write your solution here09¶
Intermediate
You have a list of dates with different formats.
Convert all dates to YYYY-MM-DD format and save to a new list.
Hint: Use the
parsermethod in thedateutillibrary
# Create a large list of dates in different formats
dates = [
"2022-01-01",
"2022/02/02",
"2022.03.03",
"2022,04,04",
"January 1, 2022",
"February 2, 2022",
"March 3, 2022",
"April 4, 2022",
"May 5, 2022",
"June 6, 2022",
"July 7, 2022",
"August 8, 2022",
"September 9, 2022",
"October 10, 2022",
"November 11, 2022",
"December 12, 2022",
"2023-01-01",
"2024/02/02",
"2025.03.03",
"2026,04,04"
]
# Write your solution here10¶
Advanced
You have a list of dates with different formats.
Use regular expressions to find dates that follow the YYYY-MM-DD format
import re
# Create a large list of dates in different formats
dates = [
"2022-01-01",
"2022/02/02",
"2022.03.03",
"2022,04,04",
"January 1, 2022",
"February 2, 2022",
"March 3, 2022",
"2025-13-13",
"May 5, 2022",
"June 6, 2022",
"July 7, 2022",
"August 8, 2022",
"September 9, 2022",
"October 10, 2022",
"November 11, 2022",
"December 12, 2022",
"2023-01-01",
"2024/02/02",
"2025.03.03",
"2026,04,04"
]
# Write solution here11¶
Intermediate
Initialize
tank_level = 0andmax_capacity = 50.Define a
pump_speedvariable to incrementtank_levelin each iteration.Use a
whileloop with the following conditions:The first time
tank_level >= max_capacity/2, print “Halfway there!” and continue.If
tank_level == max_capacity, break.
After the loop, print the final level, including “overflow by X” if exceeded.
Try with different values of pump_speed.
# Write solution here12¶
Advanced
Initialize
tank_level = 0andmax_capacity = 50.Define a
pump_speedvariable to incrementtank_levelin each iteration.Use a
whileloop 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.
After the loop, print the final level, including “overflow by X” if exceeded.
Try with different values of pump_speed.
# Write solution here13¶
Advanced
Create a
Livestockclass with attributes for species and count.Add methods to
Livestockfor checking herd status and string representation.Create a
Farmerclass with attributes for name and livestock inventory.Implement methods in
Farmerfor 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.
Update
__str__()method in both classes to provide human-readable output.Livestockprints the registered herd.Farmerprints his name and his registered herd.
Write example usage code to demonstrate each class’s functionality.
# Write solution here14¶
Intermediate
Create a file
yields.txtwith these lines:10.5 8.2 Missing 5.0Plus points for using Python to create the file.
Write a script to read the file and sum the numbers.
Use
try-exceptto ignore the “Missing” line without crashing. Print “Skipped line” when it happens.
# Write solution here15¶
Advanced
Download the file named “brgy
_population .txt” Read the file using
with open(...)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
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# Write solution here16¶
Intermediate
List:
farm_sizes = [1.2, 0.0, 3.5, -1.0, 5.0].Use
filterto keep only positive numbers (> 0).Use
mapon the result to calculate the potential production (multiply by 4.0 tons/ha).Print the final estimates.
Rewrite your solution using a single List Comprehension instead of map/filter.
Which do you prefer?
# Write solution here