<jupyter_start><jupyter_text>Python Introduction"Python is a widely used high-level, general-purpose, interpreted, dynamic programming language.Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than possible in languages such as C++ or Java.The language provides constructs intended to enable writing clear programs on both a small and large scale.Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.Python interpreters are available for many operating systems, allowing Python code to run on a wide variety of systems. CPython, the reference implementation of Python, is free and open-source software and has a community-based development model, as do nearly all of its variant implementations. CPython is managed by the non-profit Python Software Foundation."[Wikipedia](https://en.wikipedia.org/wiki/Python_(programming_language))To start out in python you can use a python distribution like [Anaconda](https://www.continuum.io/downloads) or just create your own notebooks on this server.   Usage of this Notebook- Comments in Python can be either single-line or multi-line comments.A multi-line comment is invoked with ''' and also ended with '''. For a single-line comment the hash-sign  is used. I will use line comments with double  to give instructions and  single  to prepare code snippets for you. - The different snippets of code and markdown text are stored in "cells". You can choose to run all cells or just a selected cell in the "cell" menu. To run just the selected cell you can also hold "Shift" and hit "Enter". Make sure that all cells that define your variables were executed at least once.- Intermediate results are not lost, so you can change code and re-execute it without running everything again.Example:<jupyter_code>## Please complete the following line to make a calculation with a result of 5.
## Then remove the leading '#' and hit "shift"+"enter" to calculate the value of this cell.

# 2 + 6



## Since Python 3 an integer division returns a float and the rest is no longer lost
14/3<jupyter_output><empty_output><jupyter_text>VariablesThe equals sign (=) allows you to define variables. Variable type will be inferred from the assigned value.Variable names start with an alphabetical character or an underscore ("\_") and can contain alphanumeric characters as well as additional underscores. There still are some words that are reserved by Python:+ and, as, assert, break, class, continue, def, del, elif, else, except, + exec, finally, for, from, global, if, import, in, is, lambda, not, or,+ pass, print, raise, return, try, while, with, yieldTrying to define a variable with a keyword will result in a syntax error.<jupyter_code>## Please define a variable my_string and assign a string to it
#


## A variable can be send to console via the print() function
## Please print the variable my_string
#


## The Type of a variable can be found via the type() function
## You can chain functions, so the output of one function will be the input to another function
## function2(function1(input))
## Please print the type of my_string
#


## Please define a float variable my_float and print the variable and its type.
#


## Strings can be concatenated with a + sign or by seperating the strings with a comma
my_string = 'Python ' + 'concatenates ' + 'strings' 
print(my_string)

## String variables can also be joined like this.
print('A', 'comma', 'works', 'too')

## Please define a new variable another_string. 
## Concatenate my_string and another_string and print both


## Strings can also be concatenated with variables that have a different type
## The variable my_float will be converted automatically 

## remove # when my_float is defined (see 2 steps above)
#print(my_string, my_float)


## Concatenating different types with the + does not work
#print("hello" + 3.14)<jupyter_output><empty_output><jupyter_text>Tuples and listsMost of the times programs work not with a single value but with multiple values that should be stored in a collection. Tuples and lists are collections that allow access via an index (0-based). Both can store data of different types. The difference is that a tuple is immutable while a list can be altered after creation.<jupyter_code>test_tuple = (1, 'hi', 2.3)
print(test_tuple)
test_tuple = (1, 'hi', 2.3)
## the index is zero-based and the access works as: tuple[index]
## please print the second element

#print(yourStatement)

print('Expected:', 'hi')
## a negative (-)index enables access to elements of a list or tuple in reverse order
## Please print the last element of the tuple

#print(yourStatement)

print('Expected:', 2.3)
test_list = [2, 2, 'hey', 'ho']

## in both cases it is possible to use the colon (:) to give an index range
## such as test_list[start:end]. Note that the latter index is excluded!
## Please print the second and third element

#print(yourStatement)

print('Expected:', [2, 'hey'])

## If you dont specify start or end it will start at the very first element / stop at the end of list/tuple
## Please print from the third element to the last element

# print(yourStatement)

print('Expected:', ['hey', 'ho'])
test_list = [2, 2, 'hey', 'ho']

## as lists are not immutable it is possible to append additional items via
## the .append() function
## Please append the string "lets go"

# 

print('Added an element:\n', test_list)
test_list = [2, 2, 'hey', 'ho', 'lets go']

## it is also possible to remove items from the list with the .remove() function
## note that just the first occurence is removed
## Please remove both occurences of '2'

#

print('Removed both 2´s:\n', test_list)
test_list = ['hey', 'ho', 'lets go']

## to remove items based on their index the .pop() function is used
## Please remove the first item
#

print('Removed first item:\n', test_list)<jupyter_output><empty_output><jupyter_text>To see all the methods that are available on an object use the dir() function.  The entries that are surrounded by double underscores (\__entry\__) are private attributes and should not be used.<jupyter_code>dir(test_list)
## to see additional information regarding those methods you can use
## the help function. 
help(test_list.insert)
## Create a list that contains all days of the week
## start as follows and then use the .append() and .insert() functions shown above
## there is one entry that does not belong, remove it! 
## print your result!
days_of_the_week = ['monday', 'thursday', 'holiday', 'saturday']


print(days_of_the_week)<jupyter_output><empty_output><jupyter_text>Sets"an unorderered collection of unique elements"<jupyter_code># define a Set
a = {1,2,3}
print(a)

# add an element to a set
a.add(4)

print(a)
a.add(4)

print(a)
# what is common in two sets
a = {1,2,3}
b = {3,4,5}
print(a.intersection(b))
# what is the difference of two sets
a = {1,2,3}
b = {3,4,5}
print(a.difference(b))
# show the distinct elements of a combined set
a = {1,2,3}
b = {3,4,5}
c = a.union(b)
print(c)<jupyter_output><empty_output><jupyter_text>for-loopsPython gives you several ways to iterate over your objects. The classic for-loop uses an auto-incrementing variable to count the progress and iterates until the length of the list is reached. The counting variable is then used as index for the list.Please note that Python uses a starting colon (:) and indentation to highlight codeblocks.<jupyter_code>## Look up the function to determine the length of the list days_of_the_week
## Store it in the variable list_length

list_length = 0  

print("List length:", list_length)

## The range is also zero-based and excludes the last element
print("Range used:", range(list_length))

for i in range(list_length):
    ## Please print for each iteration the corresponding day from days_of_the_week
    ## remove "pass", it is a placeholder so unfinished code does not throw an error
    pass<jupyter_output>List length: 0
Range used: range(0, 0)<jupyter_text>But there is also another construct that works as a foreach-loop. The different elements will be extracted from the list and can be used directly.<jupyter_code>for day in days_of_the_week:
    print(day)<jupyter_output><empty_output><jupyter_text>DictionarysDictionarys are objects that store key:value pairs. They are also called "mappings" or "associative arrays". If you want to access a certain value you can use the key to retrieve a single element instead of iterating over the whole object.<jupyter_code>ages = {"Rick": 35, "Bob": 42, 'Linda': 33}

print('Ricks age is', ages['Rick'])
## by standard a dictionary iterates over all the keys
for person in ages:
    print('The age of', person, 'is', ages[person])
## but using the .items() function of the dictionary it is also possible
## to get keys and values directly
for person, age in ages.items():
    print('The age of', person, 'is', age)
## create a new dictionary that contains of your 5 favorite fruits and their colors

# fruits = 

## print the color of your favorite<jupyter_output><empty_output><jupyter_text>FunctionsTo determine the list length we used the len() function before.Now we want to implement this in a simple function.  The function definition has the following schema:  def function_name(parameter1, optional_parameter2 = default_value):Followed by your code and the return of the result. It is possible to use optional parameters that are substituted by a default value if no value is given.  All optional parameters have to be at the end of your parameter list.<jupyter_code>def count_characters(word):
    count = 0
    
    # please count the number of characters in a word
    
    return count

## the new function is called by the print function
print(count_characters('test'))
days_of_the_week = ['monday',
                    'tuesday',
                    'wednesday',
                    'thursday',
                    'friday',
                    'saturday',
                    'sunday']


## create your own function that prints all days of the week list
def print_list(my_list):
    ## pass is a python placeholder if you want to define a function without writing actual code yet
    ## just delete or comment it out!
    pass
    ## iterate over your list
    
        ## print all the days, here we dont use a return value
        
print_list(days_of_the_week)<jupyter_output><empty_output><jupyter_text>if-constructsThe function takes a day of the week as input and prints if you have to work on this day.  Note that an optional parameter "work_saturday" is used that indicates if you have to work saturdays or not- "if" indicates the check for a given condition- "elif" is an else-if in python, the first condition was not met so another condition is tested- "else" defines what happens when none of the conditions are fulfilled- compare two values with value1 == value2, if both are equal it returns True otherwise False- chain several conditions with "and": if value1 == value2 and value3 == 'something'<jupyter_code>def determine_workday(day, work_saturday = True):

    day = str.lower(day)
    ## Please check if the given day is a sunday, nobody should have to work on a sunday!
    ## Print the result
    #
    
    ## if the first condition was not met we need to test another condition
    ## test if the day is a saturday and also if the person has to work on saturdays or not
    ## Print the result
    #
    
    ## all other days are workdays, no need to check more conditions. 
    ## Please write the statement that handles all other cases
    ## Print the result as well as the input workday
    #
    
determine_workday('Wednesday')

## make another call to the function where the day is Saturday and set the optional parameter to False 
#<jupyter_output><empty_output><jupyter_text>Reading filesReading the content of a file is a common task. There are several native python ways to do this:- return a string with all the characters- return the next line- return a certain line- iterate over all lines  Complete read<jupyter_code>## the second parameter is the read-mode
## r = read, w = write, a = append, r+ = read and write
file = open('testfile.txt', 'r') 

print(file.read())<jupyter_output>Hello World 
This is our new text file 
and this is another line. 
Why? Because we can.<jupyter_text>Print the next line<jupyter_code>file = open('testfile.txt', 'r')

print(file.readline())
print(file.readline())<jupyter_output>Hello World 

This is our new text file<jupyter_text>Print a certain line<jupyter_code>file = open('testfile.txt', 'r') 

lines=file.readlines()
print(lines[0])
print(lines[3])<jupyter_output>Hello World 

Why? Because we can.<jupyter_text>Read line by line<jupyter_code>file = open('testfile.txt', 'r') 

for line in file: 
    print(line)<jupyter_output>Hello World 

This is our new text file 

and this is another line. 

Why? Because we can.<jupyter_text>Writing filesThe same open() function is also used to write a textfile. We will read and write in the following section. - use mode 'a' to append to a file, 'w' to overwrite existing content.- you need to .close() a file so buffer gets written to disk.- if a file you try to open does not exist it will be created.<jupyter_code>## Please change the filename to your name, so your file does not get overwritten by someone else
f = open("testfile2.txt", "a")
f.write("Now the file has more content!\n",)
f.close()<jupyter_output><empty_output><jupyter_text>Exercise- Please read the file "testfile.txt".- Count the number of characters per line- Write the line and the number of characters for this line in a new file, put your name in the filename so it does not get overwritten<jupyter_code>file = open('testfile.txt', 'r')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07888880913071566, 'fraction_numerical': 0.005455459048165961, 'mean_word_length': 4.4358174014826375, 'pattern_counts': {'":': 3, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '459730', 'license': 'Apache-2.0', 'n_tokens_mistral': 3916, 'n_tokens_neox': 3786, 'n_words': 1897, 'path': 'Big_Data_Platform/Docker/HMI/Jupyter/src/notebooks/Python Introduction.ipynb', 'repo_name': 'PriiOmega/KOARCH', 'size': '13931'}