Skip to content
This repository was archived by the owner on Jun 25, 2018. It is now read-only.

Commit 7851e3a

Browse files
committed
Solve issues with new ex2_0_1
- count the number of words on the words list not the lyrics because character like ‘i’ will be counted many times - change wording for 2.1.1 c. if the sequence passed contains base N, use the mean... - Improve explanation of arguments of functions - Remove format string in day2 session3
1 parent b970818 commit 7851e3a

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

Diff for: Introduction_to_python_day_2_session_1.ipynb

+44-46
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@
358358
"</table>\n",
359359
"\n",
360360
"\n",
361-
"- c. If the sequence passed in above contains `N` bases, use the mean weight of the other bases as the weight."
361+
"- c. If the sequence passed contains base `N`, use the mean weight of the other bases as the weight of base `N`."
362362
]
363363
},
364364
{
@@ -455,6 +455,8 @@
455455
"cell_type": "markdown",
456456
"metadata": {},
457457
"source": [
458+
"### Mandatory arguments\n",
459+
"\n",
458460
"The arguments we have passed to functions so far have all been _mandatory_, if we do not supply them or if supply the wrong number of arguments python will throw an error also called an exception:"
459461
]
460462
},
@@ -467,8 +469,9 @@
467469
"outputs": [],
468470
"source": [
469471
"def square(number):\n",
470-
" y = number*number\n",
471-
" return y"
472+
" # one mandatory argument\n",
473+
" y = number*number\n",
474+
" return y"
472475
]
473476
},
474477
{
@@ -486,7 +489,7 @@
486489
"cell_type": "markdown",
487490
"metadata": {},
488491
"source": [
489-
"Mandatory arguments are assumed to come in the same order as the arguments in the function definition, but you can also opt to specify the arguments using the argument names as _keywords_, supplying the values corresponding to each keyword with a `=` sign."
492+
"**Mandatory arguments are assumed to come in the same order as the arguments in the function definition**, but you can also opt to specify the arguments using the argument names as _keywords_, supplying the values corresponding to each keyword with a `=` sign."
490493
]
491494
},
492495
{
@@ -509,6 +512,7 @@
509512
"outputs": [],
510513
"source": [
511514
"def repeat(seq, n):\n",
515+
" # two mandatory arguments\n",
512516
" result = ''\n",
513517
" for i in range(0,n):\n",
514518
" result += seq\n",
@@ -533,13 +537,14 @@
533537
},
534538
"outputs": [],
535539
"source": [
536-
"print(repeat(seq=\"CTA\", 3))"
540+
"print(repeat(seq=\"CTA\", n=3))"
537541
]
538542
},
539543
{
540544
"cell_type": "markdown",
541545
"metadata": {},
542546
"source": [
547+
"### Arguments with default values\n",
543548
"Sometimes it is useful to give some arguments a default value that the caller can override, but which will be used if the caller does not supply a value for this argument. We can do this by assigning some value to the named argument with the `=` operator in the function definition."
544549
]
545550
},
@@ -579,14 +584,17 @@
579584
" \n",
580585
"myFunction()\n",
581586
"myFunction()\n",
587+
"myFunction()\n",
588+
"myFunction([])\n",
589+
"myFunction([])\n",
582590
"myFunction([])"
583591
]
584592
},
585593
{
586594
"cell_type": "markdown",
587595
"metadata": {},
588596
"source": [
589-
"One can either create a \"new\" default variable every time a function is run:"
597+
"... or avoid modifying *mutable* default arguments."
590598
]
591599
},
592600
{
@@ -597,27 +605,25 @@
597605
},
598606
"outputs": [],
599607
"source": [
600-
"def myFunction(parameters=None):\n",
601-
" \n",
602-
" if parameters is None:\n",
603-
" parameters = []\n",
604-
" \n",
608+
"def myFunction(parameters):\n",
609+
" # one mandatory argument without default value\n",
605610
" parameters.append( 100 )\n",
606611
" print(parameters)\n",
607612
" \n",
608-
"myFunction()\n",
609-
"myFunction()\n",
610-
"myFunction([80])\n",
611-
"myFunction([80])"
613+
"my_list = []\n",
614+
"myFunction(my_list)\n",
615+
"myFunction(my_list)\n",
616+
"myFunction(my_list)\n",
617+
"my_new_list = []\n",
618+
"myFunction(my_new_list)"
612619
]
613620
},
614621
{
615622
"cell_type": "markdown",
616623
"metadata": {},
617624
"source": [
618-
"... or avoid modifying *mutable* default arguments.\n",
619-
"\n",
620-
"By using the `+` operator which returns the result of the expression as a new value, instead of `list.extend()` that modified the list in-place and returns nothing."
625+
"### Position of mandatory arguments\n",
626+
"Arrange function arguments so that *mandatory* arguments come first:"
621627
]
622628
},
623629
{
@@ -628,19 +634,19 @@
628634
},
629635
"outputs": [],
630636
"source": [
631-
"def myFunction(parameters=[]):\n",
632-
" results = parameters + [100]\n",
633-
" print(results)\n",
637+
"def runSimulation(initialTemperature, nsteps=1000):\n",
638+
" # one mandatory argument followed by one with default value\n",
639+
" print(\"Running simulation starting at\", initialTemperature, \"K and doing\", nsteps, \"steps\")\n",
634640
" \n",
635-
"myFunction()\n",
636-
"myFunction()"
641+
"runSimulation(300, 500)\n",
642+
"runSimulation(300)"
637643
]
638644
},
639645
{
640646
"cell_type": "markdown",
641647
"metadata": {},
642648
"source": [
643-
"Arrange function arguments so that *mandatory* arguments come first:"
649+
"As before, no positional argument can appear after a keyword argument, and all required arguments must still be provided."
644650
]
645651
},
646652
{
@@ -651,11 +657,7 @@
651657
},
652658
"outputs": [],
653659
"source": [
654-
"def runSimulation(initialTemperature, nsteps=1000):\n",
655-
" print(\"Running simulation starting at\", initialTemperature, \"K and doing\", nsteps, \"steps\")\n",
656-
" \n",
657-
"runSimulation(300, 500)\n",
658-
"runSimulation(300)"
660+
"runSimulation( nsteps=100, initialTemperature=300 )"
659661
]
660662
},
661663
{
@@ -666,15 +668,7 @@
666668
},
667669
"outputs": [],
668670
"source": [
669-
"def badFunction(nsteps=1000, initialTemperature):\n",
670-
" pass\n"
671-
]
672-
},
673-
{
674-
"cell_type": "markdown",
675-
"metadata": {},
676-
"source": [
677-
"As before, no positional argument can appear after a keyword argument, and all required arguments must still be provided."
671+
"runSimulation( initialTemperature=300 )"
678672
]
679673
},
680674
{
@@ -685,7 +679,7 @@
685679
},
686680
"outputs": [],
687681
"source": [
688-
"runSimulation( nsteps=100, 300 )"
682+
"runSimulation( nsteps=100 ) # Error: missing required argument 'initialTemperature'"
689683
]
690684
},
691685
{
@@ -696,7 +690,7 @@
696690
},
697691
"outputs": [],
698692
"source": [
699-
"runSimulation( nsteps=100 )"
693+
"runSimulation( nsteps=100, 300 ) # Error: positional argument follows keyword argument"
700694
]
701695
},
702696
{
@@ -714,7 +708,14 @@
714708
},
715709
"outputs": [],
716710
"source": [
717-
"runSimulation( initialTemperature=300, numSteps=100 )"
711+
"runSimulation( initialTemperature=300, numSteps=100 ) # Error: unexpected keyword argument 'numSteps'"
712+
]
713+
},
714+
{
715+
"cell_type": "markdown",
716+
"metadata": {},
717+
"source": [
718+
"Function cannot be defined with mandatory arguments after default ones."
718719
]
719720
},
720721
{
@@ -725,11 +726,8 @@
725726
},
726727
"outputs": [],
727728
"source": [
728-
"def runSimulation(initialTemperature, nsteps=1000):\n",
729-
" print(\"Running simulation starting at\", initialTemperature, \"K and doing\", nsteps, \"steps\")\n",
730-
" \n",
731-
"runSimulation(initialTemperature=300, nsteps=500)\n",
732-
"runSimulation(initialTemperature=300)"
729+
"def badFunction(nsteps=1000, initialTemperature):\n",
730+
" pass"
733731
]
734732
},
735733
{

Diff for: Introduction_to_python_day_2_session_3.ipynb

+4-4
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@
218218
"source": [
219219
"# one line at a time\n",
220220
"fileObj = open( \"data/datafile.txt\" )\n",
221-
"print(\"1st line: %r\" % fileObj.readline())\n",
222-
"print(\"2nd line: %r\" % fileObj.readline())\n",
223-
"print(\"3rd line: %r\" % fileObj.readline())\n",
224-
"print(\"4th line: %r\" % fileObj.readline())\n",
221+
"print(\"1st line:\", fileObj.readline())\n",
222+
"print(\"2nd line:\", fileObj.readline())\n",
223+
"print(\"3rd line:\", fileObj.readline())\n",
224+
"print(\"4th line:\", fileObj.readline())\n",
225225
"fileObj.close()"
226226
]
227227
},

Diff for: solutions/ex2_0_1.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
And the world will live as one
3838
"""
3939

40+
# Change all character to lower ones
41+
lyrics = lyrics.lower()
42+
4043
# Split into words
4144
words = lyrics.split()
4245
# Print the total number of words
@@ -49,11 +52,11 @@
4952
# Calculate the frequency of each word and store the result into a dictionary
5053
results = {}
5154
for w in unique_words:
52-
results[w.lower()] = lyrics.count(w)
55+
results[w.lower()] = words.count(w)
5356

5457
# Print each unique word along with its frequency
5558
for r in results:
56-
print(results[r], '\ttimes for word:', r)
59+
print(results[r], '\t', r)
5760

5861
# Find the most frequent word in the song
5962
most_frequent = 0
@@ -63,4 +66,4 @@
6366
most_frequent_word = r
6467

6568
# Print the most frequent word with its frequency
66-
print(most_frequent_word, 'is the most frequent word being used', most_frequent, 'times.')
69+
print('"', most_frequent_word, '" is the most frequent word being used', most_frequent, 'times.')

0 commit comments

Comments
 (0)