-
Notifications
You must be signed in to change notification settings - Fork 2
/
7_imports.py
36 lines (27 loc) · 1.31 KB
/
7_imports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# IMPORTS
# Let's say we want to generate a random number between 1 and 3. How
# would be do it? The 'random' module is a really easy way to do this:
import random
print(random.randint(1,3))
print()
# ^ Woah, where did that comes from?
# When you 'import' a module, such as 'random', you are accepting all the
# code from that particular module. What's a module, exactly? It could be a
# another .py file or a file of .py files. Let's see what random is:
print(random) # Out: <module 'random' from 'C:\\Python36\\lib\\random.py'>
# ^ Hmm, looks like it gives us a file path in our computer. It points to a py
# file.py called random.py! Random.py is just another python file that
# consists of a class, functions, methods, and variables that we can use.
# 'math' is a nother popular module that contains many popular math functions
# and numbers
import math
print(math.pi) # Out: 3.141592653589793#
print(math.radians(360)) # Out: 6.283185307179586
# Bonus: If you only need one or two things from the module, you can
# specifically say so:
from math import sin, pi
print(sin(pi / 2)) # Out: 1.0
# ^ Now, you don't have to place 'math.' in front of it
# import statements should always be placed on the first lines of your script.
# For the purposes of this tutorial, I placed it about the script, but that is
# not best practices