Python 08

Python Exercises

For loop practice

It is time to practice our skills. By far, we have learned many things like functions, lists, dictionaries, etc.

Here are the exercises for maintain and develope the skills.

Exercise 1. double()

Create a function double which takes a list and returns with values with doubled values.

'''
>>> double([1, 2, 3, 4, 5])
[2, 4, 6, 8, 10]
'''
Toggle Answer πŸ‘‡
def double(numbers: list) -> list:
  result = []
  for number in numbers:
    result.append(number * 2)

  return result

print(double([1, 2, 3, 4, 5]))

Loop pattern

As you practice more of these loops, you will notice that there is a pattern for loops. The pattern is following:

# create empty list
result = []
# loop through & append to that list
for item in list:
# return that list
  return result
# ❗ Watch out for indentation ❗

Exercise 2. count_words()

Create a function that takes a string and returns a number of words.

'''
count words if given a string, should count and return the number of words

phrase: str

count_words('Hi my name is Smith')
5
'''

Here are some helpful functions to make this function.

method .split() it splits a string into a list, you can specify the separator in (),and default is whitespace

example:

my_string = "apple, banana, orange, strawberry"
my_string.split(", ")
# returns ['apple', 'banana', 'orange', 'strawberry']

function len() returns the number of items in an object

example:

fruits = ['🍎', '🍐', '🍊', '🍌', 'πŸ“', 'πŸͺ']
len(fruits)

# returns 6
Toggle Answer πŸ‘‡
def count_words(phrase: str) -> int:
  words = phrase.split()
  return len(words)
  # or return len(phrase.split())

print(count_words('Hi my name is Smith'))

Exercise 3. sum_list()

Create a function that takes a list of numbers and returns their sum.

'''
Create a function that given a list of numbers, it can return their sum
>>> sum_list([1, 2, 3])
6
'''
Toggle Answer πŸ‘‡
def sum_list(numbers: list) -> int:
  count = 0
  for number in numbers:
    count += number

  return count

print(sum_list([1, 2, 3]))

Exercise 4. find_max()

Create a function that take a list of numbers and returns the biggest number.

'''
>>> find_max([1, 5, 10 ,3, 20])
20
'''
Toggle Answer πŸ‘‡
def find_max(numbers: list) -> int:
  current_max = numbers[0]
  for number in numbers:
    if current_max < number:
      current_max = number
  return current_max

print(find_max([1, 5, 10 ,3, 20]))

Pseudocode for find_max()

To write complex code, you need to write a pseudocode that helps your logic clear.

This is an example of a pseudocode for find_max()

'''
numbers = [1, 5, 10 ,3, 20]
current_max = 1 # numbers[0]
is 1 > current_max? no
is 5 > current_max? yes
current_max = 5
is 10 > current_max? yes
current_max = 10
is 3 > current_max? no
is 20 > cuurent_max? yes
current_max = 20
end the loop
'''

Dictionary Practice

Exercise 5. word_frequency()

Create function that take a string and returns a dictionary that contains the words as a key and the number of words as a value.

'''
word_frequency('I love Batman because I am Batman')
{'I': 2, 'love': 1, 'Batman': 2, 'am': 1 ..}
'''

'''
PSEUDOCODE
# Split the phrase into a list
# Create empty result dictionary

# loop 1
is "I" in result? no
create a new key value pair
set the key to "I" and value to 1

# loop 2
is "love" in result? no
create a new key value pair
set the key to "love" and value to 1

...

# loop 5
is "I" in result? Yesß
increment the value of "I" by 1

...

'''
Toggle Answer πŸ‘‡
def word_frequency(phrase: str) -> dict:
  result = {}
  words = phrase.split()
  for word in words:
    if word not in result:
      result[word] = 1
    else:
      result[word] += 1

  return result

print(word_frequency('I love Batman because I am Batman'))

Categories:

Updated:

Comments