raymondEDS commited on
Commit
5847a8e
·
1 Parent(s): c0de98c

updating homework

Browse files
app/.DS_Store CHANGED
Binary files a/app/.DS_Store and b/app/.DS_Store differ
 
app/pages/__pycache__/week_2.cpython-311.pyc CHANGED
Binary files a/app/pages/__pycache__/week_2.cpython-311.pyc and b/app/pages/__pycache__/week_2.cpython-311.pyc differ
 
app/pages/week_2.py CHANGED
@@ -52,16 +52,17 @@ def show():
52
 
53
  # Using a function from the library
54
  print(math.sqrt(16)) # This will print 4.0
55
- """, line_numbers=True)
56
 
57
  # Interactive Import Exercise
58
  st.subheader("Try it yourself!")
59
  import_code = st.text_area("Try importing and using the math library:",
60
  "import math\nprint(math.sqrt(25))",
61
  height=100)
 
62
  if st.button("Run Import Code"):
63
  output = capture_output(import_code)
64
- st.code(output, line_numbers=True)
65
 
66
  # Print Statements Section
67
  st.header("2. Print Statements")
@@ -80,16 +81,17 @@ def show():
80
 
81
  # Print multiple items
82
  print("The answer is:", 42)
83
- """, line_numbers=True)
84
 
85
  # Interactive Print Exercise
86
  st.subheader("Try it yourself!")
87
  print_code = st.text_area("Try some print statements:",
88
  'print("Hello, World!")\nname = "Python"\nprint(f"Hello, {name}!")',
89
  height=100)
 
90
  if st.button("Run Print Code"):
91
  output = capture_output(print_code)
92
- st.code(output, line_numbers=True)
93
 
94
  # Basic Arithmetic Section
95
  st.header("3. Basic Arithmetic")
@@ -114,16 +116,17 @@ def show():
114
  # Division
115
  result = 15 / 3
116
  print(result) # Prints 5.0
117
- """, line_numbers=True)
118
 
119
  # Interactive Arithmetic Exercise
120
  st.subheader("Try it yourself!")
121
  arithmetic_code = st.text_area("Try some arithmetic operations:",
122
  'print(5 + 3)\nprint(10 - 4)\nprint(6 * 7)\nprint(15 / 3)',
123
  height=100)
 
124
  if st.button("Run Arithmetic Code"):
125
  output = capture_output(arithmetic_code)
126
- st.code(output, line_numbers=True)
127
 
128
  # Lists Section
129
  st.header("4. Lists")
@@ -145,16 +148,74 @@ def show():
145
 
146
  # List length
147
  print(len(fruits)) # Prints 4
148
- """, line_numbers=True)
149
 
150
  # Interactive List Exercise
151
  st.subheader("Try it yourself!")
152
  list_code = st.text_area("Try working with lists:",
153
  'fruits = ["apple", "banana", "cherry"]\nprint(fruits[0])\nfruits.append("orange")\nprint(fruits)\nprint(len(fruits))',
154
  height=100)
 
155
  if st.button("Run List Code"):
156
  output = capture_output(list_code)
157
- st.code(output, line_numbers=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
 
159
  # Practice Exercise
160
  st.header("Practice Exercise")
@@ -171,32 +232,125 @@ def show():
171
  practice_code = st.text_area("Write your solution here:",
172
  'import math\n\nnumbers = [4, 9, 16, 25]\n\nfor num in numbers:\n print(f"Number: {num}, Square root: {math.sqrt(num)}")',
173
  height=150)
 
174
  if st.button("Run Practice Code"):
175
  output = capture_output(practice_code)
176
- st.code(output, line_numbers=True)
177
 
178
  st.markdown("""
179
  ## Part 2: Data Cleaning Lab
180
 
181
  In this lab, we'll learn how to clean and prepare data using pandas. We'll work with the Advertising dataset and practice common data cleaning techniques.
182
 
183
- This lab is hosted in a Jupyter notebook environment. We will create a new notebook for this lab.
184
  """)
185
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
  st.markdown("""
188
  ## Week 2: Reference Material
189
 
190
  Please refer to the following links:
191
- - [Pandas Documentation](https://pandas.pydata.org/docs/)
192
- - [Numpy Documentation](https://numpy.org/doc/)
193
- - [Matplotlib Documentation](https://matplotlib.org/stable/users/index.html)
194
- - [Seaborn Documentation](https://seaborn.pydata.org/index.html)
195
- For learning more about python use the following link:
196
- - [Introduction to Statistical Learning](https://www.statlearning.com/resources-python)
197
- - [Learning Python notebook](https://github.com/intro-stat-learning/ISLP_labs/blob/stable/Ch02-statlearn-lab.ipynb)
198
- For our dataset used today for class:
199
- - [Advertising Dataset](https://www.statlearning.com/s/Advertising.csv)
 
200
  """)
201
 
202
  # Weekly Assignment
@@ -211,17 +365,7 @@ def show():
211
  - What is the data type of each variable?
212
  - What is the range of each variable?
213
  - What is the mean of each variable?
214
-
215
  **Due Date:** End of Week 2
216
  """)
217
-
218
- # Assignment Submission
219
- #st.subheader("Submit Your Assignment")
220
- #with st.form("assignment_form"):
221
- # script_file = st.file_uploader("Upload your Python script (.py)")
222
- # comments = st.text_area("Additional comments or questions")
223
- # if st.form_submit_button("Submit Assignment"):
224
- # if script_file is not None:
225
- # st.success("Assignment submitted successfully!")
226
- # else:
227
- # st.error("Please upload your Python script.")
 
52
 
53
  # Using a function from the library
54
  print(math.sqrt(16)) # This will print 4.0
55
+ """, language="python", line_numbers=True)
56
 
57
  # Interactive Import Exercise
58
  st.subheader("Try it yourself!")
59
  import_code = st.text_area("Try importing and using the math library:",
60
  "import math\nprint(math.sqrt(25))",
61
  height=100)
62
+ st.code(import_code, language="python", line_numbers=True)
63
  if st.button("Run Import Code"):
64
  output = capture_output(import_code)
65
+ st.code(output, language="python", line_numbers=True)
66
 
67
  # Print Statements Section
68
  st.header("2. Print Statements")
 
81
 
82
  # Print multiple items
83
  print("The answer is:", 42)
84
+ """, language="python", line_numbers=True)
85
 
86
  # Interactive Print Exercise
87
  st.subheader("Try it yourself!")
88
  print_code = st.text_area("Try some print statements:",
89
  'print("Hello, World!")\nname = "Python"\nprint(f"Hello, {name}!")',
90
  height=100)
91
+ st.code(print_code, language="python", line_numbers=True)
92
  if st.button("Run Print Code"):
93
  output = capture_output(print_code)
94
+ st.code(output, language="python", line_numbers=True)
95
 
96
  # Basic Arithmetic Section
97
  st.header("3. Basic Arithmetic")
 
116
  # Division
117
  result = 15 / 3
118
  print(result) # Prints 5.0
119
+ """, language="python", line_numbers=True)
120
 
121
  # Interactive Arithmetic Exercise
122
  st.subheader("Try it yourself!")
123
  arithmetic_code = st.text_area("Try some arithmetic operations:",
124
  'print(5 + 3)\nprint(10 - 4)\nprint(6 * 7)\nprint(15 / 3)',
125
  height=100)
126
+ st.code(arithmetic_code, language="python", line_numbers=True)
127
  if st.button("Run Arithmetic Code"):
128
  output = capture_output(arithmetic_code)
129
+ st.code(output, language="python", line_numbers=True)
130
 
131
  # Lists Section
132
  st.header("4. Lists")
 
148
 
149
  # List length
150
  print(len(fruits)) # Prints 4
151
+ """, language="python", line_numbers=True)
152
 
153
  # Interactive List Exercise
154
  st.subheader("Try it yourself!")
155
  list_code = st.text_area("Try working with lists:",
156
  'fruits = ["apple", "banana", "cherry"]\nprint(fruits[0])\nfruits.append("orange")\nprint(fruits)\nprint(len(fruits))',
157
  height=100)
158
+ st.code(list_code, language="python", line_numbers=True)
159
  if st.button("Run List Code"):
160
  output = capture_output(list_code)
161
+ st.code(output, language="python", line_numbers=True)
162
+
163
+ # Loops Section
164
+ st.header("5. Loops")
165
+ st.markdown("""
166
+ Loops are used to repeat a block of code multiple times. Python has two main types of loops:
167
+ - `for` loops: Used to iterate over a sequence (like a list, string, or range)
168
+ - `while` loops: Used to repeat code while a condition is true
169
+ """)
170
+
171
+ with st.expander("For Loop Examples"):
172
+ st.code("""
173
+ # Basic for loop
174
+ fruits = ["apple", "banana", "cherry"]
175
+ for fruit in fruits:
176
+ print(fruit)
177
+
178
+ # For loop with range
179
+ for i in range(5): # Prints numbers 0 to 4
180
+ print(i)
181
+
182
+ # For loop with index
183
+ for i, fruit in enumerate(fruits):
184
+ print(f"Index {i}: {fruit}")
185
+ """, language="python", line_numbers=True)
186
+
187
+ with st.expander("While Loop Examples"):
188
+ st.code("""
189
+ # Basic while loop
190
+ count = 0
191
+ while count < 5:
192
+ print(count)
193
+ count += 1
194
+
195
+ # While loop with break
196
+ while True:
197
+ user_input = input("Enter 'quit' to exit: ")
198
+ if user_input == 'quit':
199
+ break
200
+ print(f"You entered: {user_input}")
201
+ """, language="python", line_numbers=True)
202
+
203
+ # Interactive Loop Exercise
204
+ st.subheader("Try it yourself!")
205
+ st.markdown("""
206
+ Try these exercises:
207
+ 1. Create a for loop that prints numbers from 1 to 10
208
+ 2. Create a while loop that counts down from 5 to 1
209
+ 3. Use a for loop to print each letter in your name
210
+ """)
211
+
212
+ loop_code = st.text_area("Write your loop code here:",
213
+ '# Exercise 1\nfor i in range(1, 11):\n print(i)\n\n# Exercise 2\ncount = 5\nwhile count > 0:\n print(count)\n count -= 1\n\n# Exercise 3\nname = "Python"\nfor letter in name:\n print(letter)',
214
+ height=150)
215
+ st.code(loop_code, language="python", line_numbers=True)
216
+ if st.button("Run Loop Code"):
217
+ output = capture_output(loop_code)
218
+ st.code(output, language="python", line_numbers=True)
219
 
220
  # Practice Exercise
221
  st.header("Practice Exercise")
 
232
  practice_code = st.text_area("Write your solution here:",
233
  'import math\n\nnumbers = [4, 9, 16, 25]\n\nfor num in numbers:\n print(f"Number: {num}, Square root: {math.sqrt(num)}")',
234
  height=150)
235
+ st.code(practice_code, language="python", line_numbers=True)
236
  if st.button("Run Practice Code"):
237
  output = capture_output(practice_code)
238
+ st.code(output, language="python", line_numbers=True)
239
 
240
  st.markdown("""
241
  ## Part 2: Data Cleaning Lab
242
 
243
  In this lab, we'll learn how to clean and prepare data using pandas. We'll work with the Advertising dataset and practice common data cleaning techniques.
244
 
245
+ Let's start with some basic examples of working with data in pandas:
246
  """)
247
 
248
+ # Example 1: Reading CSV from URL
249
+ st.header("Example 1: Reading CSV from URL")
250
+ st.markdown("""
251
+ There are several ways to read a CSV file from a URL using pandas. Here are some examples:
252
+ """)
253
+
254
+ with st.expander("Method 1: Using pandas.read_csv()"):
255
+ st.code("""
256
+ import pandas as pd
257
+
258
+ # Method 1: Direct URL
259
+ url = "https://www.statlearning.com/s/Advertising.csv"
260
+ df = pd.read_csv(url)
261
+ print(df.head())
262
+ """, line_numbers=True)
263
+
264
+ with st.expander("Method 2: Using requests and StringIO"):
265
+ st.code("""
266
+ import pandas as pd
267
+ import requests
268
+ from io import StringIO
269
+
270
+ # Method 2: Using requests
271
+ url = "https://www.statlearning.com/s/Advertising.csv"
272
+ response = requests.get(url)
273
+ data = StringIO(response.text)
274
+ df = pd.read_csv(data)
275
+ print(df.head())
276
+ """, line_numbers=True)
277
+
278
+ # Example 2: Answering Questions about the Dataset
279
+ st.header("Example 2: Answering Questions about the Dataset")
280
+ st.markdown("""
281
+ Once we have loaded our data, we can answer various questions about it. Here are some common questions and how to answer them:
282
+ """)
283
+
284
+ with st.expander("Question 1: How many rows and columns are in the dataset?"):
285
+ st.code("""
286
+ # Get the shape of the dataframe
287
+ print(f"Number of rows: {df.shape[0]}")
288
+ print(f"Number of columns: {df.shape[1]}")
289
+ """, line_numbers=True)
290
+
291
+ with st.expander("Question 2: What are the column names and data types?"):
292
+ st.code("""
293
+ # Get column names
294
+ print("Column names:")
295
+ print(df.columns.tolist())
296
+
297
+ # Get data types
298
+ print("\nData types:")
299
+ print(df.dtypes)
300
+ """, line_numbers=True)
301
+
302
+ with st.expander("Question 3: What are the basic statistics of numerical columns?"):
303
+ st.code("""
304
+ # Get descriptive statistics
305
+ print(df.describe())
306
+ """, line_numbers=True)
307
+
308
+ with st.expander("Question 4: Are there any missing values?"):
309
+ st.code("""
310
+ # Check for missing values
311
+ print("Missing values per column:")
312
+ print(df.isnull().sum())
313
+ """, line_numbers=True)
314
+
315
+ with st.expander("Question 5: What are the unique values in categorical columns?"):
316
+ st.code("""
317
+ # For each column, print unique values
318
+ for column in df.select_dtypes(include=['object']).columns:
319
+ print(f"\nUnique values in {column}:")
320
+ print(df[column].unique())
321
+ """, line_numbers=True)
322
+
323
+ # Interactive Exercise
324
+ st.header("Try it yourself!")
325
+ st.markdown("""
326
+ Now it's your turn to try these examples. Use the code editor below to:
327
+ 1. Load the Advertising dataset from the URL
328
+ 2. Answer the questions above about the dataset
329
+ """)
330
+
331
+ # Code editor for interactive exercise
332
+ exercise_code = st.text_area("Write your code here:",
333
+ 'import pandas as pd\n\n# Your code here',
334
+ height=200)
335
+
336
+ if st.button("Run Code"):
337
+ output = capture_output(exercise_code)
338
+ st.code(output, line_numbers=True)
339
 
340
  st.markdown("""
341
  ## Week 2: Reference Material
342
 
343
  Please refer to the following links:
344
+ - Library Documentation
345
+ - [Pandas Documentation](https://pandas.pydata.org/docs/)
346
+ - [Numpy Documentation](https://numpy.org/doc/)
347
+ - [Matplotlib Documentation](https://matplotlib.org/stable/users/index.html)
348
+ - [Seaborn Documentation](https://seaborn.pydata.org/index.html)
349
+ - Learning Python
350
+ - [Introduction to Statistical Learning](https://www.statlearning.com/resources-python)
351
+ - [Learning Python notebook](https://github.com/intro-stat-learning/ISLP_labs/blob/stable/Ch02-statlearn-lab.ipynb)
352
+ For our dataset used today for class:
353
+ - [Advertising Dataset](https://www.statlearning.com/s/Advertising.csv)
354
  """)
355
 
356
  # Weekly Assignment
 
365
  - What is the data type of each variable?
366
  - What is the range of each variable?
367
  - What is the mean of each variable?
368
+ 4. Think about what research question you want to answer with this dataset.
369
  **Due Date:** End of Week 2
370
  """)
371
+