форкнуто от main/python-labs
Родитель
b80d5a6828
Сommit
9dbd830a77
@ -0,0 +1,76 @@
|
||||
# Индивидуальное контрольное задание по Теме 3
|
||||
Таболин Иван, А-01-23
|
||||
Вариант 4
|
||||
## Задание
|
||||
M1_4
|
||||
1) Как установить рабочий (текущий) каталог в среде? Какую пользу можно получить от такой установки?
|
||||
|
||||
2) Напишите инструкции, обеспечивающие подсчет числа букв (без запятой, скобок и пробелов) в данном предложении. Отобразите результат с использованием формата по шаблону: " в предложении ХХ букв".
|
||||
|
||||
3) Создайте числовой объект со значением 2345. Поочередно представьте и отобразите на экране это число в двоичном, восьмеричном и шестнадцатеричном виде. Определите класс созданного объекта и отобразите список его атрибутов. Напишите инструкцию определения числа разрядов в двоичном представлении числа.
|
||||
|
||||
4) Создайте объект со значением {-45,78,90,-3,56}. Определите класс этого объекта. Атрибуты объекта запишите в объект-кортеж. Напишите инструкцию, позволяющую проверить наличие метода clear у этого кортежа.
|
||||
|
||||
5) Подсчитайте сумму элементов в объекте, созданном в п.4. Отобразите результат по шаблону: "Сумма элементов=ХХХ".
|
||||
|
||||
## Решение
|
||||
1) Рабочий каталог устанавливается следующим образом:
|
||||
```py
|
||||
import os
|
||||
os.chdir('')
|
||||
```
|
||||
Внутри кавычек указывается путь к рабочему каталогу.
|
||||
Польза установки рабочего каталога заключается в том, что можно не указывать путь к файлам, находящимся в этом же каталоге.
|
||||
2)
|
||||
```py
|
||||
str1='Напишите инструкции, обеспечивающие подсчет числа букв (без запятой, скобок и пробелов) в данном предложении.';str1
|
||||
'Напишите инструкции, обеспечивающие подсчет числа букв (без запятой, скобок и пробелов) в данном предложении.'
|
||||
str1=str1.replace(' ','');str1=str1.replace(',','');str1=str1.replace('.','');str1=str1.replace('(','');str1=str1.replace(')','')
|
||||
str1
|
||||
'Напишитеинструкцииобеспечивающиеподсчетчислабуквбеззапятойскобокипробеловвданномпредложении'
|
||||
l=len(str1);l
|
||||
91
|
||||
out='В предложении %d букв'
|
||||
print(out % l)
|
||||
В предложении 91 букв
|
||||
```
|
||||
3)
|
||||
```py
|
||||
n10=2345
|
||||
n2=bin(n10);n2;type(n2);dir(n2)
|
||||
'0b100100101001'
|
||||
<class 'str'>
|
||||
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
|
||||
|
||||
kol_razr=len(bin(n10)) - 2; kol_razr
|
||||
12
|
||||
|
||||
n8=oct(n10);n8;type(n8);dir(n8)
|
||||
'0o4451'
|
||||
<class 'str'>
|
||||
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
|
||||
|
||||
n16=hex(n10);n16;type(n16);dir(n16)
|
||||
'0x929'
|
||||
<class 'str'>
|
||||
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
|
||||
```
|
||||
4)
|
||||
```py
|
||||
mnoz1={-45,78,90,-3,56}
|
||||
type(mnoz1);dir(mnoz1)
|
||||
<class 'set'>
|
||||
['__and__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
|
||||
attr=tuple(dir(mnoz1))
|
||||
'clear' in dir(attr)
|
||||
False
|
||||
```
|
||||
5)
|
||||
```py
|
||||
sum(mnoz1)
|
||||
176
|
||||
s = sum(mnoz1)
|
||||
out2='Сумма элементов=%g'
|
||||
print(out % s)
|
||||
В предложении 176 букв
|
||||
```
|
||||
Загрузка…
Ссылка в новой задаче