User:Milanand/Python 3 Programming/Conditional Statements

Decisions
A Decision is when a program has more than one choice of actions depending on a variable's value. Think of a traffic light. When it is green, we continue our drive. When we see the light turn yellow, we reduce our speed, and when it is red, we stop. These are logical decisions that depend on the value of the traffic light. Luckily, Python has a decision statement to help us when our application needs to make such decision for the user.

If Statements
Here is a warm-up exercise - a short program to compute the absolute value of a number: What does the computer do when it sees this piece of code? First it prompts the user for a number with the statement. Next it reads the line. If  is less than zero Python runs the line. Otherwise Python runs the line.

More formally, Python looks at whether the expression  is true or false. An  statement is followed by an indented block of statements that are run when the expression is true. After the  statement is an optional   statement and another indented block of statements. This 2nd block of statements is run if the expression is false.

Expressions can be tested several different ways. Here is a table of all of them: Another feature of the  command is the   statement. It stands for "else if," which means that if the original  statement is false and the   statement is true, execute the block of code following the   statement. Here's an example: Notice how the   is only tested when the   statement fails to be true. allows multiple tests to be done in a single if statement.