Conditional Statements

Some decisions are inevitable

inevitable decisions Under certain conditions some decisions are sometimes in normal live 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:
elif condition_2:
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:

The following Python script implements this "dog years algorithm":
age = input("Age of the dog: ")
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:

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)
C programmers have to get used to a different notation in Python:
max = a if (a > b) else b;

German tax calculator in Python

Taxable income (zvE) has to be assigned to a tarif zone.1 After this the amount of tax (StB) (for so-called "Einzelveranlagung") can be calculated with the corresponding formula:
A Python function calculating the tax from the income tarif looks like this: (No limit or warranty!!!)
def taxes(income):
    """Calculation of taxes to be paid for a taxable income x"""
    if income <= 8004:
        tax = 0
    elif income <= 13469:
        y = (income -8004.0)/10000.0
        tax = (912.17 * y + 1400)*y
    elif income <= 52881:
        z = (income -13469.0)/10000.0
        tax = (228.74 * z +2397.0)*z +1038.0
    elif income <= 250730:
        tax = income * 0.42 - 8172.0
        tax = income * 0.44 - 15694
    return tax

If "Ehegattensplitting" applies, the amount can be calculated like this:
taxes = 2 * taxes(income / 2)


1 Corresponding German legislative text:
Einkommensteuergesetz, § 52 Anwendungsvorschriften
(41) § 32a Absatz 1 ist ab dem Veranlagungszeitraum 2010 in der folgenden Fassung anzuwenden:
 "(1) 1Die tarifliche Einkommensteuer bemisst sich nach dem zu versteuernden Einkommen. 2Sie beträgt vorbehaltlich der §§ 32b, 32d, 34, 34a, 34b und 34c jeweils in Euro für zu versteuernde Einkommen
bis 8 004 Euro (Grundfreibetrag):
von 8 005 Euro bis 13 469 Euro:
(912,17 * y + 1 400) * y;
von 13 470 Euro bis 52 881 Euro:
(228,74 * z + 2 397) * z + 1 038;
von 52 882 Euro bis 250 730 Euro:
0,42 * x - 8 172;
von 250 731 Euro an:
0,45 * x - 15 694.
3"y" ist ein Zehntausendstel des 8 004 Euro übersteigenden Teils des auf einen vollen Euro-Betrag abgerundeten zu versteuernden Einkommens. 4"z" ist ein Zehntausendstel des 13 469 Euro übersteigenden Teils des auf einen vollen Euro-Betrag abgerundeten zu versteuernden Einkommens. 5"x" ist das auf einen vollen Euro-Betrag abgerundete zu versteuernde Einkommen. 6Der sich ergebende Steuerbetrag ist auf den nächsten vollen Euro-Betrag abzurunden."