<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Contents and Objective
Describing several commands and methods that will be used throughout the simulations
<b>Note
Step1:  Tuples
Similar to lists but "immutable", i.e., entries can be appended, but not be changed
Defined by tuple( ) or by brackets with entities being separated by comma
Referenced by index in square brackets; <b>Note</b>
Step2:  Dictionaries
Container in which entries are of type
Step3:  Sets
As characterized by the naming, sets are representing mathematical sets; no double occurences of elements
Defined by keyword set of by curly braces with entities being separated by comma
<b>Note</b>
Step4:  Flow Control
Standards commands as for, while, ...
Functions for specific purposes
<b>Note
Step5:  While Loops
while loops in Python are (as usual) constructed by checking condition and exiting loop if condition becomes False
<b>Note
Step6:  Functions
Defined by key-word def followed by list of arguments in brackets
Doc string defined directly after def by ''' TEXT '''
Values returned by key word return; <b>Note
<ASSISTANT_TASK:>
Python Code:
# defining lists
sport_list = [ 'cycling', 'football', 'fitness' ]
first_prime_numbers = [ 2, 3, 5, 7, 11, 13, 17, 19 ]
# getting contents
sport = sport_list[ 2 ]
third_prime = first_prime_numbers[ 2 ]
# printing
print( 'All sports:', sport_list )
print( 'Sport to be done:', sport )
print( '\nFirst primes:', first_prime_numbers )
print( 'Third prime number:', third_prime )
# adapt entries and append new entries
sport_list[ 1 ] = 'swimming'
sport_list.append( 'running' )
first_prime_numbers.append( 23 ) 
# printing
print( 'All sports:', sport_list )
print( 'First primes:', first_prime_numbers )
Explanation: Contents and Objective
Describing several commands and methods that will be used throughout the simulations
<b>Note:</b> Basic knowledge of programming languages and concepts is assumed. Only specific concepts that are different from, e.g., C++ or Matlab, are provided.
<b>NOTE 2:</b> The following summary is by no means complete or exhaustive, but only provides a short and simplified overview of the commands used throughout the simulations in the lecture. For a detailed introduction please have a look at one of the numerous web-tutorials or books on Python, e.g., 
https://www.python-kurs.eu/
https://link.springer.com/book/10.1007%2F978-1-4842-4246-9
https://primo.bibliothek.kit.edu/primo_library/libweb/action/search.do?mode=Basic&vid=KIT&vl%28freeText0%29=python&vl%28freeText0%29=python&fn=search&tab=kit&srt=date
Cell Types
There are two types of cells:
Text cells (called 'Markdown'): containing text, allowing use of LaTeX
Math/code cells: where code is being executed
As long as you are just reading the simulations, there is no need to be concerned about this fact.
Data Structures
In the following sections the basic data structures used in upcoming simulations will be introduced.
Basic types as int, float, string are supposed to be well-known.
Lists
Container-type structure for collecting entities (which may even be of different type)
Defined by key word list( ) or by square brackets with entities being separated by comma
Referenced by index in square brackets; <b>Note</b>: indexing starting at 0
Entries may be changed, appended, sliced,...
End of explanation
# defining tuple
sport_tuple = ( 'cycling', 'football', 'fitness' )
# getting contents
sport = sport_tuple[ 2 ]
# printing
print( 'All sports:', sport_tuple )
print( 'Sport to be done:', sport )
# append new entries
sport_tuple += ( 'running', )
# printing
print( 'All sports:', sport_tuple )
print()
# changing entries will fail
# --> ERROR is being generated on purpose
# --> NOTE: Error is handled by 'try: ... except: ...' statement
try:
    sport_tuple[ 1 ] = 'swimming'
except:
    print('ERROR: Entries within tuples cannot be adapted!')
Explanation: Tuples
Similar to lists but "immutable", i.e., entries can be appended, but not be changed
Defined by tuple( ) or by brackets with entities being separated by comma
Referenced by index in square brackets; <b>Note</b>: indexing starting at 0
End of explanation
# defining dictionaries
sports_days = { 'Monday': 'pause', 'Tuesday': 'fitness', 'Wednesday' : 'running', 
            'Thursday' : 'fitness', 'Friday' : 'swimming', 'Saturday' : 'cycling', 
            'Sunday' : 'cycling' }
print( 'Sport by day:', sports_days )
print( '\nOn Tuesday:', sports_days[ 'Tuesday' ])
# Changes are made by using the key as identifier
sports_days[ 'Tuesday' ] = 'running'
print( 'Sport by day:', sports_days )
Explanation: Dictionaries
Container in which entries are of type: ( key : value )
Defined by key word dict or by curly brackets with entities of shape "key : value" being separated by comma
Referenced by key in square brackets --> <b>Note</b>: Indexing by keys instead of indices might be a major advantage (at least sometimes)
End of explanation
# defining sets
sports_set = { 'fitness', 'running', 'swimming', 'cycling'}
print( sports_set )
print()
# indexing will fail
# --> ERROR is being generated on purpose
try:
    print( sports_set[0] )
except:
    print('ERROR: No indexing of sets!')
# adding elements (or not)
sports_set.add( 'pause' )
print(sports_set)
sports_set.add( 'fitness' )
print(sports_set)
# union of sets (also: intersection, complement, ...)
all_stuff_set = set( sports_set )
union_of_sets = all_stuff_set.union( first_prime_numbers)
print( union_of_sets )
Explanation: Sets
As characterized by the naming, sets are representing mathematical sets; no double occurences of elements
Defined by keyword set of by curly braces with entities being separated by comma
<b>Note</b>: As in maths, sets don't possess ordering, so there is no indexing of sets!
End of explanation
# looping in lists simply parsing along the list
for s in sport_list:
    print( s )
print()
# looping in dictionaries happens along keys
for s in sports_days:
    print( '{}: \t{}'.format( s, sports_days[ s ] ) )
Explanation: Flow Control
Standards commands as for, while, ...
Functions for specific purposes
<b>Note:</b> Since commands and their concept are quite self-explaining, only short description of syntax is provided
For Loops
for loops in Python allow looping along every so-called iterable as, e.g., list, tuple, dicts.... <b>Note</b>: Not necessarily int
Syntax: for i in iterable:
<b>Note:</b> Blocks are structured by indentation; sub-command (as, e.g., in a loop) are indented
End of explanation
# initialize variables
sum_primes = 0
_n = 0
# sum primes up to sum-value of 20
while sum_primes < 20:
    
    # add prime of according index
    sum_primes += first_prime_numbers[ _n ]
    
    # increase index
    _n += 1
    
print( 'Sum of first {} primes is {}.'.format( _n, sum_primes ) )
Explanation: While Loops
while loops in Python are (as usual) constructed by checking condition and exiting loop if condition becomes False
<b>Note:</b> Blocks are structured by indentation; sub-command (as, e.g., in a loop) are indented
End of explanation
def get_n_th_prime( n, first_prime_numbers ):
    '''
    DOC String
    
    IN: index of prime number, list of prime numbers
    OUT: n-th prime number
    '''
    
    # do something smart as, e.g., checking that according index really exists
    # "assert" does the job by checking first arg and--if not being TRUE--providing text given as second arg
    try: 
        val = first_prime_numbers[ n - 1 ]
    except:
        return '"ERROR: Index not feasible!"'
    
    # NOTE: since counting starts at 0, (n-1)st number is returned
    # Furthermore, there is no need for a function here; a simple reference would have done the job!
    
    return first_prime_numbers[ n - 1 ]
# show doc string
print( help( get_n_th_prime ) )
# apply functions
N = 3
print( '{}. prime number is {}.'.format( N, get_n_th_prime( N, first_prime_numbers ) ) )
print()
N = 30
print( '{}. prime number is {}.'.format( N, get_n_th_prime( N, first_prime_numbers ) ) )
Explanation: Functions
Defined by key-word def followed by list of arguments in brackets
Doc string defined directly after def by ''' TEXT '''
Values returned by key word return; <b>Note:</b> return "value" can be scalar, list, dict, vector, maxtrix,...
End of explanation
<END_TASK>