Skip to content

Commit 41d7d11

Browse files
author
stanleycelestin1
committed
2 parents 4dd7e7d + 60591ae commit 41d7d11

1 file changed

Lines changed: 131 additions & 40 deletions

File tree

PythonTutorial.ipynb

Lines changed: 131 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Session 1 Tutorial\n",
7+
"# Session 1 Python Tutorial/ Parsing of a File Tutorial\n",
88
"\n",
99
"*Written By: **Nicholas Archibong\n",
1010
"\n",
11-
"In this Tutorial, you are taken through how to open a csv file, going through the file and checking for a specific entry, changing that entry, adding in a new coloumn, and finally writing the changed file into a new file for other purposes. \n"
11+
"In this Tutorial, you are taken through how to open a csv file, going through the file and checking for a specific entry, changing that entry, adding in a new coloumn, writing the changed file into a new file for other purposes, as well as going over some basics in python. \n"
1212
]
1313
},
1414
{
@@ -17,29 +17,23 @@
1717
"source": [
1818
"#### Reading in of file\n",
1919
"\n",
20-
"In this section, the csv module is imported into our script to allow access to some of their useful methods. Such as reading in a file and writing to a file. "
20+
"In this section, the csv module is imported into our script to allow access to some of their useful methods. Such as reading in a file and writing to a file. \n",
21+
"\n",
22+
"*** You will have to change the readFile and writeFile to the location of the github repository that you cloned onto your desktop"
2123
]
2224
},
2325
{
2426
"cell_type": "code",
25-
"execution_count": 2,
27+
"execution_count": null,
2628
"metadata": {
2729
"scrolled": true
2830
},
29-
"outputs": [
30-
{
31-
"name": "stdout",
32-
"output_type": "stream",
33-
"text": [
34-
"[['\\ufeffName', '# of Rings', '# of points', '# of games', '# of assists', 'career high'], ['Michael Jordan', '6', '32292', '', '5633', '69'], ['Russel Westbrook', '', '15000', '410', '2384', '51'], ['LeBron James', '4', '28787', '634', '', '61'], ['Dwyane Wade', '3', '21231', '612', '1234', '55'], ['Kevin Durant', '1', '', '439', '', '54'], ['Stephen Curry', '2', '35235', '378', '2343', '54'], ['Allen Iverson', '', '24368', '', '5624', '55'], ['Kobe Bryant', '5', '31700', '1346', '', '81'], ['Bill Russell', '11', '14522', '963', '214', '37']]\n"
35-
]
36-
}
37-
],
31+
"outputs": [],
3832
"source": [
3933
"import csv # module that contains the csv methods\n",
4034
"\n",
41-
"readFile = 'nba_players.csv' # file reading from\n",
42-
"writeFile = 'nba_players_edited.csv' # file writing to\n",
35+
"readFile = '' # file reading from\n",
36+
"writeFile = '' # file writing to\n",
4337
"\n",
4438
"with open(readFile, 'r') as rf: # open csv file\n",
4539
" data = csv.reader(rf) # read in csv file\n",
@@ -48,6 +42,117 @@
4842
"print(playerList)"
4943
]
5044
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"#### Loop Practice\n",
50+
"\n",
51+
"There are two type of loops in python, the while loop and the for loop. Both have similar functionality, however, they have their key differences. A while loop is used for iterations where the end is not known, while the for loop could be used for iterations where the end is known, or you have a specific number of iterations to complete. "
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"##### Counting of numbers from 1 to 10\n",
59+
"\n",
60+
"This example, you are to complete the code to have both the while loop and the for loop count from 1 to 10. "
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {},
67+
"outputs": [],
68+
"source": [
69+
"for num in range(,):\n",
70+
" print(num)"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": null,
76+
"metadata": {},
77+
"outputs": [],
78+
"source": [
79+
"countNum = \n",
80+
"while countNum <= :\n",
81+
" print(countNum)\n",
82+
" countNum += "
83+
]
84+
},
85+
{
86+
"cell_type": "markdown",
87+
"metadata": {},
88+
"source": [
89+
"#### More practice with loops\n",
90+
"\n",
91+
"In the next example, there is a list of strings. You are tasked with completing the code that is missing in order to find the string name 'Avery'. \n",
92+
"\n",
93+
"Counter Concept: A counter is a variable that typically increments when you go through an iteration of a loop. It's useful in the sense that you could use a counter to access list indices. "
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": null,
99+
"metadata": {},
100+
"outputs": [],
101+
"source": [
102+
"# practice with loops\n",
103+
"import random\n",
104+
"nameList = ['Michael', 'John', 'James', 'Rick', 'Avery', 'Roi', 'Matthew', 'Peter']\n",
105+
"random.shuffle(nameList)\n",
106+
"\n",
107+
"nameCounter = \n",
108+
"while nameList[nameCounter] != '':\n",
109+
" nameCounter += \n",
110+
" print('Avery not found yet in while loop...')"
111+
]
112+
},
113+
{
114+
"cell_type": "code",
115+
"execution_count": null,
116+
"metadata": {},
117+
"outputs": [],
118+
"source": [
119+
"import random\n",
120+
"nameList = ['Michael', 'John', 'James', 'Rick', 'Avery', 'Roi', 'Matthew', 'Peter']\n",
121+
"random.shuffle(nameList)\n",
122+
"\n",
123+
"for name in nameList:\n",
124+
" if name == '':\n",
125+
" break\n",
126+
" else:\n",
127+
" print('Avery not found yet in for loop...')"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"metadata": {},
133+
"source": [
134+
"#### User I/O \n",
135+
"\n",
136+
"The input funciton is used to gather information from the user. You could type cast it to change the type from string to any of the other data types. "
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": null,
142+
"metadata": {},
143+
"outputs": [],
144+
"source": [
145+
"userInput = 'c'\n",
146+
"whileCounter = 1\n",
147+
"\n",
148+
"while userInput != 'q':\n",
149+
" userInput = input('To quit please enter \\'q\\': ').lower()\n",
150+
" whileCounter += 1\n",
151+
"\n",
152+
"print('Program Terminated...')\n",
153+
"print('Number of iterations is...%',whileCounter)"
154+
]
155+
},
51156
{
52157
"cell_type": "markdown",
53158
"metadata": {},
@@ -56,24 +161,18 @@
56161
"\n",
57162
"In this section of code, you, the user, are looking for a \"specific value\" in the file. This specific value could be anything you want it, however, in this file, there are missing values \"''\" which you would like to replace with a 'NULL' instead to signify that that particular value was missing. \n",
58163
"\n",
59-
"A nested for loop is used to access the indcies of the nest list. This allows us to hit every index and check if there if it is missing a value or not. Two counters are used to allow the for loops to traverse the list correctly. "
164+
"A nested for loop is used to access the indcies of the nest list. This allows us to hit every index and check if there if it is missing a value or not. Two counters are used to allow the for loops to traverse the list correctly. \n",
165+
"\n",
166+
"Python Nested Lists Documentation: http://www.dreamincode.net/forums/topic/193487-a-quick-overview-of-nested-lists-in-python/"
60167
]
61168
},
62169
{
63170
"cell_type": "code",
64-
"execution_count": 2,
171+
"execution_count": null,
65172
"metadata": {
66173
"scrolled": true
67174
},
68-
"outputs": [
69-
{
70-
"name": "stdout",
71-
"output_type": "stream",
72-
"text": [
73-
"[['\\ufeffName', '# of Rings', '# of points', '# of games', '# of assists', 'career high'], ['Michael Jordan', '6', '32292', 'NULL', '5633', '69'], ['Russel Westbrook', 'NULL', '15000', '410', '2384', '51'], ['LeBron James', '4', '28787', '634', 'NULL', '61'], ['Dwyane Wade', '3', '21231', '612', '1234', '55'], ['Kevin Durant', '1', 'NULL', '439', 'NULL', '54'], ['Stephen Curry', '2', '35235', '378', '2343', '54'], ['Allen Iverson', 'NULL', '24368', 'NULL', '5624', '55'], ['Kobe Bryant', '5', '31700', '1346', 'NULL', '81'], ['Bill Russell', '11', '14522', '963', '214', '37']]\n"
74-
]
75-
}
76-
],
175+
"outputs": [],
77176
"source": [
78177
"rowCounter = 0; # counter that allows indexing\n",
79178
"\n",
@@ -99,7 +198,7 @@
99198
},
100199
{
101200
"cell_type": "code",
102-
"execution_count": 3,
201+
"execution_count": null,
103202
"metadata": {
104203
"collapsed": true
105204
},
@@ -124,17 +223,9 @@
124223
},
125224
{
126225
"cell_type": "code",
127-
"execution_count": 4,
226+
"execution_count": null,
128227
"metadata": {},
129-
"outputs": [
130-
{
131-
"name": "stdout",
132-
"output_type": "stream",
133-
"text": [
134-
"[['\\ufeffName', '# of Rings', '# of points', '# of games', '# of assists', 'career high', '> 60pts'], ['Michael Jordan', '6', '32292', 'NULL', '5633', '69', '1'], ['Russel Westbrook', 'NULL', '15000', '410', '2384', '51', '0'], ['LeBron James', '4', '28787', '634', 'NULL', '61', '1'], ['Dwyane Wade', '3', '21231', '612', '1234', '55', '0'], ['Kevin Durant', '1', 'NULL', '439', 'NULL', '54', '0'], ['Stephen Curry', '2', '35235', '378', '2343', '54', '0'], ['Allen Iverson', 'NULL', '24368', 'NULL', '5624', '55', '0'], ['Kobe Bryant', '5', '31700', '1346', 'NULL', '81', '1'], ['Bill Russell', '11', '14522', '963', '214', '37', '0']]\n"
135-
]
136-
}
137-
],
228+
"outputs": [],
138229
"source": [
139230
"with open(writeFile, 'w+') as wf: # open to write to csv file\n",
140231
" wr = csv.writer(wf, dialect='excel')\n",
@@ -153,7 +244,7 @@
153244
"language_info": {
154245
"codemirror_mode": {
155246
"name": "ipython",
156-
"version": 3.0
247+
"version": 3
157248
},
158249
"file_extension": ".py",
159250
"mimetype": "text/x-python",
@@ -164,5 +255,5 @@
164255
}
165256
},
166257
"nbformat": 4,
167-
"nbformat_minor": 0
168-
}
258+
"nbformat_minor": 1
259+
}

0 commit comments

Comments
 (0)