PYTHON TUTORIALS

Python Tutorials: Conditionals

[Enter image here]

When writing scripts or defining functions, you'll likely come across the need for tasks to be accomplished based on a variable's value or a similar condition. Conditional Statements provide a solution to this problem. This tutorial will cover the following learning objectives:

  • How and When to Use If-Else Statements
  • How and When to Use If-Elif-Else Statements
  • How and When to Use Logical Operators

How and When to Use If-Else Statements




Summary

  • If you want to perform an action based on a single condition, use an If-Else Statement. This is used with the following syntax:
    if [variable/value] [Relational Operator] [variable/value]:
       [task to perform]
    else:
       [task to perform]
  • If you have a variable with a Boolean data type to be used as a condition, use the following syntax:
    if [variable]:
       [task to perform]
    else:
       [task to perform]
  • NOTE: Notice that the line of code after the initial "if" statement is indented. This is Python's way of knowing when code is grouped together into a block of code.

How and When to Use If-Elif-Else Statements




Summary:

  • If you want to perform an action based on more than one condition, use an If-Elif-Else Statement. This is used with the following syntax:
    if [variable/value] [Relational Operator] [variable/value]:
       [task to perform]
    elif [variable/value] [Relational Operator] [variable/value]:
       [task to perform]
    else:
       [task to perform]
  • NOTE: In conditonal statements, the first condition that gets tested as "True" will be executed and all other conditions will be ignored. Thus, it's important to orde your conditions carefully.

How and When to Use Logical Operators




Summary:

  • In Python, Logical Operators are used in conditional statements to create multiple conditions within a single block of code (a single level of an if-elif-else statement).
  • The and operator is used to check if two or more conditions are true. Example:
    if age >= 18 and gender == 'Male':
       print('Adult Male')
    else:
       print('Not an Adult Male')
  • The or operator is used to check if at least one condition in a list of conditions is true. Example:
    if price < 100 or color = 'brown':
       print('Item matches search parameters')
    else:
       print('Item does not match search parameters')
  • The not operator is used to switch the value of a Boolean variable. Example:
    if not discounted:
       print('item is not discounted')
    else:
       print('Item is discounted.')

Exercise

Congratulations! You just completed the Conditionals Tutorial! To help test your knowledge, let's practice creating some If-Else Statements combined with logical operators.
**It's highly recommended that you complete the exercise outlined in the previous tutorial before beginning this exercise.**

Instructions:

  1. Open your IDE (e.g., VS Code, PyCharm, Spyder).
  2. Create a New Python File. Name it "conditionals.py"
  3. In the Newly Created Python File, complete the following tasks:
    1. Declare a variable named "calories" and give it a value of 500.
    2. Declare a variable named "protein_amt" and give it a value of 12.
    3. Declare a variable named "sugar_amt" and give it a value of 33.
    4. Declare a variable named "organic" and give it a value of True.
    5. Create a Conditonal Statement that outputs "This food is healthy" if "calories" is less than or equal to 500 and "protein_amt" is greater than 5. Otherwise, output "This food is unhealthy".
    6. Create a Conditional Statement that outputs "This food is within your diet" if "sugar_amt" is less than 20 or "organic" is True. Otherwise output "This food is not within your diet".
    7. Create a Conditional Statement that switches the Boolean state of "organic" and outputs "This food is not organic". Otherwise, if "protein_amt" is equal to 10 then output "This food matches your protein intake levels". Otherwise output "You shouldn't consume this food because it's not organic and doesn't match your protein intake levels".
  4. Exercise Completed! Click here to view the answers.
  5. Have any issues with the above exercise? Post your question on Discord!