Last active
November 29, 2017 10:54
-
-
Save pelid/8709a594ddb5fe696cea435241baab36 to your computer and use it in GitHub Desktop.
Debug prints & data types. Use print, type, dir commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cities = ['Moscow', 'Paris', 'Berlin', 'Madrid', 'Oslo'] | |
for i in cities: | |
print(i) | |
# Упражение на навык отладки кода с помощью print(), type() и dir(): | |
# - какой тип данных хранится в cities ? | |
# - какой тип данных хранится в i ? | |
# - откуда берутся значения i? | |
# - какие методы и атрибуты есть у переменной i ? | |
# - какие методы и атрибуты есть у переменной cities ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
friends = ['John', 'Pat', 'Gary', 'Michael'] | |
for i, name in enumerate(friends): | |
print("iteration {iteration} is {name}".format(iteration=i, name=name)) | |
# Ответь на вопросы используя print, type и dir: | |
# - какой тип данных хранится в i ? | |
# - какой тип данных хранится в name ? | |
# - какой тип данных имеет enumerate(friends) ? | |
# - откуда берутся значения i и name ? | |
# - какие методы есть у строки "iteration {iteration} is {name}" ? | |
# - какой тип данных передается в print ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import localtime | |
activities = { | |
8: 'Sleeping', | |
9: 'Commuting', | |
17: 'Working', | |
18: 'Commuting', | |
20: 'Eating', | |
22: 'Resting', | |
} | |
time_now = localtime() | |
hour = time_now.tm_hour | |
for activity_time in sorted(activities.keys()): | |
if hour < activity_time: | |
print(activities[activity_time]) | |
break | |
else: | |
print('Unknown, AFK or sleeping!') | |
# Ответь на вопросы используя print, type и dir: | |
# - какой тип данных имеет значение переменной activities ? | |
# - какой тип данных у time_now ? | |
# - какие методы есть у time_now ? | |
# - какой тип данных имеет hour ? | |
# - какой тип данных имеет activities.keys(), что хранит? | |
# - какой тип данных имеет sorted(activities.keys()) ? | |
# - чем sorted(activities.keys()) отличается от activities.keys() ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
try: | |
total = sum(int(arg) for arg in sys.argv[1:]) | |
print('sum =', total) | |
except ValueError as error: | |
print('Please supply integer arguments') | |
# Ответь на вопросы используя print, type и dir: | |
# - что делает эта программа? как её запустить? как воспользоваться? | |
# - что хранит sys.argv ? какой тип данных? откуда берутся эти значения? | |
# - какой тип данных имеет переменная sys? какие у нее есть аттрибуты? | |
# - какой тип данных передается на вход функции sum ? какие у него есть методы и свойства? | |
# - какой тип данных имеет error ? какие у него есть атрибуты? | |
# - выведи traceback ошибки |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
def iter_primes(): | |
numbers = itertools.count(2) | |
while True: | |
prime = next(numbers) | |
yield prime | |
numbers = filter(prime.__rmod__, numbers) | |
for p in iter_primes(): | |
if p > 1000: | |
break | |
print(p) | |
# Ответь на вопросы используя print, type и dir: | |
# - какой тип данных имеет numbers на строке 4 ? что это? какие у него есть методы? | |
# - что хранится в перменной prime? | |
# - что делает next ? | |
# - какой тип данных у prime.__rmod__ ? | |
# - что делает filter ? | |
# - какой тип данных имеет numbers на строке 9 ? | |
# - какой тип данных имеет значение переменной iter_primes ? А какой тип данных у результата вызова iter_primes() ? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment