<jupyter_start><jupyter_text>Python basicThis notebook will give you a fast look into the Python programming language. Enjoy!  1. First stepsThis is an interactive notebook. That means you are able to execute Python code directly in this file. To execute a 'cell' (these grey blocks with code), press `Ctrl`+`Enter` after selecting a cell, or hit `Run` above. To edit a 'cell', just press `Enter`; to exit the edit mode press `Esc`.<jupyter_code>print("Hello, world!")<jupyter_output>Hello, world!<jupyter_text>This means Python tells the computer to `print` everything within quotes. This results in the quoted text being displayed just below the grey cell block, i.e. the text is 'printed' to the screen. Since this is an interactive notebook, let's get coding as soon as possible!  1.1 Python errorsBefore we write our first lines of code it is important to know how to recognise when something goes wrong. Learning to code is irrevocably intertwined with recognising, fixing and handling errors. Luckily Python does a very good job at telling you what is wrong so you will know where to start looking.   Exercise 1.1.1Try to run the code below.<jupyter_code>print("This is Python"<jupyter_output><empty_output><jupyter_text>**Notice** that the notebook shows `SyntaxError: unexpected EOF while parsing` instead of printing the provided message. The reason for this error as we call it is that the code is missing a closing bracket.   Exercise 1.1.2Add in the bracket in the copied code below to complete the statement and check if it runs.<jupyter_code>print("This is Python"<jupyter_output><empty_output><jupyter_text>Not only a closing bracket can lead to errors and there are many reasons for Python throwing an error. Luckily Python always tells you _where_ the error is happening and _why_. If you run into problems during the execution of this notebook, do not hesitate to ask for help.  1.2 About Python functionsAs you can see, Python does nothing more than providing the computer with a set of instructions on what and how to execute. In this case, `print` is a _function_ which applies some functionality to the sentence between the parentheses. Apart from `print` Python has over 60 built-in other functions, and there are more than _100,000_ python packages which add even more functionality by providing their own functions. To give an example, there is a Python package which allows you to load images, and a package that can do complex mathmatical operations. There are even packages that build complete neural networks in just a few lines of code!  1.3 Python variablesApart from functions, Python has some important basic functionality. Before we told Python to directly print a sentence to the notebook, but we can also _assign variables_. This means that you can store stuff in hypothetical boxes which Python remembers for you. See it as a very large warehouse (your computer memory) with rows and rows of shelves where you can temporarily store things. When you place the number 20 (the item) in a box (b) Python puts it on a shelf to be used for later and makes sure it will not be forgotten.Take a look at the example below.<jupyter_code>a = 10
b = 20

c = a + b

print(c)<jupyter_output><empty_output><jupyter_text>**Notice** that we now print numbers instead of sentences, which shows that Python can handle a variety of _variable types_.  Important for now are:* Rounded numbers, such as `10`, `-5`, `422`. These are called `int`, short for 'integer'. * Decimal numbers, such as `2.1`, `3.14`, `-98748.0`. These are called `float`. * Words and sentences, displayed between (single or double, both are allowed) quotes, called `string`. We have already seen `'This is Python'` and `'Hello, world!'`, but can be anything as long at it is between quotes. Confusing but important to note: numbers can also be in a `string` and will be treated as such. `'16'` (notice the quotes) will be recognised by Python as a `string` and mathematical operations will not work. Try it out below.<jupyter_code>'16' / 2<jupyter_output><empty_output><jupyter_text>1.5 First practiceBelow you will find a few exercises to practice with Python variables.  Exercise 1.5.1 Complete the code below in such a way that the tortillas are divided equally and the error is fixed. Write your code after `portion =` and replace the dots.Hint: you can use Python as a calculator, including but not limited to `+`, `-`, `/` and `*`-signs.<jupyter_code>people = 3
tortillas = 15

# fix here
portion = ..

print('Everyone can eat {} tortillas.'.format(portion))<jupyter_output><empty_output><jupyter_text>**Check** Is the code still correct if we change the number of people, or the number of tortillas? Never forget to test these cases or you might end up with a bug during production.  Exercise 1.5.2 Complete the code below in such a way that it prints the correct amount. Write your code after `price_total` and replace the dots.<jupyter_code>apple_price = 0.43
apples = 12

# fix the statement below
price_total = ..

print('You owe {} $'.format(price_total))<jupyter_output><empty_output><jupyter_text>Exercise 1.5.3 Now write a program that calculates the temperature in Celcius, given a temperature in Fahrenheit. The official formula for this conversion reads:     (temperature in F − 32) × 5/9 = temperature in °CReplace the `0` with the correct formula in such a way that it can be tested by putting in different values for `fahrenheit`.<jupyter_code>fahrenheit = 77

# fix the statement below
celcius = 0

print('The weather forecast today reads {} degrees C'.format(celcius))<jupyter_output><empty_output><jupyter_text>2. StatementsVariables allow for the storage of information, but do not incorporate any logic. For this Python has a few statements, called `if`-`else` statements. This is useful when you would like to excute a certain work order when something is the case, and another when something is not.   2.1 Boolean valuesIn order to understand how statements work, we first have to dive a bit deeper into `boolean` values. Python essentially answers questions you ask. For example, you could ask the question _is this value in a list?_  or _is 3 smaller than 5?_. Python then tells you whether this is `True` or `False`. Execute the Python code equivalent of this below.<jupyter_code>3 < 5<jupyter_output><empty_output><jupyter_text>2.2 `if`-`else`Notice how in the example above Python answers the question, but there is no consequence. In pratice, you usually want something to happen when an answer is given. For example, we could write a program that outputs if you should wear a coat based on the outside temperature. Let us assume that this is preferable below 18 degrees Celcius and that we would like to print a message accordingly. ```if(temperature < 18):    print('A coat is recommended.')else:    print('Enjoy the weather!')```Here we ask Python _is the temperature below 18 degrees?_ and tell it _if it is so, print the following message_. We also tell it _if it is not below 18 degrees_, i.e. `else`, _do this_.Let's take another example. On a company website, a signup field could check whether a birthdate is valid by providing an error message when a birthdate is not between 1900 and 2020. See below how that would look in Python code. Try out different birth years to see what it does.<jupyter_code>birthyear = 0
if(1900 < birthyear < 2020):
    print('Success')
else:
    print('Error. Incorrect birthyear.')<jupyter_output><empty_output><jupyter_text>2.3 Indentation**Notice** that both `print` statements are _indented_, meaning they have a leading extra tab compared to the code line above. The reason for this is that Python uses these tabs to infer what code goes where. For example in the code above, Python knows the 'success' `print` statement belongs to the `if` statement because it is preceded by a tab. Everything with that indentation, as long as it is not interrupted, falls under that `if` statement. The example below shows how this works in practice. Take note of how `if` and `else` operate on the same level of indentation, and therefore belong to different code blocks. Execute the code below to see what it does.<jupyter_code>number = 3
if number < 5:
    print('Hey,')
    print('The number is smaller than 5')
else: 
    print('Hi,')
print('The number is larger than or equal to 5.')<jupyter_output><empty_output><jupyter_text>Exercise 2.3.1 As you may notice the statement `The number is larger than or equal to 5.` is wrongly executed. Fix the code below (copied from above) in such a way that it prints the correct text when putting in numbers smaller than 5. Also try to test your code with numbers larger than 5; does it behave as expected?<jupyter_code>number = 3
if number < 5:
    print('Hey,')
    print('The number {} is smaller than 5'.format(number))
else: 
    print('Hi,')
print('The number {} is larger than or equal to 5.'.format(number))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06585420393559928, 'fraction_numerical': 0.014087656529516995, 'mean_word_length': 4.715654952076678, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 48, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '76810', 'license': 'MIT', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2307, 'n_words': 1332, 'path': 'python/00_python_basic.ipynb', 'repo_name': 'andreagrioni/Tutorials', 'size': '8944'}