1.
T
he
Assi
gnment Sta
tement
and
T
yp
es
To
pi
cs:
P
yt
ho
n
’
s
I
n
t
e
r
act
i
ve
Mode
Var
iab
l
e
s
E
xpre
ssi
o
n
s
A
ssi
gnme
n
t
S
tri
n
gs,
I
n
ts,
an
d
Flo
ats
T
he
Py
th
on
Intera
cti
ve
Shell
Pyth
on
c
a
n
b
e
u
s
ed
in
a
wa
y
that
r
e
m
i
nds
you
of
a
c
a
l
c
ula
tor
.
I
n the
``c
om
m
a
nd
she
l
l
of
your
s
ys
te
m
s
i
m
pl
y
type
py
t
h
o
n
a
nd
you
wi
l
l
be
m
e
t
wi
th
a
pr
om
pt…
>>>
L
et’s
Comp
u
te
th
e
A
r
ea of a
Ci
r
c
le
U
sing
P
y
thon
>>> r = 10
>>> A = 3.14*r*r
>>> print A
314.0
Prog
r
amm
i
ng
vs
M
ath
>>> r = 10
>>> A = 3.14*r*r
>>> print A
314.0
No
t
at
ion
is dif
fer
en
t
.
In
Py
t
ho
n
,
yo
u
can’
t
say
A
= 3
.
14xr
xr
Prog
r
amm
i
ng
vs
M
ath
>>> r = 10
>>> A = 3.14*r**2
>>> print A
314.0
Nota
tion
is
diff
er
ent.
In
Py
thon
y
ou
i
ndic
ate
exp
onent
iation
wi
th
*
*
Prog
r
amm
i
ng
vs
M
ath
>>> r = 10
>>> A = 3.14*r**2
>>> print A
314.0
r
a
nd
A
a
re
va
ria
b
les
.
In alg
ebra
,
w
e
h
a
v
e
the
notion of
a v
ar
ia
b
le
too. But t
here a
r
e
s
ome big dif
fer
enc
es.
Var
i
ables
A
variab
le
i
s
a
nam
ed
m
emory
lo
cat
io
n
.
Thi
nk
o
f
a
variable
as
a
b
ox
.
It
con
t
ain
s
a
value.
T
hin
k
of
t
he
value
as
the
co
ntents
o
f
t
he
b
ox
.
>>> r = 10
>>> A = 3.14*r**2
r
->
10
314.0
A
->
“
T
he
value
of
r
i
s
10.
T
he
val
ue
of
A
is
314.
0.
”
T
he
Assi
gnment Statement
The
“=
“
sym
bol
i
n
d
icate
s
assig
nm
en
t
.
The
assig
nme
n
t
st
at
em
en
t
r
=
1
0
cr
eat
es
t
he
var
i
abl
e
r
and
assig
ns to
it
t
he val
ue
o
f 10
.
>>> r = 10
r
->
10
Forma
l
:
“ r
is
a
ss
i
gned
t
he
value
of
10”
I
nf
orma
l:
“r get
s
1
0”
T
he
Assi
gnment Statement
A
var
iab
l
e
can
b
e
use
d
in an
exp
r
essio
n
li
k
e
3.14*r**2
.
The
exp
r
essio
n i
s
evaluat
ed
an
d
th
en
st
o
r
ed.
>>> r = 10
>>> A = 3.14*r**2
r
->
10
Assi
g
nme
nt
St
at
e
m
e
nt
:
WH
E
R
E
TO
PUT
I
T
=
R
E
CI
PE
FOR
A
VA
L
UE
A
->
314.0
Or
der
is
Imp
or
tant
M
at
h
is
l
ess f
ussy
:
A
=
3
.
1
4
*
r
**
2
wher
e
r
=
10
>>> A = 3.14*r**2
>>> r = 10
NameError
: name ‘r’ is n
ot defined
Assig
nm
ent
v
s.
“Is
E
qu
al
to”
In
M
at
h
“
=“ is
use
d
t
o
say
what i
s
o
n th
e
l
eft
eq
ual
s
what
is o
n
t
he r
ig
ht
.
In
Py
t
h
o
n,
“
=“
pr
escr
i
bes
an
actio
n
,
“eval
uat
e
t
he
expr
essio
n
on
t
he
r
ig
ht
and
assig
n
it
s
val
ue
t
o
t
he
var
iabl
e
name
d
on
t
he
l
eft
.
”
>>> r = 10
>>> 3.14*r**2 = A
SyntaxError
: can’t assi
gn to an
operator
T
he
Assi
gnment Statement
Her
e we
ar
e
assi
g
n
ing t
o
S
t
he
ar
e
a
o
f
a
sem
icir
cle
t
hat
has
r
ad
ius
10.
>>> r = 10
>>> A = 3.14*r**2
>>> S = A/2
r
->
10
Assi
g
nme
nt
St
at
e
m
e
nt
:
WH
E
R
E
TO
PUT
I
T
=
R
E
CI
PE
FOR
A
VA
L
UE
A
->
314.0
157.0
S
->
T
he
Assi
gnment Statement
H
ere
w
e are
assignin
g
t
o
A
t
he
ar
ea
of
a
semici
rcle
that
has
radi
us
1
0.
No
n
ew
r
ule
s
in
t
he
t
hi
rd
assi
g
nment.
The
“recipe”
is
A/
2.
T
he t
ar
g
et
o
f t
he
assignment
is
A.
>>> r = 10
>>> A = 3.14*r**2
>>> A = A/2
r
->
10
“A
has
b
e
e
n
overw
ri
t
t
en
by
A
/2”
A
->
157.0
T
r
ac
king U
pd
ates
>>> y = 100
Bef
ore
:
T
r
ac
king U
pd
ates
>>>
y = 100
y
->
100
Af
ter
:
T
r
ac
king U
pd
ates
>>> y = 100
>>>
t = 10
y
->
100
Bef
ore
:
T
r
ac
king U
pd
ates
>>> y = 100
>>>
t = 10
y
->
100
t
->
10
Af
ter
:
T
r
ac
king U
pd
ates
>>> y = 100
>>> t = 10
>>>
y = y+t
y
->
100
t
->
10
Bef
ore
:
T
r
ac
king U
pd
ates
>>> y = 100
>>> t = 10
>>>
y = y+t
y
->
110
t
->
10
Af
ter
:
T
r
ac
king U
pd
ates
>>> y = 100
>>> t = 10
>>> y = y+t
>>>
t = t+10
y
->
110
t
->
10
Bef
ore
:
T
r
ac
king U
pd
ates
>>> y = 100
>>> t = 10
>>> y = y+t
>>>
t = t+10
y
->
110
t
->
20
Af
ter
:
T
r
ac
king U
pd
ates
>>> y = 100
>>> t = 10
>>> y = y+t
>>> t = t+10;
>>>
y = y+t
y
->
110
t
->
20
Bef
ore
:
T
r
ac
king U
pd
ates
>>> y = 100
>>> t = 10
>>> y = y+t
>>> t = t+10;
>>>
y = y+t
y
->
130
t
->
20
Af
ter
:
T
r
ac
king U
pd
ates
>>> y = 100
>>> t = 10
>>> y = y+t
>>> t = t+10
>>> y = y+t
>>>
t = t+10
y
->
130
t
->
20
Bef
ore
:
T
r
ac
king U
pd
ates
>>> y = 100
>>> t = 10
>>> y = y+t
>>> t = t+10
>>> y = y+t
>>>
t = t+10
y
->
130
t
->
30
Af
ter
:
T
r
ac
king U
pd
ates
>>> y = 100
>>> t = 10
>>> y = y+t
>>> t = t+10
>>> y = y+t
>>> t = t+10
>>>
y = y+t
y
->
130
t
->
30
Bef
ore
:
T
r
ac
king U
pd
ates
>>> y = 100
>>> t = 10
>>> y = y+t
>>> t = t+10
>>> y = y+t
>>> t = t+10
>>>
y = y+t
y
->
160
t
->
30
Af
ter
:
Assi
gnment
vs
E
q
uations
In
alg
ebra
,
t
=
t +10
d
oes
n’t
ma
ke sens
e
u
nles
s
you b
eliev
e
0 =
t-
t =
10
In
Py
thon,
t = t + 10
mea
ns
a
d
d
10
t
o the v
a
lue of
t and
s
tore
the
res
ult in t.
The
K
ey 2
-
S
t
ep
A
ct
ion
Behind
Ev
e
ry Ass
ig
nme
nt S
t
a
t
e
men
t
1.
Eval
uat
e
t
he
ex
pr
essio
n
o
n th
e
r
ig
ht
h
and
side.
2.
St
or
e
th
e r
e
sult
in
t
h
e
var
iab
l
e
nam
ed o
n
t
he
left hand side.
<
v
a
r
ia
b
le
na
me
>
=
<
e
x
p
r
e
s
s
io
n
>
Naming
V
ar
i
ables
Rule
1
.
Name
mu
st
b
e
com
p
r
ised
of
d
ig
i
t
s, u
pp
er
case
l
et
t
er
s,
l
ower
case
l
et
te
r
s,
and t
he
un
d
er
sco
r
e
char
act
er
“
_”
Rule
2
.
M
ust
b
egi
n
wi
t
h
a
l
et
te
r
o
r
un
d
er
sco
r
e
>>> radius = 10
>>> Area = 3.14*radius**
2
radius
->
10
Area
->
314.0
A
good
nam
e
f
or
a va
ri
ab
le
i
s s
hort
but
sugg
e
st
i
ve
of
it
s
role:
Circle_Ar
e
a
Prec
ed
ence
Q.
In
a
n
a
r
i
thm
e
ti
c
ex
pr
es
s
i
on,
what
i
s
the
or
d
er
of
ev
a
l
uation?
A.
Exp
onenti
atio
n
& nega
tion
c
om
es
b
e
f
or
e
m
ultipl
ic
ati
on
& d
ivis
i
on
whi
c
h
i
n tur
n
c
om
e
b
e
for
e
ad
d
i
ti
on
& su
b
tr
a
c
ti
on.
I
t
is
a
good
h
ab
it
t
o use
pare
nt
hese
s
if t
h
e
re
is
t
he
sl
i
gh
t
e
st
a
mb
ig
ui
t
y.
This:
Is
t
he
same
as:
A + B*C A + (
B*C)
-
A**2/4
-(A**
2)/4
A*B/C*D ((A*B)
/C)*D
R
evisi
t
Ci
r
c
le
Ar
ea
It
seems
tha
t P
ython eva
lua
tes
(
2
2
/
7) a
s
3
ins
tea
d
of 3
.14
2
…
WHY
?
>>> r = 10
>>> A = (22/7)*r**2
>>> print A
300.0
A
d
i
ff
e
re
nt
k
i
nd
of
arit
h
me
t
i
c.
We
have
a
re
lat
e
d
e
x
p
e
ri
e
nce
h
e
re
.
1
1+3
=
2
in “clock
a
ri
t
hme
t
i
c”
Integer
s
and
D
ec
i
mals
In
m
a
th
we
dis
ti
ngu
ish
bet
ween
int
eger
nu
m
b
er
s
a
nd
d
ec
i
m
al
n
um
b
er
s.
Inte
ger
N
um
b
e
r
s
:
100
,
0,
-
89, 12
34
56
7
D
ec
i
m
a
l
N
um
be
r
s
:
-
2.
1,
100.01,
10
0.
0,
12
.
34
5
Integer
s
and
D
ec
i
mals
Ther
e
ar
e
d
iffer
en
t
k
i
nds
of
d
ivis
i
on.
In
teger
D
ivis
i
on:
30/
8
i
s
3
wi
th
a
r
em
ainder
of
6
D
ec
i
m
a
l
D
ivis
i
on:
30/
8 is
3.
75
int
vs
float
In
Py
thon, a
numb
er ha
s
a
type
.
The
int
type r
epr
esents
numbers a
s
integers
.
Th
e
float
t
ype rep
res
ents
numbers a
s
d
ec
ima
ls
.
I
mport
ant
t
o und
e
rst
and
t
h
e
d
iff
e
re
nce
s
and
t
h
e
int
era
ct
i
ons
int
Ar
i
thmetic
T
o
get
t
he re
m
ainde
r,
use
%
.
Pyt
ho
n
“k
now
s”
t
hat
t
he
v
a
l
ue
s
st
ored
i
n x
a
nd
y
have
t
ype
int
b
e
ca
use
t
here
are
no d
ec
i
ma
l
poi
nt
s
in t
ho
se
assi
g
nme
nt
s
.
>>> x = 30
>>> y = 8
>>> q = x/y
>>> print q
3
>>> r = x%y
>>> print r
6
float
Ar
i
thmetic
Pyt
hon
“k
nows”
t
hat
t
he
val
ue
s
st
ored
i
n x
a
nd
y
h
ave
t
ype
f
lo
a
t
b
e
ca
use
t
here
are
d
e
cimal
poi
nt
s
i
n t
hos
e
a
ss
i
gnme
nt
s.
>>> x = 30.
>>> y = 8.
>>> q = x/y
>>> print q
3.75
M
i
x
ing
float
and
int
I
n
Pyt
hon
i
f
one
op
e
rand
has t
ype
f
lo
at
and
t
he
ot
he
r has
t
ype
i
nt
,
t
hen
t
h
e
t
yp
e
i
nt
value
i
s convert
ed
t
o float
a
nd
t
h
e
e
valuat
i
on
p
roce
e
d
s.
>>> x = 30.
>>> y = 8
>>> q = x/y
>>> print q
3.75
E
x
p
li
ci
t
Type Conversi
on
int(
-
e
x
pressi
on
-
)
conve
rt
s
t
he
value
of
t
he
e
x
pressi
on
t
o
int
value
>>> x = 30.0
>>> y = 8.0
>>> q = int(x)/
int(y)
>>> print q
3
E
x
p
li
ci
t
Type Conversi
on
float(
-
e
x
pressi
on
-
)
convert
s
t
h
e
value
of
t
he e
x
pre
ss
i
on
t
o
a
float
>>> x = 30
>>> y = 8
>>> q = float(x)/float(y
)
>>> print q
3.75
An
Imp
or
tant
Disti
ncti
on
>>> x = 1.0/3.0
>>> print x
.333333333333
In
t
eger
ar
it
hm
et
ic
is
exact.
Float
ar
it
hm
et
ic
is
(usuall
y)
no
t
exact.
Str
i
ngs
So
far
we
have d
iscuss
ed co
m
put
at
ion
with
n
umber
s.
No
w we
d
iscuss
com
put
at
io
n
wi
t
h t
ext.
W
e
u
se
st
r
ings
to
r
epr
esen
t
te
x
t
.
Y
ou
are
a
“st
ri
ng p
roce
ssor”
whe
n
you
re
a
l
i
ze
7/4
m
e
ans
Jul
y
4 a
nd
not
1
.
75!
Str
i
ngs
St
r
i
n
g
s
ar
e
quo
te
d
char
acte
r
s.
Her
e
ar
e
t
h
r
ee
exampl
es:
>>> s1 = ‘
abc
’
>>> s2 = ‘ABC’
>>> s3 = ‘ A B C ‘
s1
,
s2
,
and
s3
ar
e
var
i
ab
les
wi
t
h st
r
i
ng
val
ue.
Str
i
ngs
St
r
i
n
g
s
ar
e
quo
te
d
char
acte
r
s.
Her
e
ar
e
t
h
r
ee
exampl
es:
>>> s1 = ‘
abc
’
>>> s2 = ‘ABC’
>>> s3 = ‘ A B C ‘
The
val
ues
in
s1
,
s2
,
and
s3
ar
e
al
l
d
if
fer
en
t
.
Up
per
and
lowe
r
case
m
att
er
s.
Blank
s
mat
t
e
r
Str
i
ngs
Nothing
sp
ec
ia
l
a
bou
t let
ters
…
>>> Digits = ‘1234567890
’
>>> Punctuation = ‘!:;.?
’
>>> Special = @#$%^&*()_
-
+=‘
Basical
l
y
any
k
eyst
r
o
k
e
but
t
he
r
e
ar
e
som
e
exceptio
ns
and
sp
ecial
r
u
l
es.
M
or
e
l
at
er
.
H
e
re
i
s one
:
‘S
op
hie
”’
”
s
Choice’
i
.
e
.
, Sop
hi
e
’s
Choi
ce
Str
i
ngs
ar
e Index
ed
>>> s = ‘The Beatles’
The
char
act
er
s
i
n
a
st
r
i
n
g
can
b
e
r
efer
en
ced
t
hr
o
ugh
t
heir
ind
ices.
Cal
l
ed
“subscr
ipting”
.
Sub
crip
t
ing
from
ze
ro
cre
at
es
a
d
is
conne
ct
:
‘T
’
i
s
not
t
he
f
i
rst
charac
t
e
r.
e
s
T
h
l
e
B
a
e
t
s --
>
0
1
2
3
4
5
6
7
8
9
1
0
Str
i
ngs
ar
e Index
ed
>>> s =‘The Beatles’
>>> t = s[4]
T
he
squa
re
b
rac
k
e
t
not
at
io
n is
use
d
.
N
ot
e
, a
si
ngl
e
chara
ct
e
r
i
s
a
st
ri
ng.
e
s
T
h
l
e
B
a
e
t
s --
>
0
1
2
3
4
5
6
7
8
9
1
0
t --
>
B
0
Str
i
ng
Sl
i
ci
n
g
>>> s =‘The Beatles’
>>> t = s[4:8]
We
say
t
hat
“t
i
s a
s
li
ce
of
s”.
e
s
T
h
l
e
B
a
e
t
s --
>
0
1
2
3
4
5
6
7
8
9
1
0
t --
>
B
0
1
2
3
e
a
t
Str
i
ng
Sl
i
ci
n
g
>>> s =‘The Beatles’
>>> t = s[4:]
Sam
e
as
s[4:11].
H
a
nd
y
not
a
t
io
n w
hen
you
wa
nt
a
n
“e
nd
i
ng
sl
i
ce
.
”
e
s
T
h
l
e
B
a
e
t
s --
>
0
1
2
3
4
5
6
7
8
9
1
0
t --
>
B
0
1
2
3
4
5
6
e
a
t
l
e
s
Str
i
ng
Sl
i
ci
n
g
>>> s =‘The Beatles’
>>> t = s[:4]
e
s
T
h
l
e
B
a
e
t
s --
>
0
1
2
3
4
5
6
7
8
9
1
0
t --
>
T
0
1
2
3
h
e
Sam
e
as
s[0:4].
Ha
nd
y
not
a
t
ion
when
you
want
a
“b
e
gi
nni
ng
sl
i
ce
”.
Str
i
ng
Sl
i
ci
n
g
>>> s =‘The Beatles’
>>> t = s[11]
IndexError: string index
out of
range
e
s
T
h
l
e
B
a
e
t
s --
>
0
1
2
3
4
5
6
7
8
9
1
0
Sub
scri
pt
i
ng
e
rrors
are
EXT
R
E
ME
LY
c
ommon.
Th
e
is
no
s
[1
1
].
An
ille
g
a
l
t
o
ac
cess
.
Str
i
ng
Sl
i
ci
n
g
>>> s =‘The Beatles’
>>> t = s[8:20]
e
s
T
h
l
e
B
a
e
t
s --
>
0
1
2
3
4
5
6
7
8
9
1
0
t --
>
l
0
1
2
e
s
I
t
is
“OK”
t
o sh
oot
b
e
yond
t
he
e
nd
of
t
he
source
st
ri
ng.
Str
i
ngs
Ca
n Be
Comb
i
ned
e
s
T
h
l
e
B
a
e
t
s --
>
Conca
t
e
na
t
i
on
i
s t
he
s
t
ring
a
nalog
of
ad
d
it
i
on
e
x
ce
pt
s1+s2
a
nd
s2+s1
a
re
d
iff
e
re
nt
.
>>> s1 = ‘The’
>>> s2 = ‘Beatles’
>>> s = s1+s2
Th
is
is
c
a
lled
c
onc
atena
tion
.
Conca
tenation
e
s
T
h
l
e
B
a
e
t
s --
>
N
o
li
mi
t
t
o t
he num
b
e
r
of
i
np
ut
st
ring
s:
s
=
s
2+
s2
+s
2+
s
2+
s2
>>> s1 = ‘The’
>>> s2 = ‘Beatles’
>>> s = s1 + ‘ ‘ + s2
W
e
“
a
d
d
ed
”
in a
blank.
T
ypes
St
ri
ngs
a
re
a
type:
str
So
a
t t
hi
s
poi
nt
w
e
i
ntrod
uc
ed
3
types
:
int
for
integer
s
,
e.g.,
-
12
float
for
d
ec
i
ma
ls
, e.g.,
9.12
,
-
12.0
str
for
s
tri
ngs
,
e.g.,
‘
abc
’,
’
12.0’
Pyt
hon
h
as ot
he
r
b
ui
lt
-
i
n t
ype
s.
A
nd
w
e
w
i
ll
lea
rn
t
o
mak
e
up
our
own
t
ype
s.
A
Type
is
a
Se
t of
Values and
Op
er
ati
ons
on T
h
em
int
123,
-123,
0
float
1.0,
-
.00123,
-
12.3e
-5
str
‘
abcde
’,
‘123.0’
Va
lues…
T
h
e
“e
”
not
a
t
io
n (a powe
r
-
of
-
1
0
not
a
t
io
n)
is h
and
y
for
ve
ry
large
or very
sma
ll
float
s.
T
he
li
t
e
rals
-
.
00123 and
-
12.
3e-5 are
t
he
same
num
b
e
r.
These
ar
e
cal
l
ed
“l
i
t
er
al
s”
A
Type
is
a
Se
t of
Values and
Op
er
ati
ons
on T
h
em
int
+ -
* /
u
nar
y-
**
%
float
+ -
* /
u
na
ry-
**
str
+
co
n
cat
en
at
io
n
Op
era
tions
…
T
ype
Conv
er
sion
A
st
r
ing
t
hat
enco
des
a decimal
val
ue
can b
e
r
epr
esen
t
ed
as
a
float
.
>>> s = ‘123.45’
>>> x = 2*float(s)
>>> print x
246.90
T
ype
Conv
er
sion
A
st
r
ing
th
at
en
co
d
es
an
int
eg
er
val
ue
can
be
r
epr
ese
n
t
ed
as
an
int
.
>>> s = ‘
-
123’
>>> x = 2*int(s)
>>> print x
-
246
T
ype
Conv
er
sion
Sh
o
ws
ho
w
t
o
g
et
a
st
r
i
ng
en
co
d
ing
o
f
a
fl
oat
value.
>>> x =
-
123.45
>>> s = str(x)
>>> print s
‘
-
123.45’
Autom
ati
c
T
y
p
e
Conver
sion
>>> x = 1/2.0
>>> y = 2*x
A
n
oper
at
io
n
bet
wee
n
a
float
an
d
an
int
r
esu
l
t
s
in
a
float
.
S
o
x
is
a
float
.
Thu
s,
y
is also a
float
e
ven
t
ho
u
g
h i
t
s
val
ue
happens
t
o
b
e an
int
eg
er
.
Py
th
on
is
a D
y
namic
ally
T
y
p
ed
L
angu
ag
e
>>> x = ‘
abcde
’
>>> x = 1.0
>>> x = 32
A
va
ria
b
le
can ho
ld
d
if
fer
ent
t
yp
es
of
v
a
lues
a
t
d
if
fer
ent times
.
I
n
ot
her
l
anguage
s
t
h
e
t
ype
of
a
vari
ab
le
is
f
ixe
d
.
Sum
mar
y
1.
Va
ri
a
bles
hous
e
va
lu
es
tha
t c
an
be
a
c
c
es
s
ed
.
2
.
As
si
gnment
s
ta
tements
a
s
s
ign
v
a
lu
es
t
o
v
a
ri
a
ble
s
.
3
.
Numerical
d
a
ta can be rep
res
ented
us
ing the
int
a
nd
float
types
.
4.T
ext
da
ta
c
an
b
e
r
epr
es
e
nte
d
u
s
i
ng
the
str
type.