# Python Course

Previous Chapter: input and raw_input via the keyboard
Next Chapter: While Loops

## Conditional Statements

### Some decisions are inevitable

Under certain conditions some decisions are sometimes in normal life inevitable, as we can can see in our photo. It's the same for every program, which has to solve some useful problem. There is hardly a way to program without having branches in the flow of code.
In programming and scripting languages, conditional statements or conditional constructs are used to perform different computations or actions depending on whether a condition evaluates to true or false. (Please note that true and false are always written as True and False in Python.)
The condition usually uses comparisons and arithmetic expressions with variables. These expressions are evaluated to the Boolean values True or False. The statements for the decision taking are called conditional statements, alternatively they are also known as conditional expressions or conditional constructs.
The if-then construct (sometimes called if-then-else) is common across many programming languages, but the syntax varies from language to language.

### The if Statement

The general form of the if statement in Python looks like this:

    if condition_1:
        statement_block_1
    elif condition_2:
        statement_block_2
    else:
        statement_block_3

If the condition "condition_1" is True, the statements in the block statement_block_1 will be executed. If not, condition_2 will be executed. If condition_2 evaluates to True, statement_block_2 will be executed, if condition_2 is False, the statements in statement_block_3 will be executed.

### Example Dog Years

It's a generally accepted belief, to assume that one year in the life a dog corresponds to seven years in the life of a human being. But apparently there are other more subtle methods to calculate this haunting problem, haunting at least for some dog owners.
Another subtler method works like this:

- A one year old dog roughly corresponds to a fourteen year old child
- A dog who is two years old corresponds to a 22 year old human
- Every further dog year corresponds to five human years

The following Python script implements this "dog years algorithm":

    age = input("Age of the dog: ")
    print
    if age < 0:
    	print "This can hardly be true!"
    elif age == 1:
    	print "about 14 human years"
    elif age == 2:
    	print "about 22 human years"
    elif age > 2:
    	human = 22 + (age -2)*5
    	print "Human years: ", human

    ###
    raw_input('press Return>')

There is one drawback to the script. I works only for integers, i.e. full years.

### True or False

Unfortunately it is not as easy in real life as it is in Python to differentiate between true and false:
The following objects are evaluated by Python as False:

- numerical zero values (0, 0L, 0.0, 0.0+0.0j),
- the Boolean value False,
- empty strings,
- empty lists and empty tuples,
- empty dictionaries.
- plus the special value None.

All other values are considered to be True.

### Abbreviated IF statement

C programmers usually know the following abbreviated notation for the if construct:

    max = (a > b) ? a : b;

This is an abbreviation for the following C code:

    if (a > b)
       max=a;
    else
       max=b;

C programmers have to get used to a different notation in Python:

    max = a if (a > b) else b;

Previous Chapter: input and raw_input via the keyboard
Next Chapter: While Loops