A review of core programming concepts in MTH 280, important for mastering programming fundamentals. Topics covered include:
Complex Code
If Statements
Lists
For Loops
Lab 1: Introduction to Python and basic syntax
Lab 2: Conditional statements with hints and feedback
Lab 3: Using lists and loops in Python
Complex Code: Understanding nested loops and conditional statements.
If Statements: Proper syntax and logic flow control.
Example Code:
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
else:
print("Grade: C")
Lists: How to create, modify and access elements in lists.
Example Code:
my_list = [1, 2, 3, 4]
my_list.append(5) # Adding element
print(my_list[0]) # Access first element
For Loops: Iterating over lists and performing operations.
Example Code:
for i in my_list:
print(i)
Go to the Bitbucket project page.
Click on "wiki" in the left-hand menu.
The wiki provides feedback, grades, and additional information.
The class Bitbucket repository contains solutions to previous labs.
Here are some questions to experiment with:
How many digits should be displayed?
Should values be shown in scientific notation?
Experiment in IPython for better understanding.
Example Code:
grade = 88.6023423232
print("my grade %f is pretty good" % grade)
# Output: my grade 88.602342 is pretty good
Example using two decimal points:
print("my grade %.2f is nicely formatted" % grade)
# Output: my grade 88.60 is nicely formatted
Using scientific notation for representing large or small numbers:
Example Code:
print("my grade %e in scientific notation" % grade)
Example Calculation:
hw = 90
lab = 85
exam1 = 80
exam2 = 95
grade = 0.4 * hw + 0.15 * lab + 0.2 * exam1 + 0.25 * exam2
print("My final grade is %.2f" % grade)
# Output: My final grade is 89.15
cmath
:Incrementing theta using a while
loop:
import cmath
theta = 0.0
while theta <= 6.0:
lhs = cmath.exp(theta * 1.0j)
rhs = cmath.cos(theta) + 1.0j * cmath.sin(theta)
diff = abs(lhs - rhs)
print("theta = %.1f, |LHS - RHS| = %.1e" % (theta, diff))
Example:
"""
This is a multi-line comment.
On homeworks, your short answers can go here.
"""
Use comments effectively to clarify code significance.
Maintain clear variable names.
Follow the Zen of Python: "Simple is better than complex."
Always read function documentation to avoid potential errors.
Show step-by-step how variables change with each iteration, using examples such as:
answer = 0
for i in range(1, 11):
answer += i
print(answer) # Outputs the sum of 1 to 10
SyntaxError: Mistakes in code structure.
NameError: Using a variable before it's declared.
TypeError: Operations applied to incompatible types.
To make debugging effective.
Print intermediate values to understand code flow.
Analyze variable states at critical points.
Effective logging of outputs for debugging.
Complex Code
If Statements
Lists
For Loops
Code structure guiding logic flow.
Make sure to follow correct indentation:
if condition:
# execute code