Overview
In this section, you will learn about conditionals and functions in Python. These two concepts will help you unlock the self-driving capabilities of your car! You can follow along using the conditionals_and_functions_lesson.ipynb notebook in the units/5.obstacle_avoidance folder.
What are Conditionals?
When programming, you’ll often need your code to make decisions based on some piece of data. Most programming languages allow you to do this using conditionals. A conditional checks whether something is true or false and then chooses what to do next.
Here’s a simple real-world example:
Imagine you’re on a sports team that usually practices outside. Your coach has a rule: if the weather is bad, practice moves indoors.
That’s a conditional — a decision based on a true/false statement:
- If it’s raining, the statement “The weather is bad” is True, so practice is inside.
- If it’s sunny, the statement is False, so practice is outside.
We can describe this idea in plain English like this:
if {Statement} = True:
Then do this.
else:
Then do this.
In Python, conditionals look very similar and are often written using if/else statements. They allow your program to control its flow and make decisions based on the data it receives.
Sometimes there is another part of if/else statements: elif. elif is short for else if, and it allows your program to check another condition when the previous one is false.
A conditional with elif works like this:
- Python checks the first
ifstatement.- If it’s true, that block runs and the entire conditional ends.
- If it’s false, Python continues to the next condition.
- Python checks the
elifstatement.- If this condition is true, that block runs and the conditional ends.
- If it’s false, Python moves on again.
- If none of the above conditions are true, the
elseblock runs.
Using elif allows you to create a chain of decisions without writing multiple disconnected if statements. This keeps your code organized and ensures that only one of the options will run.
Here’s the idea in plain English:
if condition A is true:
Do this
else if condition B is true:
Do this
else:
Do this
What are Functions?
When you’re writing code, you might find yourself typing the same lines over and over again. This can quickly become tedious, confusing, and can make your program hard to read. To solve this problem, programmers use something called a function to reduce repetition and organize their code.
A function is a reusable block of code that performs a specific task. You can define a function once and then use it as many times as you want. Different programming languages have different ways to define functions, but all functions must be defined before they can be used.
When defining a function, you must give it a name and a body (the code that tells the function what it should do). Here is an example of a simple function in Python:
def printName():
print("My name is Jeff.")
This function is named printName, and every time it runs it prints the same message. It doesn’t need any additional information to do its job.
You may have noticed how the function name is written. This style is called camelCase, where the first word is lowercase and each additional word begins with a capital letter. Programmers often use camelCase when naming functions that contain multiple words, because it makes the name easier to read. Function names should always describe what the function does.
Now that you’ve seen a basic example, let’s look at something a bit more useful:
def add():
sum = 1 + 2
print(sum)
This function adds 1 and 2 and prints the result. While it works, it isn’t very reusable — it can only add those two specific numbers. To make a function more flexible, programmers use parameters. Parameters allow you to pass different values into the function each time you call it.
Here’s an improved version that uses parameters:
def add(num1, num2):
sum = num1 + num2
print(sum)
Now the function can add any two numbers you choose. For example, add(3, 5) will print 8. Parameters make functions far more powerful and adaptable.
There is one more important idea you need to know: the return value. Printing a result can be useful, but sometimes your program needs to use the result for something else. In that case, the function should return the value instead of printing it.
Here is an example:
def subtract(num1, num2):
difference = num1 - num2
return difference
This function subtracts num2 from num1 and then returns the result. Returning a value allows you to save it in a variable or use it later in your program, like this:
result = subtract(10, 4)
Now the variable result contains the value 6, which your program can use however it needs.
What’s Next?
Once you have an understanding of conditionals and functions, you can continue on to the next section which will be an activity in which you use the ultrasonic sensor and the concepts you learned here to create an obstacle avoidance program!