9/5/25 Data Types, Operators, and Errors
Data Types
- Variables: Locations in memory where values can be stored
- Data types:
- Integers: whole numbers
- String: sequence of characters
- Double / float: floating point numbers
- Variable naming
- Letters, digits and underscores
- Start with a letter or underscore
- Cannot have spaces in the name
- Cannot be Python keywords (e.g., in, for, else, class, …)
- Invalid variable names shown in slides:
- 1varible # cannot start with a number
- my-variable # hyphen not allowed
- my variable # space not allowed
Variable Naming
- Rules summary:
- Start with a letter or underscore
- Follow with letters, digits, or underscores
- No spaces
- Not a Python keyword
- Examples (illustrative):
- valid: name, _temp, base1, total2
- invalid: 1varible, my-variable, my variable
Variable Assignment
- Syntax: LHS = RHS
- Requirements:
- LHS must be a single variable
- RHS can be a value, a variable, or an expression
- Examples:
- name = "Kevin"
- x = 21 # value
- y = 6 # value
- z = x # value from another variable
- x = 3+45-62+1 # expression
- z = x + 5 # expression
- z = x + y # expression
- Invalid assignments (illustrative):
- a + b = 10 # invalid: LHS must be a single variable
- x + 1 = 30 # invalid: LHS must be a single variable
Compound Assignment Statements
- Shorthand forms (same as expanded form):
- x += n # x = x + n
- x -= n # x = x - n
- x *= n # x = x * n
- x /= n # x = x / n
- x %= n # x = x % n
- x //= n # x = x // n
Mathematical Operations
- Basic operators:
- Addition (+): 15 + 3 = 18
- Subtraction (-): 15 - 3 = 12
- Multiplication (*): 15 * 3 = 45
- Division (/): 15 / 3 = 5
- Other operators:
- Integer Division (//): 5 // 3 = 1
- Modulus (%): 5 \% 3 = 2
- Exponentiation (**): 5 ** 3 = 125
Type Conversions
- Implicit Type Conversion
- Converts between data types during an operation
- No explicit instruction from the programmer
- Examples:
- 3 * 4 = 12 (int * int → int)
- 1.5 * 1.5 = 2.25 (float * float → float)
- 3 * 3.2 = 6.4 (int * float → float)
- 3.2 / 2 = 1.6 (float / int → float)
- Note: The interpreter automatically chooses the appropriate type
- Important caveat (Python 3): the statement on the slide for the last example says “float / int → int,” but in Python 3, division yields a float. The intended idea is that the operation may promote to a float as needed.
Operator Precedence
- Rules decide which operations to perform first, similar to math rules
- Precedence order (high to low):
- Parentheses
- Exponentiation
- Multiplication, division, floor division, modulus
- Addition, subtraction
- Comparison
- Logical operations
Precedence Example
- Consider: 5 + 2 * 3 ** 2
- Steps:
- Exponent first: 3^2 = 9
- Multiplication: 2 * 9 = 18
- Addition: 5 + 18 = 23
- Tip: Use parentheses to make order explicit
- Precedence order (summary):
- Parentheses → Exponentiation → Multiplication, division, floor, modulus → Addition, subtraction → Comparison → Logical operations
Activity 5: Python Operations
- Task: Write a Python program that computes the area of a triangle
- Use variables to store base and height
- Program should print base, height and area
- Area formula: \text{Area} = 0.5 \times \text{base} \times \text{height}
- Extensions:
- Prompt user for base and height and perform the computation
- Submission: code and screenshots of execution on Piazza; Improvements as a reply to your original comment
Errors in Python
- In programming, errors fall into three categories:
- Syntax errors
- Runtime errors
- Logic errors
- Key perspective: Errors are clues to fix your code; they are not failures.
Syntax Errors
- Occurs when code violates Python language rules
- The interpreter cannot run the program
- Example message (illustrative):
- print("Hello"
- File "/Users/CS115/temp.py", line 1
- print("Hello"
- ^ SyntaxError: '(' was never closed
- Strategy: Read the error; it provides hints about what went wrong
Runtime Errors
- Occurs when the program syntax is correct, but an operation is impossible
- Examples of problematic operations: division by zero, or illegal operations like attempting to multiply two strings
- Program may terminate abruptly
- Example code:
- a = 10
- b = 0
- c = a / b
- print(a)
- print(b)
- ZeroDivisionError: division by zero
Logic Errors
- Code runs without crashing but produces incorrect results
- Hardest to detect because no error message is shown
- Example:
- student1 = 80
- student2 = 90
- average = (student1 + student2) / 3
- print(average) # Output 56.666666666666664
- Wrong formula for average. Correct version: average = (student1 + student2) / 2