02 Exercise: PEP 8 Before/After Analysis
Instructions
This quiz tests your ability to analyze PEP 8 improvements in real Python code. You will compare “Before” (non-compliant) and “After” (compliant) versions of code snippets and identify the specific differences, rules applied, and reasoning behind each change.
Topics covered:
- Spacing around operators and after commas
- Import organization (grouping, sorting, unused imports)
- Naming conventions (PascalCase, snake_case, UPPER_CASE)
- Compound statements vs. separate lines
- Multi-line formatting for function calls
- Git diff implications of code formatting
Tip: Read both code versions carefully before answering. Small differences like a single space or a line break count as distinct improvements.
How many PEP 8 improvements were made between the Before and After versions?
Before:
def calc(a,b):return a+b
After:
def calc(a, b):
return a + b
Enter a number:
Which PEP 8 rules were applied in the After version?
Before:
def calc(a,b):return a+b
After:
def calc(a, b):
return a + b
Select ALL that apply:
Which PEP 8 rules were applied in the After version of these imports?
Before:
import sys,os,json
from typing import List,Dict
import numpy as np
After:
import json
import os
import sys
from typing import Dict, List
import numpy as np
Select ALL that apply:
Why is the blank line between import groups important?
import json
import os
import sys
from typing import Dict, List
# <-- this blank line
import numpy as np
How many PEP 8 improvements were made between the Before and After versions?
Before:
class dataProcessor:
def __init__(self,items):
self.items=items
self.count=len(items)
def Process(self): return [x*2 for x in self.items]
After:
class DataProcessor:
def __init__(self, items):
self.items = items
self.count = len(items)
def process(self):
return [x * 2 for x in self.items]
Enter a number:
Which PEP 8 naming convention rules were applied in the After version?
Before:
class dataProcessor:
...
def Process(self): return [x*2 for x in self.items]
After:
class DataProcessor:
...
def process(self):
return [x * 2 for x in self.items]
Select ALL that apply:
Which PEP 8 rules were applied in the After version?
Before:
camera={'x':0,'y':2.0,'z':1.5}
max_distance=100
default_threshold=0.5
After:
camera = {'x': 0, 'y': 2.0, 'z': 1.5}
MAX_DISTANCE = 100
DEFAULT_THRESHOLD = 0.5
Select ALL that apply:
Why were max_distance and default_threshold renamed to MAX_DISTANCE and DEFAULT_THRESHOLD?
How many PEP 8 improvements were made between the Before and After versions?
Before:
def process(data,threshold=10,debug=False):
result=[]
for item in data:
if item>threshold:result.append(item*2)
if debug:print(f"Processed {len(result)} items")
return result
After:
def process(data, threshold=10, debug=False):
result = []
for item in data:
if item > threshold:
result.append(item * 2)
if debug:
print(f"Processed {len(result)} items")
return result
Enter a number:
Why is breaking compound statements into separate lines important for debugging?
Before (compound):
if item>threshold:result.append(item*2)
After (separate lines):
if item > threshold:
result.append(item * 2)
How many PEP 8 improvements were made between the Before and After versions?
Before:
from dash import Dash,html,dcc
import plotly.graph_objects as go
import numpy as np
import sys
def create_visualization(x_data,y_data,title="Plot"):
fig=go.Figure()
fig.add_trace(go.Scatter(x=x_data,y=y_data,mode='lines',name='Data'))
fig.update_layout(title=title,xaxis_title="X",yaxis_title="Y")
return fig
APP_title="Road Profile Viewer"
After:
from dash import Dash, dcc, html
import numpy as np
import plotly.graph_objects as go
def create_visualization(x_data, y_data, title="Plot"):
fig = go.Figure()
fig.add_trace(go.Scatter(
x=x_data,
y=y_data,
mode='lines',
name='Data'
))
fig.update_layout(
title=title,
xaxis_title="X",
yaxis_title="Y"
)
return fig
APP_TITLE = "Road Profile Viewer"
Enter a number:
Which categories of PEP 8 rules were applied in the real-world example?
Before:
from dash import Dash,html,dcc
import plotly.graph_objects as go
import numpy as np
import sys
def create_visualization(x_data,y_data,title="Plot"):
fig=go.Figure()
fig.add_trace(go.Scatter(x=x_data,y=y_data,mode='lines',name='Data'))
fig.update_layout(title=title,xaxis_title="X",yaxis_title="Y")
return fig
APP_title="Road Profile Viewer"
Select ALL categories that apply:
Why is multi-line formatting preferred for function calls with many arguments?
Before (single line):
fig.add_trace(go.Scatter(x=x_data,y=y_data,mode='lines',name='Data'))
After (multi-line):
fig.add_trace(go.Scatter(
x=x_data,
y=y_data,
mode='lines',
name='Data'
))
A teammate changes mode='lines' to mode='markers' in a function call. Which formatting style produces a cleaner git diff?
Style A (single-line):
fig.add_trace(go.Scatter(x=x_data, y=y_data, mode='lines', name='Data'))
Style B (multi-line):
fig.add_trace(go.Scatter(
x=x_data,
y=y_data,
mode='lines',
name='Data'
))