python教程 / python tutorial

77
Python Tutorial For Beginners ŞŃ对 2015-11-20 http://pqx.ee/

Upload: ee0703

Post on 11-Apr-2017

310 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: Python教程 / Python tutorial

Python TutorialFor Beginners

对 2015-11-20

http://pqx.ee/

Page 2: Python教程 / Python tutorial

Intro 时

Page 3: Python教程 / Python tutorial

Python /ˈpaɪθɑːn/ in us

or /ˈpaɪθən/ in uk

Page 4: Python教程 / Python tutorial

Created By Guido van Rossum

in 1989- 1989 快以可⽣生起 快 “ ”

- ABC 快 ⼤大perl lisp

-

Page 5: Python教程 / Python tutorial

Monty Python's Flying Circus

BBC 来

Page 6: Python教程 / Python tutorial
Page 7: Python教程 / Python tutorial

Features and philosophy

Page 8: Python教程 / Python tutorial

• 了 了 • 了 中⼀一 了 • ( ) • 著 • ( & )

Language Features

Page 9: Python教程 / Python tutorial

• Beautiful is better than ugly • Explicit is better than implicit • Simple is better than complex • Complex is better than complicated • Readability counts

The Zen of Python

Page 10: Python教程 / Python tutorial

there should be one and preferably only one obvious way to do it

• 和 • 发好

Page 11: Python教程 / Python tutorial

batteries included

• GUI • •

Page 12: Python教程 / Python tutorial
Page 13: Python教程 / Python tutorial

为过成

“ ⾃自” “ 上” “ ”

Page 14: Python教程 / Python tutorial

Use

Page 15: Python教程 / Python tutorial

• Web Web

• Scipy Numpy

• 有 (google TensorFlow)

• GUI PyQT wxWorks

• ( perl bash)

Page 16: Python教程 / Python tutorial

Python 也

Page 17: Python教程 / Python tutorial

Python

Page 18: Python教程 / Python tutorial

Raspberry Pi python GPIO Pi python

Page 19: Python教程 / Python tutorial

quick start

Page 20: Python教程 / Python tutorial

Installation /

• brew install python

• apt-get install python

• yum install python

• … also, binary Installer for Windows

Page 21: Python教程 / Python tutorial

Hello World

print “hello world”

• Just as simple as

Page 22: Python教程 / Python tutorial

Interrupter / 能

能 evaluate

有能

* python

Page 23: Python教程 / Python tutorial

run python code /

$ python helloWorld.py

• python 有

• CPython • IPython

• PyPy

• IronPyton

• Jython

Page 24: Python教程 / Python tutorial

run python code / • REPL 也

$ python

Python 2.7.10 (default, Jul 14 2015, 19:46:27)

[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

>>> print "hello world"

hello world

(Read-Eval-Print Loop)

Page 25: Python教程 / Python tutorial

Mathematics /

+, -, *, /, //, %, **

出 (3.5 ) @

==, >, <, >=, <=, != and, or, not

=, +=, -=, *=, /=, %=

Page 26: Python教程 / Python tutorial

variables /

x = 1

x = ‘hello world!’

• 了 快

• 好

Page 27: Python教程 / Python tutorial

Mathematics / • (++) 不(--)

x = 3

x += 1

x -= 2

Page 28: Python教程 / Python tutorial

indentation /

if x >= 60:

print ‘passed!’

else:

print ‘failed!’

• { } python

Page 29: Python教程 / Python tutorial

• 快 • • 没

Page 30: Python教程 / Python tutorial

多• ...

Page 31: Python教程 / Python tutorial

comments /

# comments in single line

•'''comments in multi line

line 2

line 3

'''

Page 32: Python教程 / Python tutorial

conditions /

if x >= 90:

print ‘A’

elif x >= 75:

print ‘B’

elif x >= 60:

print ‘C’

else:

print ‘D’

• if else elif

Page 33: Python教程 / Python tutorial

conditions /

switch !!!

成if... elif... elif... else

Page 34: Python教程 / Python tutorial

loops /

x = 0

while(x<100):

x = 1

print x

• while

Page 35: Python教程 / Python tutorial

loops /

for i in range(100):

print i

• for..in..

Page 36: Python教程 / Python tutorial

loops / • continue break (c java )x = 0

while True:

x = x + 1

if x > 100:

break

Page 37: Python教程 / Python tutorial

loops / • while for..in..

• do..while

• until

• goto

Page 38: Python教程 / Python tutorial

data structures

Page 39: Python教程 / Python tutorial

numbers /

• int• float

• complex

Page 40: Python教程 / Python tutorial

str /

• 好

• % format

Page 41: Python教程 / Python tutorial

list /

• 的

• (List comprehensions)

Page 42: Python教程 / Python tutorial

dict / 都( )

• 的

Page 43: Python教程 / Python tutorial

tuple / 个• 快 都 key

• list ,

• 新

Page 44: Python教程 / Python tutorial

set / 是

• 新 list

>>> set([1, 2, 3, 4, 1, 2])

set([1, 2, 3, 4])

Page 45: Python教程 / Python tutorial

bool /

• True / False

• python :

Page 46: Python教程 / Python tutorial

None

• False

Page 47: Python教程 / Python tutorial

• int• float• complex

Mutable Immutable

• str

• list• set• dict

• tuple

Page 48: Python教程 / Python tutorial

functions

Page 49: Python教程 / Python tutorial

function / • 快 None

def square(x):

return x*x

Page 50: Python教程 / Python tutorial

• 要 快 快

def pow(x, y=2):

return x**y

Page 51: Python教程 / Python tutorial

好 在• 好 在

pow(y=3, x=2)

Page 52: Python教程 / Python tutorial

• *

def square_sum(*values):

s = 0

for i in values:

s += i*i

return s

Page 53: Python教程 / Python tutorial

( )• * *

def print_values(**values):

for k in values:

print ‘%s => %s’ % (k, values[k])

Page 54: Python教程 / Python tutorial

class and objects 了

Page 55: Python教程 / Python tutorial

class / 了 好

class Animal():

def __init__(self, name):

self.name = name

def sayHi(self):

print self.name, 'says Hi!'

Page 56: Python教程 / Python tutorial

builtin method / 要• __init__ • __del__ • __repr__ • __str__ • __unicode__

Page 57: Python教程 / Python tutorial

• 会

•__x _classname__x

Page 58: Python教程 / Python tutorial

class Cat(Animal):

def __init__(self, name='kitty'):

self.name = name

def sayHi(self):

print ‘hello %s mio!’ % self.name

Page 59: Python教程 / Python tutorial

•了

Page 60: Python教程 / Python tutorial

• 1+2 (1).__add__(2)

Page 61: Python教程 / Python tutorial

module and import

Page 62: Python教程 / Python tutorial

module /

• 最

• sys.modules

开 开 PYTHONPATH 开 要

Page 63: Python教程 / Python tutorial

import /

• 快

• (__import__)

• execfile

Page 64: Python教程 / Python tutorial

input and output 下 下

Page 65: Python教程 / Python tutorial

IO

• raw_input (python3 input)

• sys.stdin sys.stdout

• print

Page 66: Python教程 / Python tutorial

• open

• read

• write

Page 67: Python教程 / Python tutorial

json

• dumps

• loads

Page 68: Python教程 / Python tutorial

advanced features

Page 69: Python教程 / Python tutorial

• lambda ( )

• map reduce filter

Page 70: Python教程 / Python tutorial

• dir hasattr getattr setattr

• eval exec

• locals globals

Page 71: Python教程 / Python tutorial

• 成 __doc__ __name__ __file__

• : __dict__ __class__

•了: __doc__ __module__ __dict__ __bases__

Page 72: Python教程 / Python tutorial

• yield 有

• 有

• 了

• C/C++ 分 python

Page 73: Python教程 / Python tutorial

summary

Page 74: Python教程 / Python tutorial

• 了 了 • 了 中⼀一 了 • ( ) • 著 • ( & )

Features /

Page 75: Python教程 / Python tutorial

https://github.com/vinta/awesome-python

Page 76: Python教程 / Python tutorial

去 C

JAVA C#

Page 77: Python教程 / Python tutorial

Python !!!