Some decisions are inevitable
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: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3If 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
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;
German tax calculator in PythonTaxable 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:
- First zone (Grundfreibetrag): up to 8004,- € no tax has to be paid
- Second zone: zvE between 8.005 € and 13.469 €
StB = (912,17 * y + 1400)*y
y = (zvE - 8004) / 10000
- Third Zone: zvE between 13470 € and 52881 €
StB = (228,74 * z + 2397)*z
z = (zvE - 13469) / 10000
- Fourth Zone: zvE betwenn 52882 € and 250730 €
StB = 0,42 * zvE - 8172
- Fifth Zone: zvE starting with 250731 €
StB = 0,44 * zvE - 15694
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 else: tax = income * 0.44 - 15694 return taxIf "Ehegattensplitting" applies, the amount can be calculated like this:
taxes = 2 * taxes(income / 2)
Footnotes1 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.