Lesson 3

Main Concept: Making decisions with code.

Here are a few things that can help you understand the Main Concept for todays lesson:

1. Making Decisions

2. If/Else Statements

3. Comparison Operators

4. And/Or

5. Elif Statement

By the end you will be able to: Know how to make decisions in your code and do different things depending on what the user inputs.

Here's a 2 page PDF that covers the same content if that's easier for you

1. Making Decisions

Just like we make countless decisions in everyday life, it is essential that we are able to make decisions on a computer.

 

A few decisions we might want to make on a computer:

  • Is this the right password to log in with?
  • Is this person old enough to drive?
  • Is this child tall enough to ride this rollercoaster?
 

To make decisions in code, we use conditional (if/else) statements.

2. If/Else

The if statement makes a decision based on a yes or no question. Yes or no can also be though of as true or false.

 

Any expression that results to either true of false we call a boolean expression.

If the result is True, we decide to do one thing, if it is False, we decide to do another thing.

Python asks: is 10 >= 16?

Because 10 is not greater than or equal to 16, the expression is false.

So the else branch, which contains, print(“You are too young to drive”) is what runs.

 

3. Comparison Operators

The most common ways to compare two things are:

We can compare any of the same data types. However, 1 type of data can usually not compare to other types. For example, the condition: 5 > “hello” will cause an error. While, 5 < 7 will simply return True.

 

4. And/Or

Programmers often want to make a choice based on two conditions, in this case, we can use and/or to see which of those conditions is true.

5. Elif

In programming we can sometimes have several possible conditions we want to check.

 

This is when we use the elif statement. It stands for, else if, in other words, it means, if this doesn’t work, let’s see if this works.

 

Any conditional statement always starts with an if statement. After that we can have as many elif statements as we want, and then an else at the end if we’d like. The order of operations goes from the If statement then to all, top to bottom, of the elif statements, once it finds one that’s true, the program skips past all other elif/else statements. The else statement will only run if all if and elif statements end up being False.

If you'd like to have a qualified tutor explain this lessons content to you, download our Spartan Tutors app to book a session today!

Last Lessons Content

Main Concept: Building blocks of programming and writing your first programs.

By the end you will be able to: Turn your computer into a calculator and a bot that greets you.

Last Lessons Content

Next Lessons Content

Main Concept: Building blocks of programming and writing your first programs.

By the end you will be able to: Turn your computer into a calculator and a bot that greets you.

Main Concept: Dealing with strings and print formatting.

By the end you will be able to: Understand how strings are simply sequences of characters, how to do unique things with them, and how to print them in nice ways.