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.
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).
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.
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.
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.
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.
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.
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
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.
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.
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.
Write a function describe_type(value) that returns a string describing the type of the value:
"integer: <value>""float: <value>""string: <length> chars""boolean: <value>" (note: check booleans before integers since bool is a subclass of int)"other"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%"