From the course: Introduction to Python: Learn How to Program Today with Python by Pearson

Unlock this course with a free trial

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

Use conditional "if" statements

Use conditional "if" statements

All right, so, so far, we have mostly looked at scripts where you're doing line by line, do this and do this and do this. We've seen a bit of functions and that moves things around, but now let's look into some of the core concepts of control flow, which is being able to control the flow and execution of your program. The first one is conditionals. This allows you to do a flow chart where you can branch off, do one thing under one condition and do another thing under another condition. How do we do that in Python? We could work in our Python console again, But similar to functions, when there's multiple lines that need to be executed before being able to evaluate it, you have to add just an extra enter at the very end. So the basic one is just using a simple if keyword, and then you put some sort of condition in there. So the condition will evaluate to either true or false. If it's true, it'll do a thing, print, it was true. And again, we have, so we've got a keyword if, then a…

Contents