+91 81900 98289

Python Exercises

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

Rewrite the list example so it prints only skills that contain the letter t. Then write a function that returns the average of a list of scores.

When those two feel easy, read the interview set and the course syllabus. Both stay on this site.

Example 1: Start here

Expected names include Python and Excel. Git has no lowercase t. SQL has none either.

skills = ["Python", "SQL", "Git", "Excel"] # a list; the values stay in this order print([skill for skill in skills if "t" in skill.lower()]) # keep the items that pass the test

Example 2: Average the scores

Divide the total by how many scores you were given.

def average(scores): # define the function; it runs when you call it return sum(scores) / len(scores) # send this value back to the caller print(average([70, 80, 90]))

Example 3: Highest score

Start with the first score, then replace it whenever you see a larger one.

def highest(scores): # define the function; it runs when you call it best = scores[0] # the first value for score in scores: # repeat the indented lines once per value if score > best: # run the indented lines only when this is true best = score # another name for the same value return best # send this value back to the caller print(highest([71, 90, 82]))

Example 4: Words with a letter

The test keeps python and git. sql has no t.

words = ["python", "sql", "git"] # a list; the values stay in this order print([word for word in words if "t" in word]) # keep the items that pass the test
Expected names include Python and Excel. Git has no lowercase t. SQL has none either.
Call Now 81900 98289