You are on page 1of 7

I

A basic python program

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:

• 0 and"" (the empty string) evaluate to False

• Conditions (e .g., x < 4) can be put in parentheses, but do not need to be.

Tests The while loop

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

. g that is indented as a block of code.


fi And again , here is the output we get from this code: Python treats everyth in

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.

Tracing a while loop Trace output

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

# remember to increment (i.e . , avoid infinite loops) :


i C i + 1 Starting value of i: 10
10 100
print "Ending value of i : " + str(i) Ending value of i : 11
print

,.

Use #1: iteration Use #2: until

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

[u.lvt_ ~·r+l"j Ca~ e V\', l~ ei+-k ek in 4e r fool a. J1"1v. C,1--

ex h--Q po IaJ-i'lfVI ,

OJ Q -re~ ~J.d6Y' . a~ lc7.d.r ,'-1 v.Kd + J-; +


...
JeJJe1 o--f-- clctk P" in-~ ·
}-/mt , .}h~ M J~t •vv tll t--l'dvc~
11

Nt,J + v-e_ T7 ,-, x

Oll\ '1h .€_ e_ 'W..01i~.., ~ ca. h b-e U! ~ ~ -;/-1~ J


poi fl fJ Q ~UJ hVL e CLO_~ .--1-h t UJ-l~ •

8n ./--e;po I Q+I~ : f)}

r-QJu/J CW

,'J

t>t_ pt/ po ]~o ~ : t M pol 011'~ 1 u• +ht u1e ai ~~}4-J


-
)t_
c'

4-he orf +he


C.u.t~ b~vwl o'QY\j e...

c, b.r et ~ al~ d.Kd f.J Sul, J e_d- t-o CJ.. o{ e};Ye._ t

1 V~ l e_ i+etJ\It ~ t J t~ Ce (t- ~ ~edd


~ -4C\ "- q q 9b t '2--'\ ~ CZ. ~l\ S ~
.,

l -_)rf.\- - 1
, "
pn> ., ya m.1 a re.

JIJY<' +, cJo,-e >J""" ;fil~ , Now


al~ ~~le~ o vr 1{ I f-' LJJ •''j < ,F It > ' Ll•H ( )
(e~ tloJf. aJj

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

0. = a PPend Co,,l c/f•j)

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

~ ':t.}h1JV1 />"ro,P"- m•b


we ~a~ 4n ~ if} wfMi ~~
'So ~ ' €l ~·t e
e_oli~ < T0 h'\ct le, <}-hifl~

~ ~ +.
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

ol~ • ~,d ],t1+h.


~ A ~- I, U .\,fllt In ~rmtJ.i'o.,, or cl ~-le. WIik~ i .ia:r ii'\ ¼,
I G-m p4,i-& S ,11, t Q,j e al, vi ct1 . ~ .'1 -t-h•" 3 iw 1M €o.l J
~ +,, ~
l'Y\a, 11 i U lCl h .1--h eJ e -;f.il el • Ul ~ "' t r ~

~ n}-e.J' iii t in ~
2
&mp~.

~o I ~

e'ripe u'~ llj u,¥ b kn,u; -A. w h ~k


fil t1 :f,;r m~ IAJ't;cJ, .!'4-oW

k ~ a ptdf\~ prvJ ,qh..


~ ~)(•. lt4 LU c}elte ~MHnp
c,V)~~.
+w C ~u_Jy ~ l~t o;/ UJt}1 1ur et(CW

u,VJ will l,· tc.e ~ be J'~ ot-J


Out l ,j.--1
a1
·tavtJ "
In
a_ ~Jc+ ~·te,

You might also like