| Start | ||||||||
|---|---|---|---|---|---|---|---|---|
| Conditional Expression (or "The Ternary Operator") The ternary operator is used for inline conditional expressions. It is best used in simple, concise operations that are easily read.
The result of this expression will be as it is read in English - if the conditional expression is True, then it will evaluate to the expression on the left side, otherwise, the right side. Tenary operations can also be nested, as here: They also provide a method of including conditionals in lambda functions (opens new window). |
||||||||
| if, elif, and else In Python you can define a series of conditionals using Outputs Using |
||||||||
| Truth Values The following values are considered falsey, in that they evaluate to
All other values in Python evaluate to Note: A common mistake is to simply check for the Falseness of an operation which returns different Falsey values where the difference matters. For example, using |
||||||||
| Boolean Logic Expressions Boolean logic expressions, in addition to evaluating to And operatorThe Or operatorThe Lazy evaluationWhen you use this approach, remember that the evaluation is lazy. Expressions that are not required to be evaluated to determine the result are not evaluated. For example: In the above example, Testing for multiple conditionsA common mistake when checking for multiple conditions is to apply the logic incorrectly. This example is trying to check if two variables are each greater than 2. The statement is evaluated as - Each variable needs to be compared separately. Another, similar, mistake is made when checking if a variable is one of multiple values. The statement in this example is evaluated as - Again each comparison must be made separately Using the in operator is the canonical way to write this. |
||||||||
Else statement The else statement will execute it's body only if preceding conditional statements all evaluate to False. |
||||||||
| Using the cmp function to get the comparison result of two objects Python 2 includes a Suppose you need to print
This function is removed on Python 3. You can use the |
||||||||
If statement The The condition can be any valid expression: |
||||||||
| Conditional Expression Evaluation Using List Comprehensions Python allows you to hack list comprehensions to evaluate conditional expressions. For instance, Example: Here The inbuilt This could be used with list comprehensions to return the first(ie. index 0), second(ie. index 1) and last(ie. index -1) element of the list. Giving us a conditional of this type: Finally, in all the examples above Python evaluates both branches before choosing one. To only evaluate the chosen branch: where adding the Example: Syntax
|
||||||||
| Testing if an object is None and assigning it You'll often want to assign something to an object if it is The simplest way to do this is to use the (Note that it is more Pythonic to say But this can be optimized slightly by exploiting the notion that But there is a more Pythonic way. The following code is also equivalent: This does a Short Circuit evaluation (opens new window). If |
Read Article
https://develop.horje.com/learn/1434/reference