<jupyter_start><jupyter_text>Control and Flow  Turing completeness Now that we understand how we can use objects to store and model our data, we only need to be able to control the flow of ourprogram in order to have a program that can, in principle, do anything!Specifically we need to be able to:* Control whether a program statement should be executed or not, based on a variable. "Conditionality"* Jump back to an earlier point in the program, and run some statements again. "Branching" Once we have these, we can write computer programs to process information in arbitrary ways: we are *Turing Complete*!  Conditionality Conditionality is achieved through Python's `if` statement:<jupyter_code>x = 5

if x < 0:
    print(x, " is negative")<jupyter_output><empty_output><jupyter_text>The absence of output here means the if clause prevented the print statement from running.<jupyter_code>x=-10

if x < 0:
    print(x, " is negative")<jupyter_output>-10  is negative<jupyter_text>The first time through, the print statement never happened. The **controlled** statements are indented. Once we remove the indent, the statements will once again happen regardless.   Else and Elif Python's if statement has optional elif (else-if) and else clauses:<jupyter_code>x = 5
if x < 0:
    print("x is negative")
else:
    print("x is positive")
x = 5
if x < 0:
    print("x is negative")
elif x == 0:
    print("x is zero")
else:
    print("x is positive")<jupyter_output>x is positive<jupyter_text>Try editing the value of x here, and note that other sections are found.<jupyter_code>choice =  'high'

if choice == 'high':
    print(1)
elif choice == 'medium':
    print(2)
else:
    print(3)<jupyter_output>1<jupyter_text>Comparison `True` and `False` are used to represent **boolean** (true or false) values.<jupyter_code>1 > 2<jupyter_output><empty_output><jupyter_text>Comparison on strings is alphabetical.<jupyter_code>"UCL" > "KCL"<jupyter_output><empty_output><jupyter_text>But case sensitive:<jupyter_code>"UCL" > "kcl"<jupyter_output><empty_output><jupyter_text>There's no automatic conversion of the **string** True to true:<jupyter_code>True == "True"<jupyter_output><empty_output><jupyter_text>In python two there were subtle implied order comparisons between types, but it was  bad style to rely on these.In python three, you cannot compare these.<jupyter_code>'1' < 2
'5' < 2
'1' > 2<jupyter_output><empty_output><jupyter_text>Any statement that evaluates to `True` or `False` can be used to control an `if` Statement.  Automatic Falsehood Various other things automatically count as true or false, which can make life easier when coding:<jupyter_code>mytext = "Hello"
if mytext:
    print("Mytext is not empty")
    
mytext2 = ""
if mytext2:
    print("Mytext2 is not empty")<jupyter_output><empty_output><jupyter_text>We can use logical not and logical and to combine true and false:<jupyter_code>x=3.2
if not (x>0 and type(x)==int):
    print(x,"is not a positive integer")<jupyter_output>3.2 is not a positive integer<jupyter_text>`not` also understands magic conversion from false-like things to True or False.<jupyter_code>not not "Who's there!" # Thanks to Mysterious Student
bool("")
bool("James")
bool([])
bool(['a'])
bool({})
bool({'name': 'James'})
bool(0)
bool(1)<jupyter_output><empty_output><jupyter_text>But subtly, although these quantities evaluate True or False in an if statement, they're not themselves actually True or False under ==:<jupyter_code>[] == False
bool([]) == False<jupyter_output><empty_output><jupyter_text>Indentation In Python, indentation is semantically significant.You can choose how much indentation to use, so long as youare consistent, but four spaces isconventional. Please do not use tabs.In the notebook, and most good editors, when you press ``, you get four spaces.     No indentation when it is expected, results in an error:<jupyter_code>x=2
if x>0:
print(x)<jupyter_output><empty_output><jupyter_text>but:<jupyter_code>if x>0:
    print(x)<jupyter_output>2<jupyter_text>Pass A statement expecting identation must have some indented code.This can be annoying when commenting things out. (With ``)<jupyter_code>if x>0:
    # print x
    
print("Hello")<jupyter_output><empty_output><jupyter_text>So the `pass` statement is used to do nothing.<jupyter_code>if x>0:
    # print x
    pass

print("Hello")<jupyter_output>Hello
{'fraction_non_alphanumeric': 0.09863013698630137, 'fraction_numerical': 0.0091324200913242, 'mean_word_length': 5.698776758409786, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 72, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '17495', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1379, 'n_words': 506, 'path': 'ch00python/032conditionality.ipynb', 'repo_name': 'alvinam/eng', 'size': '4380'}