Skip to Main Content

Intermediate Concepts in Python: Conditions and Iteration

This guide was created for those interested in increasing their Python programming knowledge. This guide is best for those who have some familiarity with basic concepts in Python.

What are IF statements?

IF statements are used to set logical conditions for operations or outputs. When using IF statements, you must indent the lines that follow the IF statement.  If statements can also include ELSE statements to account for those outputs/values that fall outside of the parameters set by the IF statement.


In this section, you will learn how to:

  • Employ IF...ELSE statements (IF Statements)
  • Identify and use comparison operators (IF Statements)
  • Appropriately use the IN keyword in an IF...ELSE statement (IF Statements)
  • Describe the appropriate use of indentation (IF Statements)
  • Appropriately use the AND keyword in an IF...ELSE statement to set multiple parameters (IF Statements)
  • Appropriately use the OR keyword in an IF...ELSE statement to set multiple parameters (IF Statements)
  • Appropriately use the ELIF keyword in an IF...ELSE statement to set multiple outputs (IF Statements)
  • Use input to collect user information (IF Statements)

Comparison Symbols

When using the IF...ELSE statement, you will need to set conditions for your code. Use the following comparison operators:

 > (Greater Than)

3 > 1 (Three is greater than one)

< (Less Than)

1<3 (One is less than three)

>= (Greater Than or Equal To)

5 >= 4 (Five is greater than or equal to four)

< (Less Than or Equal To)

 3<= 4 (Three is less than or equal to four)

== (Equal To)

 3== 3 (Three is  equal to three)

!= (Not Equal To)

4!=3 (Four is not equal to three

IF Statement Code Examples

Copy the following code on the left in your IDE to execute the process shown in the image on the right.

one = 1                                                                               Jupyter Notebook View
two = 2 

if two>one:
    print ("Two is greater than one")

 


This code uses both IF and ELSE to help determine the status of outputs that do not meet the parameters set by the IF statement.

one = 1                                                                 Jupyter Notebook View
two = 2 

if two>one:
    print ("Two is greater than one")
else:
    print ("One is greater than two")

Notice that the lines after the IF and ELSE statements have been indented? In Python, indention is used to show relationship/code order. For example, in the code above, the IF statement must evaluate to true in order to print the value listed below it, same with the ELSE statement.

IN Keyword

The IN keyword in Python is used to (1) check for values in a list or other multi-value variables as part of an IF statement and (2) to iterate through a list in a FOR loop. The following code demonstrates the first use of IN.

Copy the following code on the left in your IDE to execute the process shown in the image on the right.


list1 = [1,2,4,5]                                                                   Jupyter Notebook View
if 3 in list1:
    print ("3 is in list")
else:
    print ("3 not in list")

 

 


list2 = [1,2,3,4,5]                                                               Jupyter Notebook View                                        
if 3 in list2:
    print ("3 is in list")
else:
    print ("3 not in list")

IF...ELSE & AND (Multiple Conditions)

The and keyword is used to set multiple conditions in an if or if...else statement. View the code below for more on the use of and. Both conditions must be met.

Copy the following code on the left in your IDE to execute the process shown in the image on the right.


a = 1                                                                               Jupyter Notebook View
b = 3
c = 4

if a<b and a>c:
    print("B is largest")
else:
    print("B is not the largest")

IF...ELSE & OR

The or keyword is used to set multiple conditions in an if or if...else statement. View the code below for more on the use of or. Only one condition must be met.

Copy the following code on the left in your IDE to execute the process shown in the image on the right.


a = 1                                                           Jupyter Notebook View
b = 3
c = 4
if a<=b or a<=c:
    print("A is the smallest number or equal to the others")
else:
    print("A is not the smallest or is not equal to the others")

More than One ELSE

If you are interested in establishing multiple conditions, use the ELIF keyword in your ELSE...IF statement to represent the additional conditions.

Copy and paste the following code on the left in your IDE to execute the process shown in the video.


x = 4                                                               Jupyter Notebook View

if x == 4:
    print('x is equal to 4')
elif x == 3:
    print('x is equal to 3')
elif x == 2:
    print('x is equal to 2')
elif x == 1:
    print('x is equal to 1'
else:
    print('x is not 1, 2, 3, nor 4')

 


x = 5                                                                                 Jupyter Notebook View

if x == 4:
    print('x is equal to 4')
elif x == 3:
    print('x is equal to 3')
elif x == 2:
    print('x is equal to 2')
elif x == 1:
    print('x is equal to 1'
else:
    print('x is not 1, 2, 3, nor 4')

Taking User Input

If you are building code to take user input, you can use the input function. The function takes a string input that displays when a user is prompted for input. The following code demonstrates this logic.


Imagine you are playing a number guessing game in which the person guessing has to enter a number to see if it matches one of the coded numbers (2 or 3).

x = int(input("Enter a number between 1 and 10:"))

if x == 2 or x == 3:
    print('CORRECT!')
else:
    print('INCORRECT!')

Jupyter Notebook View

After pressing RUN, you will be prompted to enter a number and press ENTER. Do not press RUN again. For the sake of this demonstration, I am including code and images for a correct response and for an incorrect response. 

Correct Response

Jupyter Notebook View

 

Incorrect Response

Jupyter Notebook View

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.