8000 Update Ch 9 solutions · MrDP18/python-basics-exercises@a0aa5d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit a0aa5d8

Browse files
committed
Update Ch 9 solutions
1 parent e775089 commit a0aa5d8

12 files changed

+108
-92
lines changed

ch09-lists-and-dictionaries/1-lists-multipurpose-containers.py

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 9.1 - Tuples are Immutable Sequences
2+
# Solutions to review exercises
3+
4+
5+
# Exercise 1
6+
# Create a tuple "cardinal_numbers" with "first", "second" and "third"
7+
cardinal_numbers = ("first", "second", "third")
8+
9+
10+
# Exercise 2
11+
# Display the second object in the tuple
12+
print(cardinal_numbers[1])
13+
14+
15+
# Exercise 3
16+
# unpack the tuple into three string and display them
17+
position1, position2, position3 = cardinal_numbers
18+
print(position1)
19+
print(position2)
20+
print(position3)
21+
22+
# Exercise 4
23+
# Create a tuple containing the letters of your name from a string
24+
my_name = tuple("David")
25+
26+
# Exercide 5
27+
# Check whether or not x is in my_name
28+
print("x" in my_name)
29+
30+
# Exercise 6
31+
# Create tuple containing all but the first letter in my_name
32+
print(my_name[1:])
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 9.2 - Lists are Mutable Sequences
2+
# Solutions to review exercises
3+
4+
5+
# Exercise 1
6+
# Create a list named food with two elements "rice" and "beans".
7+
food = ["rice", "beans"]
8+
9+
10+
# Exercise 2
11+
# Append the string "broccolo" to the food list using .append()
12+
food.append("brocolli")
13+
14+
15+
# Exercise 3
16+
# Add the strings "bread" and "pizza" to food using .extend()
17+
food.extend(["bread", "pizza"])
18+
19+
20+
# Exercise 4
21+
# Print the first two items in food using slicing notation
22+
print(food[:2])
23+
24+
# NOTE: The following is also acceptable
25+
print(food[0:2])
26+
27+
28+
# Exercise 5
29+
# Print the last item in food using index notation
30+
print(food[-1])
31+
32+
33+
# Exercise 6
34+
# Create a list called breakfast from 8000 the string "eggs, fruit, orange juice"
35+
breakfast = "eggs, fruit, orange juice".split(", ")
36+
37+
38+
# Exercise 7
39+
# Verify that breakfast has three items using len()
40+
print(len(breakfast) == 3)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 9.3 - Nesting, Copying, and Sorting Lists and Tuples
2+
# Solutions to review exercises
3+
4+
5+
# Exercise 1
6+
# Create a tuple called data with two values, (1, 2) and (3, 4)
7+
data = ((1, 2), (3, 4))
8+
9+
10+
# Exercise 2
11+
# Loop over data a print the sum of each nested tuple
12+
for i in range(len(data)):
13+
print(f"Row {i+1} sum: {data[i][0] + data[i][1]}")
14+
15+
16+
# Exercise 3
17+
# Create the list [4, 3, 2, 1] and assign it to variable numbers
18+
numbers = [4, 3, 2, 1]
19+
20+
21+
# Exercise 4
22+
# Create a copy of the number list using [:]
23+
numbers_copy = numbers[:]
24+
25+
26+
# Exercise 5
27+
# Sort the numbers list in numerical order
28+
numbers.sort()
29+
print(numbers)

ch09-lists-and-dictionaries/2-challenge-list-of-lists.py renamed to ch09-lists-and-dictionaries/4-challenge-list-of-lists.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 9.2 - Challenge: List of Lists
1+
# 9.4 - Challenge: List of Lists
22
# Solution to challenge
33

44

ch09-lists-and-dictionaries/4-make-permanent-lists.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

ch09-lists-and-dictionaries/3-challenge-wax-poetic.py renamed to ch09-lists-and-dictionaries/5-challenge-wax-poetic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 9.3 - Challenge: Wax Poetic
1+
# 9.5 - Challenge: Wax Poetic
22
# Solution to challenge
33

44

ch09-lists-and-dictionaries/5-store-relationships-in-dictionaries.py renamed to ch09-lists-and-dictionaries/6-store-relationships-in-dictionaries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 9.5 - Store Relationships in Dictionaries
1+
# 9.6 - Store Relationships in Dictionaries
22
# Solutions to review exercises
33

44

ch09-lists-and-dictionaries/6-challenge-capital-city-loop.py renamed to ch09-lists-and-dictionaries/7-challenge-capital-city-loop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 9.6 - Challenge: Capital City Loop
1+
# 9.7 - Challenge: Capital City Loop
22
# Solution to challenge
33

44

ch09-lists-and-dictionaries/7a-challenge-cats-with-hats.py renamed to ch09-lists-and-dictionaries/8a-challenge-cats-with-hats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 9.7 - Challenge: Cats With Hats
1+
# 9.8 - Challenge: Cats With Hats
22
# Solution to challenge
33

44

ch09-lists-and-dictionaries/7b-challenge-cats-with-hats.py renamed to ch09-lists-and-dictionaries/8b-challenge-cats-with-hats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 9.7 - Challenge: Cats With Hats
1+
# 9.8 - Challenge: Cats With Hats
22
# Alternative solution to challenge
33

44

ch09-lists-and-dictionaries/7c-challenge-cats-with-hats.py renamed to ch09-lists-and-dictionaries/8c-challenge-cats-with-hats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 9.7 - Challenge: Cats With Hats
1+
# 9.8 - Challenge: Cats With Hats
22
# Alternative solution to challenge using dictionaries
33

44

0 commit comments

Comments
 (0)
0