Practice ArenaPython & SQL Tracks Live
Coding Challenges
Filter by language first, explore topic by topic, tackle challenges in random order, request progressive hints when stuck, and inspect complete verified solutions.
364
Practice problems
28
Curriculum chapters
Filter by Language:277 Problems
Filter by Python Chapter:
Difficulty:
Chapter:
Showing 6 challenges · 277 total · Page 1 of 47
Write count_expenses(expenses) to return the number of records in a list. Check that it works for both a non-empty list and an empty list.
Starter Code
def count_expenses(expenses):
# Write your solution here
passExpected Output
2
0
Write pseudocode and a Python function classify_number(n) that returns "Positive" when n is greater than zero, "Negative" when n is less than zero, and "Zero" otherwise. Test all three cases.
Starter Code
def classify_number(n):
# Check whether n is positive.
# Otherwise, check whether n is negative.
# Handle zero explicitly.
pass
print(classify_number(5))
print(classify_number(-3))
print(classify_number(0))Expected Output
Positive
Negative
Zero
Create a tuple containing 10 and 20. Unpack it into first and second, then print the second value.
Starter Code
# Create and unpack the tuple
Expected Output
20
Create numbers = [4, 8, 12] and print the middle element using its index.
Starter Code
numbers = [4, 8, 12] # Print the middle element
Expected Output
8
Write a coroutine that raises ValueError('Bad data'). In main(), await it inside a try/except block and print the exception message prefixed with Error:.
Starter Code
import asyncio
async def read_data():
# Raise ValueError('Bad data').
pass
async def main():
# Await read_data() and catch ValueError.
pass
asyncio.run(main())Expected Output
Error: Bad data
Read a numerator and denominator, convert both to floats, and print their quotient with the label Result:. Handle invalid numeric input with Please enter valid numbers. and division by zero with The denominator cannot be zero. Assume the inputs are 10 and 2.
Starter Code
try:
numerator = float(input("Numerator: "))
denominator = float(input("Denominator: "))
# Calculate and print the quotient
except ValueError:
# Handle invalid numeric input
pass
except ZeroDivisionError:
# Handle division by zero
passExpected Output
Numerator: Denominator: Result: 5.0
…