Review
- Code Comments
- Printing to the Console
- Sketch height and width
- Utility functions
- Random Numbers
- Drawing Text
- Drawing Arcs
- Custom Shapes
- Loading and Drawing Images
- Examples
- Assignment #1
- Generated random numbers:
- Inclusive lower bound
- Exclusive upper bound
Identifiers
- Identifiers (variables) are names assigned to Python "things"
- Python "things" include functions, numbers, ...
- Identifiers must start with a letter (a..zA..Z) or an underscore ( _ )
- ... which can be followed by any number of letters, digits (0 .. 9) or underscores
- Identifiers are case-sensitive
- Blah and blah may refer to different things
Identifiers in Python - Examples
pi = 3.1215926
bestWord = "Antidisestablishmentarianism"
all2s = 2*2*2*2*2*2*2*2
_result = True
Reserved Words
Python has several reserved words. These cannot be used as variables.
and | as | assert | break | class | continue |
def | del | elif | else | except | exec |
finally | for | from | global | if | import |
in | is | lambda | not | or | pass |
print | raise | return | try | while | with |
yield | True | False | None | | |
Assignment
Variables are created by assigning a value using the '=' operator.
Variables on the left, value or expression on the right.
The '=' operator is not the standard mathematical statement of equivalence.
pi = 3.1215926
all2s = 2*2*2*2*2*2*2*2
a, b, c = 1, 2, 3
Types
All "things" in Python have a type
print( type( 3 ) ) # <type 'int'>
print( type( 3.14 ) ) # <type 'float'>
print( type( 3. ) ) # <type 'float'>
print( type( "iguana" ) ) # <type 'str'>
print( type( True ) ) # <type 'bool'>
print( type( print ) )
# <type 'builtin_function_or_method'>
If all Python "things" have types, what's the type of a type?
Values and Types
The type of a value is determined by its syntax.
- <type 'int'>
- Integer
- A sequence of digits with no decimal
12345, 0, -987
- <type 'float'>
- Floating Point Number
- A sequence of digits with decimal
123.45, 0.1, 4., 0., -3.14
Values and Types
The type of a value is determined by its syntax.
- <type 'str'>
- String of Characters
- A sequence of characters delimited by a pair of matched single or double quotes
"hello, world", 'hello, world'
"12345", "x = 10", ""
- <type 'bool'>
- A Boolean Value
True, False
Type Conversion
Built-in type conversion functions allow you to convert between types.
Type conversion function names are identical to type names.
Expressions
- Values can be combined into expressions using operators and functions.
- Expressions are evaluated using standard mathematical rules.
python>>> 255/2
127.5
python>>> 10 + 10
20
python>>> '10' + '10'
'1010'
Operators
Symbols that operate on one or two sub-expressions.
Infix, prefix, or postfix
- Mathematical ( +, -, *, /, **, //, %, ... )
- Perform standard mathematical operations.
- Relational ( <, >, ==, !=, <=, >=, ... )
- Test relationship between related expressions.
- Always returns a boolean value (True or False).
- Logical ( and, or, not )
- Logical conjunction (and), disjunction (or), negation (not).
- Always returns a boolean value (True or False).
Mathematical Operators
+, -, *, /, ...
1 + 2
(y2 - y1) / (x2 - x1)
P | : Parentheses |
E | : Exponentiation |
M | : Multiplication |
D | : Division |
A | : Addition |
S | : Subtraction |
Mathematical Operators
PEMDAS
1 + 2 # 3
1 + 2 * 3 # 7 (not 9)
(1 + 2) * 3 # 9
10 - 2 / 8 # 9.75
(10 - 2) / 8 # 1.0
3 * ' la' + 'nd' # ' la la land'
3 * (' la' + 'nd') # ' land land land'
Relational Operators
Expressions with relational operators return booleans
< | less than |
> | is greater than |
<= | is less than or equal to |
>= | is greater than or equal to |
== | is equivalent |
!= | is not equivalent |
True # True
10 >= 10 # False
"A" == "A" # True
Logical Operators
Expressions with logical operators return booleans
- 'and' operator is for logical conjunction
- 'or' operator is for logical disjunction
- 'not' operator is for logical negation
(2 > 1) and (3 > 2) # True
(2 > 1) and (3 > 3) # False
(2 > 1) or (3 > 3) # True
(3 < 2) # False
not (3 < 2) # True
Using Python Modules
import math
math.sin(0.5*math.pi) # 1.0
- Like putting a bag of math functions into your backpack.
- To access any math function, you must first open the 'math' bag in your backpack using dot-notation.
|
from math import *
sin(0.5*pi) # 1.0
- Like dumping the contents of a bag of math functions into your backpack.
- All math functions are right there in your own backpack - no dot-notation required.
|
from math import sin
cos(0.5*pi) # Error
- Like putting only the sin function into your backpack.
- The cos() function is not available.
|
Exploring loaded Modules
- Use the dir() function
- dir() lists everything in the top level namespace
- dir(math) lists everything in the loaded math module
dir()
from Processing import *
dir()
dir()
import Processing
dir()
dir(Processing)
Problem Solving with Variables, Expressions
Design a program that satisfies the following requirements:
- Create a window that is 600 pixels wide and 300 pixels high
- Draw two blue circles on the window connected by a black line
- The diameter of each circle must be 1/10 of the shorter window dimension
- The distance between the circles must be 1/2 of the shorter window dimension
- The center of the line should be at the center of the window
- The line should be at an angle of 45 degrees with the horizontal
Problem Solving with Variables, Expressions
- Use variables as parameters for key values
- Assign parameters at the star of your program so modifications can be localized
- Perform key calculations with parameters so parameter changes impact all dependent parts of your program
- Pattern: Sequential Statement Execution
- Define parameters
- Compute intermediate values dependent upon parameter values
- Perform final action
←
→
/
#