Python Basics: Conditional Logic and Control Flow (Overview)

Much of the Python code you’ll write is unconditional. That is, the code does not make any choices. Every line of code is executed in the order that it’s written or in the order that functions are called, with possible repetitions inside loops.

In this course, you’ll learn how to use conditional logic to write programs that perform different actions based on different conditions. Paired with functions and loops, conditional logic allows you to write complex programs that can handle many different situations.

In this Python Basics video course, you’ll learn how to:

  • Compare the values of two or more variables
  • Write if statements to control the flow of your programs
  • Handle errors with try and except
  • Apply conditional logic to create simulations

This video course is part of the Python Basics series, which accompanies Python Basics: A Practical Introduction to Python 3. You can also check out the other Python Basics courses.

Note that you’ll be using IDLE to interact with Python throughout this course.


Sample Code (.zip)

1.0 KB

Course Slides (.pdf)

3.0 MB

00:00 When you write programs, you want to be able to define certain paths for your program to take. With the tools presented in this course, you’ll be able to tell your code to run different parts of itself under different circumstances.

00:14 It’s like giving your script the ability to make choices, all of which combine to help you create powerful code that will still work under many different circumstances.

00:26 Conditional logic and control flow. What do those pretty abstract terms really mean? You’ll come to understand more as the course progresses, but as an illustration, it allows you to start defining actions your code can take if something else is true.

00:43 There are two parts to that statement. Conditional logic, which allows you to check if something is true or false. Does Bobby have more apples than Bill, for instance, or is Jane faster than June?

00:55 There’s a certain conditional logic contained in these questions. Does Bobby have more? Is Jane faster? Once you have the answer to these, which can be true or false, yes or no, then you use control flow to create branches of execution.

01:14 So all that combined would be: If Bobby has more apples, then give apples to Bill, for example. If Jane is faster, then give June good sneakers. So here you have the conditional part of the statement, and then the control flow part of the statement.

01:32 One is checking whether something is true or false, and then the control flow part is defining the actions to take if something is true or if something’s false.

01:43 For better or worse, you don’t code in plain English—you code in Python. Here’s an example of a conditional statement written in Python, something that by the end of this course, you’ll be able to read.

01:58 Here’s an example of what a more complicated execution tree might look like. It simulates the learning process for a Python student. while True is a loop that goes on forever. if python_is_awesome: if not overwhelmed: then you learn(). If you’re overwhelmed, then take a break, continue. If you need_to_sleep, break. else: continue and then reflect on your learning. But if Python is not awesome, then you just take a break until Python is awesome again. In any case, don’t worry about all these keywords, like continue, break, if, elif, else. All of this you’re going to cover in the course coming up.

02:38 Something else that you’ll cover that’s related to control flow is error handling. Errors happen, mistakes happen, and you want a way for your program to be able to handle errors if they come up.

02:50 There are ways for you to handle specific errors and to keep on running if these errors crop up.

02:59 To summarize, in this course, Conditional Logic and Control Flow, you’re going to be covering conditional logic control, flow, and error handling.

Become a Member to join the conversation.