Three Python Modules You’ll Use in Every Program

Time, Random, and Math. These are the three Python modules you’ll use in every program from programmable circuits to data analysis and games. You’ll use a least one of them in every program.

The time module contains time related functions that allow you to both count and keep track of time. The two most commonly used are:

time.time() – [no arguments] this function returns the time in seconds since the Unix Epoch in 1970. To make use of it, call it when you want to start counting, and again when you want to stop counting. Then find the difference between the start and stop. time.time() example

time.sleep() – [one argument, a time in seconds] This function causes the program to pause all actions for the specified amount of time. Useful especially in circuit and game programming.

Official Python Docs for the Time Module:

The Random module generates random value, so it’s great for simple games and testing different values.

random.randint(a,b) – Picks an exclusive integer between a and b. Good for most random number generation scenarios.

random.randrange(a, b, interval) – this function returns a value represented between a range of numbers a to b, at the interval value specified.  For example, random.randrange(1, 3, 0.5) could possible return 1.0, 1.5, 2.0, 2.5, or 3.0.

Official Python Docs for the Random Module:

The Math module allows you to use mathematical functions such as sine, cosine, or tangent, that aren’t a part of the standard operator set.

math.sqrt() – [one argument, the value to have its square root taken] Returns the square root of the argument.

math.log(a [base]) –  [One or two arguments] The logarithmic function returns the natural logarithm of a if given one argument. Otherwise, you can specify the log base in square brackets.

Other useful Math functions that are self-explanatory, where x is the argument:

  • math.sin(x)
  • math.cos(x)
  • math.tan(x)
  • math.pi 
  • math.e
  • math.degrees(x)
  • math.radians(x)

Official Python Doc for the Math Module:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s