15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2<br />

5. Loops and numbers<br />

a.<br />

b.<br />

6. Conditionals<br />

i = 0<br />

while i < 11:<br />

i += 1<br />

for i in range(11):<br />

pass<br />

n = int(raw_input('enter a number: '))<br />

if n < 0:<br />

print 'negative'<br />

elif n > 0:<br />

print 'positive'<br />

else:<br />

print 'zero'<br />

7. Loops and strings<br />

s = raw_input('enter a string: ')<br />

for eachChar in s:<br />

print eachChar # (does not print index)<br />

or<br />

for i in range(len(s)):<br />

print i, s[i]<br />

or<br />

i = 0<br />

slen = len(s)<br />

while i < slen:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!