Outline for January 9
Reading: Wentworth et al., §2
Due: Homework 1, due on January 18 at 11:55pm
- Variables
- What they are
- Variable names
- Variable types (int, float, string)
- Function type()
- Python keywords
- Variables
- Legal names
- Keywords
- How to pick variable names
- Simple assignment: variable = expression
- Expressions
- Operators +, -, *, /, //, %, **
- Precedence
- Parentheses for grouping ((, ))
- Exponentiation (**); associates right to left
- Positive, negative (unary +, -)
- Multiplication, division, integer division, remainder (*, /, //, %)
- Addition, subtraction (binary +, -)
- In general, operators of equal precedence are evaluated from the left to the right (associativity); exception noted above
- Example
- Compute the hypotenuse of a right triangle [hypotnoex.py]
- The difference between strings and integers [twoplustwo.py]
- Difference between '2 + 2', 2 + 2
- Print statements usually end lines
- Getting print statements not to end lines
- Type converter functions int, float, str
- String operators
- Operator +: string concatenation
- Operator *: repetition
- Input: input statement
- input(prompt) prints prompt, waits for user
- When user hits enter, it returns what was typed as a string
- Exceptions
- Division by zero [divby0.py]
- Typing letters when a number is expected [hypotex.py]
- Writing a program: rice on chessboard [ricechess.py]