|
| 1 | +''' |
| 2 | +Now that we've used a module, statistics, it would be a good time to |
| 3 | +explain some import practices. As with many things in programming, there |
| 4 | +are many ways to import modules, but there are certainly some best |
| 5 | +practices. |
| 6 | +
|
| 7 | +So first, when you import a module, you are basically loading that |
| 8 | +module into memory. Think of a module like a script. Many if not most modules |
| 9 | +are just a single python script. So, when you go to import it, you use the file |
| 10 | +name. This can help keep code clean and easy to read. Many python developers |
| 11 | +just program everything in 1 script. Other developers, say from a language like |
| 12 | +java are going to be very used to doing lots of imports with a file for each |
| 13 | +type of job that's happening. Just like there are many ways to import, there |
| 14 | +are many more ways to program. |
| 15 | +
|
| 16 | +So let's talk about basic importing: |
| 17 | +''' |
| 18 | + |
| 19 | +# here, we have imported the entire statistics module, and we can reference |
| 20 | +# any of the functions within it. |
| 21 | +# this loads that entire module, and will run any code that is |
| 22 | +# set to run in the module |
| 23 | +# remember if name == main? Again, that's what we use to stop code |
| 24 | +# from running when we just want to import. |
| 25 | +import statistics |
| 26 | + |
| 27 | +example_list = [5,2,5,6,1,2,6,7,2,6,3,5,5] |
| 28 | + |
| 29 | +# now, to use our import above, since we just imported statistics, |
| 30 | +# we must preceed any funciton wi thin statistics with a statistics. |
| 31 | +# so, for example, to access the mean() function within statistics: |
| 32 | + |
| 33 | +print(statistics.mean(example_list)) |
| 34 | + |
| 35 | + |
| 36 | +# and that's it. Let's look briefly at the statistics module and the mean |
| 37 | +# function...c:/python34/lib/statistics (not in site-packages) |
| 38 | +# take note of the package's location, we will talk more on that soon. |
| 39 | +# so now you see that statistics is just a python script, and mean is |
| 40 | +# just another function. |
| 41 | + |
| 42 | +# sometimes, you will see people who import things "as". This |
| 43 | +# helps to keep typing to a minimum. For example |
| 44 | + |
| 45 | +import statistics as s |
| 46 | + |
| 47 | +# above we've imported statistics as s, so when we want to reference something |
| 48 | +# within statistics, we just need to do s. now. |
| 49 | + |
| 50 | +print(s.mean(example_list)) |
| 51 | + |
| 52 | +# What if you don't even want to type that S though? Well there's an app for that! |
| 53 | +# you can just import all of the functions like so: |
| 54 | + |
| 55 | +from statistics import mean |
| 56 | + |
| 57 | +# so here, we've imported the mean function only. |
| 58 | + |
| 59 | +print(mean(example_list)) |
| 60 | + |
| 61 | +# and again we can do as |
| 62 | + |
| 63 | +from statistics import mean as m |
| 64 | + |
| 65 | +print(m(example_list)) |
| 66 | + |
| 67 | +# what if we want to import other functions? |
| 68 | + |
| 69 | +from statistics import mean, median |
| 70 | + |
| 71 | +# here we imported 2 functions. |
| 72 | + |
| 73 | +print(median(example_list)) |
| 74 | + |
| 75 | +# what if we want to use the as as well???? |
| 76 | + |
| 77 | +from statistics import mean as m, median as d |
| 78 | + |
| 79 | +print(m(example_list)) |
| 80 | +print(d(example_list)) |
| 81 | + |
| 82 | + |
| 83 | +# What if we want to just import everything from statistics like we did initially |
| 84 | +# but we dont want to type the statistics because we have |
| 85 | +# fat fingers and this will just slow us down?. |
| 86 | + |
| 87 | +from statistics import * |
| 88 | + |
| 89 | +print(mean(example_list)) |
0 commit comments