Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

6.6 KiB

Отчет по теме 4

Коваленко Дмитрий, А-01-23

4 Встроенные функции

4.1 Изучим стандартные функции

функция round

>>> round(123.456,1)
123.5
>>> round(123.456,0)
123.0
>>> round(123.456)
123

функция range

>>> gg=range(76,123,9)
>>> list(gg)
[76, 85, 94, 103, 112, 121]
>>> range(23)
range(0, 23)

Получили последовательность от 0 до 22 с шагом 1

функция zip

>>> qq = ['Kovalenko', 'Ivanov', 'Mahnov', 'Hodyuk']
>>> ff=zip(gg,qq)
>>> tuple(ff)
((76, 'Kovalenko'), (85, 'Ivanov'), (94, 'Mahnov'), (103, 'Hodyuk'))

функция eval

>>> fff=float(input('коэффициент усиления=')); dan=eval('5*fff-156')
коэффициент усиления=2
>>> dan
-146.0

функция exec

>>> exec(input('введите инструкции:'))
введите инструкции:perem=-123.456;gg=round(abs(perem)+98,3)
>>> gg
221.456

самостоятельно изучим функции divmod и map

a = map(lambda val: len(val), qq)
>>> print(list(a))
[9, 6, 6, 6]

>>> divmod(10, 3)
(3, 1)

4.2 Изучим функции модуля math

 >> import math
>>> dir(math)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
>>> help(math.factorial)
factorial(x, /)
    Find x!.
    
    Raise a ValueError if x is negative or non-integral.
>>> math.factorial(5)  
120
>>> math.sin(2 * math.pi / (7 + math.exp(0.23)))
0.6895048136223224

4.3 Изучим функции модуля cmath

import cmath
>>> dir(cmath)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10', 'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau']
>>> cmath.sqrt(1.2-0.5j)
(1.118033988749895-0.22360679774997896j)
>>> cmath.phase(1-0.5j)
-0.4636476090008061

4.4 Изучим функции модуля random

>>> import random
 dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_floor', '_inst', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randbytes', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
help(random.seed)
seed(a=None, version=2) method of random.Random instance
    Initialize internal state from a seed.
    
    The only supported seed types are None, int, float,
    str, bytes, and bytearray.
    
    None or no argument seeds from current time or from an operating
    system specific randomness source if available.
    
    If *a* is an int, all bits are used.
    
    For version 2 (the default), all of the bits are used if *a* is a str,
    bytes, or bytearray.  For version 1 (provided for reproducing random
    sequences from older versions of Python), the algorithm for str and
    bytes generates a narrower range of seeds.

>>> random.seed()

random.seed()
>>> randoms = [random.gauss(0, 1), random.random(), random.betavariate(1, 2), random.gammavariate(2, 3)]
randoms
[0.6215069973322431, 0.08807819378198645, 0.5664749235248212, 4.506587762379303]

4.5 Изучим функции модуля time

>>> import time
>>> dir(time)
['CLOCK_MONOTONIC', 'CLOCK_MONOTONIC_RAW', 'CLOCK_PROCESS_CPUTIME_ID', 'CLOCK_REALTIME', 'CLOCK_THREAD_CPUTIME_ID', 'CLOCK_UPTIME_RAW', '_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'clock_getres', 'clock_gettime', 'clock_gettime_ns', 'clock_settime', 'clock_settime_ns', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'monotonic_ns', 'perf_counter', 'perf_counter_ns', 'process_time', 'process_time_ns', 'sleep', 'strftime', 'strptime', 'struct_time', 'thread_time', 'thread_time_ns', 'time', 'time_ns', 'timezone', 'tzname', 'tzset']

>>> c1=time.time()
>>> c1
1760084296.501437

>>> c2=time.time()-c1
>>> c2
19.090100049972534

>>> dat=time.gmtime()
>>> dat
time.struct_time(tm_year=2025, tm_mon=10, tm_mday=10, tm_hour=8, tm_min=19, tm_sec=3, tm_wday=4, tm_yday=283, tm_isdst=0)
>>> dat.tm_mon
10
>>> dat.tm_min
19

>>> time.localtime()
time.struct_time(tm_year=2025, tm_mon=10, tm_mday=10, tm_hour=11, tm_min=22, tm_sec=32, tm_wday=4, tm_yday=283, tm_isdst=0)

>>> time.localtime(c1)
time.struct_time(tm_year=2025, tm_mon=10, tm_mday=10, tm_hour=11, tm_min=18, tm_sec=16, tm_wday=4, tm_yday=283, tm_isdst=0)

4.6 Изучим графические функции

>>> import pylab
>>> x=list(range(-3,55,4))
>>> t=list(range(15))
pylab.plot(t,x)
pylab.title('Первый график')
pylab.xlabel('время')
pylab.ylabel('сигнал')
pylab.show()

Полученный график сохранил как Ris1.png

>>> X1=[12,6,8,10,7] 
>>> X2=[5,7,9,11,13]
pylab.plot(X1)
pylab.plot(X2)
pylab.show()

region=['Центр','Урал','Сибирь','Юг']
naselen=[65,12,23,17]
pylab.pie(naselen,labels=region)
pylab.show()  

Полученный график сохранил как Ris2.png

X1=[12,6,8,10,7, 7, 7, 6, 12, 12, 12] 
pylab.hist(X1)
pylab.show()

region=['Центр','Урал','Сибирь','Юг']
naselen=[65,12,23,17]
pylab.bar(region, naselen)
pylab.show()

4.7 Изучим функции модуля statistics

import statistics
>>> dir(statistics)

>>> a = [1, 2, 3, 4, 5]
>>> statistics.mean(a)
3
>>> statistics.sqrt(9)
3.0
>>> statistics.exp(3)
20.085536923187668