python. Введение
TRANSCRIPT
PythonВведение
Алексей Бованенко1
Python. История
● Конец 80-х○ Декабрь 1988
● Guido van Rossum○ Нидерланды
● Python 2.0 - 16 октября 2000 ● Python 3.0 - 3 декабря 2008
2
Python. Интерпретатор
● Интерактивный режим○ python
■ >>> ввод команд● Запуск скриптов на python
○ python script.py■ текстовый файл с набором команд
3
Python. Интерактивный режим
● >>>5+2○ 8
● >>>3-1○ 2
● a=’Hello, world!’● print(a)
4
Python. Файлы .py
● Текстовые файлы● Набор команд языка python
5
Python. Числа
● Целые числа○ a, b = 12, 13
● Числа с плавающей запятой○ a, b=18.14, 13.2e2
● Приведение○ b=int(a)○ a=float(b)
6
Python. Арифметические операторы
● +, -, *, /, //, **, %, &, |, ^○ a+b○ a-b○ a*b○ a/b; a//b○ a**b○ a%b○ a&b; a|b; a^b
7
Python. Строки
● s=’Hello, world!’● s=”Hello, world!”● s=’’’Hello,
world!’’’● s=’Hello,’ ‘ world!’● s=(‘Hello,’
‘world!’)● print(s)
8
Python. Строки. \
● s=’Hello,\tworld!’○ Hello, world!
● s=’Hello,\nworld!’○ Hello,
world!● s=’’’Hello,\
… world!’’’○ Hello, world!
9
Python. Строки. Длина. Символ
● s=’Hello, world!’● len(s)
○ 13● s[0]...s[12]
○ H…!● s[2:4]
○ ll10
Python. Строки. Конкатенация, повтор символов● Конкатенация
○ s1=’Hello,’○ s2=’world!’○ s2=’ ‘+s2
s3=s1+s2● Повтор символов
○ s1=’ab’○ s2=s1*3
11
Python. Строки. Днина. Приведение
● s1=’Hello, world!’● Длина
○ len(s1)● Приведение
○ a=1○ s=str(a)○ a=int(s)
12
Python. Оператор if
● if(условие):pass
● if(условие):pass
else:pass
13
● if(условие1):pass
elif(условие2):pass
else:pass
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
Python. Цикл while
● while (условие):pass
● while(i<10):print(i)i=i+1
15
Python. Цикл for
● for i in collection:pass
● for i in range(start,end):pass
16
Python. Циклы. break
● break○ for i in range(10):
if(i==2): break print(i)
17
Python. Циклы. continue
● continue○ for i in range(10):
if(i==2): continue print(i)
18
Python. Циклы. else
● else○ for i in range(10):
print(i)if(i==11)
breakelse print(‘end’)
19
Python. List
● list = []● list = [1, 2, 3, 4, 5]● list = [[1,2,3], [4,5,6], [7,8,9]]
20
Python. List
l=[]
l.append([])
l[i].append(val)
print(l[i][j])
21
Python. List
● list.append(x)● list.extend(L)● list.insert(i, x)● list.remove(x)● list.pop([i])● list.clear()
22
Python. List
● list.index(x)● list.count(x)● list.copy() # list[:]● list.reverse()● list.sort(key=None, reverse=False)● del list[i]
23
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
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
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
Python. IO
● f.read([size])○ empty string
● f.readline()○ empty string
● f.readlines()
27
Python. IO
with open(‘filename.name’, ‘r’) as fie:
text = file.read()
with open(‘filename.name’, ‘r’) as file:
lines = file.readlines()
28
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
Python. IO
● f.write(string)● with open(‘filename,name’, ‘w’) as f:
f.write(‘Hello, world!’)
30
Python. Function
● def func_name(arg1, arg2,...): # func body # func body return val
● func_name(1,3)
31
Python. try/except
● try:● except ExceptionType:● else:● raise
32
Python. try/excepttry:
i = int(raw_input('Enter:'))
except ValueError:
print('Enter the number')
else:
print('In else')
33
Cпасибо за внимание
34
Вопросы?