<jupyter_start><jupyter_text>Repetition and Conditional Commands Some structures can be very useful when we automate tasks to solve computational problems, such as repetition and conditional blocks.  Repetition: `for`Sometimes, we might wish to repeat the same operation a number of times. For example, in our activities we may wish to print several values in sequence, to watch the changes in some function or variable. For this, we need 2 ingredients:- An action to be repeated;- The number of times we wish the action to be repeated.The action to be repeated often depends on some parameter that varies as the repetitions happen. For example, suppose we want to print all the elements in a list.<jupyter_code># Define a list here:
list = [1,3,6,9,12,15,18,21,24,27,30]
for item in list: 
    # Indentation
    print("Number "+str(item))
print("Finished.")<jupyter_output>Number 1
Number 3
Number 6
Number 9
Number 12
Number 15
Number 18
Number 21
Number 24
Number 27
Number 30
Finished.<jupyter_text>We can observe several things in the cell above.- The syntax `for item in list` can be naturally read: all commands below this line will be executed once for every `item` in the `list`.- The repetition structure defined by the keyword `for` is a **block** structure, which means that Python expects all actions to be repeated to be **indented**, that is, that they start with an offset to the right with respect to the `for` line. This is mandatory, and the indentation must be consistent (that is, be of the same size) whenever a block appears. The most usual indentation is one press of the Tab key.- The `item` variable is updated automatically; this means that every time all the lines inside the block are executed, its value increases by 1, until the list is consumed.- The last line of the previous cell, containing the command `print("Finished")` gets executed only once, exactly because it is not indented; thus, Python understands this line is outside the `for` block and is not a part of the repeated commands. Watch:<jupyter_code>for item in list: 
    # Indentation
    print("Number "+str(item))
    print("again!")<jupyter_output>Number 1
again!
Number 3
again!
Number 6
again!
Number 9
again!
Number 12
again!
Number 15
again!
Number 18
again!
Number 21
again!
Number 24
again!
Number 27
again!
Number 30
again!<jupyter_text>To execute a repetition block (also called a **loop**) without a previously defined list, we can use a few different commands. For example, `range(n)` generates a list from 0 to `n-1`:<jupyter_code># Choose n
n = 10
for item in range(n):
    print(item)<jupyter_output>0
1
2
3
4
5
6
7
8
9<jupyter_text>Note that this behavior is similar to what we observed when we talked about slicing lists and strings: since all indices start at 0, `range(n)` gives us `n` elements (from 0 to `n-1`).  Conditionals: `if`Now, if we wish to execute some action only if a certain logical condition is satisfied, we use the `if` structure: "if some condition is true, execute the first block of commands; else, execute the second.".<jupyter_code># Change the variable word and see what happens.
word = "yes"
if word == "yes!":
    print("This is true.")
else:
    print("This is false.")<jupyter_output>This is false.<jupyter_text>Note also that we need indentation here too. We used `==` in the above expression because on Python there is a difference between an assignment (assigning a value to a variable) and a comparison (test whether two variables are equal, for example). The `if` does the latter.Watch:<jupyter_code>a = 1
a == 1
a == 2<jupyter_output><empty_output><jupyter_text>The symbol for "not equal" is `!=`:<jupyter_code>a != -1<jupyter_output><empty_output><jupyter_text>Other logical conditions (for which there is a `True` or `False` response) can also be included.<jupyter_code>2 > 3
5 <= 5
3 in [1,2,3]
3 not in [1,2,3]
'a' in 'Day'<jupyter_output><empty_output><jupyter_text>We can also test more than one logical condition at a time. We can use the following keywords:- `or`: `A or B` will have a value of `True` whenever either `A` or `B` are true (or both)- `and`: `A and B` will have a value of `True` whenever both `A` and `B` are true *at the same time*.<jupyter_code>print_stuff = "yes"  # change the values for print_stuff and 
paper = "yes"        # paper and watch what happens
if print_stuff == "yes" and paper == "yes":
    print("Let's print!")
else:
    print("Won't print.")
print_stuff = "yes"  # change the values for print_stuff an
paper = "yes"        # paper and watch what happens
if print_stuff == "yes" or paper == "yes":
    print("Let's print!")
else:
    print("Won't print.")<jupyter_output>Let's print!
{'fraction_non_alphanumeric': 0.0868077827667308, 'fraction_numerical': 0.019029292281376953, 'mean_word_length': 4.62936221419976, 'pattern_counts': {'":': 3, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '172875', 'license': 'MIT', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1359, 'n_words': 695, 'path': 'content/en/Notebook 04 -  Control Flow Statements.ipynb', 'repo_name': 'mauriciolanda4/callicore', 'size': '4677'}