Python Concepts/Other Statements

Objective

 * Learn about the  statement.
 * Learn about the  statement.
 * Learn about the  statement.
 * Learn about the  statement.

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

The Assert Statement
The  statement is primarily used for debugging. It depends upon the value  being   (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  statement is to test certain conditions before the program actually runs.

This code illustrates use of the  statement: __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  statement when working with the   statement in an older lesson. Although the  statement was used in conjunction with a   statement, the   statement can be used by itself. The primary purpose of the  statement is to check if something is in a sequence. This means the  statement returns a Boolean value:   or.

Which members of  contain the letter 'n'?

Although we can use the  statement on iterators, we can use it on strings because they're sequences, also. When using the  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.

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

The Is Statement
The  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 used for identity comparison.

In the above example, two variables are created,  and. 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.

and  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.

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

The del Statement
The  statement is used to remove or delete members of a mutable sequence. To delete a global variable delete it from dictionary Strings and tuples are not mutable sequences:

Assignments

 * Work with the,  ,  , and   statements.

Further Reading or Review

 * Previous Lesson: List Comprehension
 * This Lesson:Other Statements
 * Next Lesson: Console output
 * Course Home Page