Created
May 22, 2024 04:17
-
-
Save lw13377/b2d3ed0a45fc4819f8933b8268ae1b9a to your computer and use it in GitHub Desktop.
lesson 6 homework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Homework: Lists | |
# 🔥Read carefully until the end before you start solving the exercises🔥 | |
# Practice the Basics 💪🏻 | |
# Empty, Pre-populated, and Lists within Lists | |
# You can uncomment or type the necessary code on each task | |
# --------------------------------------------------------------------- | |
# Task 1. Create three lists: | |
# List #1: Create an empty list and then use append() to populate it with the names of three of your friends. | |
# List #2: Create the same list, but use the syntax to create it pre-populated. | |
# List #3: Create the same list, but each element should be a list, | |
# where the first sub-element is the friend's name | |
# and the second sub-element is their age. | |
# List 1: | |
list_1 = [] | |
list_1.append('Tio') | |
list_1.append('Davo') | |
list_1.append('Alex') | |
print(list_1) | |
# List 2: | |
list_2 = ['Tio', 'Davo', 'Alex'] | |
print(list_2) | |
# List 3: | |
list_3 = [ | |
[['Tio'] , [16]], | |
[['Davo'] , [16]], | |
[['Alex'] , [17]] | |
] | |
print(list_3) | |
# --------------------------------------------------------------------- | |
# Task 2. Retrieve elements from a List | |
# Create print statements to retrieve the following elements from the previous lists: | |
# - From List 2: Retrieve the name of the second friend. | |
# - From List 3: Retrieve the age of the last friend you put in the list. | |
# Name of second friend | |
second_friend_name = list_2[1] | |
print(second_friend_name) | |
# Age of the last friend of the list | |
last_friend_age = list_3[2][1] | |
print(last_friend_age) | |
# --------------------------------------------------------------------- | |
# Task 3. Remove elements from a List | |
# From the lists provided, remove the requested elements. Easy peazy. | |
cities = ["Houston", "Dallas", "Austin"] | |
fruits = ["apple", "banana", "orange"] | |
# Remove Austin from cities without using its index | |
cities.remove('Austin') | |
# Remove the last element from fruits using negative indexes | |
del fruits[-1] | |
print(cities) | |
print(fruits) | |
# --------------------------------------------------------------------- | |
# Task 4. Verify if an element exists in a list | |
# Given the provided list, write code that prints `YES` if the list contains the word `cheese` | |
# The list | |
pantry = ["ham", "bread", "cheese"] | |
if 'cheese' in pantry: | |
print("YES") | |
else: | |
print("NO") | |
# --------------------------------------------------------------------- | |
# Task 5. Sorting and Reversing | |
# Given the provided list, write code that sorts and reverses it, as required. | |
numbers = [6, 34, 17, 9, 2, 11, 57, 9, 32] | |
print(numbers) | |
# Write code that sorts the list in ascending order without disturbing the original. | |
sorted_numbers = list(sorted(numbers)) | |
print(sorted_numbers) | |
# Write code that reverses (flips) the list without disturbing the original. | |
# Remember that in this case, casting is required. | |
reversed_numbers = list(reversed(numbers)) | |
print(reversed_numbers) | |
# Write code that sorts the list in place, modifying the original. | |
numbers.sort() | |
print(numbers) | |
# Write code that reverses (flips) the list in place, modifying the original. | |
numbers.reverse() | |
print(numbers) | |
# --------------------------------------------------------------------- | |
# Task 6. Stitching and Slicing | |
# You are given two lists with names of days of the week: | |
# - `work_days` contains the work week days (Mon-Fri) | |
# - `rest_days` contains the weekend days (Sat-Sun) | |
# Create a third list that contains the _concatenation_ of the previous two. | |
# Call it 'full_week' | |
# Now, write python code that prints a slice from 'full_week' with the work days. | |
work_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] | |
rest_days = ['Saturday', 'Sunday'] | |
# Concatenate work_days and rest_rays | |
full_week = work_days + rest_days | |
# Slice with the work days | |
print(full_week[0:5]) | |
# --------------------------------------------------------------------- | |
# Task 7. Aggregators and Helpers | |
# Given a list of numbers, use helpers and aggregators to answer the questions: | |
# - What's the lowest number? | |
# - What's the highest number? | |
# - What's the sum of all the numbers in the list? | |
# - How many times is the number 9 in the list? | |
# - How many total elements are in the list? | |
numbers = [6, 34, 17, 9, 2, 11, 57, 9, 32] | |
# Lowest number | |
print(min(numbers)) | |
# Highest number | |
print(max(numbers)) | |
# Sum of everything | |
print(sum(numbers)) | |
# Count number 9s | |
print(numbers.count(9)) | |
# Total number of elements | |
print(len(numbers)) | |
## Exercises 🏋🏻 | |
# --------------------------------------------------------------------- | |
# Exercise 1. The Biography Creator | |
# Create a program that will ask you for the following items and stores them in a list for later usage: | |
# - Your Name | |
# - Your Age | |
# - The name of the city where you were born | |
user_data = [] | |
name = input("Name: ") | |
age = int(input("Enter your age: ")) | |
city = input("Enter the city you were born: ") | |
user_data.append([name]) | |
user_data.append([age]) | |
user_data.append([city]) | |
bio = f"My name is {user_data[0]} , I am {user_data[1]} years old and I was born in the city {user_data[2]}." | |
print(bio) | |
# The program should use a variable with a string that will be used as a template. | |
# This template should be a sentence that can be used to build the person's biography. | |
# Fox example: | |
# biography = "My name is <NAME>, I'm <AGE> years old and I was born in <CITY>." | |
# Tips: | |
# - Use f-strings with placeholders to build the actual template, with elements of the list as values. | |
# - Use input() to gather the data. | |
# - Use print() at the end, to show the user's biography. | |
# Declare an empty list | |
# user_data = [] | |
# Gather user input | |
# name = input("Name: ") | |
# age = input("Age: ") | |
# city = input("City: ") | |
# Add user input to the list | |
# user_data.???(name) | |
# user_data.???(age) | |
# user_data.???(city) | |
# Declare your template. Use list elements as values. | |
# biography = f"???" | |
# Show the user's biography | |
# print(biography) | |
# --------------------------------------------------------------------- | |
# Exercise 2. The Card Deck ♦️♥️♠️♣️ | |
# You will be provided with a couple lists that contain the cards for a card deck. | |
# One of the lists contains the numbers, and the other one contains the faces. | |
# You will be asked to fill in the blanks to print out certain cards for a card game you've been working on. | |
# 🔥 Tip: You might want to stitch them together first. | |
# Here are the card decks. | |
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] | |
faces = ['J', 'Q', 'K'] | |
# Concatenate them first. | |
card_deck = numbers + faces | |
# Print out the numbers 1 to 6. | |
print(card_deck[0:6]) | |
# Print out the last 3. Do it using POSITIVE indexes. | |
print(card_deck[10:13]) | |
# Print out the last 3 (same as before), but using NEGATIVE indexes. | |
print(card_deck[-3:]) | |
# Print out everything EXCEPT the first and last. | |
print(card_deck[1:-1]) | |
# What would you use so the printout includes the following: | |
# Hint: It's every third card of the full deck. | |
# ['1', '4', '7', '10', 'K'] | |
print(card_deck[::3]) | |
# Print out the EVEN numbers. No faces. | |
even_numbers = [num for num in numbers if int(num) % 2 == 0] | |
print(even_numbers) | |
# --------------------------------------------------------------------- | |
# Exercise 3. The Steps Tracker 👟 | |
# Walking is a great way to improve one's health, and it can be fun! | |
# Doctors recommend 10,000 steps per day! You would like to know how many steps are YOU taking per day and per week. | |
# Write a program that will ask you the number of steps taken each day of the week, for one week. | |
# The program should put the step counts in a list, where index 0 is the number for Monday, | |
# index 1 is the number for Tuesday, and so on. | |
# Once you have all the steps counts, answer the following questions: | |
# - How many steps you took on Wednesday? | |
# - How many steps you took on the work days (Mon - Fri)? | |
# - How many steps total did you take over the whole week? | |
# - What was the least number of steps you took on a day? | |
# - What was the most number of steps you took on a day? | |
steps = [] | |
# Collect steps for each day, converting inputs to integers | |
steps.append(int(input('Steps for Monday: '))) | |
steps.append(int(input('Steps for Tuesday: '))) | |
steps.append(int(input('Steps for Wednesday: '))) | |
steps.append(int(input('Steps for Thursday: '))) | |
steps.append(int(input('Steps for Friday: '))) | |
steps.append(int(input('Steps for Saturday: '))) | |
steps.append(int(input('Steps for Sunday: '))) | |
# Steps on Wednesday | |
print("Steps on Wed:" , steps[2]) | |
# Steps on the workdays (Mon - Fri) | |
work_days_steps = steps[0:5] | |
print(work_days_steps) | |
# Total steps over the whole week | |
print("the sum of the steps is:" , sum(steps)) | |
# Least number of steps | |
print("the minimum amount of steps was:" , min(steps)) | |
# Highest number of steps | |
print("the highest amount of steps was:" , max(steps)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exercise 1. The Biography Creator
In this exercise you should try a different solution because yours is appending the user data as a nested list.
Here's a corrected version of your code: