How to Do Conditionals in Python
In programming languages, conditionals are statements that programmers use to determine how the program executes; without conditionals, computer programs could not make decisions based on the state of the program. This is true for all programming languages, including Python. However, not all programming languages use conditionals in the same way. Python uses conditional statements differently from C or Java programs.
Instructions
-
-
1
Interpret a conditional statement fully and correctly. A conditional statement represents a choice the program makes based on a certain condition. For example, you use a comparison of values to determine how a program will continue execution. The statement "x >= 5" is a conditional, in that it compares the value of the value "x." If x is greater than or equal to 5, then the condition is true. Otherwise, it is false.
-
2
Use a conditional in an "if" statement. The basic "if" statement uses a conditional. If the conditional is true, the "if" statement executes:
>>>if x >= 5:
. . . /*do something*/ -
-
3
Use a conditional in a loop. Loops are similar to "if" statements, but rather than executing once, a loop statement executes until the condition is false. For example, a "while" loop will check a conditional. If the conditional is true, the "while" loop executes. Then the "while" loop checks the conditional again, and continues to run as long as it still remains true:
while x >= 5:
. . . /*runs until x < 5*/
-
1