Lecture 2: October 1, 2019
Reading
: §2
Due
: Homework 1, due on October 10 at 11:59pm
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
]
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email:
[email protected]
MHI 289I, Programming in Health Informatics
Version of September 30, 2019 at 10:17PM
You can also obtain a PDF version of this.