ОиМП Python
Семинар 2.
Минеев Игорь Евгеньевич
“Трюк”
0 ** 0�0 ** 1
“Трюк”
0 ** 0 == 1�0 ** 1 == 0
x != 0 == True�0 ** x == 0
“Кто хочет стать питонистом”
1 % 2 // 3
0) 1
1) 0
2) ZeroDivisionError
“Кто хочет стать питонистом”
(1 % 2) // 3
0) 1
1) 0
2) ZeroDivisionError
Чему равно данное значение?
False == False != True
1) False 2) True�3) False 4) SyntaxError
Странные (нет) сравнения
a < b < c��a < b == c��F == F != T
(a < b) and (b < c)��(a < b) and (b == c)��(F == F) and (F != T)
Время вопросов
Float.
Числа с плавающей🏊 точкой - float
a = 0.1
b = a + 1
c = a % 10.3
Угадай-ка
3.5 % 1
Угадай-ка
3.5 % 1 == 0.5
Угадай-ка
3.5 % 1 == 0.5
-0.2 % 3
Угадай-ка
3.5 % 1 == 0.5
-0.2 % 3 == 2.8
Угадай-ка
3.5 % 1 == 0.5
-0.2 % 3 == 2.8
1_000_000_999_999_999.999 + 1
Угадай-ка
3.5 % 1 == 0.5
-0.2 % 3 == 2.8
1_000_000_999_999_999.999 + 1 == 1_000_001_000_000_001
Угадай-ка
3.5 % 1 == 0.5
-0.2 % 3 == 2.8
1_000_000_999_999_999.999 + 1 == 1_000_001_000_000_001
2.1 % 1
Угадай-ка
3.5 % 1 == 0.5
-0.2 % 3 == 2.8
1_000_000_999_999_999.999 + 1 == 1_000_001_000_000_001
2.1 % 1 == 0.10000000000000009
float
float('inf') + 1 == float('inf')
float('-inf') * -1 == float('inf')
float('nan') + 1 == �float('nan') > 1 ==�float('nan') < 1 ==�float('nan') == 1 ==
float('nan')
False
False
False
Время вопросов
>>> import antigravity
String.
String
a = 'Hello'
a += 'Hello' # 'HelloHello'
b = 'H' * 10
c = a * b
String
a = 'Hello'
a += 'Hello' # 'HelloHello'
b = 'H' * 10
c = a * b
Срезы [ from : to : step]
s[from] +
s[from + step] +
s[from + step + step] + … +
s[from + step * k]
где k - такое, что
step * k < to <= step * (k + 1)
Срезы [ from : to : step]
string = 'abcde12345'
string[0] == 'a'
string[-1] == '5'
string[2:4] == 'cd'
string[-5:-3] == '12'
string[-3:-6:-2] == '31'
Полезно
find rfind
split
strip lstrip rstrip
count
replace
len
index rindex
startswith endswith
join
zfill ljust rjust
Время вопросов
def procrastinate():
pass
List.
Simple example
a = [2, 3, 4, 5] # Many
b = [] # Empty
c = [1] # One
d = list() # Empty
Simple example
a = [0, 1]
a.append(2) # None returned
[1].insert(0, 0)
c = a + [3] # [0, 1, 2, 3]
c.pop() # [0, 1, 2]
Simple example
a = [0] * 3 # [0, 0, 0]
len(a) == 3
x = input().split() # list of words
a[1:]
Simple example
a = [3, 4, 5]
for elem in a:
print(elem)
for i in range(len(a)):
print(a[i])
# Только если явно нужен индекс
Simple example
a = [3, 4, 5]
for elem in reversed(a):
print(elem)
for i in range(len(a) - 1, -1, -1):
print(a[i])
# Только если явно нужен индекс
Simple example
a = [3, 4, “dad”, [1, 2, 3]]
for elem in a:
print(elem)
for i in range(len(a)):
print(a[i])
# Только если явно нужен индекс
input
a = list(map(int, input().split()))
def f(x):
return int(x)
a = list(map(f, input().split()))
input / output
a, b = [1, 2]
x, y = map(int, input().split())
x, y, *o = map(int, input().split())
a = list(map(int, input().split()))
print(*a)
Simple example *
a = [0]
a.append(a) # a.append(a.copy())
print(a[0])
a.append(2)
print(a[1][2])
print(a)
Время вопросов
While.
Выведите все натуральные числа до 10
print(1, 2, 3, 4, 5, 6, 7, 8, 9, sep='\n')
number = 1
while number < 10:
print(number)
number += 1
print(0)
While Else Sugar
number = int(input())
while number <= 10:
print(number)
number += 1
break
else:
print('number is greater than 10')
Continue
while ...:
x = int(input())
if x > 10:
continue
print(x + 1)
...
while ...:
x = int(input())
if x <= 10:
print(x + 1)
...
Время вопросов
Why does Python live on land?
Because it’s above C-level.
For.
For
for something in something_iterable:
if not something:
continue
print(something)
range(start, stop, step)
r = range(1, 5) # 1, 2, 3, 4
for i in range(10, 7, -1):
print(i)
range(stop)
r = range(5) # 0, 1, 2, 3, 4
�n = int(input())
for i in range(n):
print(i)
For Else Sugar
number = 10
for i in range(number):
if f(i):
number = i
else:
print('number not found')
For with string example
s = "Lada Niva"
for i in range(len(s)):
print(s[i])
# Doing same things
for c in s:
print(c)
Время вопросов
Dict.
Simple example
a = {2: 3, 4: 5} # Many
b = {} # Empty
c = {1: 1} # One
d = dict() # Empty
Simple example
a = {2: 3}
a[2] = 4 # {2: 4}
a[1] = 5 # {2: 4, 1: 5}
a.pop(2) # {1: 5}
a.clear() # {}
Simple example
a = {2: 3}
a[3] # error
a.get(2, None) # a.get(2) == None
a.update({4: 2})
a.popitem() # ???
Время вопросов
>>> import __hello__
Set.
Simple example
a = {2, 3, 4, 5} # Many
b = {} # Empty
c = {1} # One
d = set() # Empty
Simple example
a = {0, 1}
a.add(2) # None returned
b = {3}
a.update(b) # {0, 1, 2, 3}
c.remove(1) # {0, 2, 3}
Simple example
a = {0, 1}
len(a) == 2
a.add(1) # {0, 1}
for elem in a:
print(elem)
Simple example
a = [1, 2, 10000000, 2]
b = set(a) # {1, 2, 10000000}
c = list(b) # [10000000, 1, 2]
Example
a = set()
a.add((1, 2))
a.add([1, 2])
a.add(a)
a.add("text")
Example
a = {i * i for i in range(4)}
a == {0, 1, 4, 9}
a.update([2, 3], (7, 8))
v = x in a:
Remove
a = {0, 1, 4, 9}
a.remove(2) # error
a.discard(3)
a.pop() # ???
a.clear()
Union
a, b = {0, 1}, {2, 1}
a | b # {0, 1, 2}
a.union(b)
a == {0, 1, 2}
Intersection
a, b = {0, 1}, {2, 1}
a & b # {1}
a.intersection(b)
a == {1}
Difference
a, b = {0, 1}, {2, 1}
a - b # {0}
a.difference(b)
a == {0}
Symmetric Difference
a, b = {0, 1}, {2, 1}
a ^ b # {0, 2}
a.symmetric_difference(b)
a == {0, 2}
List
[i + 2 for i in range(10)]
[i + 2 for i in range(10) if i < 5]
Время вопросов
>>> import this
Functions.
Simple example
def jushOne():
return 1
def same(x):
return x
Simple example
def ignoreIt(ignore_me):
return None
def multiply(x, y, z):
return x * y * z
Recursion
def myFunc(x):
print(x)
if x == 0:
return
myFunc(x + 1)
return
myFunc(1)
Named arguments
def namedArgs(arg1=None, arg2=2):
return ...��namedArgs()�namedArgs(arg2=7)�namedArgs(arg2=1, arg1=8)
Named arguments
def mixedArgs(x, y, z=None):
return ...��mixedArgs(1, 2)�mixedArgs(1, 2, 3) # !!!�mixedArgs(1, 2, z=3)
Scope of variables *
if True:
x = 10
print(x)
def f():
x = 10
print(x)