# 4.5. Exercises¶

Use a

`for`

statement to print 10 random numbers.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?

- 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.

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

In statistical physics, it is common to use Stirling’s approximation for \(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

For a right triangle we have

Compute and print \(\theta\) for

Hint: use the function `atan2()`

instead of `atan()`

.

Use the double angle formula,

to compute and print

- 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 is the best way to import the `math`

module so you can use the `exp()`

function?

`exp()`

`abs()`

`int()`

`sin()`

`fabs()`

`pi`

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

module?

Write a program to convert square km to square meters.

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

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