Python Conditional Execution Explained

A Python conditional may involve steps in a program that may or may not be executed.

Look at the following Python conditional program:

x = 0
if x == 0:
++++print x, ‘is nothing.’
else:
++++print x, ‘is something’

 

Think of the word ‘if’ as asking a question. The above Python conditional is asking if x is equal to zero. In this case it is, so the indented statement directly under the ‘if’ question gets executed. Consequently, the ‘else’ is ignored. The opposite would be true if x was any other value.

Know The Python Conditional Comparison Operators

You will need to be familiar with all the comparison operators to be proficient with Python conditional logic.

Python Conditional Comparison Operators
Take a moment to study these comparison operators.

Look at how to modify the above program to effectively do the same thing.

x = 0
if x != 0:
++++print x, ‘is something’
else:
++++print x, ‘is nothing.’

 

Remember that comparison operators only look at a value, they do not change it. Hence, there is a difference between ‘=’ and ‘==’. The first assigns a value, and the latter looks at a value.

Boolean expressions use comparison operators to evaluate to ‘True’ or ‘False’.

smart = True
if smart:
++++print ‘Congratulations!’

 

Indentation is very important for a Python conditional to execute properly. The convention is four spaces, but that’s not a hard fast rule. All lines that are part of a conditional should be indented. De-indenting ends the conditional.

x = 0
if x == 0:
++++print x, ‘is nothing.’
++++print ‘sorry’ ,x
else:
++++print x, ‘is something’
++++print ‘way to go’, x,

 

Warning! Your text editor may see indenting with a tab differently than indenting with spaces. Use spaces to indent for Python. The alternative is to go into the properties of the text editor, and change the tab settings to be seen as spaces.

Using Python Conditional Logic for Nested Decisions

A Python Conditional can be nested inside other conditionals.

x = -1
if (x > 0):
++++print x, ‘is not negative’
++++if (x > 1000000):
++++++++print x, ‘is quite large’

 

In the above program, None of the conditionals will execute, because x is negative. If x is 100, then only the first conditional executes. If x is 1000001, then both conditionals execute.

A Python Conditional for Multi-Way Decisions

You can also write conditionals that involve multi-way decisions. Use the Python reserved word, ‘elif’. It means else if. Look at the following program:

x = 3
if (x < 3):
++++print ‘Small’
elif (x < 7):
++++print ‘Medium’
else:
++++print ‘Large’

 

In the above program, x would be medium. Many ‘elif’ statements maybe be used. Only one ‘else’ statement can be used.

Look at the following puzzles, and try to identify the defective line in each program. Both the left and right side programs have a defective Python conditional.

Python Conditional Multi-Way Puzzles
Look for the Python Conditionals that will never execute.

The ‘try’ and ‘except’ Special Python Conditional

The ‘try’ and ‘except’ conditional has limited use, but can be handy when building a program with some dangerous code.

This concept might be hard to imagine, so take a look at the example, for conceptual purposes.

my_string = ‘nine’
try:
++++my_number = int(my_string)
except:
++++my_number = 9

 

The code ‘my_number = int(my_string)’, is the dangerous code. It is going to blow-up the program because you can not use ‘int’ on a string. However, the program is rescued by the two lines that come below that.

This special Python conditional could be useful for controlling user input, for example. Sometimes users will make mistakes when giving input. A good program can work around user mistakes. Use the special conditional as a work around while building new programs.

Here is a solution to the classic calculate pay problem. In this problem you have to use a conditional to calculate pay because there are two pay scales – regular and overtime. How might you solve this problem with your own Python program?

 

Leave a Reply

Your email address will not be published. Required fields are marked *