Schweitzer Fachinformationen
Wenn es um professionelles Wissen geht, ist Schweitzer Fachinformationen wegweisend. Kunden aus Recht und Beratung sowie Unternehmen, öffentliche Verwaltungen und Bibliotheken erhalten komplette Lösungen zum Beschaffen, Verwalten und Nutzen von digitalen und gedruckten Medien.
This chapter is now going to cover the following important aspects of programming namely loops, if, else, and while statements. These statements are the key components of programming and allow us to operate on our objects. We have covered aspects of logic earlier when we considered comparison and equality and that is key when we look at if statements which is the first aspect we will consider here. The key to an if statement is that we need a statement that returns a true or false value. So let's consider the following statement:
Now, the first line is an assignment and the second is comparison and its with the second line that we can introduce an if statement:
What we have done here is test the return value of the statement and if its true then we increment the variable x by 1. That is the essence of an if statement, we test to see if the return of a logic statement is true and if it is we do something within that statement. We can extend the example by introducing the concept of an else statement. Essentially given the if statement tests if a statement is true the else deals with if the statement is false.
Now what we have done is assign x the variable 2 and then test if x is equal to 1 which returns False. We then set an if else statement where if the value is equal to 1 we add one to x and if not we subtract one from the value x. As we can see the value isn't equal to one so we enter the else aspect of the loop and decrement the value of x by 1.
Logic like this is really useful to allow us to make decisions in our code based on what a variable may or may not be. However, the if else loop only allows us to make a decision based on a single true or false condition. If we require more control, then we can use the elif statement alongside an if else statement. Let's improve on our example from before and introduce the concept of elif.
Here, we have set the variable x equal to 2 and then show the logic statements that we plan to use. In the first if statement we test if x is equal to 1, it isn't so we then enter the elif statement and see if x is equal to 2, it is so we then run the command in the statement and x is shown to be 2. We can expand the statement further with more elif statements so we can check for a variety of things. It is important to note that if one of the conditions on the if or elif is met we then exit the statement and continue through our code. So as soon as a condition is seen we exit the statement. Therefore, when thinking about using if statements you need to understand the context you are using them in.
If we have a fixed set of outcomes that we want to work with then an if elif statement like we showed before would work well. We next show an example where using an if elif statement doesn't quite work:
On the face of this you may think that this is a really good bit of code, however it isn't. The problem is we want to test if the win for either side is a win or a win by more than 1 goal. What happens here is the condition for a home win is met before we test for the win by more than one (which would also return a true result). So using an if statement as we do here doesn't work, instead we need to use a nested if statement.
We can see that we get the desired result. The point that this raises is that if statements can be very powerful but at the same time mistakes can be made if they are not thoroughly thought out. We next look to consider loops.
In earlier sections we have covered list (Chapter 7), tuples (Chapter 8), and dictionaries (Chapter 9) which are all important containers for values in Python. We showed how we can access and manipulate them. However, we concentrated on single instances of these objects whereas in reality this isn't the case. What you might expect is to have a collection of data which each may be contained in a list, tuple, or dictionary and you want to access it and perform some kind of operation on it. Let's create some data that allows us to do so.
What we have done here is setup a list of lists. Now if we want to access element 3 of the first list we can do so as follows:
So we access the first element of the outer list which returns us a list and then we access the value 3 of that list through the index 2. Now if we wanted to see everyones age we could write the following:
This is fairly tedious for five people, imagine if we had hundreds or thousands of people. To avoid writing the same code again and again we can use a loop to access the ages of everyone in the list. Now to make it more interesting let's work out the average age of the people in the list.
What we have done here is introduce the concept of a loop, alongside a few other things. Now, we initially setup a variable to contain the total age and set this to zero. The theory here is that we will increment the value by everyones age within the loop. Next, we enter the for loop. The syntax says for p in person, and what this means is that we iterate over all the elements in people and assign whatever it is to the variable p. The name p is totally arbitrary, we could rewrite the loop as follows:
The name isn't technically import, although this is pretty bad naming convention. The point is that when you write your loops you don't always have to use p. The loop then one by one access all the elements of the list people, where each element is a list and the indented code within the loop does something to the that element. What we do within the loop is assign the third value of the list to the variable age and then add that value to the total age variable which we initially set to be zero. This bit of code is shorthand for the following:
So we can see its a big reduction in amount of code written, which is a good thing. After that we divide the total age by the number of elements in the list which we get by using len. This then gives us the average for the list of lists, pretty cool.
That is the basics of for loops applied to lists, the key thing to remember is that you are iterating through the list, essentially going over every element of the list. Now as we discussed before the elements of the lists don't always have to be of the same type so care is needed when using loops as the logic applied to one element may not hold for all elements of the loop. Everything we have shown here for lists also apply to tuples so you could write the same type of loop on a tuple, however you can't set it up in the way we did earlier as that won't work.
One neat thing we can do with lists and loops is something known as list comprehension. Now if the problem we had is that we wanted to create a list with the squared ages of our people then we could write a loop as follows:
That all looks fine, but we could write it in one line as follows:
Looks cool doesn't it! Which one is better, well they both do the same, some may say using list comprehension is more Pythonic but you could argue its harder to read than the standard for loop example. It is really down to personal preference and what you like doing more.
Next, we will look at how to loop over dictionaries which behave differently to lists and tuples. Now if we have a list of dictionaries we can loop across the list and access the dictionary similarly to how we accessed the list within a list in the previous example. However, if we just have a dictionary we can loop over it in the following way:
The way the loop behaves in this case is very different to what we have seen before with lists. The p in person is key of the dictionary not the key and value of the dictionary, so in putting the p in a list we get the keys of the dictionary. If we want the values from the person dictionary we need to do it in the following way:
The only difference here is that we access the value from the dictionary that uses the key obtained from the loop.
Next, we consider how loops work on strings, which is again different to what we have seen before. We can loop over a string in the same way that we would a list.
So, when we loop over a list we access each individual element of the string, so appending it to the list we see an entry for each letter in the name.
The last concept we will consider in this section is while loops, these are loops that continue whilst some logic is true. We will demonstrate as follows:
What have we done here? Well initially we set a variable called score and set it equal to zero. Then we have said while score is less than four complete the code within the while loop. As for other logic considered here we need to use the colon after we have set the condition. What we see is that the output from this is that we show the values of score while score is less than four. Once it is completed we then leave the loop. Its much like a for loop, however while a for loop will work for loop will iterate over something a while loop doesn't and just continues until a condition is met which allows it to leave the loop. With a loop like this you need to be careful that the condition is met else it can stay in the loop forever!
Let's put all this together into a single example where we want to simulate a lottery draw. For those not familiar with lotteries we have a fixed number of balls that we randomly select to give a set of numbers. You win the lottery if you have the numbers selected. Now the example we will show will involve us generating six balls and a bonus ball. To generate the random balls we will use the following code:
Dateiformat: ePUBKopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet – also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an. Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.