Python Programming Practice-example programs

Created with Sketch.

Python Programming Practice-example programs

It is a valuable skill is to be able to read code. In this section we will look in depth at some simple programs and try to understand how they work.

Example 1 The following program prints Hello a random number of times between 5 and 25.

from random import randint
rand_num = randint(5,25)
 for i in range (rand_num):
print ( ' Hello ' )

 

The first line in the program is the import statement. This just needs to appear once, usually near the beginning of your program. The next line generates a random number between 5 and 25. Then, remember that to repeat something a specified number of times, we use a for loop. To repeat some- thing 50 times, we would use range (50) in our for loop. To repeat something 100 times, we would use range (100) . To repeat something a random number of times, we can use range (rand_num) , where rand_num is a variable holding a random number. Although if we want, we can skip the variable and put the randint statement directly in the range function, as shown below.

from random import randint
for i in range (randint(5,25)):
print ( ' Hello ' )

 

 

Example 2 Compare the following two programs.

from random import randint 
 rand_num = randint(1,5) 
for i in range (6):
print ( ' Hello ' *rand_num)

 

Hello Hello

Hello Hello

Hello Hello

Hello Hello

Hello Hello

Hello Hello

from random import randint
for i in range (6):
rand_num = randint(1,5)
print ( ' Hello ' *rand_num)

 

Hello Hello Hello

Hello

Hello Hello Hello Hello

Hello Hello Hello

Hello Hello

Hello

The only difference between the programs is in the placement of the rand_num statement. In the first program, it is located outside of the for loop, and this means that rand_num is set once at the beginning of the program and retains that same value for the life of the program. Thus every print statement will print Hello the same number of times. In the second program, the rand_num statement is within the loop. Right before each print statement, rand_num is assigned a new random number, and so the number of times Hello is printed will vary from line to line.

Example 3 Let us write a program that generates 10000 random numbers between 1 and 100 and counts how many of them are multiples of 12. Here are the things we will need:

• Because we are using random numbers, the first line of the program should import the

random module.

• We will require a for loop to run 10000 times.

• Inside the loop, we will need to generate a random number, check to see if it is divisible by

12, and if so, add 1 to the count.

• Since we are counting, we will also need to set the count equal to 0 before we start counting. • To check divisibility by 12, we use the modulo, % , operator.

When we put this all together, we get the following:

from random import randint
count = 0
for i in range (10000):
 num = randint(1, 100) 
if num%12==0:
 count=count+1
print ( ' Number of multiples of 12: ' , count)

 

 

Indentation matters

A common mistake is incorrect indentation. Suppose we take the above and indent the last line. The program will still run, but it won’t run as expected.

from random import randint
count = 0
for i in range (10000): 
num = randint(1, 100) 
if num%12==0:
 count=count+1
print ( ' Number of multiples of 12: ' , count)

 

When we run it, it outputs a whole bunch of numbers. The reason for this is that by indenting the print statement, we have made it a part of the for loop, so the print statement will be executed 10,000 times.

Suppose we indent the print statement one step further, like below.

from random import randint
count = 0
for i in range (10000):
 num = randint(1, 100) 
if num%12==0: 
count=count+1
print ( ' Number of multiples of 12: ' , count)

 

Now, not only is it part of the for loop, but it is also part of the if statement. What will happen is every time we find a new multiple of 12, we will print the count. Neither this, nor the previous example, is what we want. We just want to print the count once at the end of the program, so we don’t want the print statement indented at all.

Leave a Reply

Your email address will not be published. Required fields are marked *