Variables and Types
Variables and Types
x=3
message = ‘hello’
2
▪ More formally a variable is a named place in the
variables 3
Variables
You can change the contents of a variable in a later
statement
4
Python Variable Name Rules
▪ Must start with a letter or underscore _
▪ Must consist of letters and numbers and underscores
▪ Case Sensitive
▪ Good: spam eggs spam23 _speed
▪ Bad: 23spam #sign var.12
▪ Different: spam Spam SPAM
5
Reserved Words
You can not use reserved words as variable names / identifiers
>>> PI = 3.14
7
Literal/unnamed constant/magic number:
not given a name, the value that you see is
literally the value that you have.
8
An expression is a combination of values,
variables, and operators.
▪ 17
▪ x
=> x +17
9
Sentences or Lines
10
An assignment statement assigns a value
to a variable
x=5 x 5
x=x+1 x 6
11
<variable> = <expr>
12
Assignment Statements
▪ We assign a value to a variable using the
assignment statement (=)
▪ An assignment statement consists of an expression
on the right hand side and a variable to store the
result
x = 3.9 * x * ( 1 - x )
13
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
14
A variable is a memory location
used to store a value. The value x 0.6 0.93
stored in a variable can be updated
by replacing the old value (0.6)
with a new value (0.93).
x = 3.9 * x * ( 1 - x )
15
Numeric Expressions
Operator Operation
▪ Because of the lack of mathematical
+ Addition
symbols on computer keyboards - we
use “computer-speak” to express the - Subtraction
5280 ** Power
>>> zz = yy / 1000 % Remainder
5 23
>>> print(zz) 20
5
3 17
Order of Evaluation
▪ When we string operators together - Python must know which one
to do first
x = 1 + 2 * 3 - 4 / 5 ** 6
18
Operator Precedence Rules
▪ Highest precedence rule to lowest precedence rule
19
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x) 1+8/4*5
11
>>> 1+2*5
Parenthesis
Power 1 + 10
Multiplication
Addition 11
Left to Right
20
1 + 2 ** 3 / 4 * 5
>>> x = 1 + 2 ** 3 / 4 * 5
>>> print(x) 1+8/4*5
11
>>> Note 8/4 goes before 4*5 1+2*5
because of the left-right
rule.
Parenthesis 1 + 10
Power
Multiplication
Addition 11
Left to Right
21
Operator Precedence
▪ When writing code - use parenthesis
Parenthesis
▪ When writing code - keep mathematical Power
Multiplication
expressions simple enough that they are Addition
easy to understand Left to Right
23
Whole numbers are Numbers that can
represented using have fractional
the integer (int for parts are
short) data type. represented as
floating point (or
float) values.
24
▪ Complex numbers
▪ Long integers
▪ Doubles
25
String is a sequence of characters.
‘hello’
‘a’
‘1’
‘1 + 3’
“’”
26
▪True (1)
▪ False (0)
27
▪ In Python variables, literals, and
30
Type >>> eee = 'hello ' + 'there'
>>> eee = eee + 1
▪ Python knows what “type” Traceback (most recent call last):
File "<stdin>", line 1, in <module>
everything is
TypeError: cannot concatenate 'str'
▪ Some operations are prohibited and 'int' objects
34
x1q3z9ocd = 35.0
x1q3z9afd = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd
print x1q3p9afd
What is this
code doing?
35
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c=a*b
print x1q3p9afd print c
What is this
code doing?
36
x1q3z9ocd = 35.0 a = 35.0
x1q3z9afd = 12.50 b = 12.50
x1q3p9afd = x1q3z9ocd * x1q3z9afd c=a*b
print x1q3p9afd print c
hours = 35.0
rate = 12.50
What is this
pay = hours * rate
code doing?
print pay
37