Skip to content
Shop

CommunityJoin Our PatreonDonate

Sponsored Ads

Sponsored Ads

Statements & Conditional Statements

In this lesson you will learn how to use statements to tell a computer to do things with data and how to do those things under certain conditions.

Jessica
Junior Developer

Statements

Ever followed a recipe to whip up something tasty? A recipe is basically just a list of steps to follow, right? Well, a computer program is kinda like a recipe for a computer. Each little step in the program is called a statement. One simple thing a computer can do is show stuff on the screen. That’s what the print() statement is for. Let’s try it out and make the computer say your name!

python
name = "Tutorial Doctor"
print(name)

Another handy statement is the input() statement. It tells the computer to get information from the user. The code below stores the input from the user in a variable called number and then prints it to the screen using the print() statement.

python
number = input("Enter a number ")
print("You entered " + number)

Conditional Statements

A conditional statement lets you make the computer do something under certain conditions. The three conditional statements are if, else if, and else. In Python, else if is shortened to elif. The code below prints certain text based on the value of the cake_temperature variable.

python
cake_temperature = 80

if cake_temperature == 100:
    print("Take the cake out of the oven")
elif cake_temperature < 100:
    print("Leave the cake in the oven")
else:
    print("The cake is still heating up")

In English this reads as:

If the cake is 100 degrees, take it out of the oven. Otherwise, if it's less than 100 degrees, leave it in the oven. If it's anything else, the cake is still heating up.

THINK...

What do you think the program above will print to the screen? You should run it and see!

Handling Errors

Sometimes things don't go as planned. That's where try and except statements come in handy! The computer will "try" to do some code, and if it doesn't work (if there is an error) then it will do something else. In other words, you can try to do some code, except if it fails.

python
try:
  print(x)
except:
  print("An exception occurred")

You can also try to do some code except if a specific type of error happens. In the code below, we try to print name except if the name variable doesn't exist.

python
try:
    print(name)
except NameError:
    print("You did something wrong")

You can also use multiple exceptions.

python
try:
  print(x)
except NameError:
  print("Variable x is not defined")
except TypeError:
  print("Something is wrong with a type")
except:
  print("Something else went wrong")

Your Turn!

Let's build a CLI tool!

  1. Open your code editor
  2. Step 2
  3. Step 3

THINK...

Something to think about

Solution
python
first_name = input("What is your first name ? ")
middle_name = input("What is your middle name ? ")
last_name = input("What is your last name ? ")
gender = input("What is your gender ? ")
emails = input("List your emails (separate each one with a comma): ")

print("")
print("Now we will collect your birthday information")
print("")

birth_month = input("What month were you born ?  ")
birth_day = input("What day were you born ? ")
birth_year = input("What year were you born ? ")

user_data = {
    'first-name': first_name,
    'middle_name': middle_name,
    'middle_initial': middle_name[0],
    'last_name': last_name,
    "full_name": first_name + " " + last_name,
    'emails': emails,
    "birthday": {
        "year": birth_year,
        "month": birth_month,
        "day": birth_day
    },
    "gender": gender
}

print(user_data)

Resources

W3 Schools `

Terms

  • statement - instruction for a computer
  • print - display information on the screen
  • input - get information from the user
  • conditional statement - instruct a computer under certain conditions
  • if - execute a statement if a condition is true
  • else if - execute a statement if the "if" condition isn't true
  • else - execute a statement if the "if" and "else if" conditions aren't true