Review

Identifiers

Identifiers in Python - Examples

pi = 3.1215926
bestWord = "Antidisestablishmentarianism"
all2s = 2*2*2*2*2*2*2*2
_result = True

Reserved Words

Reserved Words

Python has several reserved words. These cannot be used as variables.

andasassertbreakclasscontinue
defdelelifelseexceptexec
finallyforfromglobalifimport
inislambdanotorpass
printraisereturntrywhilewith
yieldTrueFalseNone  

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.

Values and Types

The type of a value is determined by its syntax.

Type Conversion

Built-in type conversion functions allow you to convert between types.

Type conversion function names are identical to type names.

Expressions

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 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

(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

Python Modules

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

dir()
from Processing import *
dir()
dir()
import Processing
dir()
dir(Processing)

Problem Solving with Variables, Expressions

Design a program that satisfies the following requirements:

  1. Create a window that is 600 pixels wide and 300 pixels high
  2. Draw two blue circles on the window connected by a black line
  3. The diameter of each circle must be 1/10 of the shorter window dimension
  4. The distance between the circles must be 1/2 of the shorter window dimension
  5. The center of the line should be at the center of the window
  6. The line should be at an angle of 45 degrees with the horizontal

Problem Solving with Variables, Expressions

/

#