How to Use the if Statement in a Python Application - dummies

How to Use the if Statement in a Python Application

By John Paul Mueller

It is possible to use the if statement in a number of ways in a Python application. However, you immediately need to know about three common ways to use it:

  • Use a single condition to execute a single statement when the condition is true.

  • Use a single condition to execute multiple statements when the condition is true.

  • Combine multiple conditions into a single decision and execute one or more statements when the combined condition is true.

Working with relational operators

A relational operator determines how a value on the left side of an expression compares to the value on the right side of an expression. After it makes the determination, it outputs a value of true or false that reflects the truth value of the expression. For example, 6 == 6 is true, while 5 == 6 is false. These steps show how to create and use an if statement.

  1. Open a Python Shell window.

    You see the familiar Python prompt.

  2. Type TestMe = 6 and press Enter.

    This step assigns a value of 6 to TestMe. Notice that it uses the assignment operator and not the equality operator.

  3. Type if TestMe == 6: and press Enter.

    This step creates an if statement that tests the value of TestMe using the equality operator. You should notice two features of the Python Shell at this point:

    • The word if is highlighted in a different color than the rest of the statement.

    • The next line is automatically indented.

  4. Type print(“TestMe does equal 6!”) and press Enter.

    Notice that Python doesn’t execute the if statement yet. It does indent the next line. The word print appears in a special color because it’s a function name. In addition, the text appears in another color to show you that it’s a string value.

  5. Press Enter.

    The Python Shell outdents this next line and executes the if statement. Notice that the output is in yet another color. Because TestMe contains a value of 6, the if statement works as expected.

    image0.jpg

Performing multiple tasks

Sometimes you want to perform more than one task after making a decision. Python relies on indentation to determine when to stop executing tasks as part of an if statement. As long as the next line is indented, it’s part of the if statement. When the next line is outdented, it becomes the first line of code outside the if block.

A code block consists of a statement and the tasks associated with that statement. The same term is used no matter what kind of statement you’re working with, but in this case, you’re working with an if statement that is part of a code block.

  1. Open a Python Shell window.

    You see the familiar Python prompt.

  2. Type the following code into the window — pressing Enter after each line:

    TestMe = 6
    if TestMe == 6:
     print("TestMe does equal 6!")
     print("All done!")

    Notice that the shell continues to indent lines as long as you continue to type code. Each line you type is part of the current if statement code block.

  3. Press Enter.

    Python executes the entire code block.

    image1.jpg

Making multiple comparisons using logical operators

Real life often requires that you make multiple comparisons to account for multiple requirements.

In order to make multiple comparisons, you create multiple conditions using relational operators and combine them using logical operators. A logical operator describes how to combine conditions. The and keyword is a logical operator that states that both conditions must be true.

One of the most common uses for making multiple comparisons to determine when a value is within a certain range. In fact, range checking is an important part of making your application secure and user friendly. The following steps help you see how to perform this task.

  1. Open a Python File window.

    You see an editor in which you can type the example code.

  2. Type the following code into the window — pressing Enter after each line:

    Value = int(input("Type a number between 1 and 10: "))
    if (Value > 0) and (Value <= 10):
     print("You typed: ", Value)

    The example begins by obtaining an input value. You have no idea what the user has typed other than that it’s a value of some sort. The use of the int() function means that the user must type a whole number (one without a decimal portion). Otherwise, the application will raise an exception. This first check ensures that the input is at least of the correct type.

    The if statement contains two conditions. The first states that Value must be greater than 0. You could also present this condition as Value >= 1. The second condition states that Value must be less than or equal to 10. Only when Value meets both of these conditions will the if statement succeed and print the value the user typed.

  3. Choose Run→Run Module.

    You see a Python Shell window open with a prompt to type a number between 1 and 10.

  4. Type 5 and press Enter.

    image2.jpg

    The application determines that the number is in the right range.

  5. Repeat Steps 3 and 4, but type 22 instead of 5.

    The application doesn’t output anything because the number is in the wrong range. Whenever you type a value that’s outside the programmed range, the statements that are part of the if block aren’t executed.

  6. Repeat Steps 3 and 4, but type 5.5 instead of 5.

    image3.jpg

    Even though you may think of 5.5 and 5 as both being numbers, Python sees the first number as a floating-point value and the second as an integer.

  7. Repeat Steps 3 and 4, but type Hello instead of 5.

    Python displays about the same error message as before. Python doesn’t differentiate between types of wrong input. It only knows that the input type is incorrect and therefore unusable.