forked from vpavlenko/web-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_examples.py
70 lines (58 loc) · 2.08 KB
/
python_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# язык Python за десять минут
# ввод-вывод cтрок
first_word = input()
second_word = input()
spell = first_word + ' ' + second_word + '!'
print(spell)
# ввод-вывод чисел
first_number = int(input())
second_number = float(input())
print(first_number + second_number)
# смешанный вывод чисел и строк
platform = first_number + second_number
print('Платформа №' + str(platform))
print('Платформа №', platform, sep='')
# числа
radius = 5
pi = 3.14159
square = pi * radius ** 2
# строки
s = 'Tom Marvolo Riddle'
tokens = s.split()
first_name = tokens[0]
middle_name = tokens[1]
last_name = tokens[2]
s2 = first_name + ' ' + middle_name + ' ' + last_name
# конструкция 'if'. важно ставить отступы!
if (s == s2):
print('две строки равны')
else:
print('так не бывает')
# списки (изменяемые последовательности)
houses = ['Ravenclaw', 'Hufflepuff', 'Gryffindor']
houses.append('Slytherin')
print(len(houses))
# цикл 'for'. отступы вновь важны!
for house in houses:
print('Ten points to', house, end='!\n')
# set (неупорядоченные коллекции)
birth_name = 'Tom Marvolo Riddle'
birth_name_letters = set(birth_name)
birth_name_lower_letters = set(birth_name.lower())
nickname_lower_letters = set('I am Lord Voldemort'.lower())
print(birth_name_lower_letters == nickname_lower_letters)
print(len(birth_name_lower_letters))
fib_numbers = set([1, 1, 2, 3, 5, 8, 13, 21])
prime_numbers = set([2, 3, 5, 7, 11, 13, 17, 19])
union = fib_numbers | prime_numbers
intersection = fib_numbers & prime_numbers
difference = fib_numbers - prime_numbers
symmetric_difference = fib_numbers ^ prime_numbers
# проверка принадлежности
print(3 in prime_numbers)
print(4 in prime_numbers)
print('Griffindor' in houses)
print('ratio' in 'transfiguration')
# сортировка
houses.sort() # сортировка на месте
sorted_letters = sorted(houses[0]) # новый список