Skip to main content
ThePythonBook/Assessment

Python Fundamentals Assessment

Test your knowledge of Python basics including variables, data types, strings, numbers, type conversion, booleans, and input/output. Complete all 12 exercises to earn your certificate.

Progress
870 XP0/12
#1Variable Swap
Write Code

Write a function swap_values(a, b) that returns a tuple with the two values swapped.

For example, swap_values(1, 2) should return (2, 1).

Loading editor...
#2Data Type Mix-up
Fix the Bug

The function below is supposed to return a greeting like "Hello, Alice! You are 30 years old." but it crashes with a TypeError.

Find and fix the bug.

Loading editor...
#3String Multiplication
Predict Output

What does the following code print?

word = "ab"
result = word * 3
print(result)
print(len(result))

Write a program that prints the exact same output.

Loading editor...
#4F-String Formatter
Write Code

Write a function format_price(item, price) that returns a string in the format:

"Item: Coffee - Price: $4.99"

The price should always show exactly 2 decimal places. Use an f-string.

Loading editor...
#5Boolean Boundary Check
Fix the Bug

The function is_valid_age(age) should return True if age is between 0 and 150 inclusive, and False otherwise. It currently has a logic bug.

Find and fix it.

Loading editor...
#6Type Conversion Chain
Predict Output

What does the following code print?

x = "42"
y = int(x) + 8
z = str(y) + "0"
print(type(y).__name__)
print(z)

Write a program that prints the exact same output.

Loading editor...
#7Clean and Capitalize
Write Code

Write a function clean_name(name) that:

1. Strips leading/trailing whitespace

2. Converts the name to title case (first letter of each word capitalized)

3. Returns the cleaned name

Loading editor...
#8Percentage Calculator
Fix the Bug

The function calculate_percentage(part, whole) should return the percentage as a float (e.g., 75.0 for 75%). But it always returns 0 for values where part < whole.

Find and fix the bug.

Loading editor...
#9Truthy and Falsy
Predict Output

What does the following code print?

values = [0, "", "hello", None, 42, [], [1]]
count = 0
for v in values:
    if v:
        count += 1
print(count)
print(bool(""))
print(bool("0"))

Write a program that prints the exact same output.

Loading editor...
#10Number Classifier Refactor
Refactor

The function below works correctly but is repetitive and hard to read. Refactor it to be more concise and Pythonic while keeping the same behavior.

The function should return "positive even", "positive odd", "negative even", "negative odd", or "zero" based on the number.

Loading editor...
#11Type Identifier
Write Code

Write a function describe_type(value) that returns a string describing the type of the value:

  • For integers: "integer: <value>"
  • For floats: "float: <value>"
  • For strings: "string: <length> chars"
  • For booleans: "boolean: <value>" (note: check booleans before integers since bool is a subclass of int)
  • For anything else: "other"
  • Loading editor...
    #12String Builder Refactor
    Refactor

    The function below builds a formatted summary string. It works correctly but uses clunky string concatenation. Refactor it to use f-strings while keeping the exact same output.

    The function takes name, score (int), and total (int) and returns a summary like:

    "Student: Alice | Score: 85/100 | Grade: 85.0%"

    Loading editor...