+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.

Example 1: 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([]))

Example 2: 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([]))

Example 3: 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]))

Example 4: 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.
Call Now 81900 98289