Lecture 1b

There are three types of errors: syntax errors, exceptions and semantic/logical errors.

Syntax refers to the rules that define how a program should be written and interpreted.

A syntax error occurs when the code violates these rules, similar to a spelling or grammar mistake in English.

Examples of syntax errors include missing parentheses or incorrect spacing.

Exceptions are errors that are detected during the execution of a program, even if the code is syntactically correct.

They arise when a statement, despite being grammatically correct, cannot be executed.

An example is trying to convert a non-numerical string like 'abc' into an integer using the int() function.

Semantic or logical errors occur when the code runs without any error messages but does not produce the expected output.

For instance, attempting to add the numbers 12 and 3 may result in '123' instead of 15 if the code treats them as strings instead of integers.

Assignment and Variables in Python

An assignment statement links a variable name to an object. It uses the format: <variable> = <object>.

The variable is on the left side of the equals sign (=), and the object is on the right side.

A variable acts as a label or reference to an object, allowing you to retrieve and use that object later in the program.

The type of a variable matches the type of the object it is associated with.

Python is dynamically-typed, meaning the type of a variable can change during the execution of the program.

However, it is generally good practice to avoid changing a variable's type to something drastically different, such as switching from a number to a string, unless there is a clear reason.

The assignment operator (=) does not represent equality in the mathematical sense.

Expressions can be used on the right side of an assignment statement: <variable> = <expression>.

The expression is evaluated first, resulting in an object, and then the variable is linked to that resulting object.

When you assign one variable to another (e.g., x2 = x), both variables point to the same object in memory.

Therefore, changing the value of one variable will also affect the other.

Rules for Variable Names in Python

Variable names can include letters (a-z, A-Z), digits (0-9) and underscores (_).

However, they cannot begin with a digit or use reserved keywords like "if", "while" and "for".

Variable names are case-sensitive, meaning "pi" and "Pi" are treated as different variables.

Descriptive variable names are important for code readability.

Avoid using generic names like "x" and "y" unless they are genuinely descriptive in the context.

This course recommends using lowercase letters connected by underscores (snake_case) for variables (e.g. num_students) and uppercase letters connected by underscores (screaming snake case) for constants (e.g. INCHES_IN_FOOT, PI).

Constants represent values that do not change.

The main textbook uses a different naming convention (numStudent), which should not be used in this course.

Readability

Readability is crucial in programming as code is often read and modified by multiple people.

Clear variable names and comments enhance readability.

Comments are ignored by Python but provide explanations for humans. They are denoted by # and anything following the # on that line is ignored.