A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3schools.com/python/ref_keyword_assert.asp below:

Website Navigation


Python assert Keyword

Python assert Keyword

❮ Python Keywords

Example

Test if a condition returns True:

x = "hello"

#if condition returns True, then nothing happens:

assert x == "hello"

#if condition returns False, AssertionError is raised:

assert x == "goodbye"

Try it Yourself » Definition and Usage

The assert keyword is used when debugging code.

The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.

You can write a message to be written if the code returns False, check the example below.

More Examples Example

Write a message if the condition is False:

x = "welcome"

#if condition returns False, AssertionError is raised:

assert x != "welcome", "x should not be 'welcome'"

Try it Yourself »

❮ Python Keywords

Track your progress - it's free!


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4