# Отчет по теме 4 Похил Анастасия, А-02-23 ### 1. Настройка текущего каталога. ```py import os os.chdir('C://Users//Настя//Desktop//python-labs//TEMA3//') ``` ### 2. Стандартные функции. #### 2.1. Функция округления числа с заданной точностью ```py help(round) Help on built-in function round in module builtins: round(number, ndigits=None) Round a number to a given precision in decimal digits. The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as the number. ndigits may be negative. round(123.456,1) 123.5 round(123.456,0) 123.0 type(round(123.456,0)) type(round(123.456,1)) round(123.456) 123 type(round(123.456)) ``` #### 2.2. Функция range ```py gg=range(76,123,9) gg range(76, 123, 9) type(gg) list(gg) [76, 85, 94, 103, 112, 121] range(23) range(0, 23) #range(23) создает последовательность целых чисел от 0 до 22 с шагом 1 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] ``` #### 2.3. Функция zip ```py qq = ["Похил", "Сарайкина", "Снегура", "Шинкаренко"] ff=zip(gg,qq) ff tuple(ff) ((76, 'Похил'), (85, 'Сарайкина'), (94, 'Снегура'), (103, 'Шинкаренко')) ff[0] Traceback (most recent call last): File "", line 1, in ff[0] TypeError: 'zip' object is not subscriptable ``` #### 2.4. Функция eval ```py fff = float(input('коэффициент усиления=')); dan = eval('5*fff-156') коэффициент усиления=4 dan -136.0 type(dan) ``` #### 2.5. Функция exec ```py exec(input('введите инструкции:')) введите инструкции:perem=-123.456;gg=round(abs(perem)+98,3) gg 221.456 ``` -eval() - для вычислений с возвратом результата, exec() - для выполнения кода без возврата значения. #### 2.6. Применение функций abs, pow, max, min, sum, divmod, len, map ```py abs(-50) 50 abs(123) 123 pow(2,3) 8 pow(5,2) 25 max(1, 5, 3) 5 max([10, 2, 8, 4]) 10 max("a", "A") #сравнение строк в Python происходит по ASCII кодам символов 'a' max("a", "b") 'b' min([10, 2, 8, 4]) 2 sum((10, 20, 30)) divmod(10, 3) (3, 1) divmod(3.5, 2) (1.0, 1.5) len({"a": 1, "b": 2}) 2 len([1, 2, 3, 4, 5]) list(map(abs, [-1, -2, 3])) [1, 2, 3] numbers = [1, 2, 3, 4, 5] list(map(str, numbers)) ['1', '2', '3', '4', '5'] ``` ### 3. Функции из стандартного модуля 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'] help(math.factorial) Help on built-in function factorial in module math: factorial(n, /) Find n!. math.factorial(5) 120 math.sin(0) 0.0 math.acos(0) 1.5707963267948966 math.degrees(math.pi) 180.0 math.radians(180) 3.141592653589793 math.exp(1) 2.718281828459045 math.log(100) 4.605170185988092 math.log10(10) 1.0 math.sqrt(9) 3.0 math.ceil(3.7) 4 math.floor(3.2) 3 math.sin(2 * math.pi / 7 + math.exp(0.23)) 0.8334902641414562 ``` ### 4. Функции из модуля cmath. ```py cmath.sqrt(1.2-0.5j) (1.118033988749895-0.22360679774997896j) cmath.phase(1-0.5j) -0.4636476090008061 cmath.sqrt(-4) 2j ``` ### 5. Стандартный модуль 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'] help(random.seed) Help on method seed in module random: 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.random() 0.6159477691885248 random.uniform(1, 10) 7.581530495835095 random.randint(1, 100) 5 random.gauss(0, 1) -0.3506558234077829 random.choice(numbers) 1 list1 = [1, 2, 3, 4, 5] random.shuffle(list1) list1 [3, 5, 2, 4, 1] random.sample(numbers, 3) [3, 5, 2] random.betavariate(2, 5) 0.1832859047033012 random.gammavariate(2, 1) 1.4911292379429324 list2 = [ random.uniform(0, 1), # Равномерное распределение random.gauss(0, 1), # Нормальное распределение random.betavariate(2, 5), # Бета-распределение random.gammavariate(2, 1) # Гамма-распределение ] list2 [0.31800769775397375, -0.4796694435186039, 0.078752268353954, 2.8310089922616504] ``` ### 6. Функции из модуля 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 1759322889.1532655 c2=time.time()-c1 c2 17.157387256622314 dat=time.gmtime() dat time.struct_time(tm_year=2025, tm_mon=10, tm_mday=1, tm_hour=12, tm_min=48, tm_sec=51, tm_wday=2, tm_yday=274, tm_isdst=0) dat.tm_mon 10 dat.tm_year 2025 time.localtime() time.struct_time(tm_year=2025, tm_mon=10, tm_mday=1, tm_hour=15, tm_min=52, tm_sec=4, tm_wday=2, tm_yday=274, tm_isdst=0) time.asctime(time.localtime()) 'Wed Oct 1 15:54:51 2025' time.ctime(c1) 'Wed Oct 1 15:48:09 2025' time.sleep(2) time.mktime((2025, 1, 1, 0, 0, 0, 0, 0, 0)) 1735678800.0 time.localtime(c1) time.struct_time(tm_year=2025, tm_mon=10, tm_mday=1, tm_hour=15, tm_min=48, tm_sec=9, tm_wday=2, tm_yday=274, tm_isdst=0) ``` ### 7. Графические функции. ```py import pylab x = list(range(-3,55,4)) t = list(range(15)) pylab.plot(t,x) [] pylab.title('Первый график') Text(0.5, 1.0, 'Первый график') pylab.xlabel('время') Text(0.5, 0, 'время') pylab.ylabel('сигнал') Text(0, 0.5, 'сигнал') pylab.show() ``` ![График 1](Ris1.PNG) ```py X1=[12,6,8,10,7] X2=[5,7,9,11,13] pylab.plot(X1) [] pylab.plot(X2) [] pylab.show() ``` ![График 2](Ris2.PNG) ```py region=['Центр','Урал','Сибирь','Юг'] naselen=[65,12,23,17] pylab.pie(naselen,labels=region) ([, , , ], [Text(-0.191013134139045, 1.0832885038559115, 'Центр'), Text(-0.861328292412156, -0.6841882582231001, 'Урал'), Text(0.04429273995539947, -1.0991078896938387, 'Сибирь'), Text(0.9873750693480946, -0.48486129194837324, 'Юг')]) pylab.show() ``` ![График 3](Ris3.PNG) ```py rost = [170, 165, 180, 175, 160, 185, 172, 168, 178, 162] pylab.hist(rost) (array([2., 0., 1., 1., 2., 0., 1., 1., 1., 1.]), array([160. , 162.5, 165. , 167.5, 170. , 172.5, 175. , 177.5, 180. , 182.5, 185. ]), ) pylab.show() ``` ![График 4](Ris4.PNG) ```py predmety = ['Математика', 'Физика', 'Химия'] otm = [5, 4, 3] pylab.bar(predmety, otm) pylab.title('Оценки по предметам') Text(0.5, 1.0, 'Оценки по предметам') pylab.show() ``` ![График 5](Ris5.png) ### 8. Cтатистический модуль statistics. ```py import statistics statistics.mean(numbers) 3 statistics.median(numbers) 3 numbers [1, 2, 3, 4, 5] statistics.stdev(numbers) 1.5811388300841898 ``` ### 9. Закончен сеанс работы с IDLE.