Lab activity

Using Thonny

Start the "Statistica Laboratorio" virtual machine (or start you PC)

Create a new folder on Desktop, name it by you surname (e.g. "Rossi")

Start Thonny (one of the lower left icons, named "Th")

Write a simple Python program, containing just two lines as print("Ciao") and print(18, 23), and save it as firstprogram.py in the created folder

Run the program

Modify as you like the program using Thonny editor, save and run again the program.

Generating pseudo-random numbers

Method random.random(), that can be used after executing import random, returns a float random value in interval [0, 1), 0 included and 1 excluded.
Thus, it is possible to assign a random value to a variable v by executing v = random.random().
It is also possible to use random.random() in a more complex expression.
Analogously, method random.randint(a, b), where a and b are integer values (given by literals, variables, or more complex expressions) returns a random value int in interval [a, b], including the extremes.

Any time random.random() is called, a different (with very high probability) value is returned. Variability of the result of random.randint(..., ...) depends upon the interval defined by parameters.

  1. Write a program that prints a random float value in interval [0, 100) (hint: multiply the result of random.random() ...).
  2. Write a program that prints a random int value generated in set {0, 1, 2, 3, 4, 5}.
  3. Repeat running the above programs to check their behaviour
  4. Write a program that reads two integers a and b from the keyboard and prints 4 random numbers in interval [a, b].

Conditional statement

Write programs to solve the following problems.
Start solving only point 1;
then add one point at a time running again the program.

  1. Generate two random numbers in interval [0, 1), print both numbers and print the maximum.
  2. Generate four random numbers in interval [0, 1), store them into four variables, and print the generated values.
  3. Count how many of the four generated numbers are greater or equal to 0.6 and print the result.
    For example, the program could print: There are 3 numbers larger than 0.6
    (Hint: define a variable used for counting. The variable initially contains 0, and will be incremented by 1 any time ... )
  4. Compute and print the average of the 4 numbers.
  5. Print all values smaller that the average.
  6. Print the maximum among the four values.