Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Summary

In this lesson, you learned that:

  • A module is a file of Python code with functions and values for one subject. Python comes with many of them, its standard library.
  • import math at the top of the program imports a module, and you reach what's in it with a dot: math.sqrt(16), math.pi. Without the import, the program ends with NameError: name 'math' is not defined. Did you forget to import 'math'?; a typo in the module's name gives a ModuleNotFoundError.
  • from math import sqrt, pi imports only the names you list, and you write them without the module's name: sqrt(81). The name math itself isn't imported then.
  • A program of several files imports its own files the same way: import shop for the file shop.py.
  • round(x) rounds to a whole number and round(x, 2) to two decimal places. It's built in, so it needs no import. A number exactly halfway goes to the even one: round(2.5) is 2.
  • math.ceil() rounds up and math.floor() rounds down, for example to work out how many buses you need for everyone.
  • random.randint(1, 6) gives a random whole number from 1 to 6, both included, and random.choice(players) a random element of a list.
  • random.seed(42) makes a program get the same random numbers on every run, as long as it makes the same calls in the same order. A game leaves it out; a test uses it.
  • statistics.mean() gives the average of a list and statistics.median() the value in the middle, which one extreme value doesn't move.

In the next lesson, Classes and Objects, you'll learn to make your own kinds of values, such as a pet with its own name and its own methods.

Instructions

  1. 1

    Let's practice with dice. Write a function roll_dice(count) that rolls a die count times and returns the rolls as a list. Each roll is random.randint(1, 6). Print roll_dice(10): with the seed 3, it's [2, 5, 5, 2, 3, 5, 4, 6, 5, 1]. Keep the line random.seed(3): the exercise also tries other seeds.

  2. 2

    Below, roll 1000 dice with roll_dice(1000) and print their average with statistics.mean(), rounded to two decimal places: Average: 3.51. The more dice you roll, the closer their average gets to 3.5, the average of 1, 2, 3, 4, 5 and 6.

Back

8/8

  1. 1. Importing a Module
  2. 2. Rounding
  3. 3. Quiz: Rounding
  4. 4. Importing with from
  5. 5. Quiz: Imports
  6. 6. Random Numbers
  7. 7. Average and Median
  8. 8. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.