Professional Documents
Culture Documents
i • 1
while i < 10:
• comments can be put after•••
More Basics for Python print etr(i) +" is my friend."
1 +• 1
L445/L515 When we run this, we get the following output:
The Computer and Natural Language
Autumn 2011 1 is my friend•
2 i11 my friend.
3 is my friend.
9 is my friend.
Control Structures
The if statement
For an if statement, a condition (e.g., x >4) is evaluated to either True (run the
• We can't really do a lot with our code yet. but we're going to want to: code) or False (don't run it, or move on to the else block)
if > 4:
X
o make choices (if)
print "x is greater than 4"
• do the same thing several times (while and for) elif X < 4:
print "x is less than 4"
• put a repeated part of the code in another part of the program (functions) else:
print "Why, x must equal 4"
We'll look very briefly at if and while statements today •
Note:
• Conditions (e .g., x < 4) can be put in parentheses, but do not need to be.
X •• y x equals y
X <y x is less than y
X >y x is greater than y · A while l_o~p does a similar condition check, but then runs a block of code as long as
>= y that cond1t1on evaluates to True.
X xis greater than or equal toy
X <• y x is less than or equal to y
X != y
i = 1
x is not equal to y
X lS y x is the same object as y while i < 10:
x is not y x 1s not the same object as y # comments can be put after '#'
X in y x 1s a member of y print str(i) +" is my friend."
x not in y x is not a member of y i += 1
° Caution: • and =• are different: , Run the block of indented code 10 times
= assigns a value
== compares values • Each time, the variable i has a new value
Blocks of code
Loop output
1 is my friend , __ h·s means that as long as the condition 1s true, th'! block or cod.,
• For -ahile 1..,.,,..•. l 1
2 11 my friend. is run
3 is my friend .
4 is my friend . In other WOf ds. •'--
u"'
s;im•- block of code is run multiple times
5 1a my friend .
•.. usually with some of tM variable values being different
6 is my friend .
7 is my friend .
8 is my friend. It helps to trace your code to see this, 1.e., observe what happens at every step
9 1a my friend.
The code within the loop was repeated 10 times, but because i's value was different
every time, the output changed.
l • 1
while 1 <• 10: Starting value of i:
# this indented block of code will run from i • 1 to i s 10 1 1
Ending value of i : 2
print "Starting value of i: " + str(i)
Starting value of i: 2
squared • i .. 2 2 4
print i, Ending value of i: 3
print squared
,.
As we've just seen, while loops can be used to iterate over a sequence. Another. subtly different use is to perform the same actions until a certain condition
is reached.
• This is most commonly done by iterating over integers, because integers easily
count how many times you do something. user_ input :s 11 u
while len(user_input) < 10 :
user_input ~ raw_input (" Please enter a long string , ')
• You can change the way you iterate-e.g., i += 2 or i -= 1 or whatever
print "Thank you for entering a long enough string!•
ll
~. shb\,J {.w o. reo.9_ Jif-e J.a..+<'! Ca~ I.( ~-.}+d -./o a,,, a 11
.-- eA ~ s i"' Ji.a
l Ultve_ {-i-Hf"j o, i'j ~ r,Je.QP G,v,,i l'f\P ~ crui eAm,
~b(\\11V\ p~jro.m ,
~ Cu.lc \rt. j-{ W".J:' 9J p'ra w J o;f C•n 1 lrrv c./fr-.J a. CCPt W 1/'f
,.f-h t
"11!l..fi.t ma .J-,'W .fun ul-i /1JII ~ , {o.J .J-1.-.e W i
~-.\- .j. a. ru,iouJ o-f d~k ~,i'n4/ 1 po1J i'b ~
J Vb 'J tc.¾- --to (c.f\f +roJ'l ¾.
ex h--Q po IaJ-i'lfVI ,
r-QJu/J CW
,'J
l -_)rf.\- - 1
, "
pn> ., ya m.1 a re.
~cJ--it1~,
II l
le f1~1e no rn e I
0 PtVl C
,
•
J]l¾M., : . -l.:, ..;c ---te ,d rn •.I e ·
l,"" hina i1j rr,o<lt 1
b Pen f"' uf?Jo}-l'•j [ rerul a.J t0'H-f'rJ J ! edtt ,,tv.m o~urUl-
------;x O ----- 7- _ _ _ _____. ;x
x"" ort"
jl.o ye.r,)
~ e. }{_do1
')1•~
tf-f
1,\111, ,1-e
~ onl1;,
? (JF WYitt ')'\o~·
1
C I~J t [ 11
J le IYlCl me , t( ( tn o d. 1 J
wi~
~'ha• uo.-0 al\CI l;p1'J ,
c~J (n;ntW"J
J'ome '1"t> fn ~ iJ r/+,oH \'lo I,.
-to dvir.J ~'.,,J, we
f~ tto..cUte t · /JJ ~ x--1 4-,·t t ln
a
will
Nl"d It ~~
~~f\•
(JJY'~ ~ pto j -rQ}}'l
CfpEN 1 N Cn A FIL&_ !.. Be_
_,
r wt
[a~
c.«i+e Q.
OU r
~ ~ +.
olirtc~,
fo O~I" /!t_ ;fl/ t (• ,,~.+I,•• , (.Vt v1 /: 'Y\l eJ J•m.<"
(111ou'o ~ ,ri,.L J-i(t 0tn olii I< IJ.Jj,1-h °' v0,...'cthl{'
. 'Th,1 pro w•J /J t.a II uil of?rni~j o. j-1k
.
I I\ p~-Ui" Yl ,1-e_u;~~e .\-1,~ -;f{lt rs · yk,
we l,e31f'\ ~ Jh t ,fi' Ir.> 1'J 6fk"' ~er-~ ....1-,, aJ
c~~ a._
/oW1'~ oef , we. tJJ f II
~ /tit p~'
s~ p::,~\n
fnfw~~ , Sn our
_s-~~j ~))Qi, \e ~ s~ ~j
~e p~
u, ~II ~
> lJJe
Ji·1t, . pd . ..t°" ~ -t \l'Qidh le f-o .+he_
Jt{ <fht
\,rel 11'a bl e Q,.J
~ n}-e.J' iii t in ~
2
&mp~.
~o I ~