Lesson 29 of 34

Modules: math, random and More

8 min 30 XP

Python comes with hundreds of ready-made modules. Use them with import. The math module has maths functions.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

The random module is useful for games, OTPs and quizzes. You get a different answer every time you press Run!

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

Use from ... import to bring in just what you need.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

Challenge

+30 XP

Read a circle's radius with input() (it may be a decimal). Using math.pi, print its area (π × r × r) rounded to 2 decimal places.

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.