Assertions are an excellent tool for debugging and testing your Python code. When using the assert statement, Python will raise an exception if the specified condition is not True.
This allows you to immediately detect any potential problems, ensuring that the correct values or conditions are met before your code proceeds. In this tutorial, you will learn how to use the assert statement in Python 3, with examples of string manipulation, list manipulation, and mathematical operations.
Step 1: The Basic Syntax of Assert
The basic syntax of assert is as follows:
1 |
assert expression, message |
The expression represents the condition that must evaluate to True. If the expression evaluates to False, Python raises an AssertionError
exception with an optional message.
Here’s an example:
1 2 |
number = 42 assert number == 42, "Number should be 42" |
Step 2: Using Assert for String Manipulation
Consider the following function that ensures a string only has uppercase characters:
1 2 3 |
def ensure_uppercase(s): assert s.upper() == s, "String should be in uppercase" return s |
If you call the function with an uppercase string, it will return the string; otherwise, it will raise an AssertionError
.
Example:
1 2 |
uppercase = ensure_uppercase("HELLO") print(uppercase) |
HELLO
Now, let’s try it with lowercase characters:
1 |
lowercase = ensure_uppercase("hello") |
This code will raise an AssertionError
with the message “String should be in uppercase.”
Step 3: Using Assert for List Manipulation
Assertions in list manipulation can be useful to check if the required condition is met for all elements within the list. Here’s an example demonstrating the use of assert to check if all elements in a list are positive integers:
1 2 3 4 5 6 7 8 |
def ensure_positive(numbers): for n in numbers: assert n > 0, f"Element {n} should be positive" return numbers positive_numbers = ensure_positive([1, 2, 3, 4, 5]) print(positive_numbers) |
[1, 2, 3, 4, 5]
Now, let’s test with negative integers:
1 |
negative_numbers = ensure_positive([1, 2, 3, -4, 5]) |
This code will raise an AssertionError
with the message “Element -4 should be positive.”
Step 4: Using Assert for Mathematical Operations
You can use assert to check the results of mathematical operations. Consider the following example that checks if a number is even:
1 2 3 4 5 6 |
def is_even(number): assert number % 2 == 0, f"{number} should be even" return number even_number = is_even(12) print(even_number) |
12
If you now test an odd number:
1 |
odd_number = is_even(13) |
This line will raise an AssertionError
with the message “13 should be even.”
Full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def ensure_uppercase(s): assert s.upper() == s, "String should be in uppercase" return s def ensure_positive(numbers): for n in numbers: assert n > 0, f"Element {n} should be positive" return numbers def is_even(number): assert number % 2 == 0, f"{number} should be even" return number uppercase = ensure_uppercase("HELLO") print(uppercase) positive_numbers = ensure_positive([1, 2, 3, 4, 5]) print(positive_numbers) even_number = is_even(12) print(even_number) |
Conclusion
In this tutorial, you learned about the Python assert statement and its usage in various contexts such as string manipulation, list manipulation, and mathematical operations.
While useful for debugging and testing, it is important to note that assertions should not be used as a security feature or to validate input data, as they can be disabled globally in the interpreter with the ‘-O’ (optimize) command-line switch.
Therefore, it is recommended to use assert primarily during the development and testing phases of your project.