{
"cells": [
{
"cell_type": "raw",
"metadata": {
"vscode": {
"languageId": "raw"
}
},
"source": [
"---\n",
"layout: post\n",
"title: 3.8 Homework \n",
"description: Homework Hacks\n",
"comments: true\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Homework Hacks for Lesson 3.8"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.8.1 Popcorn Hack\n",
"Make a function finding numbers divisible by 2."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create an empty list\n",
"nums = []\n",
"\n",
"# Get user input for how many numbers they want to add\n",
"num_ofinputs = int(input(\"How many numbers do you want to add to the list? \"))\n",
"\n",
"# Use a loop to get inputs and append them to the list\n",
"for in range(num_of_inputs):\n",
" number = int(input(\"Enter a number: \"))\n",
" nums.append(number)\n",
"\n",
"# Use a for loop to iterate through the list and check divisibility by 2\n",
"print(\"Numbers divisible by 2:\")\n",
"for num in nums:\n",
" if num % 2 == 0: # Check divisibility by 2\n",
" print(num)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Homework Hack 1\n",
"You are tasked with writing a simple Python script that uses a for loop to iterate through a list of numbers and print each number.\n",
" \n",
" Requirements:\n",
" \n",
" Write a for loop to iterate through the given list of numbers. Print each number as you iterate through the list. Input: Use the list of numbers: [1, 2, 3, 4, 5]\n",
" \n",
" Expected Output: You should print each number from the list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define the list of numbers\n",
"numbers = [1, 2, 3, 4, 5]\n",
"\n",
"# Use a for loop to iterate through the list\n",
"for number in numbers:\n",
" # Print each number in the list\n",
" print(number)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Homework Hack 2\n",
" You are tasked with solving two exercises involving loops in Python. The goal is to understand how to iterate through different types of collections (lists and dictionaries) and print their contents.\n",
" \n",
" Requirements:\n",
" \n",
" Exercise 1: Write a for loop to iterate through a list of colors and print each color. Exercise 2: Write a for loop to iterate through a dictionary of fruits where the keys represent the type of fruit, and the values represent the quantity of each fruit. Print the fruit’s name and its corresponding quantity in the format: “fruit_name : quantity”. Input:\n",
" \n",
" For the first exercise, use the list of colors: [“red”, “blue”, “green”, “yellow”]\n",
" \n",
" For the second exercise, use the dictionary of fruits: {“apple”: 3, “banana”: 5, “cherry”: 7}\n",
" \n",
" Expected Output: You should print each color from the list, and each fruit with its quantity from the dictionary."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Exercise 1\n",
"\n",
"colors = [\"red\", \"blue\", \"green\", \"yellow\"] # List of colors\n",
"\n",
"# For loop to iterate through the list and print each color\n",
"for color in colors:\n",
" print(color)\n",
"\n",
"\n",
"# Exercise 2\n",
"\n",
"fruits = {\"apple\": 3, \"banana\": 5, \"cherry\": 7} # Dictionary of fruits with their quantities\n",
"\n",
"# For loop to iterate through the dictionary and print each fruit and quantity\n",
"for fruit, quantity in fruits.items():\n",
" print(f\"{fruit} : {quantity}\")\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{ "cells": [ { "cell_type": "raw", "metadata": { "vscode": { "languageId": "raw" } }, "source": [ "---\n", "layout: post\n", "title: 3.8 Homework \n", "description: Homework Hacks\n", "comments: true\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework Hacks for Lesson 3.8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.8.1 Popcorn Hack\n", "Make a function finding numbers divisible by 2." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create an empty list\n", "nums = []\n", "\n", "# Get user input for how many numbers they want to add\n", "num_ofinputs = int(input(\"How many numbers do you want to add to the list? \"))\n", "\n", "# Use a loop to get inputs and append them to the list\n", "for in range(num_of_inputs):\n", " number = int(input(\"Enter a number: \"))\n", " nums.append(number)\n", "\n", "# Use a for loop to iterate through the list and check divisibility by 2\n", "print(\"Numbers divisible by 2:\")\n", "for num in nums:\n", " if num % 2 == 0: # Check divisibility by 2\n", " print(num)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework Hack 1\n", "You are tasked with writing a simple Python script that uses a for loop to iterate through a list of numbers and print each number.\n", "
\n", "
Requirements:\n", "
\n", "
Write a for loop to iterate through the given list of numbers. Print each number as you iterate through the list. Input: Use the list of numbers: [1, 2, 3, 4, 5]\n", "
\n", "
Expected Output: You should print each number from the list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define the list of numbers\n", "numbers = [1, 2, 3, 4, 5]\n", "\n", "# Use a for loop to iterate through the list\n", "for number in numbers:\n", " # Print each number in the list\n", " print(number)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Homework Hack 2\n", "
You are tasked with solving two exercises involving loops in Python. The goal is to understand how to iterate through different types of collections (lists and dictionaries) and print their contents.\n", "
\n", "
Requirements:\n", "
\n", "
Exercise 1: Write a for loop to iterate through a list of colors and print each color. Exercise 2: Write a for loop to iterate through a dictionary of fruits where the keys represent the type of fruit, and the values represent the quantity of each fruit. Print the fruit’s name and its corresponding quantity in the format: “fruit_name : quantity”. Input:\n", "
\n", "
For the first exercise, use the list of colors: [“red”, “blue”, “green”, “yellow”]\n", "
\n", "
For the second exercise, use the dictionary of fruits: {“apple”: 3, “banana”: 5, “cherry”: 7}\n", "
\n", "
Expected Output: You should print each color from the list, and each fruit with its quantity from the dictionary." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 1\n", "\n", "colors = [\"red\", \"blue\", \"green\", \"yellow\"] # List of colors\n", "\n", "# For loop to iterate through the list and print each color\n", "for color in colors:\n", " print(color)\n", "\n", "\n", "# Exercise 2\n", "\n", "fruits = {\"apple\": 3, \"banana\": 5, \"cherry\": 7} # Dictionary of fruits with their quantities\n", "\n", "# For loop to iterate through the dictionary and print each fruit and quantity\n", "for fruit, quantity in fruits.items():\n", " print(f\"{fruit} : {quantity}\")\n", "\n" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 2 }