Exercise: Loops and lists

Practice lists and loops in this set of exercises. A few of them have comments to guide you towards a solution; you can follow those comments if they help or delete them and start from scratch. Whatever works for you!

Make point

Implement make_point, a function that returns a list with the two arguments x and y, each rounded to integer values.

Tip: Remember one of Python's built-in functions makes it easy to round numbers.

📺 Need help getting started? Watch me go through a similar problem in this video.

def make_point(x, y): """Returns a list with the two arguments, rounded to their integer values. >>> make_point(30, 75) [30, 75] >>> make_point(-1, -2) [-1, -2] >>> make_point(12.32, 74.11) [12, 74] """ return []

Average scores

Implement average_scores, a function that returns the average of the provided list scores.

📺 Need help getting started? Watch me go through a similar problem in this video.

🧩 Or, for more guidance in written form, try a Parsons Puzzle version first.

def average_scores(scores): """Returns the average of the provided scores. >>> average_scores([10, 20]) 15.0 >>> average_scores([90, 80, 70]) 80.0 >>> average_scores([8.9, 7.2]) 8.05 """ # Initialize a variable to 0 # Iterate through the scores # Add each score to the variable # Divide the variable by the number of scores # Return the result

Concatenator

Implement concatenator, a function that returns a string made up of all the strings from the provided list items.

(There is a way to do this with a single function call in Python, but for the purposes of this exercise, please try using a for loop to join the strings.)

🧩 For more guidance, try a Parsons Puzzle version first.

def concatenator(items): """Returns a string with all the contents of the list concatenated together. >>> concatenator(["Super", "cali", "fragilistic", "expialidocious"]) 'Supercalifragilisticexpialidocious' >>> concatenator(["finger", "spitzen", "gefühl"]) 'fingerspitzengefühl' >>> concatenator(["none", "the", "less"]) 'nonetheless' """ # Initialize a variable to empty string # Iterate through the list # Concatenate each item to the variable # Return the variable
➡️ Next up: List mutation