Python教程 / Python tutorial
-
Upload
ee0703 -
Category
Technology
-
view
355 -
download
6
Transcript of Python教程 / Python tutorial
Python TutorialFor Beginners
对 2015-11-20
http://pqx.ee/
Intro 时
Python /ˈpaɪθɑːn/ in us
or /ˈpaɪθən/ in uk
Created By Guido van Rossum
in 1989- 1989 快以可⽣生起 快 “ ”
- ABC 快 ⼤大perl lisp
-
Monty Python's Flying Circus
BBC 来
Features and philosophy
• 了 了 • 了 中⼀一 了 • ( ) • 著 • ( & )
Language Features
• 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
there should be one and preferably only one obvious way to do it
快
• 和 • 发好
batteries included
快
• GUI • •
为过成
“ ⾃自” “ 上” “ ”
Use
• Web Web
• Scipy Numpy
• 有 (google TensorFlow)
• GUI PyQT wxWorks
• ( perl bash)
•
Python 也
Python
Raspberry Pi python GPIO Pi python
quick start
Installation /
• brew install python
• apt-get install python
• yum install python
• … also, binary Installer for Windows
Hello World
print “hello world”
• Just as simple as
Interrupter / 能
能 evaluate
能
有
有能
* python
能
能
run python code /
$ python helloWorld.py
• python 有
• CPython • IPython
• PyPy
• IronPyton
• Jython
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)
Mathematics /
+, -, *, /, //, %, **
出 (3.5 ) @
==, >, <, >=, <=, != and, or, not
=, +=, -=, *=, /=, %=
variables /
x = 1
x = ‘hello world!’
• 了 快
•
• 好
Mathematics / • (++) 不(--)
x = 3
x += 1
x -= 2
indentation /
if x >= 60:
print ‘passed!’
else:
print ‘failed!’
• { } python
• 快 • • 没
多• ...
comments /
# comments in single line
•
•'''comments in multi line
line 2
line 3
'''
conditions /
if x >= 90:
print ‘A’
elif x >= 75:
print ‘B’
elif x >= 60:
print ‘C’
else:
print ‘D’
• if else elif
conditions /
switch !!!
成if... elif... elif... else
loops /
x = 0
while(x<100):
x = 1
print x
• while
loops /
for i in range(100):
print i
• for..in..
loops / • continue break (c java )x = 0
while True:
x = x + 1
if x > 100:
break
loops / • while for..in..
• do..while
• until
• goto
data structures
numbers /
• int• float
• complex
str /
• 好
• % format
•
list /
•
• 的
• (List comprehensions)
dict / 都( )
•
• 的
tuple / 个• 快 都 key
• list ,
• 新
set / 是
• 新 list
>>> set([1, 2, 3, 4, 1, 2])
set([1, 2, 3, 4])
bool /
• True / False
• python :
None
• False
•
• int• float• complex
Mutable Immutable
• str
• list• set• dict
• tuple
•
functions
function / • 快 None
def square(x):
return x*x
• 要 快 快
def pow(x, y=2):
return x**y
好 在• 好 在
pow(y=3, x=2)
• *
def square_sum(*values):
s = 0
for i in values:
s += i*i
return s
( )• * *
def print_values(**values):
for k in values:
print ‘%s => %s’ % (k, values[k])
class and objects 了
class / 了 好
class Animal():
def __init__(self, name):
self.name = name
def sayHi(self):
print self.name, 'says Hi!'
builtin method / 要• __init__ • __del__ • __repr__ • __str__ • __unicode__
• 会
•__x _classname__x
class Cat(Animal):
def __init__(self, name='kitty'):
self.name = name
def sayHi(self):
print ‘hello %s mio!’ % self.name
了
•了
•
• 1+2 (1).__add__(2)
•
module and import
module /
• 最
• sys.modules
开 开 PYTHONPATH 开 要
import /
• 快
• (__import__)
• execfile
input and output 下 下
IO
• raw_input (python3 input)
• sys.stdin sys.stdout
• open
• read
• write
json
• dumps
• loads
advanced features
• lambda ( )
•
• map reduce filter
•
—
• dir hasattr getattr setattr
• eval exec
• locals globals
要
• 成 __doc__ __name__ __file__
• : __dict__ __class__
•了: __doc__ __module__ __dict__ __bases__
• yield 有
• 有
• 了
• C/C++ 分 python
summary
• 了 了 • 了 中⼀一 了 • ( ) • 著 • ( & )
Features /
https://github.com/vinta/awesome-python
去 C
JAVA C#
Python !!!