From the course: Programming Foundations: Fundamentals

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Working with simple conditions

Working with simple conditions - Python Tutorial

From the course: Programming Foundations: Fundamentals

Working with simple conditions

- With if statements, we've discovered a way to add a bit more flexibility and excitement to our programs, but so far, if the condition is false, then we just don't do anything. But typically, we'll want to take some action in this case as well. This is done with the if-else statement. It looks very similar to our normal if statement, except that it has the else keyword along with a colon, followed by a code block. In programming, this is called the else clause, because you never find it without an accompanying if statement. Just like in English, if I said, every morning before I go to work, and that's it, that would be an incomplete thought. It's just a clause. I'd have to add something like, every morning before I go to work, I enjoy a cup of coffee. Now and the thought is complete. Similarly, we don't use the else clause without an if statement. Let's look at an example. If five is greater than six, then…

Contents