python. Введение

34
Python Введение Алексей Бованенко 1

Upload: alexey-bovanenko

Post on 14-Apr-2017

144 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Python. Введение

PythonВведение

Алексей Бованенко1

Page 2: Python. Введение

Python. История

● Конец 80-х○ Декабрь 1988

● Guido van Rossum○ Нидерланды

● Python 2.0 - 16 октября 2000 ● Python 3.0 - 3 декабря 2008

2

Page 3: Python. Введение

Python. Интерпретатор

● Интерактивный режим○ python

■ >>> ввод команд● Запуск скриптов на python

○ python script.py■ текстовый файл с набором команд

3

Page 4: Python. Введение

Python. Интерактивный режим

● >>>5+2○ 8

● >>>3-1○ 2

● a=’Hello, world!’● print(a)

4

Page 5: Python. Введение

Python. Файлы .py

● Текстовые файлы● Набор команд языка python

5

Page 6: Python. Введение

Python. Числа

● Целые числа○ a, b = 12, 13

● Числа с плавающей запятой○ a, b=18.14, 13.2e2

● Приведение○ b=int(a)○ a=float(b)

6

Page 7: Python. Введение

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. Введение

Python. Строки

● s=’Hello, world!’● s=”Hello, world!”● s=’’’Hello,

world!’’’● s=’Hello,’ ‘ world!’● s=(‘Hello,’

‘world!’)● print(s)

8

Page 9: Python. Введение

Python. Строки. \

● s=’Hello,\tworld!’○ Hello, world!

● s=’Hello,\nworld!’○ Hello,

world!● s=’’’Hello,\

… world!’’’○ Hello, world!

9

Page 10: Python. Введение

Python. Строки. Длина. Символ

● s=’Hello, world!’● len(s)

○ 13● s[0]...s[12]

○ H…!● s[2:4]

○ ll10

Page 11: Python. Введение

Python. Строки. Конкатенация, повтор символов● Конкатенация

○ s1=’Hello,’○ s2=’world!’○ s2=’ ‘+s2

s3=s1+s2● Повтор символов

○ s1=’ab’○ s2=s1*3

11

Page 12: Python. Введение

Python. Строки. Днина. Приведение

● s1=’Hello, world!’● Длина

○ len(s1)● Приведение

○ a=1○ s=str(a)○ a=int(s)

12

Page 13: Python. Введение

Python. Оператор if

● if(условие):pass

● if(условие):pass

else:pass

13

● if(условие1):pass

elif(условие2):pass

else:pass

Page 14: Python. Введение

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. Введение

Python. Цикл while

● while (условие):pass

● while(i<10):print(i)i=i+1

15

Page 16: Python. Введение

Python. Цикл for

● for i in collection:pass

● for i in range(start,end):pass

16

Page 17: Python. Введение

Python. Циклы. break

● break○ for i in range(10):

if(i==2): break print(i)

17

Page 18: Python. Введение

Python. Циклы. continue

● continue○ for i in range(10):

if(i==2): continue print(i)

18

Page 19: Python. Введение

Python. Циклы. else

● else○ for i in range(10):

print(i)if(i==11)

breakelse print(‘end’)

19

Page 20: Python. Введение

Python. List

● list = []● list = [1, 2, 3, 4, 5]● list = [[1,2,3], [4,5,6], [7,8,9]]

20

Page 21: Python. Введение

Python. List

l=[]

l.append([])

l[i].append(val)

print(l[i][j])

21

Page 22: Python. Введение

Python. List

● list.append(x)● list.extend(L)● list.insert(i, x)● list.remove(x)● list.pop([i])● list.clear()

22

Page 23: Python. Введение

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. Введение

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. Введение

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. Введение

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. Введение

Python. IO

● f.read([size])○ empty string

● f.readline()○ empty string

● f.readlines()

27

Page 28: Python. Введение

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. Введение

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. Введение

Python. IO

● f.write(string)● with open(‘filename,name’, ‘w’) as f:

f.write(‘Hello, world!’)

30

Page 31: Python. Введение

Python. Function

● def func_name(arg1, arg2,...): # func body # func body return val

● func_name(1,3)

31

Page 32: Python. Введение

Python. try/except

● try:● except ExceptionType:● else:● raise

32

Page 33: Python. Введение

Python. try/excepttry:

i = int(raw_input('Enter:'))

except ValueError:

print('Enter the number')

else:

print('In else')

33

Page 34: Python. Введение

Cпасибо за внимание

34

Вопросы?