RM

Lec 4

MTH 280 Exam Preparation Notes

Overview

A review of core programming concepts in MTH 280, important for mastering programming fundamentals. Topics covered include:

  • Complex Code

  • If Statements

  • Lists

  • For Loops

Labs and Review Topics

Labs Covered

  • Lab 1: Introduction to Python and basic syntax

  • Lab 2: Conditional statements with hints and feedback

  • Lab 3: Using lists and loops in Python

Main Topics for Review

  • 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)

Lab Feedback

Instructions for Accessing Feedback and Grades:

  1. Go to the Bitbucket project page.

  2. Click on "wiki" in the left-hand menu.

  3. The wiki provides feedback, grades, and additional information.

  4. The class Bitbucket repository contains solutions to previous labs.

Formatted Printing

Control Over Numerical Values in Output

Here are some questions to experiment with:

  • How many digits should be displayed?

  • Should values be shown in scientific notation?

Best Practices

  • Experiment in IPython for better understanding.

Print Examples

Basic Formatting

  • Example Code:

grade = 88.6023423232
print("my grade %f is pretty good" % grade)
# Output: my grade 88.602342 is pretty good

Improved Formatting

  • Example using two decimal points:

print("my grade %.2f is nicely formatted" % grade)
# Output: my grade 88.60 is nicely formatted

Scientific Notation

Using scientific notation for representing large or small numbers:

  • Example Code:

print("my grade %e in scientific notation" % grade)

Python Script Example for Final Grade Calculation

Inputs: Homework, Lab, Exam 1, Exam 2

  • 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

Euler's Formula Example

Computational Example Using 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))

Multi-line Comments

Syntax for Multi-line Comments:

  • Example:

"""
This is a multi-line comment.
On homeworks, your short answers can go here.
"""

Strategy Outline

Key Strategies for Coding:

  • 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.

Simulation of Code

Hand Simulating Code to Understand Logic:

  • 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

Understanding Error Messages

Common Python Error Messages:

  • SyntaxError: Mistakes in code structure.

  • NameError: Using a variable before it's declared.

  • TypeError: Operations applied to incompatible types.

Importance of Reading Error Messages:

To make debugging effective.

Debugging Techniques

Helpful Debugging Strategies:

  • Print intermediate values to understand code flow.

  • Analyze variable states at critical points.

  • Effective logging of outputs for debugging.

Review of Key Concepts

Focus on Repetitive Topics:

  • Complex Code

  • If Statements

  • Lists

  • For Loops

Branching in Programming

Introduction to If-Then Statements:

  • Code structure guiding logic flow.

Syntax of If Statements:

Make sure to follow correct indentation:

if condition:
    # execute code