{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercises\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 1.1 \n",
"What does `myString[0:10:2]` do? \n",
"What could the `2` mean in that context?\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"02468\n",
"019876543210\n"
]
}
],
"source": [
"myString = '012345678910'\n",
"print(myString[0:10:2])\n",
"# The 2 is the step/increment between each selected index in the sequence. In this case, every 2nd letter is kept.\n",
"# This allows the following neat trick to reverse a string: have steps of -1\n",
"print(myString[::-1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"## Exercise 1.2 \n",
"I am trying to make a division. Could you make my code work?"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.0\n"
]
}
],
"source": [
"a=12\n",
"b='36'\n",
"# The problem here, is that \"a\" in an int(), and \"b\" is a str().\n",
"# -> we have to convert \"b\" to a number type (int or float) in order for the division to work.\n",
"division = int(b)/a\n",
"print(division)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"## Exercise 1.3 \n",
"1. Put your favorite quote in a string variable."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"myQuote=\"- Regarde-moi, mon cher, et dis quelle espérance Pourrait bien me laisser cette protubérance !\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Find a way to replace all spaces by '@' in the quote.\n",
"Since `myQuote` is a string, we look at `help(str)` to search for methods available for string objects. We can see that there is a `replace()` method that allows replacing characters within a string."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"- Regarde-moi, mon cher, et dis quelle espérance Pourrait bien me laisser cette protubérance !\n",
"-@Regarde-moi,@mon@cher,@et@dis@quelle@espérance@Pourrait@bien@me@laisser@cette@protubérance@!\n"
]
}
],
"source": [
"modifedQuote = myQuote.replace(' ','@')\n",
"print(myQuote)\n",
"print(modifedQuote)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"## Exercise 1.4\n",
"Consider the two dictionnaries below, describing the number of doctorates delivered in 1999 in the US (adapted from [nsf data](http://www.nsf.gov/statistics/infbrief/nsf11305/)):"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"science_doc = {\"Agricultural sciences\"\t: 1065,\n",
" \"Biochemistry\"\t: 759,\n",
" \"Molecular biology\"\t: 716,\n",
" \"Neurosciences\"\t: 431,\n",
" \"Other biological sciences\"\t: 3675,\n",
" \"Computer sciences\"\t: 856,\n",
" \"Earth, atmospheric, and ocean sciences\"\t: 706,\n",
" \"Mathematics\"\t: 1083,\n",
" \"Chemistry\"\t: 2132,\n",
" \"Physics and astronomy\"\t: 715,\n",
" \"Astrology\" : 2012, \n",
" \"Psychology\"\t: 3668,\n",
" \"Social sciences\"\t: 4063}\n",
"\n",
"engineering_doc = {\"Aerospace/aeronautical engineering\"\t: 206,\n",
" \"Chemical engineering\"\t: 576,\n",
" \"Civil engineering\"\t: 506,\n",
" \"Electrical engineering\"\t: 1236,\n",
" \"Industrial/manufacturing engineering\"\t: 211,\n",
" \"Materials science engineering\"\t: 393,\n",
" \"Mechanical engineering\"\t: 786,\n",
" \"Other engineering\"\t: 1416 }\n",
"\n",
"humanities_doc = {\"Foreign languages and literature\"\t : 626,\n",
" \"History\"\t : 960,\n",
" \"Letters\"\t : 1516,\n",
" \"Other humanities\"\t : 1934}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. merge these three dictionnaries in a `all_doc` dictionnary.\n",
"Looking at `help(dict)`, we find there is an `update` method that \"Update D from dict/iterable E and F.\"\n",
"This method adds key-value pairs to a dicitonnary from another dictionnary.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Agricultural sciences': 1065, 'Biochemistry': 759, 'Molecular biology': 716, 'Neurosciences': 431, 'Other biological sciences': 3675, 'Computer sciences': 856, 'Earth, atmospheric, and ocean sciences': 706, 'Mathematics': 1083, 'Chemistry': 2132, 'Physics and astronomy': 715, 'Astrology': 2012, 'Psychology': 3668, 'Social sciences': 4063, 'Aerospace/aeronautical engineering': 206, 'Chemical engineering': 576, 'Civil engineering': 506, 'Electrical engineering': 1236, 'Industrial/manufacturing engineering': 211, 'Materials science engineering': 393, 'Mechanical engineering': 786, 'Other engineering': 1416, 'Foreign languages and literature': 626, 'History': 960, 'Letters': 1516, 'Other humanities': 1934}\n",
"Are the two dict identical? True\n"
]
}
],
"source": [
"all_doc = {}\n",
"all_doc.update(science_doc)\n",
"all_doc.update(engineering_doc)\n",
"all_doc.update(humanities_doc)\n",
"print(all_doc)\n",
"\n",
"# Alternatively, we could also copy one of the dict as a starting point with the \"copy()\" method of dict:\n",
"all_doc_2 = science_doc.copy()\n",
"all_doc_2.update(engineering_doc)\n",
"all_doc_2.update(humanities_doc)\n",
"print('Are the two dict identical?', all_doc == all_doc_2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. what is the length of the `all_doc` dictionnary"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Length of the dicitonnary: 25\n"
]
}
],
"source": [
"print( \"Length of the dicitonnary:\" , len( all_doc ) )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. add the \"Health\" doctorates of the `all_doc` dictionnary, with 1407 doctorates awarded"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"all_doc['Health'] = 1407"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. multiply by 2 the number of \"Physics and astronomy\" doctorates in `all_doc`"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Physics and astronomy : 715\n",
"Physics and astronomy : 1430\n"
]
}
],
"source": [
"key = 'Physics and astronomy'\n",
"print(key , ':' , all_doc[key] )\n",
"all_doc[key] = all_doc[key] * 2 # Alternatively: all_doc[key] *= 2\n",
"print(key , ':' , all_doc[key] )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. remove the Astology key from `all_doc`\n",
"Look at `help(dict)`: there is a pop method that \"remove specified key\"."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Agricultural sciences': 1065, 'Biochemistry': 759, 'Molecular biology': 716, 'Neurosciences': 431, 'Other biological sciences': 3675, 'Computer sciences': 856, 'Earth, atmospheric, and ocean sciences': 706, 'Mathematics': 1083, 'Chemistry': 2132, 'Physics and astronomy': 1430, 'Psychology': 3668, 'Social sciences': 4063, 'Aerospace/aeronautical engineering': 206, 'Chemical engineering': 576, 'Civil engineering': 506, 'Electrical engineering': 1236, 'Industrial/manufacturing engineering': 211, 'Materials science engineering': 393, 'Mechanical engineering': 786, 'Other engineering': 1416, 'Foreign languages and literature': 626, 'History': 960, 'Letters': 1516, 'Other humanities': 1934, 'Health': 1407}\n"
]
}
],
"source": [
"all_doc.pop(\"Astrology\")\n",
"print(all_doc)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"## Exercise 1.5\n",
"Set a variable called `isDivisible` to `True` if 7453102041884640481897 is evenly divisible by 135138672131"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"# The % operator gives the remainder of the euclidian division of two numbers.\n",
"# A number x is evenly divisible by y if the remainder of the euclidian division is equal to 0,\n",
"# or, to put it in semi-code : x % y is equal to 0.\n",
"# We then use the \"==\" comparison operator to return a booleann value (True or False).\n",
"isDivisible = (7453102041884640481897 % 135138672131) == 0\n",
"print(isDivisible)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"## Exercise 1.6\n",
"Use the `find()` method to determine the position(s) of the motif \"CTCGA\" in the following sequence:\n",
" \n",
"GTGCCCCTCGAGAGGAGGGCGCGCGCCGCGCGCTCGACGCGATCGGCGCTCAGCGAGCGAGCTCCTCGAAGCGATCCGCGCGCGCT\n",
"\n",
"Is there a limitation here?"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6\n",
"32\n",
"64\n",
"-1\n",
"[6, 32, 64]\n"
]
}
],
"source": [
"seq = \"GTGCCCCTCGAGAGGAGGGCGCGCGCCGCGCGCTCGACGCGATCGGCGCTCAGCGAGCGAGCTCCTCGAAGCGATCCGCGCGCGCT\"\n",
"print(seq.find(\"CTCGA\"))\n",
"\n",
"# Limitation:\n",
"# find() only returns the 1st occurence of the motif... \n",
"# To find other occurrences of \"CTCGA\", we can use the optionnal argument \"start\" of the \n",
"# \"find()\"\" method and pass the index of the last found motif + 1:\n",
"print(seq.find(\"CTCGA\", 7))\n",
"print(seq.find(\"CTCGA\", 33))\n",
"print(seq.find(\"CTCGA\", 65)) # Note that \"find()\" returns -1 when no match is found.\n",
"\n",
"# Of course, it would be nicer to automate this:\n",
"# Here is a sneak peak at how to do this using loops, which we'll see in the next lesson.\n",
"positions = [] # Instantiate a list were we will store the motif positions\n",
"pos = seq.find('CTCGA')\n",
"while pos != -1 : # While the motif is found...\n",
" positions.append(pos) # ... add the motif to the result list.\n",
" pos = seq.find('CTCGA' , pos + 1) # Attempt to find another motif\n",
"print(positions)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"## Exercise 1.7\n",
"1. Build a list containing the name of all swiss cantons."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"cantonList = ['Zürich',\n",
"'Bern',\n",
"'Luzern',\n",
"'Uri',\n",
"'Schwyz',\n",
"'Obwalden',\n",
"'Nidwalden',\n",
"'Glarus',\n",
"'Zug',\n",
"'Fribourg',\n",
"'Solothurn',\n",
"'Basel-Stadt',\n",
"'Basel-Landschaft',\n",
"'Schaffhausen',\n",
"'Appenzell Ausserrhoden',\n",
"'Appenzell Innerrhoden',\n",
"'St. Gallen',\n",
"'Graubünden',\n",
"'Aargau',\n",
"'Thurgau',\n",
"'Ticino',\n",
"'Vaud',\n",
"'Valais',\n",
"'Neuchâtel',\n",
"'Geneva',\n",
"'Jura']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. sort it alphabetically\n",
"Looking at `help(list)`, we can see that there is a `sort` method that does just this:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Aargau', 'Appenzell Ausserrhoden', 'Appenzell Innerrhoden', 'Basel-Landschaft', 'Basel-Stadt', 'Bern', 'Fribourg', 'Geneva', 'Glarus', 'Graubünden', 'Jura', 'Luzern', 'Neuchâtel', 'Nidwalden', 'Obwalden', 'Schaffhausen', 'Schwyz', 'Solothurn', 'St. Gallen', 'Thurgau', 'Ticino', 'Uri', 'Valais', 'Vaud', 'Zug', 'Zürich']\n"
]
}
],
"source": [
"cantonList.sort()\n",
"print(cantonList)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. reverse it\n",
"Again, looking at `help(list)` shows that there is a `reverse` method that does this:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Zürich', 'Zug', 'Vaud', 'Valais', 'Uri', 'Ticino', 'Thurgau', 'St. Gallen', 'Solothurn', 'Schwyz', 'Schaffhausen', 'Obwalden', 'Nidwalden', 'Neuchâtel', 'Luzern', 'Jura', 'Graubünden', 'Glarus', 'Geneva', 'Fribourg', 'Bern', 'Basel-Stadt', 'Basel-Landschaft', 'Appenzell Innerrhoden', 'Appenzell Ausserrhoden', 'Aargau']\n"
]
}
],
"source": [
"cantonList.reverse()\n",
"print(cantonList)\n",
"# Alternatively, you can use the slicing trick seen earlier: cantonList[::-1]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}