People often write short programs In Python and use them by themselves to calculate or automate something. Such programs are also known as scripts. It is not unusual for scripts to have some or all of the input data contained within the script itself instead of being loaded. For example, the following script calculates a 20 percent discount:
Instructions for using this script could be: “In the first two lines of the script, set the values you want and then run the script.”
You won’t see a similar instruction for programs you install on your computer or mobile phone. We call such programs applications, and they are written so that users do not need to know (and most often they cannot know) what the statemets of that program look like.
With scripts, there is no strict division between users and developers as with applications. Scripts are often written for personal use or for a user who uses or understands programming. All the programs in this guide are actually more scripts than applications.
We have already emphasized that this manual is not intended only for future professionals in programming. If you do not program applications, you can still benefit from programming. You may write a script or customize an existing one, as expected in the presumed instruction of the previous example.
Changing data by input¶
Let’s make the previous example more general. Suppose that in one store we are entitled to a 20 percent discount over the regular prices. We are interested in discounted prices of various products, whose regular prices we know.
The solution we already know how to write is to load the regular price of the product and then calculate and print the reduced price. This program can look like this:
We can run the program multiple times, setting each time a regular price for one of the products we are interested in.
In the previous example, it was necessary to run the program multiple times and enter the data (regular price of a product) each time. In case we know in advance all the data that we would enter one by one at different executionss of the program, then it is more comfortable to enter all that data directly into the program and to repeat the calculation and printing of the results for each of the data in one execution.
Let’s say that the regular prices of the products we are interested in are 250, 120, and 310 and we want to calculate the discounted prices for those products with a single execution. Here is how we can do it:
Note: notation (250, 120, 310) is called a tuple.
Running the program we see that it prints:
250 200.0 120 96.0 310 248.0
We notice that the last two lines of the program were executed three times, with the variable regular_price receiving the values 250, 120, 310 in that order. We achieved this with the statement
for. The parts of the program that are repeated are called loops, so we can say that in the previous example we used a for loop.
The following figure shows the main elements of the for loop:
Required elements are written in red (the words
inand the colon character
:in the first line). These elements are written in the same way in each for statement.
The loop variable is written in blue. At that place we write the name of the variable that will take the values specified in the tuple. In our example, the loop variable is regular_price.
A tuple of values is written in green. At that place we write comma-separated values in parentheses. These are the values that loop variable will take in turn. In our example, the tuple is (250, 120, 310).
The body of the loop is written in black. These are commands that are executed once for each value of the loop variable. Loop variables may or may not be used in statements of the loop body.
Statements of the loop body are written indented with respect to the first line of the for statement. It is common to use 4 indentation spaces and we will stick to that recommendation.
Examples and tasks¶
Example - when to go
Ronnie should arrive at destination no later than 5:00 pm. Depending on the way of travel he chooses, Ronnie may need 55, 70, 85, or 95 minutes. Write a program that prints for each way of travel when Ronnie needs to leave at the latest to arrive on time.
A program that solves this task could look like this:
Task - trip duration
George intends to start a 600-kilometer car trip at 9 a.m. and is interested in arriving time if he was traveling at an average speed of 90, 100, 120 or 130 kilometers per hour. Finish the program to list the time of arrival at the destination for each of the aforementioned average speeds.
Task - final grade
The sum of 5 Katie’s grades so far is 23. Katie expects another grade from the final control task. Finish the program below so that for each possible final grade (1, 2, 3, 4, or 5) it prints what the average grade would be in that case.
Task - allowance
Theo makes a plan for spending his pocket money over a 14-day vacation. Write a program that, for an average daily spend of 5, 10, or 20 euro, lists how much money in total Theo would need in each case.