Вы находитесь на странице: 1из 59

1

WWW.PENTALOG.COM
2

WWW.PENTALOG.COM
Python is an interpreted high-level programming
language for general-purpose programming.
Created by Guido van Rossum and first
released in 1991, Python has a design philosophy
that emphasizes code readability, notably using
significant whitespace. It provides constructs that
enable clear programming on both small and
large scales.

WWW.PENTALOG.COM
- easy to learn
- powerful programming language
- efficient high-level data structures
- simple but effective approach to OOP
- elegant syntax
- dynamic typing
- scripting and rapid application development

WWW.PENTALOG.COM
Guido van Rossum is a
Dutch programmer best known
as the author of the Python
programming language, for
which he is the "Benevolent
Dictator For Life" (BDFL).
From 2005 to December 2012,
he worked at Google, where
he spent half of his time
developing the Python
language. In January 2013, he
started working for Dropbox.

WWW.PENTALOG.COM
Monty Python were a British surreal comedy group who
created their sketch comedy show Monty Python's Flying
Circus, which first aired on the BBC in 1969.

WWW.PENTALOG.COM
Who uses python?

Bitbucket, Dropbox, Facebook, Google, Instagram, Netflix,


Pinterest, Quora, Reddit, Spotify, SurveyMonkey, Uber,
Yelp, Youtube

https://realpython.com/world-class-companies-using-python/

https://stackshare.io/python

https://www.python.org/about/success/

WWW.PENTALOG.COM
Installing python (Windows)

WWW.PENTALOG.COM
pip, pypi.org

youtube-dl
requests
flask
ipython

WWW.PENTALOG.COM
Editors & IDEs

IDLE
Vim, Emacs
Atom, Visual Studio Code
Notepad++
Sublime Text
PyCharm

10

WWW.PENTALOG.COM
Sublime Text

< 10 MB
Settings: tabs to spaces
File: Open Folder
View: sidebar, minimap
View: layout 1, 2, 3, 4
Plugins: Anaconda

11

WWW.PENTALOG.COM
Python Syntax

12

WWW.PENTALOG.COM
Variables, Data Types

question = 'What is the answer?'


answer = 42
percent = 0.25
power_on = True
disabled = False

13

WWW.PENTALOG.COM
Boolean operations

and
or
not

14

WWW.PENTALOG.COM
Console Input/Output

print('Some text')
print(1024)
print(question, answer)
name = input('Your name:')
age = int(input('Your age:'))

15

WWW.PENTALOG.COM
Conditionals

if age < 18:


print("You're still a kid")
elif len(name) < 2:
print("That's not a name")
else:
print('Hello ', name, '. Enjoy your beer!')

16

WWW.PENTALOG.COM
Looping with while

while not done:


option = read_option()
if option == 0:
break
if option > 9:
print('Valid options: 1..9')
continue
done = process_option(option)

17

WWW.PENTALOG.COM
Data Structures

18

WWW.PENTALOG.COM
Data Structures

list - ['this', 'is', 'the', 1]


set - {1, 3, 5, 7, 11}
dict - {'John': 24, 'Jane': 22}
tuple - (13.5, 7, 255)

19

WWW.PENTALOG.COM
Lists

a = [1, 2, 3, 4, 5]
print(a)
print(a[0], a[-1], a[1:2])

del a[0]

a.append('x')
has_item = 4 in a

20

WWW.PENTALOG.COM
Looping with for

elements = ['some', 'items', 3.14, 42, True]

for item in elements:


print(item)

21

WWW.PENTALOG.COM
List comprehension

fruits = ['Banana', 'Apple', 'Lime']


loud_fruits = [f.upper() for f in fruits]
print(loud_fruits)

22

WWW.PENTALOG.COM
Dictionaries

d = {'k': 'value'}
has_key = 'k' in d
value = d['k']
other_value = d.get('k2', 'missing')
d['k3'] = 'some value'
del d['k']

for key, value in d.items():


print(key, value)

23

WWW.PENTALOG.COM
Tuples

p = (100, 120, 150)


c = (33, 44, 55)

p[0], p[1], p[2]

24

WWW.PENTALOG.COM
Named Tuples
from collections import namedtuple

Point = namedtuple('Point', ['x', 'y', 'z'])


Color = namedtuple('Color', ['r', 'g', 'b'])

p = Point(1, 2, 3)
c = Color(r=100, g=200, b=250)

print(p.x, p[0], c.b, c[2])

25

WWW.PENTALOG.COM
Files

26

WWW.PENTALOG.COM
File I/O

f = open('data.txt', 'r')
data = f.read()
f.close()

with open('data.txt') as f:
data = f.read()

# do stuff with data after closing the file

27

WWW.PENTALOG.COM
Functions

28

WWW.PENTALOG.COM
Functions

def do_the_dew(*args, **kwargs):


pass

def add(a=42, b=0):


return a + b

r = [add(1, 1), add(a=1, b=2), add(b=3), add()]

29

WWW.PENTALOG.COM
Classes and OOP

30

WWW.PENTALOG.COM
Classes

class Example:
def __init__(self, x):
self.x = x

def show_info(self):
print('Example:', self.x)

ex = Example(3.14159) # object
ex.show_info()

31

WWW.PENTALOG.COM
Classes

@property

@classmethod
@staticmethod

def __repr__(self):
# ...

32

WWW.PENTALOG.COM
OOP

Abstraction
Encapsulation
Inheritance
Polymorphism

Duck Typing

33

WWW.PENTALOG.COM
Design Patterns

34

WWW.PENTALOG.COM
Design Patterns

Iterator
__iter__, __next__, StopIteration

Generator
yield

@Decorator

35

WWW.PENTALOG.COM
Modules and Packages

36

WWW.PENTALOG.COM
Modules, Packages
# some_module.py
# some_package/__init__.py

import some_module
import some_package
from some_module import SomeClass, some_fn

if __name__ == '__main__':
# ...

37

WWW.PENTALOG.COM
Standard Library

38

WWW.PENTALOG.COM
Standard Library

datetime: date, datetime


time: time, sleep
os: getcwd(), system(),
sys: argv
random: choice, sample, randrange, random
smtplib, poplib, email
json, csv
sqlite3

39

WWW.PENTALOG.COM
Threads & Processes

import threading
Thread, start, join, target, Lock

import multiprocessing
Process, start, join, target, Lock

40

WWW.PENTALOG.COM
Unit Testing

import unittest

class TestMath(unittest.Testcase):

def test_add_with_zero(self):
result = 42 + 0
self.assertEquals(result, 42)

41

WWW.PENTALOG.COM
Errors and Exceptions

42

WWW.PENTALOG.COM
Errors & Exceptions

Syntax Error vs Exception

Common: ValueError, TypeError, KeyError

try/except/finally

raise

43

WWW.PENTALOG.COM
Web Development

44

WWW.PENTALOG.COM
Flask
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
return 'Hello, WWW!'

app.run(host='0.0.0.0', port=5000)

45

WWW.PENTALOG.COM
HTML

<html>
<head>
<meta charset="UTF-8"> <meta name="viewport" …
<title> … </title>
<style> CSS </style>
</head>
<body>
<h1> … </h1> <p>...</p> <img src=".." width="...">
<form method=".."> <input name=".." type=".."> </form>
</body>
</html>

46

WWW.PENTALOG.COM
CSS

p {
background-color: silver;
border: 1px red dotted;
color: black;
display: block;
font-family: sans-serif;
font-size: 16px;
margin: 10px;
padding: 4px;
text-align: justify;
}
47

WWW.PENTALOG.COM
RDBMS, SQL, sqlite3

48

WWW.PENTALOG.COM
SQL & RDBMS

CREATE
ALTER
DROP

SELECT
INSERT
UPDATE
DELETE

49

WWW.PENTALOG.COM
SQL & RDBMS

PK
Index

FK
One-to-many

Many-to-many
junction table

50

WWW.PENTALOG.COM
sqlite3

import sqlite3

conn = sqlite3.connect('example.db')
c = conn.cursor()

x = ('0; DROP TABLE example; -- ',) # never trust user input


c.execute('SELECT * FROM example WHERE x=?', x)
result = c.fetchone()

conn.close()

51

WWW.PENTALOG.COM
Git

git config

git init
git clone

git diff
git add
git commit -m "Verb regarding the changes"

git push origin master

52

WWW.PENTALOG.COM
Heroku

heroku login
heroku create

server.py with a Flask app


requirements.txt with Flask and waitress

Procfile
web: waitress-serve server:app

git push heroku master

53

WWW.PENTALOG.COM
What’s next?

54

WWW.PENTALOG.COM
Life-Long Learning

55

WWW.PENTALOG.COM
Flask

https://youtube.com/playlist?list=PL-osiE80TeTs4UjLw5MM6OjgkjFeUxCYH

56

WWW.PENTALOG.COM
Django

https://www.djangoproject.com/
57

WWW.PENTALOG.COM
Q&A

58

WWW.PENTALOG.COM
59

WWW.PENTALOG.COM

Вам также может понравиться