Or maybe a little game you can play with your friends. Ask the user for a name and a password and if they enter the correct one, display a message. Now you can try and create your own little programs. Print('Are you sure you should be here? At this time?') AndĪ condition statement can check multiple statements, not just one: if 5 > 4 and 5 100: We can write more conditions that are true: It doesn’t matter, the code always will run the code inside the first condition that it’s true. Let’s check the second condition: It is year equal to 2019? Yes. It is the current year bigger than 2019? No. Print('Cool, we are in the same space-time') We can create a secondary (and a third, and a fourth, and…) conditional statement with “ elif“: year = 2019 What if we want to check multiple things? Maybe I want to check if the temperature is below X degrees to grab a jacket, if it is above Y degrees to wear a shirt and if it is between X and Y to wear a thin sweatshirt. In the next chapter, you’ll learn how to repeat code tasks using loops.The past code is pretty limited. If number_of_guests help you compare multiple variables. If you have two values a and b, you can use the following comparison operators in Python: Print("stay inside!") Define Complex Conditions With Comparative ExpressionsĬomparative expressions let you compare different expressions to each other and evaluate whether that expression is true or false. Let’s say you want to go to the beach only if it’s sunny and a weekend, but if it’s sunny and a weekday, you need to be at work. These operators can be mixed and matched for your needs. Not: check if a condition is not true (i.e., false). Or: check if at least one condition is true. If you want to check multiple conditions for a single outcome in the same if statement, you can use logical operators:Īnd: check if two conditions are both true. Define Multiple Conditions With Logical Operators But if snowing were also false, the program would execute the final else statement and print stay inside! . Because snowing is True, the code will print build a snowman . This code will first check if sunny is true, and because it is false, it will then check if snowing is true. You can code that with the following syntax: sunny = False Let’s say you want to go to the beach if it’s warm outside, build a snowman if it’s snowing, or otherwise stay home. You then end the statement with an else clause. Define Alternative Conditions by Adding an Elif ClauseĪn if/elif/else statement allows you to define multiple conditions. The elif keyword allows you to add as many conditions as you’d like. If it were set to false, the code would print stay inside! . In this code snippet, since sunny is true, you will see go to the beach! in your terminal. So to only print a statement under a certain condition, you can do the following: sunny = True When using if/else statements in Python, the syntax looks like this: if my_boolean: Now let's expand the weather example to include an else clause:Įlse (for example, if it's raining), stay inside! Sounds simple enough, so where does "else" come in? We covered true-and-false statements in Part 1 when we spoke about booleans. Line 2 will only execute if the condition sunny is True . With an if statement, you can run certain lines of code only if a certain condition is true. If that condition is false, the code won't run. One of the most important and basic building blocks of a conditional flow is the if statement. Define Conditions With If/Else Statements Let's see how this type of logic works in code. In this scenario, whether or not you go to the beach is dependent on the weather as a condition. You make decisions based on conditions in your daily life all the time.įor example: If it's sunny outside, go to the beach. In this first chapter on program flow, we will look at how to program your code using conditional statements.Ĭonditionals are ways to control the logic and flow of your code using different conditions. Their principal responsibility is to act and follow the. Some lines will only be read once, others multiple times, and others still may be skipped entirely, depending on how you've programmed them. Conditional statements are one of the most important topics to master in any programming language. Program flow is the order in which lines of code are executed.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |