Python Classroom
  • Introduction
  • About Python Classroom
  • Python Cloud Options
    • CS50 IDE
      • CS50 IDE Overview
      • CS50 IDE and Python
      • CS50 IDE Debugging
      • CS50 IDE Shortcuts
      • Linux Bash Shell
      • Vim Mode
        • Vim Tutorial
    • CS50 Sandbox
    • PythonAnywhere
    • Repl.it
  • Python Curriculum Map
    • Pedagogy
      • Python Professional Development
      • Teaching Tips
      • Assessment Tips
      • Rubrics
      • Activities
        • Picture Activity
        • Map Activity
        • Crossing the Bridge
        • NIM
        • Mastermind
        • Cards
          • Card Deck Algorithms
          • Sorting Cards
        • River Crossing
          • Jealous Boyfriends
          • Cannibals and Priests
          • Family
          • Police and the Thief
          • Humans and Monkeys
          • Moving Money
        • Crossing the River
        • Traveling Salesperson
        • Logic Problems
        • CIA Crack the Code
        • IQ Test
        • Puzzles
    • AP Computer Science Principles Framework
  • Python Philosphy
    • How to Practice Python
  • microbit
  • Turtle Graphics
    • Turtle Examples
    • Turtle Activities
    • Turtle Maze Problems
    • Turtle Graphics with loops
    • Turtle Snake
    • Turtle Graphics with Conditionals
  • Output
    • Output Examples
    • Output Mistakes
  • Variables
    • Variable Data Type Examples
    • Variable Role Examples
    • Variables Mistakes
    • Variables Problems
  • Math
    • Math Examples
    • Math Mistakes
    • Math Problems
    • Math Self Check
  • Input
    • Input Examples
    • Input Mistakes
    • Input Problems
  • Decisions
    • if
      • if Examples
      • if Mistakes
      • if Problems
    • if else
      • if else Examples
      • if else Problems
      • if / if else Problems
    • if elif else
      • if elif else Examples
      • if elif else Problems
    • nested if
      • nested if Examples
      • nested if Problems
    • Logical Operators
      • Logical Operators Examples
    • Adventure Game Project
  • Loops
    • while loop - count up
      • Examples
      • Problems
    • while loop - countdown
      • Examples
      • Problems
    • while loop - sentinel value
      • Problems
    • while loop - sentinel menu
      • Problems
    • for loop - range (one argument)
      • Examples
      • Problems
    • for loop - range (two arguments)
      • Problems
    • for loop - range (three arguments)
      • Problems
  • Lists
    • Lists - Numbers
      • Problems
    • Lists - Strings
      • Problems
      • Shopping Project
  • Dictionaries
  • String Methods
  • Functions
    • Variable Scope
    • Functions - no parameters
    • Functions - one parameter
    • Functions - return
    • Functions - lists
  • Files
  • Classes
    • Inheritance
  • Python Projects
    • Adventure Game
    • Restaurant Project
    • Trivia Game
    • Family Tree Project
  • Raspberry Pi
    • Raspberry Pi Models
    • Raspberry Pi Setup
  • Roblox
  • Glossary
Powered by GitBook
On this page
  • Objective
  • Introductory Problem
  • if
  • Examples
  • Example 1
  • Example 2
  • Example 3
  • Example 4

Was this helpful?

  1. Decisions

if

PreviousDecisionsNextif Examples

Last updated 5 years ago

Was this helpful?

Objective

After working through this lesson, you’ll be able to

  • Write Python code using if statements.

Introductory Problem

Write Python code that asks:

  • How many aunts do you have?

    • Assign the input to the variable aunts

  • How many uncles do you have?

    • Assign the input to the variable uncles

intro.py
aunts = int(input("How many aunts do you have? "))
uncles = int(input("How many uncles do you have? "))
print("I have",aunts,"aunts and",uncles,"uncles")

if

The if conditional statement can be True or False. When the if statement executes, the condition is tested and if it is true, the indented block of code is executed. Otherwise, it is skipped.

Indentation is Python's method for grouping statements.

Examples

Example 1

Ask the user for a number. Write if statements to determine.

  • if the number is positive.

    • Print positive

  • if the number negative

    • Print negative

  • if the number is 0

    • Print is zero.

num = int(input("Give me a number: "))
if num > 0:
    print(num, "is positive")
if num < 0:
    print(num, "is negative")
if num == 0:
    print(num, "is zero")

Example 2

Ask the user if it is hot today.

  • If the user enters "Yes", print "The summer is always hot!"

  • If the user enters "No", print "It's time to move!".

hot = input("Is it hot today? ")
if hot == "Yes":
    print("It's always hot!")
if hot == "No":
    print("It's always cold!")

Example 3

Ask the user how much money they have.

  • If the user enters 99.99 or more, print "You're rich!"

  • If the user enters less than 99.99, print "You're not rich!"

money = float(input("How much money do you have? "))
if money >= 99.99:
    print("You're rich!")
if money < 99.99:
    print("You're not rich!")

Example 4

Using the code from the Introductory Problem

  • If the user has more aunts than uncles

    • Print "You have more aunts than uncles"

  • If the user has more uncles than aunts

    • Print "You have more uncles than aunts"

  • If the user has the same number of aunts and uncles

    • Print "You have the same number of aunts and uncles

  • If the user does not have the same number of aunts and uncles

    • Print "You do not have the same number of aunts and uncles

uncles = int(input("How many uncles do you have? "))
aunts = int(input("How many aunts do you have? "))
if aunts > uncles:
    print("You have more aunts than uncles")
if aunts < uncles:
    print("You have more uncles than aunts")
if aunts == uncles:
    print("You have the same number of aunts and uncles")
if aunts != uncles:
    print("You do not have the same number of aunts and uncles")