The if condition

The if condition or the if statement takes a statement and returns either a Boolean True or a Boolean False value after evaluating the statement. If the condition returns True, the code proceeding the if statement (equally indented) is executed. If the statement/condition evaluates to False, then either the else block of code gets executed if there is one, or the block of code following the if block is executed, so the if block is effectively skipped. Let's take a look at the if code in action.

From now on, we are going to look at how scripting works. We will either be creating script files or carrying out exercises. For this reason, go ahead and create a file on gedit or any editor of your choice and name it if_condition.py. Alternatively, we can type gedit if_condition.py in the Terminal:

We then type the following code:

a=44
b=33
if a > b:
print("a is greater")
print("End")

Now, in order to run this script, we can simply type python3.5 if_condition.py in the Terminal:

The Python print method by default adds  to the string to be printed, using which we can see two outputs in different lines. Note that the syntax of the if statement is as follows:

  if <condition> : and then indented code

Whether or not we use brackets with the conditions is up to us. As you can see, the condition evaluated to True, so the line a is greater was printed. For if conditions in Python, anything that does not evaluate to zero (0), False, None, or empty would be treated as True and the code proceeding the if statement will get executed.

Let's see another example of the if condition in conjunction with the and...or and and...not logical operators. 

Let's create another file called if_detailed.py and type the following code:

You might have noticed that at the beginning of the file, we have a statement that reads #! /usr/bin/python3.5. This means we don't have to type python3.5 every time we execute the code. It directs the code to use the program placed at /usr/bin/python3.5 to execute it every time it's executed as an executable. We need to change the permissions of the file to make it executable. Do this, and then execute the code as follows:

The output produced is self-explanatory. As I mentioned before, anything that doesn't evaluate to 0, False, None, or empty is taken as True and the if block is executed. This explains why the first three if conditions were evaluated to True and the message was printed, but the fourth message was not printed. From line 19 onwards, we have made use of logical operators. In Python, the conjunction operation is carried by the and operator, which is the same as &&, which we use with C, C++, and Java.  For the short circuit Boolean operator, we have the or keyword in Python, which is the same as || in C, C++, and Java. Finally, the not keyword provides negation in Python, as ! does in other languages.

It should be noted that in Python, null byte characters are represented by a reserved keyword, None, which is the same as null in languages such as Java or C#.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.32.230