Conditionals in thonny2/23/2023 ![]() ![]() The second branch contains another if statement, which has two branches of its own. The first branch contains a simple statement. The outer conditional contains two branches. We could have written the three-branch example like this: if x = y: One conditional can also be nested within another. Even if more than one condition is true, only the first true branch executes. If one of them is true, the corresponding branch executes, and the statement ends. If the first is false, the next is checked, and so on. If there is an else clause, it has to be at the end, but there doesn't have to be one.Įach condition is checked in order. There is no limit on the number of elif statements. If the condition is false, the second set of statements is executed. ![]() If the remainder when x is divided by 2 is 0, then we know that x is even, and the program displays a message to that effect. ![]() The syntax looks like this: if x% 2 = 0 : In that case, you can use the pass statement, which does nothing.Ī second form of the if statement is alternative execution, in which there are two possibilities and the condition determines which one gets executed. Occasionally, it is useful to have a body with no statements (usually as a place holder for code you haven't written yet). There is no limit on the number of statements that can appear in the body, but there must be at least one. Statements like this are called compound statements because they stretch across more than one line. The statement consists of a header line that ends with the colon character (:) followed by an indented block. If statements have the same structure as function definitions or for loops 1. If the logical condition is false, the indented statement is skipped. If the logical condition is true, then the indented statement gets executed. We end the if statement with a colon character (:) and the line(s) after the if statement are indented. ![]() The boolean expression after the if statement is called the condition. The simplest form is the if statement: if x > 0 : Conditional statements give us this ability. In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. You might want to avoid it until you are sure you know what you are doing. This flexibility can be useful, but there are some subtleties to it that might be confusing. Any nonzero number is interpreted as "true." > 17 and True True Strictly speaking, the operands of the logical operators should be boolean expressions, but Python is not very strict. X > 0 and x y) is true if x > y is false that is, if x is less than or equal to y. The semantics (meaning) of these operators is similar to their meaning in English. There are three logical operators: and, or, and not. The = operator is one of the comparison operators the others are: x != y # x is not equal to y True and False are special values that belong to the class bool they are not strings: The following examples use the operator =, which compares two operands and produces True if they are equal and False otherwise: The first condition checks whether x is less than y, while the alternative condition checks whether x is greater than y.Īs x is equal to a value less than y, the first condition is satisfied, which results in a value of 5 being added to x.Conditional execution Boolean expressionsĪ boolean expression is an expression that is either true or false. In the first example, x is compared to y. However, the indentation remains an important part of the syntax of the conditional statement.Ĭheck out the examples below to see elif in action and see how print statements can be replaced with other code. Recall that the print code in the examples above can be replaced by any code that will execute in Python.Īs the code provided with the if, elif, and else statements gets longer, it is common to add blank lines to make it easier to see which code will be executed with whitch statement. If condition : print ( "First condition is true." ) elif alternative_condition : print ( "First condition is not true but alternative condition is true." ) else : print ( "Neither of these conditions is true, so this statement is printed." ) Spacing and Execution of Code Lines ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |