Python assert keyword

Mfano

Test if the condition returns True:

x = "hello"
# If the condition returns True, nothing will happen:
assert x == "hello"
# If the condition returns False, it will raise an AssertionError:
assert x == "goodbye"

Mfano wa Kufungua

Uhusiano na Matumizi

The assert keyword is used when debugging code.

The assert keyword allows you to test if conditions in your code return True, otherwise, the program will raise an AssertionError.

You can write a message that will be output if the code returns False, see the following example.

Mfano zaidi

Mfano

If the condition is False, write a message:

x = "hello"
# If the condition returns False, it will raise an AssertionError:
assert x == "goodbye", "x should be 'hello'"

Mfano wa Kufungua