1 of 76

ОиМП Python

Семинар 2.

Минеев Игорь Евгеньевич

2 of 76

“Трюк”

0 ** 0�0 ** 1

3 of 76

“Трюк”

0 ** 0 == 1�0 ** 1 == 0

x != 0 == True�0 ** x == 0

4 of 76

“Кто хочет стать питонистом”

1 % 2 // 3

0) 1

1) 0

2) ZeroDivisionError

5 of 76

“Кто хочет стать питонистом”

(1 % 2) // 3

0) 1

1) 0

2) ZeroDivisionError

6 of 76

Чему равно данное значение?

False == False != True

1) False 2) True�3) False 4) SyntaxError

7 of 76

Странные (нет) сравнения

a < b < c��a < b == c��F == F != T

(a < b) and (b < c)��(a < b) and (b == c)��(F == F) and (F != T)

8 of 76

Время вопросов

9 of 76

Float.

10 of 76

Числа с плавающей🏊 точкой - float

a = 0.1

b = a + 1

c = a % 10.3

11 of 76

Угадай-ка

3.5 % 1

12 of 76

Угадай-ка

3.5 % 1 == 0.5

13 of 76

Угадай-ка

3.5 % 1 == 0.5

-0.2 % 3

14 of 76

Угадай-ка

3.5 % 1 == 0.5

-0.2 % 3 == 2.8

15 of 76

Угадай-ка

3.5 % 1 == 0.5

-0.2 % 3 == 2.8

1_000_000_999_999_999.999 + 1

16 of 76

Угадай-ка

3.5 % 1 == 0.5

-0.2 % 3 == 2.8

1_000_000_999_999_999.999 + 1 == 1_000_001_000_000_001

17 of 76

Угадай-ка

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

18 of 76

Угадай-ка

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

19 of 76

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

20 of 76

Время вопросов

>>> import antigravity

21 of 76

String.

22 of 76

String

a = 'Hello'

a += 'Hello' # 'HelloHello'

b = 'H' * 10

c = a * b

23 of 76

String

a = 'Hello'

a += 'Hello' # 'HelloHello'

b = 'H' * 10

c = a * b

24 of 76

Срезы [ from : to : step]

s[from] +

s[from + step] +

s[from + step + step] + … +

s[from + step * k]

где k - такое, что

step * k < to <= step * (k + 1)

25 of 76

Срезы [ from : to : step]

string = 'abcde12345'

string[0] == 'a'

string[-1] == '5'

string[2:4] == 'cd'

string[-5:-3] == '12'

string[-3:-6:-2] == '31'

26 of 76

Полезно

find rfind

split

strip lstrip rstrip

count

replace

len

index rindex

startswith endswith

join

zfill ljust rjust

27 of 76

Время вопросов

def procrastinate():

pass

28 of 76

List.

29 of 76

Simple example

a = [2, 3, 4, 5] # Many

b = [] # Empty

c = [1] # One

d = list() # Empty

30 of 76

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]

31 of 76

Simple example

a = [0] * 3 # [0, 0, 0]

len(a) == 3

x = input().split() # list of words

a[1:]

32 of 76

Simple example

a = [3, 4, 5]

for elem in a:

print(elem)

for i in range(len(a)):

print(a[i])

# Только если явно нужен индекс

33 of 76

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])

# Только если явно нужен индекс

34 of 76

Simple example

a = [3, 4, “dad”, [1, 2, 3]]

for elem in a:

print(elem)

for i in range(len(a)):

print(a[i])

# Только если явно нужен индекс

35 of 76

input

a = list(map(int, input().split()))

def f(x):

return int(x)

a = list(map(f, input().split()))

36 of 76

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)

37 of 76

Simple example *

a = [0]

a.append(a) # a.append(a.copy())

print(a[0])

a.append(2)

print(a[1][2])

print(a)

38 of 76

Время вопросов

39 of 76

While.

40 of 76

Выведите все натуральные числа до 10

print(1, 2, 3, 4, 5, 6, 7, 8, 9, sep='\n')

number = 1

while number < 10:

print(number)

number += 1

print(0)

41 of 76

While Else Sugar

number = int(input())

while number <= 10:

print(number)

number += 1

break

else:

print('number is greater than 10')

42 of 76

Continue

while ...:

x = int(input())

if x > 10:

continue

print(x + 1)

...

while ...:

x = int(input())

if x <= 10:

print(x + 1)

...

43 of 76

Время вопросов

Why does Python live on land?

Because it’s above C-level.

44 of 76

For.

45 of 76

For

for something in something_iterable:

if not something:

continue

print(something)

46 of 76

range(start, stop, step)

r = range(1, 5) # 1, 2, 3, 4

for i in range(10, 7, -1):

print(i)

47 of 76

range(stop)

r = range(5) # 0, 1, 2, 3, 4

�n = int(input())

for i in range(n):

print(i)

48 of 76

For Else Sugar

number = 10

for i in range(number):

if f(i):

number = i

else:

print('number not found')

49 of 76

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)

50 of 76

Время вопросов

51 of 76

Dict.

52 of 76

Simple example

a = {2: 3, 4: 5} # Many

b = {} # Empty

c = {1: 1} # One

d = dict() # Empty

53 of 76

Simple example

a = {2: 3}

a[2] = 4 # {2: 4}

a[1] = 5 # {2: 4, 1: 5}

a.pop(2) # {1: 5}

a.clear() # {}

54 of 76

Simple example

a = {2: 3}

a[3] # error

a.get(2, None) # a.get(2) == None

a.update({4: 2})

a.popitem() # ???

55 of 76

Время вопросов

>>> import __hello__

56 of 76

Set.

57 of 76

Simple example

a = {2, 3, 4, 5} # Many

b = {} # Empty

c = {1} # One

d = set() # Empty

58 of 76

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}

59 of 76

Simple example

a = {0, 1}

len(a) == 2

a.add(1) # {0, 1}

for elem in a:

print(elem)

60 of 76

Simple example

a = [1, 2, 10000000, 2]

b = set(a) # {1, 2, 10000000}

c = list(b) # [10000000, 1, 2]

61 of 76

Example

a = set()

a.add((1, 2))

a.add([1, 2])

a.add(a)

a.add("text")

62 of 76

Example

a = {i * i for i in range(4)}

a == {0, 1, 4, 9}

a.update([2, 3], (7, 8))

v = x in a:

63 of 76

Remove

a = {0, 1, 4, 9}

a.remove(2) # error

a.discard(3)

a.pop() # ???

a.clear()

64 of 76

Union

a, b = {0, 1}, {2, 1}

a | b # {0, 1, 2}

a.union(b)

a == {0, 1, 2}

65 of 76

Intersection

a, b = {0, 1}, {2, 1}

a & b # {1}

a.intersection(b)

a == {1}

66 of 76

Difference

a, b = {0, 1}, {2, 1}

a - b # {0}

a.difference(b)

a == {0}

67 of 76

Symmetric Difference

a, b = {0, 1}, {2, 1}

a ^ b # {0, 2}

a.symmetric_difference(b)

a == {0, 2}

68 of 76

List

[i + 2 for i in range(10)]

[i + 2 for i in range(10) if i < 5]

69 of 76

Время вопросов

>>> import this

70 of 76

Functions.

71 of 76

Simple example

def jushOne():

return 1

def same(x):

return x

72 of 76

Simple example

def ignoreIt(ignore_me):

return None

def multiply(x, y, z):

return x * y * z

73 of 76

Recursion

def myFunc(x):

print(x)

if x == 0:

return

myFunc(x + 1)

return

myFunc(1)

74 of 76

Named arguments

def namedArgs(arg1=None, arg2=2):

return ...��namedArgs()namedArgs(arg2=7)namedArgs(arg2=1, arg1=8)

75 of 76

Named arguments

def mixedArgs(x, y, z=None):

return ...��mixedArgs(1, 2)mixedArgs(1, 2, 3) # !!!mixedArgs(1, 2, z=3)

76 of 76

Scope of variables *

if True:

x = 10

print(x)

def f():

x = 10

print(x)