+91 81900 98289

Python Challenges

This Python Challenges lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

Write a function that returns the highest score in a list. Then change it so an empty list returns None instead of raising an error.

The starter below returns the highest of three scores.

Start here

max of an empty list raises ValueError. The guard avoids that.

def highest(scores): # define the function; it runs when you call it if not scores: # run the indented lines only when this is true return None # send this value back to the caller return max(scores) # send this value back to the caller print(highest([71, 90, 82])) print(highest([]))

Highest, including an empty list

Check for an empty list before you call max.

def highest(scores): # define the function; it runs when you call it if not scores: # run the indented lines only when this is true return None # send this value back to the caller return max(scores) # send this value back to the caller print(highest([71, 90, 82])) print(highest([]))

Count the passes

Add 1 for every score that passes.

def passes(scores): # define the function; it runs when you call it return sum(1 for score in scores if score >= 80) # send this value back to the caller print(passes([71, 90, 82]))

Reverse the words

reversed walks the list from the end. list turns that walk into a new list.

def flip(words): # define the function; it runs when you call it return list(reversed(words)) # send this value back to the caller print(flip(["Python", "SQL", "Git"]))
max of an empty list raises ValueError. The guard avoids that.