🎯 Unit 1 Activities

Table of Contents


πŸ’» ACTIVITY PROGRAM SETUP INSTRUCTIONS
  1. Go to the public template repository for our class: BWL-CS Python Template
  2. Click the button above the list of files then select Create a new repository
  3. Specify the repository name: CS3-Unit1-Activity#

    Replace # with the specific activity number.

  4. Click

    Now you have your own personal copy of this starter code that you can always access under the Your repositories section of GitHub! πŸ“‚

  5. Now on your repository, click and select the Codespaces tab
  6. Click Create Codespace on main and wait for the environment to load, then you’re ready to code!

πŸ›‘ When class ends, don’t forget to SAVE YOUR WORK! Codespaces are TEMPORARY editing environments, so you need to COMMIT changes properly in order to update the main repository for your program.

There are multiple steps to saving in GitHub Codespaces:

  1. Navigate to the Source Control menu on the LEFT sidebar
  2. Click the button on the LEFT menu
  3. Type a brief commit message at the top of the file that opens, for example: updated main.py
  4. Click the small βœ”οΈ checkmark in the TOP RIGHT corner
  5. Click the button on the LEFT menu
  6. Finally you can close your Codespace!

πŸ§‘β€πŸ³ ACTIVITY #1: Cooking with Functions

In this activity, you’ll write Python functions that represent steps in cooking simple recipes. Your goal is to practice different types of function definitions β€” with and without arguments, with and without return values.

You’ll also call your functions, and sometimes store the result if the function returns a value.

PART A: Functions with No Arguments + No Return

Define two functions that just print steps, like a recipe card.

#Function that prints the steps to boil water
def boil_water():
    print("Fill a pot with water.")
    print("Put the pot on the stove.")
    print("Turn on the heat and wait for it to boil.")

# Function that prints a welcome message for the recipe
def welcome_message():
    print("Welcome to Python's Virtual Kitchen!")
    print("Today, we’ll learn how to make delicious recipes using functions.")

πŸ“£ Call both of these functions in your code to test them out!

PART B: Functions with Arguments + No Return

Now define a function that takes in ingredients and prints them out as part of a recipe.

# Function that prints the ingredients for a recipe
def list_ingredients(main_ingredient, side_dish):
    print(f"Main Ingredient: {main_ingredient}")
    print(f"Side Dish: {side_dish}")

πŸ“£ Call your function with two different combinations of ingredients.

PART C: Functions with Arguments + Return

Define a function that returns a string representing a combined dish.

# Function that returns a final dish name
def make_dish(ingredient1, ingredient2):
    return f"{ingredient1} and {ingredient2} Surprise!"

πŸ“£ Call the function and store the result in a variable, then print the result using print().

PART D: Functions with Optional Arguments

Define a function that makes a recipe with a default spice unless the user specifies one.

# Define a function with a default argument
def add_spice(dish, spice="paprika"):
    print(f"Adding a dash of {spice} to the {dish}. Yum!")

πŸ“£ Call it once with the spice and once without the spice.

PART E: Review Challenge

Write a cook_recipe() function that calls 3 or more of your previous functions in the correct order to simulate making a dish from start to finish.

Example idea:

def cook_recipe():
    welcome_message()
    boil_water()
    list_ingredients("Pasta", "Garlic Bread")
    dish = make_dish("Pasta", "Tomato Sauce")
    print("Dish created:", dish)
    add_spice(dish)

πŸ“£ Then, call cook_recipe() at the end of your script to run the full simulation.


πŸ’Ώ ACTIVITY #2: Data Collection Drills

In this activity, you’ll practice working with the 4 built-in data collections: Lists, Tuples, Sets, and Dictionaries.

Each drill is short and focused. TYPE the code, RUN it, and then record your observations as comments (#) in your program.

PART A: Lists

Lists are ordered, changeable collections.

  1. Create a list of letters:
    my_list = ["h", "e", "l", "l", "o"]
    
  2. Print the list to check it:
    print(my_list)
    # What do you see? Record it here:
    
  3. Add an exclamation mark to the end:
    my_list.append("!")
    
  4. Print the list again:
    print(my_list)
    # What changed? Describe the difference between the original and updated list:
    
  5. How many items are in the list?
    print(len(my_list))
    # How many elements are there? Record it:
    
  6. Get the last two items using positive indexes:
    print(my_list[4:6])
    # What values were returned? Why do those indexes work?
    
  7. Try slicing from index 4 to the end:
    print(my_list[4:])
    # What do you notice? Is the result the same as above?
    
  8. Now try negative indexing:
    print(my_list[-2:])
    # What does this do? Why is it useful?
    
  9. Remove the first "l":
    my_list.remove("l")
    print(my_list)
    # What changed? Where did it remove the "l" from?
    
  10. Put it back at index 2:
    my_list.insert(2, "l")
    print(my_list)
    # Did it go where you expected? What is at index 2 now?
    
  11. Delete the first element:
    del my_list[0]
    print(my_list)
    # What item got removed?
    
  12. Remove and save the last item:
    last_item = my_list.pop()
    print(last_item)
    print(my_list)
    # What item was removed? What’s left in the list?
    
  13. Print the item at index 2:
    print(my_list[2])
    # What is it?
    
  14. Check if "!" is in the list:
    print("!" in my_list)
    # True or False? Why?
    
  15. Sort the list in reverse order:
    my_list.sort(reverse=True)
    print(my_list)
    # What is the new order? Did sort() return anything?
    
  16. Create a sorted copy without changing the original:
    print(sorted(my_list))
    print(my_list)
    # Did the original change? What’s the difference between sorted() and sort()?
    

PART B: Tuples

Tuples are like lists, but immutable β€” once created, they can’t be changed.

  1. Create a simple tuple:
    my_tuple = ("red", "green", "blue")
    print(my_tuple)
    # What values are inside this tuple?
    
  2. Access individual items:
    print(my_tuple[0])
    print(my_tuple[-1])
    # What was printed in each case? Why does negative indexing work?
    
  3. Try to change a value:
    my_tuple[1] = "yellow"
    # What happens? What error do you get?
    
  4. Create a single-item tuple:
    one_item = ("hello",)
    print(one_item)
    # What do you notice about the syntax?
    
  5. Count how many times an item appears:
    colors = ("red", "blue", "red", "green", "red")
    print(colors.count("red"))
    # How many "red" entries are there?
    
  6. Find the index of the first match:
    print(colors.index("green"))
    # What position is "green" in the tuple?
    

PART C: Sets

Sets are unordered collections that only keep unique values.

  1. Make a set:
    my_set = {"apple", "banana", "cherry"}
    print(my_set)
    # Does the order match what you typed? Why or why not?
    
  2. Add a new item:
    my_set.add("orange")
    print(my_set)
    # What changed?
    
  3. Try adding a duplicate:
    my_set.add("banana")
    print(my_set)
    # Did anything change? Why or why not?
    
  4. Remove an item:
    my_set.remove("apple")
    print(my_set)
    # Which item is gone?
    
  5. Use discard() instead of remove():
    my_set.discard("pear")
    # What happened? Was there an error?
    
  6. Combine sets:
    tropical = {"banana", "mango", "papaya"}
    all_fruits = my_set.union(tropical)
    print(all_fruits)
    # What items are in the result? Any duplicates?
    

PART D: Dictionaries

Dictionaries store data in key-value pairs β€” think of them like labeled containers.

  1. Make a dictionary:
    my_dict = {"name": "Sam", "age": 16, "grade": "11th"}
    print(my_dict)
    # What keys and values are present?
    
  2. Get a value using its key:
    print(my_dict["age"])
    # What value did this return?
    
  3. Try accessing a missing key:
    print(my_dict["school"])
    # What error do you get?
    
  4. Change the grade:
    my_dict["grade"] = "12th"
    print(my_dict)
    # Did the value update?
    
  5. Add a new key-value pair:
    my_dict["school"] = "Central High"
    print(my_dict)
    # What’s the new key and value?
    
  6. Remove a key:
    del my_dict["age"]
    print(my_dict)
    # Which key is missing now?
    
  7. Use .get() to safely access a key:
    print(my_dict.get("locker"))
    # What value was returned? Was there an error?
    
  8. Loop through keys and values:
    for key, value in my_dict.items():
     print(key, "->", value)
    # What got printed? How does this help you read the dictionary?
    

Acknowledgement

Content on this page is adapted from LearnPython - Nina Zakharenko.