<jupyter_start><jupyter_text>"Official" Online-Tutorialhttps://docs.python.org/3.9/tutorial/index.html "Official" Beginners guidehttps://wiki.python.org/moin/BeginnersGuide  Control-flow Truth-values The interpreter processes the instructions in the source code in a sequiential order, step by step, line by line. Often however, we need to deviate from this strict sequence to exectute code only under certain _conditions_. For such _conditions_ we test wether things are `True` or `False`.<jupyter_code>no = False
yes = True
no<jupyter_output><empty_output><jupyter_text>Ein weiterer Wert, den Varialblen annehmen können ist `None`, wenn kein Wertvorgliegt  ComparisonsWhether a condition holds or not, we can check with the comparison operator `==` . Note, that there are two equal signs as opposed to the assignment opererator `=`.Such a comparison result in eihter `True` or `False`<jupyter_code>1 + 1 == 2
1 + 1 == 3<jupyter_output><empty_output><jupyter_text>The results of such comparisons can be assigned to variables. They are taking on the datatype `bool`<jupyter_code>yes = (True, False, 1, 4.5)
type(yes)
yes = 1 + 4 == 5
yes
wrong  = 1 * 5.33 > 5
wrong<jupyter_output><empty_output><jupyter_text>Comparison operatorsNext to the equality-operator `==` a number of additional operators are available:- `!=` checks if something is *not* true.- `` check whether the left hand operand is smaller or larger than the right hand operand  - `` extends this to "smaller than or equal" and "larger than or equal".Note that such comparisons work for the built-types such as numerical values etc. but can be adjusted for any custom type<jupyter_code>1 != 2
# "a" comes before "b"
"a" < "b" 
# ord() returns the unicode-codepoint as a number
print (ord("&"), ord("%"))
"&" > "%"<jupyter_output><empty_output><jupyter_text>Combining conditionals: logic operatorsTo combine conditions such as - "it is raining **and** the sun is shining" or- "either it rains  **or** the sun is shining"Python provides the logic operators `and` and `or`.<jupyter_code>sun_is_shining = True
it_is_raining = True
sun_is_shining and it_is_raining
sun_is_shining or it_is_raining<jupyter_output><empty_output><jupyter_text>The possible combination of operands and there results can be expressed as a table: |   a   |   b   |  a **and** b |  a **or** b  ||------|-------|-----------|----------|| True  | True  |   True    |   True   || True  | False |   False   |   True   || False | True  |   False   |   True   || False | False |   False   |  False   |  *Excersise* : Write expressions answering the following questions:- is 3 larger than 1?- is 15 not equal to 14?- is 3 smaller or equal to 2.5?- list all conditions under which "It is raining  **and** the sun is shining" **not** true?  Conditional execution of code: the   `if` operatorDecistion to control the flow of the code can be made in the form "if this then that" with the `if` oprator and a subsequent `:` to indecate "then".<jupyter_code>if 1 + 2 == 2 : print("correct")
if 1 - 1 != 0 : print ("correct")
yes = 1 + 2 == 3
yes<jupyter_output><empty_output><jupyter_text>The instructions that should be exectuted, wenn `if` conditions hold, are most often structured as  single **code block**. **Code Blocks** are indented by _either_ spaces or tabs (but not mixed). They help to reduce the clutter that other Programming languages bring to the table that often use {} braces.The Jupyter notebooks thake care of the indentation if we end a line with the "then" operator `:`<jupyter_code>yes = True
if yes :
    print ("this is only printed if the condition is true, otherwise this block is ignored")
    print (yes)

print ("we left the indented code block and continue our work")<jupyter_output><empty_output><jupyter_text>if-then-elseDifferent outcomes of conditionals can be processed with `else`. Only one of the codeblocks is executed.<jupyter_code>yes = True
if yes:
    print ("yes, that's right")
else:
    print ("no")<jupyter_output><empty_output><jupyter_text>Wir können die Variable jetzt auf einen neuen Wert setzen und Zelle oben neuausführen, wenn die Variable `wahr` den Wert `False` angemommen hat.<jupyter_code>wahr = False<jupyter_output><empty_output><jupyter_text>Wenn-Dann-Ansonsten-Wenn-Andernfalls<jupyter_code>number = 52
if number <= 10:
    print ("number is smaller than 10")
elif number > 10 and number < 20:
    print ("number between  10 and 20")
elif number > 20 and number < 40:
    print ("The number is between twenty and forty")
else:
    print ("The number is neither smaller than 10, nor between 10 and 20")<jupyter_output><empty_output><jupyter_text>LoopsLoops are applied if a script has to repeat operations. A simple example is the processing of a list of values.In loops we are checking for whether a  halting condition is fullfilled, or a code block should be exectuted again.   The `for`-loopA `for` loop allows us to go through all elements in a sequence such as a list, a tuple or a string. Each time a new iteration of the loop is triggerd a temporary variable is created that takes on the value of the item from the sequence.In the block of the function - the indented part - we can define statements **once** that are then executed for every item in the sequence without repeating the same operation.This is an important part of the  [**"Don't Repeat Yourself" (DRY)**](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) principle incomputer programming.<jupyter_code>#instead of this
a_list = [1, 5, 70, 100]
print (f"Die {a_list [0]}")
print (a_list [1])
print (a_list [2])
print (a_list [3])

print ("-------------")
#do this
for number in a_list:
    print (f"The current value of the varible 'number' is: {number}")
    print (number + 99)<jupyter_output><empty_output><jupyter_text>Createing sequences with `range`Sequences of numbers can quickly be created with the `range` function. As a _convention_ (but not a rule) simple counting variables in programming are often named "i" or "j" and "k".The range function also allows us to create incrementor steps.<jupyter_code>list(range(5,20,2))
sum = 0
for i in range (5,20,2):
    sum = sum + i
    print(f"i currenly is {i},\t The sum is now {sum}")<jupyter_output><empty_output><jupyter_text>Iterating / looping over dictionaries<jupyter_code>layers = {"sand-lime-brick-inner": 10.0, "Mineral wool": 28.0, "Air 1 cm": 1.0, "sand-lime-brick-outer": 12.0 }
layers.keys()
for layer in layers.keys():
    print (layer)
overal_width = 0.0
for width in layers.values():
    print (width)
    overal_width = overal_width + width

    
print (f"The overal width is {overal_width} cm.")<jupyter_output><empty_output><jupyter_text>Walk through all keys an values of a dictionary with `.items()`<jupyter_code>for layer, thickness in layers.items():
    print (f"{layer} \t: {thickness}" )<jupyter_output><empty_output><jupyter_text>Excercise: Calculate the  U-value of the wallThermal resistance Rsi = 0.13Thermal resistance Rse = 0.04Rconstruciton = $\frac{d_{1}}{\lambda_{R1}} + \frac{d_{2}}{\lambda_{R2}} + \ldots + \frac{d_{n}}{\lambda_{Rn}} $U-value = $ \frac{1}{R_{si} + R_{construction} + R_{se}}$ | Material | thermal transmittance $\lambda\$ in W/(m*K) ||------|-------|| lime brick | 0.99 || mineral wool | 0.04 || air layer 1 cm | 0.15 |<jupyter_code>lambdas = {"KS": 0.99, "Mineralwolle": 0.04, "Luftschicht 1 cm" : 0.15}
r_gesamt = 0
for schicht, dicke in schichten.items():
    l = 0
    if schicht.find("KS") > -1:
        l = lambdas["KS"]
    elif schicht.find("Mineralwolle") > -1:
        l = lambdas[schicht]
    elif schicht.find("Luftschicht 1 cm") > -1:
        l = 0.15
    r = dicke / l / 100
    r_gesamt = r_gesamt + r 
    print (f"{schicht} \t: {dicke:<3} \t lambda: {l}, R={r}" )
print(f"R-Gesamt : {r_gesamt}")
U = 1/(0.13+r_gesamt+0.04) 
U<jupyter_output><empty_output><jupyter_text>The `while`-loopIn a `while`-loop instructions are exectuted until a condition is fullfiled.<jupyter_code>number = 0
while number < 10:
    number = number + 2
    print (number)
    
print("The loop has terminated, the condition does not return true")<jupyter_output><empty_output><jupyter_text>Stopping a loop with `break``for`-loops can be stoped if a condition is fulfilled using the `break` key word:<jupyter_code>for i in range (1, 10):
    if i + 5 > 10:
        break
    print (f"i is {i}, we are in the loop")
print ("Loop has terminated")<jupyter_output><empty_output><jupyter_text>Stopping the current iteration with  `continue`In the following loop the modulo Operator `%` checks for a rest during a disvision by 2. If this results in `0`, it is an even number, the `if` the condition is true and the current loop iteration is stopped and `continued` for the next `i`. This has the effect, that only odd numbers are printed.<jupyter_code>for i in range (1, 10):
    if i % 2 == 0:
        continue
    print (i)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11464181450707388, 'fraction_numerical': 0.018975971255333485, 'mean_word_length': 4.333532934131736, 'pattern_counts': {'":': 6, '<': 79, '<?xml version=': 0, '>': 81, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '307597', 'license': 'MIT', 'n_tokens_mistral': 3001, 'n_tokens_neox': 2838, 'n_words': 1102, 'path': '02_control_flow_and_loops.ipynb', 'repo_name': 'jakob-beetz/ifcopenshell-notebooks', 'size': '8906'}