<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Python the basics
Step1:  Basic python datatypes
Numerical types
Python supports the following numerical, scalar types
Step2:  A Python shell can therefore replace your pocket calculator, with the basic arithmetic operations addition, substraction, division ... are natively implemented
+, -, *, /, % (modulo) natively implemented
operation| python implementation 
----------
Step3:  Attention !
Step4:  Containers
Lists
A list is an ordered collection of objects, that may have different types. The list container supports slicing, appending, sorting ...
Indexing starts at 0 (as in C, C++ or Java), not at 1 (as in Fortran or Matlab)!
Step5:  Slicing
Step6:  Note that L[start
Step7:  Warning, with views equal to each other, they point to the same point in memory. Changing one of them is also changing the other!!
Step8:  List methods
Step9:  <div class="alert alert-success">
    <b>EXERCISE</b>
Step10:  
Step11:  <div class="alert alert-success">
    <b>EXERCISE</b>
Step12:  Concatenating lists is just the same as summing both lists
Step13:  <div class="alert alert alert-danger">
    <b>Note</b>
Step14:  The list itself is reversed and no output is returned, so reverted is None, which can not be added to a list
Step15:  List comprehensions
List comprehensions are a very powerful functionality. It creates an in-list for-loop option, looping through all the elements of a list and doing an action on it, in a single, readable line.
Step16:  and with conditional options
Step18:  Cool, this works! let's check more about strings
Step19:  The newline character is \n, and the tab character is \t.
Step20:  Strings are collections like lists. Hence they can be indexed and sliced, using the same syntax and rules.
Step21:  Accents and special characters can also be handled in Unicode strings (see http
Step22:  A string is an immutable object and it is not possible to modify its contents. One may however create new strings from the original one.
Step23:  We won't introduce all methods on strings, but let's check the namespace and apply a few of them
Step24:  String formatting to make the output as wanted can be done as follows
Step25:  The format string print options in python 3 are able to interpret the conversions itself
Step26:  <div class="alert alert alert-success">
    <b>Exercise</b>
Step27:  <div class="alert alert alert-success">
    <b>Exercise</b>
Step28:  Dictionaries
A dictionary is basically an efficient table that maps keys to values. It is an unordered container
It can be used to conveniently store and retrieve values associated with a name
Step29:  Adding an extra element
Step30:  You can get the keys and values separately
Step31:  <div class="alert alert alert-success">
    <b>Exercise</b> Put all keys of the `hourly_wage` dictionary in a list as strings.  If they are not yet a string, convert them
Step32:  Tuples
Tuples are basically immutable lists. The elements of a tuple are written between parentheses, or just separated by commas
<ASSISTANT_TASK:>
Python Code:
# Two general packages
import os
import sys
Explanation: Python the basics: datatypes
DS Data manipulation, analysis and visualization in Python
May/June, 2021
© 2021, Joris Van den Bossche and Stijn Van Hoey  (&#106;&#111;&#114;&#105;&#115;&#118;&#97;&#110;&#100;&#101;&#110;&#98;&#111;&#115;&#115;&#99;&#104;&#101;&#64;&#103;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;, &#115;&#116;&#105;&#106;&#110;&#118;&#97;&#110;&#104;&#111;&#101;&#121;&#64;&#103;&#109;&#97;&#105;&#108;&#46;&#99;&#111;&#109;). Licensed under CC BY 4.0 Creative Commons
This notebook is largely based on material of the Python Scientific Lecture Notes (https://scipy-lectures.github.io/), adapted with some exercises.
Importing packages
Importing packages is always the first thing you do in python, since it offers the functionalities to work with.
Different options are available:
<span style="color:green">import <i>package-name</i></span>  <p> importing all functionalities as such
<span style="color:green">from <i>package-name</i> import <i>specific function</i></span>  <p> importing a specific function or subset of the package
<span style="color:green">from <i>package-name</i> import *  </span>   <p> importing all definitions and actions of the package (sometimes better than option 1)
<span style="color:green">import <i>package-name</i> as <i>short-package-name</i></span>    <p> Very good way to keep a good insight in where you use what package
import all functionalities as such
End of explanation
an_integer = 3
print(type(an_integer))
an_integer
# type casting: converting the integer to a float type
float(an_integer)
a_float = 0.2
type(a_float)
a_complex = 1.5 + 0.5j
# get the real or imaginary part of the complex number by using the functions
# real and imag on the variable
print(type(a_complex), a_complex.real, a_complex.imag)
a_boolean = (3 > 4)
a_boolean
Explanation: Basic python datatypes
Numerical types
Python supports the following numerical, scalar types:
* integer
* floats
* complex
* boolean
End of explanation
print (7 * 3.)
print (2**10)
print (8 % 3)
Explanation: A Python shell can therefore replace your pocket calculator, with the basic arithmetic operations addition, substraction, division ... are natively implemented
+, -, *, /, % (modulo) natively implemented
operation| python implementation 
----------:| --------------------- 
 addition  | +
 substraction | -
 multiplication | *
 division | /
 modulo | %
 exponentiation | **
End of explanation
print(3/2)
print(3/2.)
print(3.//2.)  #integer division
Explanation: Attention !
End of explanation
a_list = [2.,'aa', 0.2]
a_list
# accessing individual object in the list
a_list[1]
# negative indices are used to count from the back
a_list[-1]
Explanation: Containers
Lists
A list is an ordered collection of objects, that may have different types. The list container supports slicing, appending, sorting ...
Indexing starts at 0 (as in C, C++ or Java), not at 1 (as in Fortran or Matlab)!
End of explanation
another_list = ['first', 'second', 'third', 'fourth', 'fifth']
print(another_list[3:])
print(another_list[:2])
print(another_list[::2])
Explanation: Slicing: obtaining sublists of regularly-spaced elements
End of explanation
another_list[3] = 'newFourth'
print(another_list)
another_list[1:3] = ['newSecond', 'newThird']
print(another_list)
Explanation: Note that L[start:stop] contains the elements with indices i such as start<= i < stop 
(i ranging from start to stop-1). Therefore, L[start:stop] has (stop-start) elements.
Slicing syntax: L[start:stop:stride]
all slicing parameters are optional
Lists are mutable objects and can be modified
End of explanation
a = ['a',  'b']
b = a
b[0] = 1
print(a)
Explanation: Warning, with views equal to each other, they point to the same point in memory. Changing one of them is also changing the other!!
End of explanation
#dir(list)
a_third_list = ['red', 'blue', 'green', 'black', 'white']
# Appending
a_third_list.append('pink')
a_third_list
# Removes and returns the last element
a_third_list.pop()
a_third_list
# Extends the list in-place
a_third_list.extend(['pink', 'purple'])
a_third_list
# Reverse the list
a_third_list.reverse()
a_third_list
# Remove the first occurence of an element
a_third_list.remove('white')
a_third_list
# Sort list
a_third_list.sort()
a_third_list
Explanation: List methods:
You can always list the available methods in the namespace by using the dir()-command:
End of explanation
a_third_list.count?
a_third_list.index??
Explanation: <div class="alert alert-success">
    <b>EXERCISE</b>: What happens if you put two question marks behind the command?
</div>
End of explanation
a_third_list = ['red', 'blue', 'green', 'black', 'white']
# remove the last two elements
a_third_list = a_third_list[:-2]
a_third_list
Explanation: 
End of explanation
a_third_list[::-1]
Explanation: <div class="alert alert-success">
    <b>EXERCISE</b>: Mimick the functioning of the *reverse* command using the appropriate slicing command:
</div>
End of explanation
a_list = ['pink', 'orange']
a_concatenated_list = a_third_list + a_list
a_concatenated_list
Explanation: Concatenating lists is just the same as summing both lists:
End of explanation
reverted = a_third_list.reverse()
## comment out the next lines to test the error:
#a_concatenated_list = a_third_list + reverted
#a_concatenated_list
Explanation: <div class="alert alert alert-danger">
    <b>Note</b>: Why is the following not working?
</div>
End of explanation
# Repeating lists
a_repeated_list = a_concatenated_list*10
print(a_repeated_list)
Explanation: The list itself is reversed and no output is returned, so reverted is None, which can not be added to a list
End of explanation
number_list = [1, 2, 3, 4]
[i**2 for i in number_list]
Explanation: List comprehensions
List comprehensions are a very powerful functionality. It creates an in-list for-loop option, looping through all the elements of a list and doing an action on it, in a single, readable line.
End of explanation
[i**2 for i in number_list if i>1]
[i**2 for i in number_list if i>1]
# Let's try multiplying with two on a list of strings:
print([i*2 for i in a_repeated_list])
Explanation: and with conditional options:
End of explanation
s = 'Never gonna give you up'
print(s)
s = "never gonna let you down"
print(s)
s = '''Never gonna run around 
    and desert you'''         
print(s)
s = Never gonna make you cry, 
    never gonna say goodbye
print(s)
## pay attention when using apostrophes! - test out the next two lines one at a time
#print('Hi, what's up?')
#print("Hi, what's up?")
Explanation: Cool, this works! let's check more about strings:
Strings
Different string syntaxes (simple, double or triple quotes)
End of explanation
print('''Never gonna tell a lie and hurt you.
Never gonna give you up,\tnever gonna let you down
Never \ngonna\n run around and\t desert\t you''')
Explanation: The newline character is \n, and the tab character is \t.
End of explanation
a_string = "hello"
print(a_string[0])
print(a_string[1:5])
print(a_string[-4:-1:2])
Explanation: Strings are collections like lists. Hence they can be indexed and sliced, using the same syntax and rules.
End of explanation
print(u'Hello\u0020World !')
Explanation: Accents and special characters can also be handled in Unicode strings (see http://docs.python.org/tutorial/introduction.html#unicode-strings).
End of explanation
#a_string[3] = 'q'   # uncomment this cell
Explanation: A string is an immutable object and it is not possible to modify its contents. One may however create new strings from the original one.
End of explanation
#dir(str) # uncomment this cell
another_string = "Strawberry-raspBerry pAstry package party"
another_string.lower().replace('r', 'l', 7)
Explanation: We won't introduce all methods on strings, but let's check the namespace and apply a few of them:
End of explanation
print('An integer: %i; a float: %f; another string: %s' % (1, 0.1, 'string'))
Explanation: String formatting to make the output as wanted can be done as follows:
End of explanation
print('An integer: {}; a float: {}; another string: {}'.format(1, 0.1, 'string'))
n_dataset_number = 20
sFilename = 'processing_of_dataset_%d.txt' % n_dataset_number
print(sFilename)
Explanation: The format string print options in python 3 are able to interpret the conversions itself:
End of explanation
[el for el in dir(list) if not el[0]=='_']
Explanation: <div class="alert alert alert-success">
    <b>Exercise</b>: With the `dir(list)` command, all the methods of the list type are printed. However, we're not interested in the hidden methods. Use a list comprehension to only print the non-hidden methods (methods with no starting or trailing '_'):
</div>
End of explanation
sentence = "the quick brown fox jumps over the lazy dog"
#split in words and get word lengths
[len(word) for word in sentence.split()]
Explanation: <div class="alert alert alert-success">
    <b>Exercise</b>: Given the previous sentence `the quick brown fox jumps over the lazy dog`, split the sentence and put all the word-lengths in a list. 
</div>
End of explanation
# Always key : value combinations, datatypes can be mixed
hourly_wage = {'Jos':10, 'Frida': 9, 'Gaspard': '13', 23 : 3}
hourly_wage
hourly_wage['Jos']
Explanation: Dictionaries
A dictionary is basically an efficient table that maps keys to values. It is an unordered container
It can be used to conveniently store and retrieve values associated with a name
End of explanation
hourly_wage['Antoinette'] = 15
hourly_wage
Explanation: Adding an extra element:
End of explanation
hourly_wage.keys()
hourly_wage.values()
hourly_wage.items() # all combinations in a list
# ignore this loop for now, this will be explained later
for key, value in hourly_wage.items():
    print(key,' earns ', value, '€/hour')
Explanation: You can get the keys and values separately:
End of explanation
hourly_wage = {'Jos':10, 'Frida': 9, 'Gaspard': '13', 23 : 3}
str_key = []
for key in hourly_wage.keys():
    str_key.append(str(key))
str_key
Explanation: <div class="alert alert alert-success">
    <b>Exercise</b> Put all keys of the `hourly_wage` dictionary in a list as strings.  If they are not yet a string, convert them:
</div>
End of explanation
a_tuple = (2, 3, 'aa', [1, 2])
a_tuple
a_second_tuple = 2, 3, 'aa', [1,2]
a_second_tuple
Explanation: Tuples
Tuples are basically immutable lists. The elements of a tuple are written between parentheses, or just separated by commas
End of explanation
<END_TASK>