4.5. Exercises¶

1. Use a for statement to print 10 random numbers.

2. Repeat the above exercise but this time print 10 random numbers between 25 and 35.

4.5.1. Contributed Exercises¶

Q-1: After completing the reading, what concepts are still unclear to you? If nothing is unclear, what did you find most interesting?

Q-1: (TEST) The correct code to generate a random number between 1 and 100 (inclusive) is:

• prob = random.randrange(1, 101)
• This will generate a number between 1 and 101, but does not include 101.
• prob = random.randrange(1, 100)
• This will generate a number between 1 and 100, but does not include 100. The highest value generated will be 99.
• prob = random.randrange(0, 101)
• This will generate a number between 0 and 100. The lowest value generated is 0. The highest value generated will be 100.
• prob = random.randrange(0, 100)
• This will generate a number between 0 and 100, but does not include 100. The lowest value generated is 0 and the highest value generated will be 99.

In statistical physics, it is common to use Stirling’s approximation for $$N!$$,

$N!\approx N^N e^{-N} \sqrt{2\pi N}.$

Obtain an integer from the user, assign it to N, and print out, in order, $$N!$$, Stirling’s approximation, and the relative percent error. The relative percent error is calculated as

$\text{error} = \left| \frac{\text{approx} - \text{exact}}{\text{exact}} \right| 100\%.$

For a right triangle we have

$\tan(\theta) = \frac{\text{opp}}{{adj}}$

Compute and print $$\theta$$ for

\begin{align} \left(\text{opp}=1,\text{adj}=1\right), \left(\text{opp}=1,\text{adj}=-1\right), \left(\text{opp}=-1,\text{adj}=1\right),\text{and} \left(\text{opp}=-1,\text{adj}=-1\right). \end{align}

Hint: use the function atan2() instead of atan().

Use the double angle formula,

$\sin(2\theta) = 2\sin(\theta)\cos(\theta),$

to compute and print

$\sin(\pi/4)$

Q-1: Which is the best way to import the math module so you can use the exp() function?

• import math
• Correct! This will make it obvious to anyone who reads your code when you use something from the math module.
• from math import exp
• This imports only the exp() function and puts it in the global namespace. This can make it difficult to tell where this function comes from.
• from math import *
• This imports everything from math and puts it in the global namespace. This can make it difficult to tell when the math module is being used.
• import math as m
• Don't change the names of modules unless it is the convention for that module. This makes your code harder to read.

Q-1: Which of the following functions are part of the math module?

• exp()

• abs()

• int()

• sin()

• fabs()

• pi

Write a program to convert square km to square meters.

In mathematics, the Leibniz formula for π, named after Gottfried Leibniz, states that

$\frac{\pi}{4} = 1 - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + \frac{1}{9} - ...$

Write a program to calculate π when n is 99 and 999. When n becomes bigger, does π becomes more accurate?

Write a program to print an n*n times table. n could be any number. For example, the following is a 5*5 times table:

1 x 1 = 1

1 x 2 = 2 2 x 2 = 4

1 x 3 = 3 2 x 3 = 6 3 x 3 = 9

1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16

1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25

Write a Python program to print 10 random integers between 1 and 100, inclusive. Print one random integer per line. Use a loop. I have started the program for you.