Outline for January 9
Reading
: §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
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: compute pay with overtime
Matt Bishop
Department of Computer Science
University of California at Davis
Davis, CA 95616-8562 USA
Last modified: Version of January 9, 2019 at 5:10PM
Winter Quarter 2019
You can get a PDF version of this