форкнуто от main/python-labs
Вы не можете выбрать более 25 тем
Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
269 строки
10 KiB
Markdown
269 строки
10 KiB
Markdown
# Отчет по Теме 4
|
|
|
|
Грудинин Егор, А-03-23
|
|
|
|
## 1 Стандартные функции
|
|
```py
|
|
>>> round(123.456,1); round(123.456,0)
|
|
123.5
|
|
123.0
|
|
>>> round(123.456,-1)
|
|
120.0
|
|
>>> type(round(123.456,1))
|
|
<class 'float'>
|
|
>>> type(round(123.456,0))
|
|
<class 'float'>
|
|
>>> round(123.456); type(round(123.456))
|
|
123
|
|
<class 'int'>
|
|
```
|
|
Если же в качестве второго аргумента функции round нияего не указывать, то округление будет происходить до целого. Поэтому результат - число целочисленного типа данных (int). В противном же случае результатом будет число вещественного типа.
|
|
```py
|
|
>>> gg=range(76,123,9); gg; type(gg)
|
|
range(76, 123, 9)
|
|
<class 'range'>
|
|
>>> list(gg)
|
|
[76, 85, 94, 103, 112, 121]
|
|
>>> range(23)
|
|
range(0, 23)
|
|
>>> list(range(23))
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
|
|
```
|
|
Если в функцию range передать всего один аргумент, то результатом выполнения функции будет итерируемый объет класса range с целочисленными значениями от 0 до того числа, которое было указано в качестве аргумента, не включительно c шагом по умолчанию, равным единице.
|
|
|
|
```py
|
|
>>> qq = ['Grudinin', 'Butko', 'Vasiliev', 'Semenov']
|
|
>>> ff = zip(gg,qq); ff
|
|
<zip object at 0x000001FA9B4AD340>
|
|
>>> type(ff)
|
|
<class 'zip'>
|
|
>>> tuple(ff)
|
|
((76, 'Grudinin'), (85, 'Butko'), (94, 'Vasiliev'), (103, 'Semenov'))
|
|
>>> ff[0]; ff[3]
|
|
Traceback (most recent call last):
|
|
File "<pyshell#32>", line 1, in <module>
|
|
ff[0]; ff[3]
|
|
TypeError: 'zip' object is not subscriptable
|
|
```
|
|
В получившемся кортеже мы видим 4 объекта-кортежа, что соответствует длине самого короткого списка из двух (len(gg) = 6, len(qq) = 4). Заметим также, что к объекту ff типа zip нельзя обращаться по индексам.
|
|
```py
|
|
>>> fff=float(input('коэффициент усиления=')); dan=eval('5*fff-156');
|
|
коэффициент усиления=100
|
|
>>> dan; type(dan)
|
|
344.0
|
|
<class 'float'>
|
|
>>> 5*100 - 156
|
|
344
|
|
>>> exec(input('введите инструкции:'))
|
|
введите инструкции:perem=-123.456;gg=round(abs(perem)+98,3)
|
|
>>> gg
|
|
221.456
|
|
>>> abs(-10.12)
|
|
10.12
|
|
>>> pow(2,5)
|
|
32
|
|
>>> pow(2,5.3)
|
|
39.396621227037315
|
|
>>> pow(2.4,-5.3)
|
|
0.009657849177552984
|
|
>>> max(1,-2)
|
|
1
|
|
>>> min([1,3,-5,-122])
|
|
-122
|
|
>>> sum([1,3,5,3,7,4])
|
|
23
|
|
|
|
>>> divmod(9,5)
|
|
(1, 4)
|
|
>>> a = [1,2,3,4,5,6,7,8,9]
|
|
>>> len(a)
|
|
9
|
|
>>> a = map(int, input().split()); a
|
|
1 2 3 4 5 6
|
|
<map object at 0x000001FA9878E4D0>
|
|
>>> list(a)
|
|
[1, 2, 3, 4, 5, 6]
|
|
```
|
|
## 2 Функции из стандартного модуля math
|
|
```py
|
|
>>> import math
|
|
>>> dir(math)
|
|
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fma', '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', 'sumprod', 'tan', 'tanh', 'tau', 'trunc', 'ulp']
|
|
>>> math.factorial(5)
|
|
120
|
|
>>> math.sin(math.pi/2)
|
|
1.0
|
|
>>> math.acos(-1)
|
|
3.141592653589793
|
|
>>> math.degrees(math.pi)
|
|
180.0
|
|
>>> math.radians(270)
|
|
4.71238898038469
|
|
>>> math.pi/2*3
|
|
4.71238898038469
|
|
>>> math.exp(1); math.exp(3)
|
|
2.718281828459045
|
|
20.085536923187668
|
|
|
|
>>> math.log(math.exp(1))
|
|
1.0
|
|
>>> math.log(math.exp(3))
|
|
3.0
|
|
>>> math.log10(pow(10,5))
|
|
5.0
|
|
>>> math.sqrt(121); math.sqrt(25)
|
|
11.0
|
|
5.0
|
|
>>> math.ceil(3.2); math.ceil(6.999)
|
|
4
|
|
7
|
|
>>> math.floor(3.2); math.floor(6.999)
|
|
3
|
|
6
|
|
>>> math.sin(2*math.pi/7 + pow(math.exp(1), 0.23))
|
|
0.8334902641414562
|
|
```
|
|
## 3 Функции из модуля cmath
|
|
```py
|
|
>>> import cmath
|
|
>>> dir(cmath)
|
|
['__doc__', '__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 Стандартный модуль random
|
|
```py
|
|
>>> import random
|
|
>>> dir(random)
|
|
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_ONE', '_Sequence', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_fabs', '_floor', '_index', '_inst', '_isfinite', '_lgamma', '_log', '_log2', '_os', '_parse_args', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', 'betavariate', 'binomialvariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'main', 'normalvariate', 'paretovariate', 'randbytes', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
|
|
>>> random.seed()
|
|
>>> random.uniform(1,10)
|
|
7.820969962495622
|
|
>>> random.random()
|
|
0.21580642037220688
|
|
>>> random.randint(1,10)
|
|
1
|
|
>>> random.gauss(10, 2)
|
|
6.560077457806456
|
|
>>> random.choice([1,2,3,4,5,6,7,8,9])
|
|
1
|
|
>>> x = [1,2,3,4,5,6,7,8,9]
|
|
>>> random.shuffle(x); x
|
|
[5, 7, 4, 3, 8, 1, 9, 2, 6]
|
|
>>> random.betavariate(1,2)
|
|
0.31509637997467377
|
|
>>> random.gammavariate(1,2)
|
|
0.0676205462545973
|
|
>>> ls = [random.uniform(0,10), random.gauss(0,3), random.betavariate(1,2), random.gammavariate(1,2)]; ls
|
|
[9.745005344582257, 2.16302978480045, 0.8426318147572717, 0.1932454384006428]
|
|
```
|
|
## 5 Функции из модуля time
|
|
```py
|
|
>>> import time
|
|
>>> dir(time)
|
|
['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', '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']
|
|
>>> c1 = time.time(); c1
|
|
1759138103.1649246
|
|
>>> c2=time.time()-c1; c2
|
|
21.219618320465088
|
|
>>> dat=time.gmtime(); dat
|
|
time.struct_time(tm_year=2025, tm_mon=9, tm_mday=29, tm_hour=9, tm_min=29, tm_sec=17, tm_wday=0, tm_yday=272, tm_isdst=0)
|
|
>>> dat.tm_mon
|
|
9
|
|
>>> dat.tm_year; dat.tm_sec
|
|
2025
|
|
17
|
|
>>> time.localtime()
|
|
time.struct_time(tm_year=2025, tm_mon=9, tm_mday=29, tm_hour=12, tm_min=30, tm_sec=53, tm_wday=0, tm_yday=272, tm_isdst=0)
|
|
>>> time.asctime(time.localtime())
|
|
'Mon Sep 29 12:31:59 2025'
|
|
>>> time.ctime()
|
|
'Mon Sep 29 12:32:27 2025'
|
|
>>> time.ctime(time.time())
|
|
'Mon Sep 29 12:33:19 2025'
|
|
>>> time.sleep(10)
|
|
>>> time.mktime(time.localtime())
|
|
1759138661.0
|
|
>>> time.time()
|
|
1759138670.3741279
|
|
```
|
|
## 6 Графические функции
|
|
```py
|
|
>>> import pylab
|
|
>>> x=list(range(-3,55,4))
|
|
>>> t=list(range(15))
|
|
>>> pylab.plot(t,x)
|
|
[<matplotlib.lines.Line2D object at 0x0000015807FF87D0>]
|
|
>>> pylab.title('Первый график')
|
|
Text(0.5, 1.0, 'Первый график')
|
|
>>> pylab.xlabel('время')
|
|
Text(0.5, 0, 'время')
|
|
>>> pylab.ylabel('сигнал')
|
|
Text(0, 0.5, 'сигнал')
|
|
>>> pylab.show()
|
|
```
|
|

|
|
|
|
Мы видим отображение линйной зависимости x(t) в виде графика функции, являющегося прямой.
|
|
```py
|
|
>>> X1=[12,6,8,10,7]; X2=[5,7,9,11,13]
|
|
>>> pylab.plot(X1)
|
|
[<matplotlib.lines.Line2D object at 0x00000158080ACF50>]
|
|
>>> pylab.plot(X2)
|
|
[<matplotlib.lines.Line2D object at 0x00000158080AD090>]
|
|
>>> pylab.show()
|
|
```
|
|

|
|
```py
|
|
>>> region=['Центр','Урал','Сибирь','Юг']
|
|
>>> naselen=[65,12,23,17]
|
|
>>> pylab.pie(naselen,labels=region)
|
|
([<matplotlib.patches.Wedge object at 0x0000015807183B60>, <matplotlib.patches.Wedge object at 0x000001580AB76990>, <matplotlib.patches.Wedge object at 0x000001580AB76D50>, <matplotlib.patches.Wedge object at 0x000001580AB76FD0>], [Text(-0.191013134139045, 1.0832885038559115, 'Центр'), Text(-0.861328292412156, -0.6841882582231001, 'Урал'), Text(0.04429273995539947, -1.0991078896938387, 'Сибирь'), Text(0.9873750693480946, -0.48486129194837324, 'Юг')])
|
|
>>> pylab.show()
|
|
```
|
|

|
|
|
|
```py
|
|
>>> data = [random.gauss() for i in range(1000)]
|
|
>>> pylab.hist(data, bins = 30)
|
|
(array([ 1., 0., 0., 0., 0., 3., 4., 15., 10., 19., 35., 53., 53.,
|
|
60., 90., 82., 90., 99., 96., 67., 65., 52., 32., 34., 9., 14.,
|
|
9., 4., 3., 1.]), array([-3.99649362, -3.76019089, -3.52388816, -3.28758542, -3.05128269,
|
|
-2.81497996, -2.57867722, -2.34237449, -2.10607176, -1.86976903,
|
|
-1.63346629, -1.39716356, -1.16086083, -0.92455809, -0.68825536,
|
|
-0.45195263, -0.2156499 , 0.02065284, 0.25695557, 0.4932583 ,
|
|
0.72956103, 0.96586377, 1.2021665 , 1.43846923, 1.67477197,
|
|
1.9110747 , 2.14737743, 2.38368016, 2.6199829 , 2.85628563,
|
|
3.09258836]), <BarContainer object of 30 artists>)
|
|
>>> pylab.show()
|
|
```
|
|

|
|
|
|
```py
|
|
>>> cat = ['Сентябрь', 'Октябрь', 'Ноябрь']
|
|
>>> values = [100, 200, 30]
|
|
>>> pylab.bar(cat, values)
|
|
<BarContainer object of 3 artists>
|
|
>>> pylab.title('Столбчатая диаграмма')
|
|
Text(0.5, 1.0, 'Столбчатая диаграмма')
|
|
>>> pylab.xlabel('Месяц')
|
|
Text(0.5, 0, 'Месяц')
|
|
>>> pylab.ylabel('Количество заявок, шт')
|
|
Text(0, 0.5, 'Количество заявок, шт')
|
|
>>> pylab.show()
|
|
```
|
|

|
|
|
|
## 7 Статистические функции
|
|
```py
|
|
>>> import statistics
|
|
>>> data = [random.gauss(10,3) for i in range(1000)]
|
|
>>> statistics.mean(data)
|
|
10.13981944059001
|
|
>>> statistics.stdev(data)
|
|
3.053185233060045
|
|
>>> statistics.median(data)
|
|
10.073166882507437
|
|
``` |