Пример #1
0
Use this.
Imports the module X, and creates a reference to that module in the current namespace. Or in other words, after you’ve run this statement, you can use X.name to refer to things defined in module X.
"""
from X import *

"""
Do not use this. Namespace pollution
After you’ve run this statement, you can simply use a plain name to refer to things defined in module X. But X itself is not defined, so X.name doesn’t work.
"""
from X import a, b, c

"""
You can now use a and b and c in your program. But not X.a or X.d.
"""

import X as s

"""
This will allow you to basically rename the module to whatever you want. People generally do this to shorten the name of the module. Matplotlib.pyplot is often imported as plt and numpy is often imported as np, for example.
"""
from statistics import mean as m

"""
Same as above but with the functions/objects
"""

from statistics import mean as m, median as d

print(m(example_list))
print(d(example_list))
Пример #2
0
# allows you to reference the statistics module without having to type its name each time you call it
import statistics as s

# this allows you to reference items within a module without having to use the dot operator
# you can import multiple items simultaneously
from statistics import mean, median

# 'from' and 'as' can be used in conjunction like this:
from statistics import mode as m, stdev as d

# we can import everything contained in a module
# use this if you want to call the functions without having to say 'statistics.'
from statistics import *

numbers = [
    1,
    2,
    3,
    4,
    4,
    5,
    6,
    7,
    8,
    9,
    0,
]
print(s.variance(numbers))
print(mean(numbers))
print(d(numbers))
import statistics
lista = [1, 5, 2, 6, 4, 2, 6, 5, 4, 52, 1, 5, 2, 5, 6, 1]
print(statistics.mean(lista))
# wow moduł liczy na średnią z liczb zawartych w liście!
# to jest podstawowy sposób importowania modułów, ale istnieje wiele więcej

# dobra, to teraz kolejny sposób

import statistics as s  # teraz cały moduł ukryty jest pod literą {
print(s.mean(lista))

# możesz importować tylko niektóre funkcje z danego moduły, żeby nie zapychać pamięci
from statistics import mean
print(mean(lista))

# i teraz użyć odpowiedniego skrótu
from statistics import mean as m
print(m(lista))

# mozemy importować kilka funkcji
from statistics import mean as m, median as d
print(m(lista))
print(d(lista))

# możemy zaimportować także wszsytkie funkcje z danego modułu i wedy wywołując
# poszczególne funkcje nie trzeba wpisywać nazwy biblikoteki

from statistics import *
print(mean(lista))
print(variance(lista))
Пример #4
0
# want. People generally do this to shorten the name of the module.
# Matplotlib.pyplot is often imported as plt and numpy is often imported as
# np, for example

import statistics as s
print(s.mean(example_list))

# What if you don't even want to type that S though? Well there's an app for
# that! You can just import each function within the module you plan to use:

from statistics import mean
# so here, we've imported the mean function only.
print(mean(example_list))

# and again we can do as
from statistics import mean as m
print(m(example_list))

# What about more functions?

from statistics import mean as m, median as d
print(m(example_list))
print(d(example_list))

# What if we want to just import everything from statistics like we did
# initially, but we don't want to type the statistics because we have fat
# fingers and this will just slow us down?.

from statistics import *
print(mean(example_list))
Пример #5
0
#   23. Module Import Syntax

import statistics as s
from statistics import median, stdev as d, variance as v

list = [1, 12, 67, 2, 3, 7, 9, 7, 4]

print('mean:', s.mean(list))
print('median:', median(list))
print('stdev:', d(list))
print('variance:', v(list))