0203 Repeating Code With a Loop

  • Upload
    radebap

  • View
    213

  • Download
    1

Embed Size (px)

DESCRIPTION

python prevod

Citation preview

There are two fundamentals types of loops in Python. There is while loops and forloops, and let's start by looking at the while loop.So we'll take our whileloop.py and make a little copy of it, and callit whileloop-working.Go ahead and open that, and you'll see this is a simple Fibonacci series.It's a common exercise for while loops.So here we are assigning two values to a and b: 0 and 1. The 0 will get assignedto a, and the 1 will get assigned to b. We say while b < 50 and we go ahead anddo things we'll make b grow in a Fibonacci series and we print it each timeusing the print function.So if I go ahead and run this and select the Python Run, you'll see here we getthe Fibonacci series.So while loops just work like that. They say while, and then there is a condition.And as long as this condition is true, the while loop will continue to execute.And as soon as this condition becomes not true, then the while loop will endand execution will continue after it if there is any code after it, which inthis case we don't have.Down here I could say print ("Done") and if I save that and run it, you'll see we get Doneat the end.You'll notice that the print ("Done") is not indented. If I were to indent ithere, then it would become part of the loop.But by having it back here, at the same indention level as the while statementitself, then it is no longer part of this suite of code that is controlled by the while loop.So that's how while loops work.Let's take a look at for loops.Go ahead and make a copy of forloop.py, and I'll name it forloop-workingand we'll open that.What a for loop does is it works with what's called an iterator.An iterator is an object that each time you call it,it gives you the next value.So in this case, we open a file and it's this lines.txt, which we can see righthere; it just has five lines of text.Then for line in fh, that's the file handle, which is actually an object and ithas this method readlines, which is an iterator.So for each time this fh.readlines is called, it will put the next value in theline variable, which will be the next line of text, and then we'll print it.So when I run it, we get these lines of text.Now, you'll notice that there is an extra line ending, an extra new line, ineach one of these, and that's because the print function puts a new line at theend, and each of these lines actually has a new line at the end of it, so we get two for each one.If you hover the mouse in Eclipse, you hover the mouse over the print function,you get a little bit of documentation for it.You'll notice that there is an end option, and it defaults to having this new line.So if I just say end= ' ', empty string like that and I save it and run it,now, we don't have that effect anymore.So, the for loop works likes this.It calls an iterator, and its values get put in this variable here.It has a colon, like all the control structures in Python, and the block of codeunderneath it is indented.So for each line of readlines, it will take the line, put it in the line variable,and then print it, just like we see here.So that's how for loops work in Python, and those are the two types of loops thatwe'll see in Python.We have while loops, which are traditional, conditional loops, and for loops,which work with iterators.