Other Statements - Python (2016)

Python (2016)

CHAPTER 16: Other Statements

This chapter is meant to cover some of the other statements in Python that haven’t been covered in complete detail yet.

Assert Statement

This statement is used mostly for debugging. It works by checking a specific condition—if the condition fails, then the statement will prompt an error. This is used to test a set of conditions before there is an actual execution of the program. This may be used, for example, to make sure that a specific variable spam is not zero in number. Below is a demonstration:

>>>try:

... Spam=0

... Assert spam ==0

... Spam=0/spam

... Except:

... Print(“spam may be zero”

... Else:

... Print(“spam is not zero”)

...

spam may be zero

In Statement

This might be familiar, as we last saw in when we worked with for statements. While the in statement was used exclusively in conjunction with for in that lesson, it can also be used as a standalone statement. Its primary purpose is to check if something can be found in a particular sequence. The statement then returns a Boolean value to verify (or deny) the existence of the item.

Here is an example of the in statement in action:

>>>”and” in (“or”, “nor”, “in”, “at”, “and”, “;”)

True

>>>”apple” in “(“orange”, “pomelo”, “watermelon”, “kiwi”, “dragon fruit”)

False

While the in statement can be used on iterators, we can also use it on plain strings since they are also sequences. When using it on strings, the statement will check if the specified item is a substring of the specified string. This will mean that you can check if something is found in something else (e.g., a word within a word). Here is an example:

>>>”x” in “exasperate”

True

>>>”juice” in “jam”

False

Also, do not forget that using the in statement is one of the fastest ways of comparing two variables.

Is Statement

This statement is primarily used to check if the two specified variables are unique. This would mean that bot the variables are not the same, or that they do not point to the same set of values. Unlike the “==” notation used for comparing equality, the is statement is used for comparing identity. Below is a demonstration:

>>>spam=”bread”

>>>eggs=”butter”

>>>spam is eggs

False

In this example, the programmer created two variables—spam and eggs. They have been assigned to different strings. Because these are not the same strings, their variables are likewise different. Now, let us try creating two variables with the exact same text in the strings:

>>>spam=”coffee”

>>>eggs=”coffee”

>>>spam is eggs

False

Why false? It’s because the two are still not the same. Although both of them have the same text in the strings, each of the strings are uniquely created and placed in the computer’s memory. The only way to create two exact same valuables will be to have one of them point to the other. Thus:

>>>spam = “Hi”

>>>eggs=spam

>>>spam is eggs

True

Pass Statement

This statement is used primarily as syntactic sugar, while also serving as a “nop”—an instruction that does nothing (contraction of no-op). Plainly put, the pass statement does nothing. This does not mean it is useless, however, as it serves to leave a code block empty:

>>>if True

... Pass

...

>>>for spam in range(15):

... Pass

...