PYTHON MADE SIMPLE (2015)
Chapter 6: Use of Nested Control flow statements in Python
By now I believe you are becoming conversant with the abilities and potential of Python language. It can be applied in a variety of ways as simple statements and as well as complex or combined statements. One such example is well demonstrated in the use of for-if and else statements.
In this case now, note that for and if statements can be nested inside each other in an indented block of code. For instance, from a series of values, one can be able to only select positive values from a mix of numerical in an arbitrary list of numbers.
For example. Using the following code, it is easier to select only positive values from a mix of numbers:
Just for a moment, try to read the program by following the comments provided in the code. Using a human eye, you will be able to select the numbers carefully, but using a computer, this will be made systematically checking every number consistently. Using the if statement, every number can be checked determine whether it can be printed.
This can be observed below:
Nesting of the statements as in the use of If expands the possibilities of applications of Loops. This will allow you to be able to use different things in loops and allowing consistency and decision making between alternatives.
Besides the if loops, there are also the while loops which also work with the if statements. Nesting if statements and the while loop allows you to be able to make far better nested programs.
Pythons can be used to express graphical interpretation of results.
For instance, when a balloon is released and allowed to bounce within a room, this can be expressed in Python to expresses the randomness of times it will bounce off to the edges of the room.
This can be allowed to operate from random locations which can be applied in Python. The program can be run in python shell.
Find a sample program that can be used to express a bouncing balloon.
bounceBall (-3, 1)
This parameter provides the amount and shape of movement of the balloon when bouncing in every of the animation. Other values can also be expressed in shell and magnitude of less than 10 values can be used.
Animations before the use and applications were purely scripts, one was required to define the direction and how many moves to be considered. However, the direction of every motion may change with every bounce. Hence, in python, the graphics objects shapes combined with a central animation step.
This can be expressed in the following example code:
The use of the values, dx and dy allows the values to change as the ball gets to the boundary. Using an example, a bouncing balloon may get to the left side as it is moving to the left and up. The bouncing effect will have impact on the nature of the horizontal movement but the ball will keep with its upward motion. A reverse effect in the motion of the horizontal movement can also be captured in Python. This allows the bouncing balloon to change horizontal shift and hence the negative sign to be used in coding will be the reverse. This can be expressed as below:
dx = -dx
In this case, the value of dy will not change. However, this may not be the case in every of the animation step since it does not happen every other time. This therefore provides for the use of if statement. The center of the balloon can be estimated using the coordinates which is expressed in the form of x and y (x,y). The lowest point the bouncing balloon can reach can be expressed as xLow.
The value of xLow should always be greater than 0 while the value of edge of the window is also at a coordinate 0. Otherwise, at this position, the values of the ball would be half way of the screen. The x coordinate of the center of the balloon also needs to be the length of the radius.
The process of animation takes place in small steps and this is possible by allowing the ball to take one first small and quick step of where it needs to go (xLow), the ball can then also be reversed to the original position where it belongs.
There are associated bounding variables that can be used to express the bouncing variables xHigh, yLow and yHigh, which represents the radius away from the edge in the form of coordinates and same conditions to assess the possible bouncing off each of the possible edge. One coordinate will automatically reverse in whichever way as long as one of the edge is hit. This code can be expressed as follows:
This can also be expressed using the elif sentence structure. This will allow for the extra testing. This will imply that if is indeed true that x < xLow, then, it may be impossible for it to be true in the case when x > xHigh. Therefore, both tests are not needed together. The use of the elif clause will also provide for elimination of the unnecessary tests when both x and y is to be used.
The middle if should not be changed to make it an elif as used in many other cases. This is because there is a possibility that a ball can reach a corner and therefore, it may require that both dx and dy should be changed.
A variety of access methods for graphics are present that can be used using different shapes such as circles which can be defined by describing the centre point.
They can be accessed by using getCenter() method. This will be able to generate a clone of the points which can be returned. Every coordinate can be retrieved by using getX() and getY() criteria.
This may be a new feature that can be applied in the definition of the central function of a bouncing balloon in a box bounceInBox.
This animation will be able to develop a repeat loop which can be accomplished through a series of 600 steps.
This can be captured in this experiment.
This will move a balloon while bouncing from any point within a rectangle. This is what is expressed in the function of the code: getRandomPoint. This function allows the use of randrange function derived from the main module random.
Parameters with functions range and randrange expresses the end which has been defined in the past and which is the last value that is actually desired.
The full program that will allow one to animate a complete program that will be able to show a repeating bouncing balloon/ball is as expressed below. Note that there is the use of the function, bounceInBox and getRandomPoint to as the program to be complete.
These are also not the only functions that can be done. Not everything has been described as demonstrated in the code below: many other things will need more research to understand how they function. But, they can be used together so that a program will be able to function appropriately. This program will be able to show a ball bouncing off the sides of the window.
Write a program excellent.py that will have a function printShort in the heading: The program should be able to read a list of strings and then it should be able to print the one with at most three characters.
Test the program developed using a variety if hints to determine the len of functions.
This should be tested in the python shell interaction.
This will; have to start with the line >>>
Even Print Exercise
Develop a program and name it good.py with a function printEven and heading:
Use different numbers and ensure that it will be able to print even numbers. The program will be similar to this one:
Even List Exercise
Just like above, create another program and name it good2.py. This should be able to select only even numbers using the function chooseEven (nums)
Write a program even2.py with a function chooseEven with heading:
Test the function and confirm whether only the even numbers are chosen from the list required.
Unique List Exercise
Create a new program named crazylib.py which can be used in getting the keys using the getKeys function. The program will be able to create a list of every occurrence of keys from a cue developed in a story format. The keys will be developed in the order in which they appear in the list. However, they will have repetitions. The original version used to gets keys and deleting every duplicate in the data allowing the formation of sets from the keys. The only challenge in that the sets may not be ordered. Consequently, when the program iterate through the codes, the order of the cues will bear no resemblance to the order in which they occur.
Copymadlib2.pytomadlib2a.py, and add a function with this heading:
One can use the Boolean operators in for checking of membership in a sequence:
It can also be used withnot, asnot in, to mean the opposite:
Hence, what you can note here is that the work is in two forms:
1. This determines an item that is in the sequence
2. This is an item that is not in the sequence
To be able to make this a success. One needs to make sure that the list is in order. A syntax can also be used to append elements to a new lists which may or may not be in the new list.
After using the uniqueList function, one can also replace the last line of the getKeys so that it can also use the uniqueList so as to remove the duplicates in the keyList.
Try to create this and run it to ensure that cues values are in order.