<jupyter_start><jupyter_text>Session 1.1: BasicsIn this first section we will learn how to use the Jupyter Notebook and how to execute Python code. Furthermore, by doing so we will see how information is stored in variables. You are currently looking at a jupyter notebook. These notebooks are one of the possibilities how to write and execute python code.A jupyter notebook consists of separate code cells. A cell can be executed either by clicking on the play button in the tool bar on top or by pressing __shift + enter__.Execute the cell below. (__shift + enter__)<jupyter_code>1 + 1<jupyter_output><empty_output><jupyter_text>As you can see you can use python like a simple calculator. You can use the operators +, -, \*, / and parentheses () just like you would expect for a normal calculator. The python symbol to calculate powers is \*\*. Use the next cell to calculate $\sqrt{5-3}$ with python.<jupyter_code>#@solution
(5-3)**(1/2)<jupyter_output><empty_output><jupyter_text>If you make a mistake you can always edit the content of a cell and execute it again to update it. You can also use the top bar to insert additional cells for example. (__Insert -> Insert Cell Below__)  VariablesFor more complex calculations it is convenient to store numbers as variables. In principle, any combination of letters, numbers, and underscores which is not starting with a number can be used as a variable name. For readability purposes, it is recommended to use lowercase words separated by underscores as variable names. The = sign is used to assign a value to a variable.Our first variables.<jupyter_code>#@solution
my_first_variable = 10
my_second_variable = 5.34

my_first_variable + my_second_variable<jupyter_output><empty_output><jupyter_text>Let's say you want to calculate the gravitational force of some object with a mass of 4 kg. Assign 9.81 to a variable called acceleration! Assign 4 to a variable called mass!  Multiply both variables to calculate the gravitational force!<jupyter_code>#@solution
acceleration = 9.81
mass = 4
acceleration * mass<jupyter_output><empty_output><jupyter_text>Note, that the = is similar to the = in math, but it is not quite the same. Python always evaluates code on the right of the equal sign and assigns it to the variable on the left. So we can do:<jupyter_code>x = 5 + 2<jupyter_output><empty_output><jupyter_text>But we can't do:<jupyter_code>5 + 2 = x<jupyter_output><empty_output><jupyter_text>Variables can also be used on the left side. Note, that the left side in evaluated first and then assigned to the variable.<jupyter_code>x = 5
x = x + 1

x<jupyter_output><empty_output><jupyter_text>Here, we took the value of x, added 1 to it and then assigned the result back to the same name x. As a shortcut for such an operation we can use the += operator:<jupyter_code>x = 5
x += 1

x<jupyter_output><empty_output><jupyter_text>The same also works for +=, -=, *= and /=.  Assign 3 to a variable called x!  Multiply x with 2 and assign the result to y!  Perform any calculation with x and y!<jupyter_code>#@solution
x = 3
y = x * 2
y**x<jupyter_output><empty_output><jupyter_text>Variables can also hold data types other than numbers. For example they can store text. In programming a text piece is called a string. To tell python that something is a string and for example not a variable name, it has to be enclosed in quotes. String can also be used for "calculations".<jupyter_code>#@solution
my_first_string = 'Kartoffe'
my_second_string = 'lauf'

my_first_string + my_second_string * 2<jupyter_output><empty_output><jupyter_text>KeywordsKeywords are special words which are reserved and have a specific meaning. Python has a set of keywords that cannot be used as variables in programs. All keywords in Python are case sensitive.<jupyter_code># example of a keyword
import
import keyword
keyword.kwlist<jupyter_output><empty_output><jupyter_text>Comments Sometimes you want to add additional information to a human reader of your code but you don't want python to evaluate it. We can use the `` sign to tell python that everything behind this sign in the same line is a comment and should not be evaluated. For example:<jupyter_code>5 + 7 # here we add 5 and 7 
# 3 * 5 this calulation is not evaluated because it is inside the comment.<jupyter_output><empty_output><jupyter_text>Basic data types In general, a data type defines the format, sets the upper & lower bounds of the data so that a program could use it appropriately. However, Python data types are just more than that. In Python, we don’t need to declare a variable with explicitly mentioning the data type. This feature is famously known as dynamic typing.Everything including variables, functions, modules in Python is an object (we will come back to that in a day 4). * Numbers* Strings* Bytes----* Booleans* Lists* Tuples* Sets* Dictionaries  Numbers_Numbers_ are one of the most prominent Python data types. Unlike many languages which have only __integers__ and __floats__, Python introduces __complex__ as a new type of numbers. To make a number imaginary or complex add a `j` or `J` after its value.<jupyter_code>#@solution
# integer number
num_int = 2
# float number
num_float = 3.0
# complex number
num_c = 3+5j
#@solution
num_c2 = complex(1,-9)
num_c2<jupyter_output><empty_output><jupyter_text>_Integers_ in Python don’t have any size limitation as long as the required memory is available.A _float_ type number can have precision up to 15 decimal places.  StringsA sequence of one or more characters enclosed within either single quotes `'` or double quotes `"` is considered as `string` in Python. Any letter, a number or a symbol could be a part of the sting. Python also supports multi-line strings which require a triple quotation mark (`"""`) at the start and one at the end.<jupyter_code>#@solution
str1 = 'A string wrapped in single quotes'
str1
#@solution
str2 = "A string enclosed within double quotes"
str2
#@solution
str3 = """A multiline string
starts and ends with
a triple quotation mark."""
str3<jupyter_output><empty_output><jupyter_text>Also, the strings in Python are immutable. It means the memory will be allocated once and re-used thereafter. The strings in Python 2 are by default non-Unicode (ASCII) but also have support for Unicode. On the other hand, Python 3 strings are all Unicode (UTF-8).  BytesThe `byte` is an immutable type in Python. It can store a sequence of bytes (each 8-bits) ranging from 0 to 255. The `bytes` are machine-readable objects whereas the `strings` are just in human-readable form. Since the byte is machine-readable, so they can be directly stored to the disk. Whereas, the strings first need to encoded before getting on to the disk.<jupyter_code># Make an empty bytes object (8-bit bytes)
empty_object = bytes(16)
empty_object<jupyter_output><empty_output><jupyter_text>Operations with data typesOne has to be careful with applying operations to different data types, since the outcome might be somehow unexpected.<jupyter_code>2 * 2 # simple multiplication
"2" * 2  # string is taken twice
"2" * "2" # this will not work
#@solution
# how to get result of 2nd example with two strings
a = "2"
b = "2"
a * int(b) # convert string to integer first<jupyter_output><empty_output><jupyter_text>Type conversion works also for the other data types.<jupyter_code>int("2")
str(2)
float("2")<jupyter_output><empty_output><jupyter_text>Built-in Functions A convenient way to get things done in python is to use functions. Functions are indicated by round brackets which are appended directly after the name of the function. Inside the brackets the input is handed over to the function.<jupyter_code>#@solution
print("Hey out there.")<jupyter_output>Hey out there.<jupyter_text>__print()__ simply prompts the input on the screen. __help()__ can be used to get information about functions or variables.<jupyter_code>#@solution
help(print)<jupyter_output><empty_output><jupyter_text>A function can also return something, a number for example. Often, it is obvious what the function does.<jupyter_code>#@solution
len("Hey out there.")<jupyter_output><empty_output><jupyter_text>If a function retruns a value, this value can be stored in a variable for later use.<jupyter_code>#@solution
lenght = len("Hey out there.")
lenght<jupyter_output><empty_output><jupyter_text>We have already seen, that a variable can be either a number or a string. In programming, it is important to keep in mind of which data type your variables are. A handy way to check this is __type()__.<jupyter_code>#@solution
print(type(1))
print(type(1.0))
print(type("1"))<jupyter_output><type 'int'>
<type 'float'>
<type 'str'><jupyter_text>Functions can also accept more then one input variable. In the simplest case they are separated by  , .<jupyter_code>#@solution
x = 5
print("x is", x)
x += 1
print("x is", x)
print("Lenght of 'Hey' is", len("Hey"))<jupyter_output>('x is', 5)
('x is', 6)
("Lenght of 'Hey' is", 3)<jupyter_text>Use __print()__ and the variables below to display the time in a common way, e.g. 10:30.<jupyter_code>#@solution
hh = 10
mm = 30
print(hh, ":", mm)             # very simple way
print("{}:{}".format(hh, mm))  # new style of string formating
print("%i:%i" % (hh, mm))      # old style<jupyter_output>(10, ':', 30)
10:30
10:30<jupyter_text>Data objects have their own functions Strings for example can be also seen as __Objects__. This means that they can have attributes but also own functions which can do some work for you.<jupyter_code>my_string = "Some text."
#@solution
print(my_string.upper())
print(my_string.lower())
print(my_string.title())
print(my_string.replace("text", "information"))
print(my_string.split())
#@solution
# if function returns the same type of object:
my_string.replace("text", "information").upper()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07770545158665582, 'fraction_numerical': 0.011594792514239219, 'mean_word_length': 5.263057324840764, 'pattern_counts': {'":': 1, '<': 100, '<?xml version=': 0, '>': 101, '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': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '654997', 'license': 'Apache-2.0', 'n_tokens_mistral': 2977, 'n_tokens_neox': 2819, 'n_words': 1326, 'path': '01_Basics_solutions.ipynb', 'repo_name': 'KonstanzPythonSchool/day_01', 'size': '9832'}