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.

Junior Developer
What is a Statement?
A statement is a single instruction for a computer. A very useful statement in Python is the print() statement. This statement lets you display information on your screen.
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 gets input from the user, stores it in a variable called number, and then prints it to the screen using the print() statement.
number = input("Enter a number ")
print("You entered " + number)Conditional Statements
A conditional statement lets you tell the computer to do something under certain conditions. There are three conditional statements in Python. The three conditional statements are if, else if, and else. Note that in Python, else if is shortened to elif. In conditional_statements.py the code checks the value of the cake_temperature variable and conditionally prints some text based on that value.
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 program would read 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 the code you write has an error and you need to do something as a result. That's where the 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.
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.
try:
print(name)
except NameError:
print("You did something wrong")You can also use multiple exceptions.
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!
- Open your code editor
- Step 2
- Step 3
THINK...
Something to think about
Solution
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
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


