Ch02 Expressions
Ch02 Expressions
Statements
Chapter 2
x = 12.2 x 12.2
y = 14
y 14
Variables
• A variable is a named place in the memory where a programmer can store
data and later retrieve the data using the variable “name”
• Case Sensitive
http://en.wikipedia.org/wiki/Mnemonic
x1q3z9ocd = 35.0
x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
print(x1q3p9afd)
hours = 35.0
What are these bits rate = 12.50
of code doing? pay = hours * rate
print(pay)
Sentences or Lines
x = 2 Assignment statement
x = x + 2 Assignment with expression
print(x) Print statement
x = 3.9 * x * ( 1 - x )
A variable is a memory location x 0.6
used to store a value (0.6)
0.6 0.6
x = 3.9 * x * ( 1 - x )
0.4
0.4
The right side is an expression. Once the
expression is evaluated, the result is
placed in (assigned to) the variable on the
0.936
left side (i.e., x).
Expressions…
Numeric Expressions
Operator Operation
• Because of the lack of mathematical
symbols on computer keyboards - we + Addition
use “computer-speak” to express the - Subtraction
classic math operations
* Multiplication
• Asterisk is multiplication / Division
% Remainder
Numeric Expressions
>>> xx = 2 >>> jj = 23
>>> xx = xx + 2 >>> kk = jj % 5 Operator Operation
>>> print(xx) >>> print(kk)
+ Addition
4 3
>>> print(4 ** 3) - Subtraction
>>> yy = 440 * 12
>>> print(yy) 64 * Multiplication
5280
>>> zz = yy / 1000 4R3 / Division
Integer
>>> print(zz) 5 23 //
Division
5.28 20 ** Power
>>> rr = yy // 1000
5 3 % Remainder
Order of Evaluation
• When we string operators together - Python must know which one
to do first
x = 1 + 2 * 3 - 4 / 5 ** 6
Operator Precedence Rules
Highest precedence rule to lowest precedence rule:
• Left to right
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x)
11.0 1 + 8 / 4 * 5
>>>
1 + 2 * 5
Parenthesis
Power
Multiplication 1 + 10
Addition
Left to Right
11
Operator Precedence Parenthesis
Power
• Remember the rules top to bottom Multiplication
Addition
• When writing code - use parentheses Left to Right
Y
What Does “Type” Mean?
• In Python variables, literals, and
constants have a “type” >>> ddd = 1 + 4
>>> print(ddd)
• Python knows the difference between 5
an integer number and a string >>> eee = 'hello ' + 'there'
>>> print(eee)
hello there
• For example “+” means “addition” if
something is a number and
“concatenate” if something is a string
concatenate = put together
Type Matters
>>> eee = 'hello ' + 'there'
• Python knows what “type” >>> eee = eee + 1
everything is Traceback (most recent call last):
File "<stdin>", line 1, in
• Some operations are <module>
TypeError: can only concatenate
prohibited
str (not "int") to str
>>> type(eee)
• You cannot “add 1” to a string <class'str'>
>>> type('hello')
• We can ask Python what type <class'str'>
something is by using the >>> type(1)
<class'int'>
type() function >>>
Several Types of Numbers
>>> xx = 1
• Numbers have two main types
>>> type (xx)
<class 'int'>
- Integers are whole numbers:
>>> temp = 98.6
-14, -2, 0, 1, 100, 401233
>>> type(temp)
<class'float'>
- Floating Point Numbers have
>>> type(1)
decimal parts: -2.5 , 0.0, 98.6, 14.0
<class 'int'>
• There are other number types - they >>> type(1.0)
<class'float'>
are variations on float and integer
>>>
Type Conversions # integer and floating point in an expression
>>> print(float(99) + 100)
199.0
# convert integer into float
• When you put an integer and >>> i = 42
>>> type(i)
floating point in an
expression, the integer is <class'int’>
>>> f = float(i)
implicitly converted to a float
>>> print(f)
Conversions
>>> print(sval + 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str
0 (GF) 1 (GF)
1 2
2 3
• Why comment?
# All done
print(bigword, bigcount)
Summary
• Type • Integer Division
• Operator precedence
Exercise2
Write a program to prompt the user for hours and rate per
hour to compute gross pay.
Enter Hours: 35
Enter Rate: 2.75
Pay: 96.25
Exercise1 solution
Y = (a * x**2 + b * x + c) // (d - e * x)
Exercise2 solution
Acknowledgements / Contributions