Skip to content

Commit 4743310

Browse files
authored
Merge pull request #9 from Duke-GCB/zmielko-patch-1
Minor update on descriptions and last part
2 parents 1180053 + 125bcb3 commit 4743310

2 files changed

Lines changed: 103 additions & 126 deletions

File tree

intro-programming-python-instructor.ipynb

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
}
3131
},
3232
"source": [
33-
"## Python & Notebook"
33+
"## Python in the Jupyter Notebook Environment"
3434
]
3535
},
3636
{
@@ -46,7 +46,7 @@
4646
"cell_type": "markdown",
4747
"metadata": {},
4848
"source": [
49-
"The gray boxes are cells. We write code in the cells with instructions for the computer to execute. Let's start by telling the computer to add 5 and 3 and print the result. "
49+
"The gray boxes are cells. We write code in the cells with instructions for the computer to execute. Let's start by adding 5 and 3 and printing the result. "
5050
]
5151
},
5252
{
@@ -87,10 +87,10 @@
8787
"outputs": [
8888
{
8989
"ename": "SyntaxError",
90-
"evalue": "invalid syntax (<ipython-input-2-50b4ae29d403>, line 1)",
90+
"evalue": "invalid syntax (3449611317.py, line 1)",
9191
"output_type": "error",
9292
"traceback": [
93-
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-2-50b4ae29d403>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print(Hello world)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
93+
"\u001b[0;36m Input \u001b[0;32mIn [2]\u001b[0;36m\u001b[0m\n\u001b[0;31m print(Hello world)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
9494
]
9595
}
9696
],
@@ -102,7 +102,7 @@
102102
"cell_type": "markdown",
103103
"metadata": {},
104104
"source": [
105-
"OK so Python didn't like that, but there's a lot to learn here\n",
105+
"OK so Python returned an error, but there's a lot to learn here:\n",
106106
"\n",
107107
"- Errors can be really helpful.\n",
108108
"- It's common to make mistakes, don't be discouraged\n",
@@ -205,7 +205,7 @@
205205
"cell_type": "markdown",
206206
"metadata": {},
207207
"source": [
208-
"And we can print multiple things by inserting commas. So let's all greet our neighbor:"
208+
"And we can print multiple items by inserting commas. So we can combine our greeting and neighbor to greet our neighbor:"
209209
]
210210
},
211211
{
@@ -340,7 +340,7 @@
340340
"traceback": [
341341
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
342342
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
343-
"\u001b[0;32m<ipython-input-11-6fb37bd547ba>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmy_float\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m50.0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmy_string\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"Hello\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmy_float\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mmy_string\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
343+
"Input \u001b[0;32mIn [11]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m my_float \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m50.0\u001b[39m\n\u001b[1;32m 2\u001b[0m my_string \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHello\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mmy_float\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mmy_string\u001b[49m)\n",
344344
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'float' and 'str'"
345345
]
346346
}
@@ -508,7 +508,7 @@
508508
"cell_type": "markdown",
509509
"metadata": {},
510510
"source": [
511-
"One more optional parameter you can use is called a step in the format **[start:end:step]**. This parameter determines what you \"count by\" and is 1 by default. "
511+
"One more optional parameter you can use is called a **step** in the format **[start:end:step]**. This parameter determines what you \"count by\" and is 1 by default. "
512512
]
513513
},
514514
{
@@ -793,10 +793,10 @@
793793
"outputs": [
794794
{
795795
"ename": "SyntaxError",
796-
"evalue": "invalid syntax (<ipython-input-28-3051ded84a5c>, line 1)",
796+
"evalue": "invalid syntax (3233142308.py, line 1)",
797797
"output_type": "error",
798798
"traceback": [
799-
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-28-3051ded84a5c>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 1a\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
799+
"\u001b[0;36m Input \u001b[0;32mIn [28]\u001b[0;36m\u001b[0m\n\u001b[0;31m 1a\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
800800
]
801801
}
802802
],
@@ -866,7 +866,7 @@
866866
"traceback": [
867867
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
868868
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
869-
"\u001b[0;32m<ipython-input-31-74803fcdf01e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mr3\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'G'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
869+
"Input \u001b[0;32mIn [31]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m r3[\u001b[38;5;241m2\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mG\u001b[39m\u001b[38;5;124m'\u001b[39m\n",
870870
"\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment"
871871
]
872872
}
@@ -1110,7 +1110,7 @@
11101110
}
11111111
],
11121112
"source": [
1113-
"my_dictionary = {\"key\":\"value\", \"second_key\":2, 3:\"third_value\"}\n",
1113+
"my_dictionary = {\"key\": \"value\", \"second_key\": 2, 3: \"third_value\"}\n",
11141114
"print(my_dictionary)"
11151115
]
11161116
},
@@ -1281,7 +1281,7 @@
12811281
"cell_type": "markdown",
12821282
"metadata": {},
12831283
"source": [
1284-
"As an exercise, let's get the total quantity of nucleotides we have. This loop should look a lot like counting the bases in the sequence"
1284+
"As an exercise, let's get the total quantity of nucleotides we have in the dictionary counts. This loop should look a lot like counting the bases in the sequence."
12851285
]
12861286
},
12871287
{
@@ -1404,7 +1404,7 @@
14041404
"\n",
14051405
"We've looked at strings and dictionaries as collection types. There's another one that's really useful, and it's called a list. It's an ordered collection of elements. Lists use the `[]` square brackets, and elments are separated by commas.\n",
14061406
"\n",
1407-
"Here's one that contains our 3 sequences.:"
1407+
"Here's one that contains our 3 sequences."
14081408
]
14091409
},
14101410
{
@@ -1516,7 +1516,7 @@
15161516
"source": [
15171517
"### Making choices / Conditionals\n",
15181518
"\n",
1519-
"When writing programs, it's very handy to examine some data or some results, and make a decision about what to do next. This is called a conditional, and most common version is the `if-else`.\n",
1519+
"When writing programs, it's usefull to examine some data or some results, and make a decision about what to do next. This is called a conditional, and most common version is the `if-else`.\n",
15201520
"\n",
15211521
"Here's how it works. we start with an `if` keyword, and then we write some expression that will either be **True** or **False**. Then we write a colon, and indent what should happen **if** the expression was **True**. \n",
15221522
"\n",
@@ -2034,7 +2034,7 @@
20342034
" | index(...)\n",
20352035
" | S.index(sub[, start[, end]]) -> int\n",
20362036
" | \n",
2037-
" | Return the lowest index in S where substring sub is found, \n",
2037+
" | Return the lowest index in S where substring sub is found,\n",
20382038
" | such that sub is contained within S[start:end]. Optional\n",
20392039
" | arguments start and end are interpreted as in slice notation.\n",
20402040
" | \n",
@@ -2073,8 +2073,8 @@
20732073
" | isidentifier(self, /)\n",
20742074
" | Return True if the string is a valid Python identifier, False otherwise.\n",
20752075
" | \n",
2076-
" | Use keyword.iskeyword() to test for reserved identifiers such as \"def\" and\n",
2077-
" | \"class\".\n",
2076+
" | Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n",
2077+
" | such as \"def\" or \"class\".\n",
20782078
" | \n",
20792079
" | islower(self, /)\n",
20802080
" | Return True if the string is a lowercase string, False otherwise.\n",
@@ -2143,6 +2143,19 @@
21432143
" | If the separator is not found, returns a 3-tuple containing the original string\n",
21442144
" | and two empty strings.\n",
21452145
" | \n",
2146+
" | removeprefix(self, prefix, /)\n",
2147+
" | Return a str with the given prefix string removed if present.\n",
2148+
" | \n",
2149+
" | If the string starts with the prefix string, return string[len(prefix):].\n",
2150+
" | Otherwise, return a copy of the original string.\n",
2151+
" | \n",
2152+
" | removesuffix(self, suffix, /)\n",
2153+
" | Return a str with the given suffix string removed if present.\n",
2154+
" | \n",
2155+
" | If the string ends with the suffix string and that suffix is not empty,\n",
2156+
" | return string[:-len(suffix)]. Otherwise, return a copy of the original\n",
2157+
" | string.\n",
2158+
" | \n",
21462159
" | replace(self, old, new, count=-1, /)\n",
21472160
" | Return a copy with all occurrences of substring old replaced by new.\n",
21482161
" | \n",
@@ -2268,7 +2281,7 @@
22682281
" | __new__(*args, **kwargs) from builtins.type\n",
22692282
" | Create and return a new object. See help(type) for accurate signature.\n",
22702283
" | \n",
2271-
" | maketrans(x, y=None, z=None, /)\n",
2284+
" | maketrans(...)\n",
22722285
" | Return a translation table usable for str.translate().\n",
22732286
" | \n",
22742287
" | If there is only one argument, it must be a dictionary mapping Unicode\n",
@@ -2303,7 +2316,7 @@
23032316
"3. Reverses their order\n",
23042317
"4. (Bonus) Print the first letter from each word\n",
23052318
"\n",
2306-
"Hint: Use the `help()` function on `str` and `list` to see what methods are available to assist with the task. "
2319+
"**Hint:** Use the `help()` function on `str` and `list` to see what methods are available to assist with the task. "
23072320
]
23082321
},
23092322
{
@@ -2536,7 +2549,9 @@
25362549
"cell_type": "markdown",
25372550
"metadata": {},
25382551
"source": [
2539-
"To test that the function works, we could eyeball it, and we can also test that double-reversing gives us the original sequence:"
2552+
"To test that the function works, we could:\n",
2553+
"- see if the output is correct ourselves\n",
2554+
"- test that double-reversing gives us the original sequence"
25402555
]
25412556
},
25422557
{
@@ -3037,14 +3052,7 @@
30373052
"cell_type": "markdown",
30383053
"metadata": {},
30393054
"source": [
3040-
"We've done a good job of organizing our code into functions here, but we've only been running them from this notebook. So next, we're going to take our code and put it in a script - starting with the `read_sequence` function.\n"
3041-
]
3042-
},
3043-
{
3044-
"cell_type": "markdown",
3045-
"metadata": {},
3046-
"source": [
3047-
"This is going to be very familiar, since we're doing the same thing we did in bash, just with a different language!"
3055+
"We've done a good job of organizing our code into functions here, but we've only been running them from this notebook. So next, we're going to take our code and put it in a script - starting with the `read_fasta` function."
30483056
]
30493057
},
30503058
{
@@ -3081,7 +3089,7 @@
30813089
" f = open(filename)\n",
30823090
" for line in f:\n",
30833091
" line = line.strip()\n",
3084-
" if not '>' in line:\n",
3092+
" if '>' not in line:\n",
30853093
" # Append to the last sequence\n",
30863094
" sequence = sequence + line\n",
30873095
" f.close()\n",
@@ -3090,6 +3098,19 @@
30903098
"print(read_fasta('ae.fa'))\n"
30913099
]
30923100
},
3101+
{
3102+
"cell_type": "markdown",
3103+
"metadata": {},
3104+
"source": [
3105+
"Our script reads our `ae.fa` file every time we run it, but we know most programs don't work that way. The programs we used in bash expected a data file as an *argument*, and that's a good convention for programs we write too.\n",
3106+
"\n",
3107+
"In Python, our program can get these arguments, but we have to load a module called `sys` from the standard library, a collection of modules included in python but not available by default. The documentation for these is part of the documentation for python: https://docs.python.org/3/library/sys.html\n",
3108+
"\n",
3109+
"Libraries are incredibly useful - there are libraries for working with numeric and scientific data, generating plots, fetching data from the web, working with image and document files, databases, etc. And of course, there's a library for getting things like your script's command-line arguments.\n",
3110+
"\n",
3111+
"So, let's change our `read_fasta.py` program slightly."
3112+
]
3113+
},
30933114
{
30943115
"cell_type": "code",
30953116
"execution_count": null,
@@ -3105,7 +3126,7 @@
31053126
" f = open(filename)\n",
31063127
" for line in f:\n",
31073128
" line = line.strip()\n",
3108-
" if not '>' in line:\n",
3129+
" if '>' not in line:\n",
31093130
" # Append to the last sequence\n",
31103131
" sequence = sequence + line\n",
31113132
" f.close()\n",
@@ -3114,6 +3135,13 @@
31143135
"print(read_fasta(sys.argv[1]))\n"
31153136
]
31163137
},
3138+
{
3139+
"cell_type": "markdown",
3140+
"metadata": {},
3141+
"source": [
3142+
"But what happens if we don't have an input file name? According to the documentation, sys.argv, returns a list where the first item sys.argv[0] is the name of the script by default, and each additional item in the list are the command line arguments. If no argument was passed, sys.argv should be a list of just the script name."
3143+
]
3144+
},
31173145
{
31183146
"cell_type": "code",
31193147
"execution_count": null,
@@ -3129,52 +3157,19 @@
31293157
" f = open(filename)\n",
31303158
" for line in f:\n",
31313159
" line = line.strip()\n",
3132-
" if not '>' in line:\n",
3160+
" if '>' not in line:\n",
31333161
" # Append to the last sequence\n",
31343162
" sequence = sequence + line\n",
31353163
" f.close()\n",
31363164
" return sequence\n",
31373165
"\n",
31383166
"if len(sys.argv) < 2:\n",
31393167
" print('Usage:', sys.argv[0], '<sequence.fa>')\n",
3140-
" exit(1)\n",
3168+
" sys.exit(1)\n",
31413169
"\n",
31423170
"print(read_fasta(sys.argv[1]))\n"
31433171
]
31443172
},
3145-
{
3146-
"cell_type": "markdown",
3147-
"metadata": {},
3148-
"source": [
3149-
"`$ python read_fasta.py`"
3150-
]
3151-
},
3152-
{
3153-
"cell_type": "markdown",
3154-
"metadata": {},
3155-
"source": [
3156-
"Our script reads our `ae.fa` file every time we run it, but we know most programs don't work that way. The programs we used in bash expected a data file as an *argument*, and that's a good convention for programs we write too.\n",
3157-
"\n",
3158-
"In Python, our program can get these arguments, but we have to load a library to access them. Google it!\n",
3159-
"\n",
3160-
"`import sys`\n",
3161-
"\n",
3162-
"https://docs.python.org/3/library/sys.html\n",
3163-
"\n",
3164-
"Libraries are incredibly useful - there are libraries for working with numeric and scientific data, generating plots, fetching data from the web, working with image and document files, databases, etc. And of course, there's a library for getting things like your script's command-line arguments.\n",
3165-
"\n",
3166-
"So, let's change our `read_fasta.py` program slightly\n",
3167-
"\n",
3168-
" import sys\n",
3169-
" read_fasta(sys.argv[1])\n",
3170-
"\n",
3171-
"And run it. But what happens if we don't have a file name\n",
3172-
"\n",
3173-
" if len(sys.argv) < 2:\n",
3174-
" print 'Usage:', sys.argv[0], '<sequence.fa>'\n",
3175-
" exit(1)\n"
3176-
]
3177-
},
31783173
{
31793174
"cell_type": "markdown",
31803175
"metadata": {},
@@ -3186,7 +3181,7 @@
31863181
"metadata": {
31873182
"anaconda-cloud": {},
31883183
"kernelspec": {
3189-
"display_name": "Python 3",
3184+
"display_name": "Python 3 (ipykernel)",
31903185
"language": "python",
31913186
"name": "python3"
31923187
},
@@ -3200,7 +3195,7 @@
32003195
"name": "python",
32013196
"nbconvert_exporter": "python",
32023197
"pygments_lexer": "ipython3",
3203-
"version": "3.7.7"
3198+
"version": "3.9.12"
32043199
}
32053200
},
32063201
"nbformat": 4,

0 commit comments

Comments
 (0)