Python/Other Statements

Objective

Books-aj.svg aj ashton 01f.png
  • Learn about the assert statement.
  • Learn about the in statement.
  • Learn about the is statement.
  • Learn about the pass statement.

Lesson

This lesson will go over some basic Python statements that haven't been learned in depth yet.

The Assert Statement

The assert statement is primarily used for debugging. It depends upon the value __debug__ being True (which it is under normal circumstances.) It will check a condition and if that condition fails, it will raise an AssertionError. The purpose of the assert statement is to test certain conditions before the program actually runs.

This code illustrates use of the assert statement:

print ('__debug__ =', __debug__ )

for spam in (0,1) :
    print ('\nspam =', spam)

    try:
        assert spam == 0 # Checking that spam is zero.
    except AssertionError:
        print("AssertionError detected!")
    except:
        print("Unknown error detected!")
    else:
        print("spam was zero, no error raised!")

    try:
        if not spam == 0 : raise AssertionError # Equivalent to "assert" above.
    except AssertionError:
        print("AssertionError detected!")
    except:
        print("Unknown error detected!")
    else:
        print("spam was zero, no error raised!")
__debug__ = True

spam = 0
spam was zero, no error raised!
spam was zero, no error raised!

spam = 1
AssertionError detected!
AssertionError detected!

The In Statement

You might remember the in statement when working with the for statement in an older lesson. Although the in statement was used in conjunction with a for statement, the in statement can be used by itself. The primary purpose of the in statement is to check if something is in a sequence. This means the in statement returns a Boolean value: True or False.

>>> "and" in ("or", "xor", "and", "not")
True
>>> "inv" in ("or", "xor", "and", "not")
False
>>> "a" in ("aa", "aaa", "aaaa")
False

Which members of ("or", "xor", "and", "not") contain the letter 'n'?

>>> [p for p in ("or", "xor", "and", "not") if 'n' in p]
['and', 'not']
>>>

Although we can use the in statement on iterators, we can use it on strings because they're sequences, also. When using the in statement on strings, the statement will check if a string is within a second string. This means you can check for part of a word within a word.

>>> "a" in "aaa"
True
>>> "spam" in "spa"
False
>>> "spa" in "spam"
True
>>> "cake" in "pancake"
True


You should remember that using the in statement is one of the fastest ways to compare two variables.

The Is Statement

The is statement is used to check if two variables are unique. This means that both variables don't have the same value, or they don't point to the same value. This means, unlike == which is used for equality comparison, is is used for identity comparison.

>>> spam = "Hello!"
>>> eggs = "Hi!"
>>> spam is eggs
False


In the above example, two variables are created, spam and eggs. They are both assigned to two different strings. Because these aren't the same string, the variables aren't the same. Now let's create both of the variables with the same text in their strings.

>>> spam = "Hello!"
>>> eggs = "Hello!"
>>> spam is eggs
False


spam and eggs still aren't the same. This is because, although they have the same text in their strings, each string is created uniquely and put into its own memory position. To have the two variables point to the same string, you'll need to assign one of the variables to the other.

>>> spam = "Hello!"
>>> eggs = spam
>>> spam is eggs
True

The Pass Statement

The pass statement is used mostly for syntactic sugar and serves as a nop. This means that the pass statement doesn't do anything. The primary purpose of the pass statement is to provide a statement when the syntax requires it, but when no action is required.

>>> if True:
...     pass
...
>>> for spam in range(10):
...     pass
...

Assignments

Crystal Clear app kedit.svg
  • Work with the assert, in, is, and pass statements.
Sweden road sign A10.svg Completion status: Ready for testing by learners and teachers. Please begin!

References

1. Python's documentation:

"7.3. The assert statement," "7.4. The pass statement," "7.8. The raise statement"


2. Python's methods:


3. Python's built-in functions:


  This article uses material from the Wikipedia page available here. It is released under the Creative Commons Attribution-Share-Alike License 3.0.


Python/Other_Statements
 

Manage research, learning and skills at IT1me. Create an account using LinkedIn to manage and organize your IT knowledge. IT1me works like a shopping cart for information -- helping you to save, discuss and share.


  Contact Us  |  About IT1me.com |  IT Training & References |  IT Careers |  IT Hardware |  IT Software |  IT Books