Skip to content

Commit 1c9559e

Browse files
committed
Exercises for Ch 6 - 10
1 parent 1b94981 commit 1c9559e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+532
-0
lines changed

06-flow-control/exercise-1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# To what values do the following expressions evaluate?
2+
3+
False or (True and False) # False
4+
True or (1 + 2) # True
5+
(1 + 2) or True # 3
6+
True and (1 + 2) # 3
7+
False and (1 + 2) # Flase
8+
(1 + 2) and True # True
9+
(32 * 4) >= 129 # False
10+
False != (not True) # False
11+
True == 4 # False
12+
False == (847 == '847') # True

06-flow-control/exercise-2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Write a function, even_or_odd, that determines whether its argument is an even or odd number. If it's even, the function should print 'even'; otherwise, it should print 'odd'. Assume the argument is always an integer.
2+
3+
def even_or_odd(number):
4+
if number % 2 == 0:
5+
print('Even')
6+
else:
7+
print('Odd')
8+
9+
even_or_odd(3)

06-flow-control/exercise-3.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Without running the following code, what does it print? Why?
2+
3+
def bar_code_scanner(serial):
4+
match serial:
5+
case '123':
6+
print('Product1')
7+
case '113':
8+
print('Product2')
9+
case '142':
10+
print('Product3')
11+
case _:
12+
print('Product not found!')
13+
14+
bar_code_scanner('113')
15+
bar_code_scanner(142)
16+
17+
# It will print 'Product2' for '113' and 'Product not found!' for 142, as 142 integer doesn't match the string'142' defined in the program.

06-flow-control/exercise-4.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Refactor this code to use a regular if statement instead.
2+
3+
# def baz():
4+
# return ('bar' if foo() else qux())
5+
6+
def baz():
7+
if foo():
8+
return 'bar'
9+
else:
10+
return qux()

06-flow-control/exercise-5.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# What does this code output, and why?
2+
3+
def is_list_empty(my_list):
4+
if my_list:
5+
print('Not Empty')
6+
else:
7+
print('Empty')
8+
9+
is_list_empty([])
10+
11+
# It outputs Empty due to a falsy empty list.

06-flow-control/exercise-6.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write a function that takes a string as an argument and returns an all-caps version of the string when the string is longer than 10 characters. Otherwise, it should return the original string. Example: change 'hello world' to 'HELLO WORLD', but don't change 'goodbye'.
2+
3+
def caps(word):
4+
return word.upper() if len(word) > 10 else word
5+
6+
print(caps('hello world'))
7+
print(caps('goodbye'))

06-flow-control/exercise-7.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def number_range(number):
2+
if number < 0:
3+
print(f'{number} is less than 0')
4+
elif 0 <= number <= 50:
5+
print(f'{number} is between 0 and 50')
6+
elif 51 <= number <= 100:
7+
print(f'{number} is between 51 and 100')
8+
else:
9+
print(f'{number} is greater than 100')
10+
11+
number_range(101)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# If you have a list named people, how can you determine the number of entries in that list?
2+
3+
people = ['elon', 'jeff', 'sam']
4+
5+
print(len(people))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Bob expects the following code to print the names in alphabetical order. Without running the code, can you say whether it will? Explain your answer.
2+
3+
names = { 'Chris', 'Clare', 'Karis', 'Karl',
4+
'Max', 'Nick', 'Victor' }
5+
print(names)
6+
7+
# sets being unordered collections, it wont always be in an alphabetical order.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Consider the data in the following table:
2+
#
3+
# Name Country
4+
# Alice USA
5+
# Francois Canada
6+
# Inti Peru
7+
# Monika Germany
8+
# Sanyu Uganda
9+
# Yoshitaka Japan
10+
#
11+
# You need to write some Python code to determine the country name associated with one of the listed names. Your code should include the data structure(s) you need and at least one test case to ensure the code works.
12+
13+
places = {
14+
'Alice': 'USA',
15+
'Francois': 'Canada',
16+
'Inti': 'Peru',
17+
'Monika': 'Germany',
18+
'Sanyu': 'Uganda',
19+
'Yoshitaka': 'Japan',
20+
}
21+
22+
print(places['Inti'])

0 commit comments

Comments
 (0)