Download - Python. Введение
![Page 1: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/1.jpg)
PythonВведение
Алексей Бованенко1
![Page 2: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/2.jpg)
Python. История
● Конец 80-х○ Декабрь 1988
● Guido van Rossum○ Нидерланды
● Python 2.0 - 16 октября 2000 ● Python 3.0 - 3 декабря 2008
2
![Page 3: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/3.jpg)
Python. Интерпретатор
● Интерактивный режим○ python
■ >>> ввод команд● Запуск скриптов на python
○ python script.py■ текстовый файл с набором команд
3
![Page 4: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/4.jpg)
Python. Интерактивный режим
● >>>5+2○ 8
● >>>3-1○ 2
● a=’Hello, world!’● print(a)
4
![Page 5: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/5.jpg)
Python. Файлы .py
● Текстовые файлы● Набор команд языка python
5
![Page 6: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/6.jpg)
Python. Числа
● Целые числа○ a, b = 12, 13
● Числа с плавающей запятой○ a, b=18.14, 13.2e2
● Приведение○ b=int(a)○ a=float(b)
6
![Page 7: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/7.jpg)
Python. Арифметические операторы
● +, -, *, /, //, **, %, &, |, ^○ a+b○ a-b○ a*b○ a/b; a//b○ a**b○ a%b○ a&b; a|b; a^b
7
![Page 8: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/8.jpg)
Python. Строки
● s=’Hello, world!’● s=”Hello, world!”● s=’’’Hello,
world!’’’● s=’Hello,’ ‘ world!’● s=(‘Hello,’
‘world!’)● print(s)
8
![Page 9: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/9.jpg)
Python. Строки. \
● s=’Hello,\tworld!’○ Hello, world!
● s=’Hello,\nworld!’○ Hello,
world!● s=’’’Hello,\
… world!’’’○ Hello, world!
9
![Page 10: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/10.jpg)
Python. Строки. Длина. Символ
● s=’Hello, world!’● len(s)
○ 13● s[0]...s[12]
○ H…!● s[2:4]
○ ll10
![Page 11: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/11.jpg)
Python. Строки. Конкатенация, повтор символов● Конкатенация
○ s1=’Hello,’○ s2=’world!’○ s2=’ ‘+s2
s3=s1+s2● Повтор символов
○ s1=’ab’○ s2=s1*3
11
![Page 12: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/12.jpg)
Python. Строки. Днина. Приведение
● s1=’Hello, world!’● Длина
○ len(s1)● Приведение
○ a=1○ s=str(a)○ a=int(s)
12
![Page 13: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/13.jpg)
Python. Оператор if
● if(условие):pass
● if(условие):pass
else:pass
13
● if(условие1):pass
elif(условие2):pass
else:pass
![Page 14: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/14.jpg)
Python. Логические операторы
● >, <, >=, <=, ==, !=○ a<b; a>b; a<=b; a>=b; a==b; a!=b
● and, or, not○ if((условие1)and(условие2)):○ if((условие1)or(условие2)):○ if(not(условие1)):
14
![Page 15: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/15.jpg)
Python. Цикл while
● while (условие):pass
● while(i<10):print(i)i=i+1
15
![Page 16: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/16.jpg)
Python. Цикл for
● for i in collection:pass
● for i in range(start,end):pass
16
![Page 17: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/17.jpg)
Python. Циклы. break
● break○ for i in range(10):
if(i==2): break print(i)
17
![Page 18: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/18.jpg)
Python. Циклы. continue
● continue○ for i in range(10):
if(i==2): continue print(i)
18
![Page 19: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/19.jpg)
Python. Циклы. else
● else○ for i in range(10):
print(i)if(i==11)
breakelse print(‘end’)
19
![Page 20: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/20.jpg)
Python. List
● list = []● list = [1, 2, 3, 4, 5]● list = [[1,2,3], [4,5,6], [7,8,9]]
20
![Page 21: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/21.jpg)
Python. List
l=[]
l.append([])
l[i].append(val)
print(l[i][j])
21
![Page 22: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/22.jpg)
Python. List
● list.append(x)● list.extend(L)● list.insert(i, x)● list.remove(x)● list.pop([i])● list.clear()
22
![Page 23: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/23.jpg)
Python. List
● list.index(x)● list.count(x)● list.copy() # list[:]● list.reverse()● list.sort(key=None, reverse=False)● del list[i]
23
![Page 24: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/24.jpg)
Python. Set
● s = { ‘apple’, ‘banana’, ‘orange’ }● ‘tulip’ in s● s = set(‘Hello, world’)
○ set(['!', ' ', 'e', 'd', 'H', 'l', 'o', ',', 'r', 'w'])● a = set(‘Hello’)● s - a
○ set(['!', ' ', 'd', ',', 'r', 'w'])24
![Page 25: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/25.jpg)
Python. Dict
● d=dict()● d={'a':1, 'b':2}● d['c']=4
○ {'a': 1, 'c': 4, 'b': 2}● for k,v in d.items():
... print(k,v)● d.keys()
25
![Page 26: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/26.jpg)
Python. IO
● open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)○ 'r' - open for reading (default)○ 'w' - open for writing, truncating the file first○ 'x' - open for exclusive creation, failing if the file already exists○ 'a' - open for writing, appending to the end of the file if it exists○ 'b' - binary mode○ 't' - text mode (default)
● close()
26
![Page 27: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/27.jpg)
Python. IO
● f.read([size])○ empty string
● f.readline()○ empty string
● f.readlines()
27
![Page 28: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/28.jpg)
Python. IO
with open(‘filename.name’, ‘r’) as fie:
text = file.read()
with open(‘filename.name’, ‘r’) as file:
lines = file.readlines()
28
![Page 29: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/29.jpg)
Python. IO
with open(‘filename.name’, ‘r’) as file:
for line in file:
print(line)
with open(‘filename.name’, ‘r’) as file:
lines = list(file)
29
![Page 30: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/30.jpg)
Python. IO
● f.write(string)● with open(‘filename,name’, ‘w’) as f:
f.write(‘Hello, world!’)
30
![Page 31: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/31.jpg)
Python. Function
● def func_name(arg1, arg2,...): # func body # func body return val
● func_name(1,3)
31
![Page 32: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/32.jpg)
Python. try/except
● try:● except ExceptionType:● else:● raise
32
![Page 33: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/33.jpg)
Python. try/excepttry:
i = int(raw_input('Enter:'))
except ValueError:
print('Enter the number')
else:
print('In else')
33
![Page 34: Python. Введение](https://reader031.vdocuments.pub/reader031/viewer/2022021922/587fe70d1a28ab46228b596b/html5/thumbnails/34.jpg)
Cпасибо за внимание
34
Вопросы?