作者:Guido van Rossum
首次发行时间:1991
版本系列:2.7 / 3.5
分类:强类型动态语言
支持系统:几乎全部
本讲义的基础环境为python2.7/3.5。代码在这两种系统上测试通过。
其中有少量代码有2/3不兼容现象,在讲课时会特别提出。
本讲义的系统环境为Linux(Debian Stretch),有少量指令在windows下有变化,亦会特别提出。
知名公司
知名应用
在命令行下执行python,进入python交互界面。类似这样:
Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160409] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
出现这个界面,说明你正常的安装了python。
Jupyter是一种记事本工具。可以插入代码,进行执行,显示结果,并把这些内容保存成记事本以供后续查阅。
下面我们会大量使用Jupyter进行代码执行展示。
在交互界面敲入1 + 2,出现如下结果:
>>> 1 + 2
3
在交互界面敲入print(‘1+2’),出现如下结果:
>>> print('1+2')
1+2
1 + 2
3
print('1+2')
1+2
用任意编辑器(随你喜欢)新建一文件,修改内容为print('1+2'),然后将其保存为xxx.py文件。
然后用python执行文件:python xxx.py
注意,如果python不在默认路径中(常见于windows),需要写完整路径。
以下代码,%%开头的为jupyter内部指令。writefile为写入文件。
!开头的为系统指令。cat和rm为linux系统指令,windows下需要做变更。
%%writefile test.py
print('1+2')
Writing test.py
!cat test.py
print('1+2')
!python test.py
1+2
!rm test.py
练习:写出数字和字符串的例子,并加以注释。
52.3E-4
0.00523
'string'
'string'
"string"
'string'
'''string'''
'string'
True
True
False
False
1 == 0
False
1 == 1
True
None
在单引号字符串中表达单引号需要转义,双引号和三引号同。例如:
“what’s wrong”是合法字符串。’what’s wrong’会出错,需要写为’what\’s wrong’。
转义也可以用于表达不可见字符,例如tab:\t。
如果要表达\本身,也需要转义,写为\。
'what\'s wrong'
"what's wrong"
在字符串的开始加r,使得字符串中不发生转义。
常用于大量需要表达\的字符串。
r'what\'s wrong'
"what\\'s wrong"
print(r'what\'s wrong')
what\'s wrong
在字符串前加u,表示这是一个unicode字符串。
unicode字符串和普通字符串的区别在于。unicode字符串中,每一个位表示一个“字”。
而普通字符串中,一个“字”可能由多个位表示。尤其是中文的情况下。
unicode字符串和普通字符串外形相似,但是很多地方无法混用。
更进一步信息在“字符编码简说”这章有详细解释。
u'string'
u'string'
'string' == u'string'
True
# -*- coding: utf-8 -*-
'中文' == u'中文'
# 在Python2下不相等,Python3下相等。因为Python3下都是Unicode。
-c:2: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
False
变量需要一个名字。
首字母需要是字母或下划线,其余部分可以是字母,下划线和数字。
变量大小写敏感,但是使用大小写区分变量是不可取的。
每个变量都会指向一个值,每个值都拥有一个类型。
变量在运行过程中可以指向不同的值,拥有不同的类型。
因此在运行前无法知道名字所对应的类型。(动态类型语言特征)
算符是连接一个或多个元素的符号,用来表达计算。
常见算符:
+ - * ** / // % << >> & | ^ ~ < > <= >= == != not and or
算符和赋值的快速写法
a = 10
a = a + 10
a += 10
使用()可以改变优先级顺序
计算以下表达式的求值顺序,并求值算出结果:
1 + 8 <= 1 + 2 * 3
1 + 2 and 3 * 4 or 5 ** 6
1 + 8 <= 1 + 2 * 3
False
1 + 2 and 3 * 4 or 5 ** 6
12
表达式为一串由算符连接的元素。
表达式一般会有值,值为按照优先级依次求解表达式得到的结果。
有些表达式在语法上亦不可求值。
表达式除了值以外,还可能造成“副作用”。即造成环境改变,或执行某些操作。
Pi = 3.14
r = 5
area = Pi * r * r
print("area is {0}".format(area))
area is 78.5
将变量的值格式化到字符串中,形成新的字符串。
常用于输出数据内容。
name = 'shell'
print('my name is {0} and the length of name is {1}'.format(name, len(name)))
print('my name is %s and the length of name is %d' % (name, len(name)))
my name is shell and the length of name is 5 my name is shell and the length of name is 5
执行流控制代码控制流的先后执行顺序。
Python语句执行以行为单位。每行可选的可以以分号;结尾。
但是物理上的一行可以包含多个执行单位,例如:
i = 5; print(i);
5
同一个执行单位也可以拆分为多行,例如:
print(
'Hello, world')
Hello, world
Python使用对齐表达逻辑,例如:
a = 1
if a == 1:
print('ok')
print('done')
ok done
当print(‘done’)和if对齐时,必然被执行。而和print(‘ok’)对齐时,在条件下执行。
因此对齐不合适的代码会引发语法错误。
a = 0
if a == 1:
print('ok')
print('done')
print('------')
a = 1
if a == 1:
print('ok')
print('done')
done ------ ok done
a = 0
if a == 1:
print('ok')
print('done')
print('------')
a = 1
if a == 1:
print('ok')
print('done')
------ ok done
if True: print('ok')
ok
# input是python3写法,raw_input是对应的python2写法。
try:
input = raw_input
except NameError:
pass
number = 23
guess = int(input('Enter an integer : '))
if guess == number:
print('Congratulations, you guessed it.')
print('(but you do not win any prizes!)')
elif guess < number:
print('No, it is a little higher than that')
else:
print('No, it is a little lower than that')
print('Done')
Enter an integer : 10 No, it is a little higher than that Done
try:
input = raw_input
except NameError:
pass
number = 23
running = True
while running:
guess = int(input('Enter an integer : '))
if guess == number:
print('Congratulations, you guessed it.')
running = False
elif guess < number:
print('No, it is a little higher than that.')
else:
print('No, it is a little lower than that.')
else:
print('The while loop is over.')
print('Done')
Enter an integer : 10 No, it is a little higher than that. Enter an integer : 30 No, it is a little lower than that. Enter an integer : 23 Congratulations, you guessed it. The while loop is over. Done
for i in range(1, 5):
print(i)
else:
print('The for loop is over')
1 2 3 4 The for loop is over
try:
input = raw_input
except NameError:
pass
while True:
s = input('Enter something : ')
if s == 'quit':
break
print('Length of the string is %d' % len(s))
print('Done')
Enter something : 1 Length of the string is 1 Enter something : quit Done
try:
input = raw_input
except NameError:
pass
while True:
s = input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
print('Too small')
continue
print('Input is of sufficient length')
Enter something : 1 Too small Enter something : 123 Input is of sufficient length Enter something : quit
说出下面代码的执行过程和输出结果:
for i in range(12):
if i < 5:
continue
elif i > 9:
break
else:
print(i)
for i in range(12):
if i < 5:
continue
elif i > 9:
break
else:
print(i)
5 6 7 8 9
定义:
def say_hello():
print('hello world')
调用:
say_hello()
hello world
可以通过参数向函数传递数据。
def print_max(a, b):
if a > b:
print('{0} is maximum'.format(a))
elif a == b:
print('{0} is equal to {1}'.format(a, b))
else:
print('{0} is maximum'.format(b))
print_max(3, 4)
4 is maximum
# python2.7下通过,python3下报错
print_max(3, '4')
print_max('3', 4)
4 is maximum 3 is maximum
在函数体内定义的变量称为局部变量。
局部变量在函数执行时产生,在函数结束时消亡。函数外无法看到局部变量。
参数可视为一种局部变量。
对局部变量的修改不影响全局变量。
x = 50
def func():
x = 2
print('Changed local x to {0}'.format(x))
func()
print(x)
Changed local x to 2 50
x = 50
def func(x):
print('Changed local x to {0}'.format(x))
func(2)
print(x)
Changed local x to 2 50
在模块内定义的变量叫全局变量。
全局变量在全局可见,函数体内可以引用全局变量。
函数可以用global关键字声明某变量是全局的。
x = 50
def func():
print('x is {0}'.format(x))
func()
x is 50
x = 50
def func():
global x
print('x is {0}'.format(x))
x = 2
print('Changed x to {0}'.format(x))
print('x is {0} before function.'.format(x))
func()
print('x is {0} after function.'.format(x))
x is 50 before function. x is 50 Changed x to 2 x is 2 after function.
变量可见的范围叫做变量的作用域。
局部变量的作用域在函数内,全局变量的作用域在模块内(关于模块后文会讲)。
作用域的基本原则是,内层作用域可访问外层作用域的变量,反之外层不可访问内层变量。
如果两个作用域内有同一个名字,那么内层的起作用。
python内的“local中有变量定义”,是以local中存在对变量构建的语法(例如赋值)为标准的。
这个构建语法所在行可能尚未执行(或永远无法执行到),但是这并不影响“local中有变量定义”这个事实。
当local中定义了变量,但是变量尚未完成构建时,对变量做引用,会发生什么?
x = 1
def func():
print(x)
x = 2
func()
--------------------------------------------------------------------------- UnboundLocalError Traceback (most recent call last) <ipython-input-46-a910e175aa0b> in <module>() 5 x = 2 6 ----> 7 func() <ipython-input-46-a910e175aa0b> in func() 2 3 def func(): ----> 4 print(x) 5 x = 2 6 UnboundLocalError: local variable 'x' referenced before assignment
函数定义时,可以给参数一个值。当调用时,该参数并未给出的情况下,使用默认值。
提示:从安全性角度来说,不要使用可变对象作为参数默认值。
def say(message, times=1):
print(message * times)
say('Hello')
say('World', 5)
Hello WorldWorldWorldWorldWorld
def append1(l=[]):
l.append(1)
return l
print(append1([]))
print(append1([]))
print('------')
print(append1())
print(append1())
[1] [1] ------ [1] [1, 1]
遇到希望使用可变对象作为参数默认值的情况,需要使用None作为默认值。
在函数中判断是否为None,是的话再对参数做默认值赋值处理。
以此模式来避免上述问题。
def append1(l=None):
if l is None:
l = []
l.append(1)
return l
print(append1([]))
print(append1([]))
print('------')
print(append1())
print(append1())
[1] [1] ------ [1] [1]
将函数内数据传递回去。
def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y
print(maximum(2, 3))
print(maximum(2, 2))
3 The numbers are equal
Python允许一次返回多个值。
多值返回的本质是使用tuple中转(后面详细讲解)。
def f():
return 1,2,3,4,5
print (f())
a,b,c,d,e = f()
print(c)
(1, 2, 3, 4, 5) 3
Python允许为每个函数增加一个文档字符串,这个文档字符串可以被代码访问。
具体方式是,在函数的第一个逻辑行定义一个字符串(不执行任何操作)。
访问方式是function.__doc__。
def print_max(x, y):
'''Prints the maximum of two numbers. \nThe two values must be integers.'''
x = int(x)
y = int(y)
if x > y:
print('{} is maximum'.format(x))
else:
print('{} is maximum'.format(y))
print_max(3, 5)
print(print_max.__doc__)
5 is maximum Prints the maximum of two numbers. The two values must be integers.
一种快速定义简单函数的方法。
def inc(n):
return n+1
print(inc(10))
inc = lambda n: n+1
print(inc(10))
11 11
定义fib函数。
输入n,fib的输出为数列第n项的值。
数列某项等于前两项之和。
def fib(n):
if n <= 1: return 1
return fib(n-1) + fib(n-2)
fib(10)
89
预先写好的代码,供其他代码使用。
一个module是一个独立文件。
每个module拥有自己的全局变量空间。数据定义,函数,均定义在其中。
%%writefile testmod.py
varible1 = 1
varible2 = 2
def add(a, b):
return a+b
if __name__ == '__main__':
print('run as script')
Writing testmod.py
!ls -l testmod.py
-rw-r--r-- 1 shell shell 111 10月 28 20:06 testmod.py
Windows下要用dir testmod.py
import引入某个module对象,可使用module.name的方式引用它的全局变量。
import testmod
testmod.varible1
1
from import引入模块中的某个(或全部)变量。
被引入变量不需要module.name来访问,仅需要name。
注意:不推荐引入全部变量,会引起名称空间污染。
from testmod import varible2
varible2
2
列出某个模块的所有变量。
import testmod
dir(testmod)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'add', 'varible1', 'varible2']
当import时,python会试图去编译出pyc文件来。
pyc是被编译过的py文件,加载pyc文件可以跳过语法解析过程。
当py日期新于pyc时,重新生成pyc。所以日期紊乱可能导致执行老代码。
在Python3(3.2以后)中,会在当前目录下生成__pycache__目录,来缓存pyc文件。
这样可以避免多个Python解释器无法互相载入对方的pyc文件。
!ls -l testmod*
-rw-r--r-- 1 shell shell 111 10月 28 20:06 testmod.py -rw-r--r-- 1 shell shell 333 10月 28 20:06 testmod.pyc
!ls -l __pycache__/testmod*
-rw-r--r-- 1 shell shell 356 10月 28 19:58 __pycache__/testmod.cpython-35.pyc
由于这个系统使用了python2作为基础,因此没有__pycache__。
模块有一个属性,__name__。当这个属性为'__main__'时,说明当前模块被作为脚本运行。
模块被作为以脚本运行时,不生成pyc文件(因为不是import)。
__name__
'__main__'
testmod.__name__
'testmod'
!tail -n 2 testmod.py
if __name__ == '__main__':
print('run as script')
!python testmod.py
run as script
!rm testmod*
从组织结构上说,package是比modules更大一级的结构。
一个package里可以包含多个modules和packages。
一般一个package是一个独立目录,里面有__init__.py文件。
这个文件指明了如何加载整个package。
import sys
for name in dir(sys):
if name.startswith('s') and name.endswith('e'):
print(name)
setprofile settrace
具有顺序的一组对象。
可以随意访问其中的任意位置。
可以添加和删除。
其中的元素不需要是同类型。
import sys
shoplist = ['apple', 'mango', 'carrot', 'banana']
print('I have {} items to purchase.'.format(len(shoplist)))
sys.stdout.write('These items are: ')
for item in shoplist:
sys.stdout.write(str(item) + ' ')
print('\nI also have to buy rice.')
shoplist.append('rice')
print('My shopping list is now {0}'.format(shoplist))
print('I will sort my list now')
shoplist.sort()
print('Sorted shopping list is {0}'.format(shoplist))
print('The first item I will buy is {0}'.format(shoplist[0]))
olditem = shoplist[0]
del shoplist[0]
print('I bought the {0}'.format(olditem))
print('My shopping list is now {0}'.format(shoplist))
I have 4 items to purchase. These items are: apple mango carrot banana I also have to buy rice. My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice'] I will sort my list now Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice'] The first item I will buy is apple I bought the apple My shopping list is now ['banana', 'carrot', 'mango', 'rice']
具有顺序的一组对象。
可以随意访问其中的任意位置。
不可以添加和删除。
其中的元素不需要是同类型。
zoo = ('python', 'elephant', 'penguin')
print('Number of animals in the zoo is {0}'.format(len(zoo)))
new_zoo = 'monkey', 'camel', zoo
print('Number of cages in the new zoo is {0}'.format(len(new_zoo)))
print('All animals in new zoo are {0}'.format(new_zoo))
print('Animals brought from old zoo are {0}'.format(new_zoo[2]))
print('Last animal brought from old zoo is {0}'.format(new_zoo[2][2]))
print('Number of animals in the new zoo is {0}'.format(len(new_zoo)-1+len(new_zoo[2])))
Number of animals in the zoo is 3
Number of cages in the new zoo is 3
All animals in new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))
Animals brought from old zoo are ('python', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
Number of animals in the new zoo is 5
r = 1, 2, 3
r
(1, 2, 3)
a, b, c = r
print(a)
print(b)
print(c)
1 2 3
a, b = b, a
print(a)
print(b)
2 1
shoplist = ['apple', 'mango', 'carrot', 'banana']
name = 'swaroop'
print('Item 0 is {0}'.format(shoplist[0]))
print('Item 1 is {0}'.format(shoplist[1]))
print('Item 2 is {0}'.format(shoplist[2]))
print('Item 3 is {0}'.format(shoplist[3]))
print('Item -1 is {0}'.format(shoplist[-1]))
print('Item -2 is {0}'.format(shoplist[-2]))
print('Character 0 is {0}'.format(name[0]))
print('Item 1 to 3 is {0}'.format(shoplist[1:3]))
print('Item 2 to end is {0}'.format(shoplist[2:]))
print('Item 1 to -1 is {0}'.format(shoplist[1:-1]))
print('Item start to end is {0}'.format(shoplist[:]))
print('characters 1 to 3 is {0}'.format(name[1:3]))
print('characters 2 to end is {0}'.format(name[2:]))
print('characters 1 to -1 is {0}'.format(name[1:-1]))
print('characters start to end is {0}'.format(name[:]))
print('reversed characters is {0}'.format(name[::-1]))
Item 0 is apple Item 1 is mango Item 2 is carrot Item 3 is banana Item -1 is banana Item -2 is carrot Character 0 is s Item 1 to 3 is ['mango', 'carrot'] Item 2 to end is ['carrot', 'banana'] Item 1 to -1 is ['mango', 'carrot'] Item start to end is ['apple', 'mango', 'carrot', 'banana'] characters 1 to 3 is wa characters 2 to end is aroop characters 1 to -1 is waroo characters start to end is swaroop reversed characters is pooraws
primes = [2, 3, 5, 7]
primes
[2, 3, 5, 7]
for i in range(10, 100):
prime = True
for j in primes:
if i % j == 0:
prime = False
break
if prime:
primes.append(i)
print(primes)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
tuple_primes = tuple(primes)
print(tuple_primes)
(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97)
print(tuple_primes[::-1])
(97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2)
print(tuple(reversed(tuple_primes)))
(97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41, 37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2)
# Python2下可以不需要int,因为len结果是整数,整数除法结果是整数。
# 在Python3,没有int会导致出错。
list(reversed(primes[:int(len(primes)/2)])), list(reversed(primes[int(len(primes)/2):]))
([37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2], [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41])
primes[int(len(primes)/2-1)::-1], primes[:int(len(primes)/2-1):-1]
([37, 31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2], [97, 89, 83, 79, 73, 71, 67, 61, 59, 53, 47, 43, 41])
l = range(12)
t = [i * i for i in l]
print(t)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]
t = [i * i for i in l if i % 2 != 0]
print(t)
[1, 9, 25, 49, 81, 121]
思考一下,列表推导式是否能够将素数列表直接实现出来?为什么?
然后自己测试一下。
具有一系列成对对象。一个叫做key,一个叫做value。
无顺序,key不可重复。
通过其中一个(key)可以唯一确定另外一个(value),反之未必。
可以添加和删除。
其中的元素(包括key和value)不需要是同类型。
key必须是不可变对象。
ab = {
'Swaroop': 'swaroop@swaroopch.com',
'Larry': 'larry@wall.org',
'Matsumoto': 'matz@ruby-lang.org',
'Spammer': 'spammer@hotmail.com'
}
print("Swaroop's address is {}".format(ab['Swaroop']))
del ab['Spammer']
print('There are {} contacts in the address-book'.format(len(ab)))
print('Name list: %s' % ', '.join(ab))
for name, address in ab.items():
print('Contact {} at {}'.format(name, address))
ab['Guido'] = 'guido@python.org'
if 'Guido' in ab:
print("Guido's address is {}".format(ab['Guido']))
Swaroop's address is swaroop@swaroopch.com There are 3 contacts in the address-book Name list: Swaroop, Matsumoto, Larry Contact Swaroop at swaroop@swaroopch.com Contact Matsumoto at matz@ruby-lang.org Contact Larry at larry@wall.org Guido's address is guido@python.org
具有一组对象。无顺序,不可重复。
可以添加和删除。
其中的元素不需要是同类型,但必须是不可变对象。
bri = set(['brazil', 'russia', 'india'])
bri = {'brazil', 'russia', 'india'} # python2.7以后可用
'india' in bri
True
'usa' in bri
False
bric = bri.copy()
bric.add('china')
print(bric)
bric.issuperset(bri)
set(['brazil', 'china', 'india', 'russia'])
True
bric.remove('russia')
print(bric)
bri & bric
set(['brazil', 'china', 'india'])
{'brazil', 'india'}
bri | bric
{'brazil', 'china', 'india', 'russia'}
name = 'Swaroop'
if name.startswith('Swa'):
print('Yes, the string starts with "Swa"')
if 'a' in name:
print('Yes, it contains the string "a"')
pos = name.find('war')
if pos != -1:
print('position of the string "war" is %d' % pos)
print("the string replace war to peace is %s" % name.replace('war', 'peace'))
print("the string replace o to 0 is %s" % name.replace('o', '0'))
print("upper case: %s" % name.upper())
print("lower case: %s" % name.lower())
delimiter = '_*_'
mylist = ['Brazil', 'Russia', 'India', 'China']
print(delimiter.join(mylist))
print(delimiter.join(mylist).split(delimiter))
print(delimiter.join(mylist).split('*'))
Yes, the string starts with "Swa" Yes, it contains the string "a" position of the string "war" is 1 the string replace war to peace is Speaceoop the string replace o to 0 is Swar00p upper case: SWAROOP lower case: swaroop Brazil_*_Russia_*_India_*_China ['Brazil', 'Russia', 'India', 'China'] ['Brazil_', '_Russia_', '_India_', '_China']
每个国家都有一定的地区号码,现在我们知道这些国家和地区号码关系:
+30 希腊 +45 丹麦 +51 秘鲁 +65 新加坡 +86 中国
写代码求出以下电话号码里面有几个国家:
+86 1123 +65 1234 +51 2347 +30 9123 +65 1246 +30 2347 +86 2935
number2country = {
'+30': u'希腊',
'+45': u'丹麦',
'+51': u'秘鲁',
'+65': u'新加坡',
'+86': u'中国'
}
numbers = "+86 1123 +65 1234 +51 2347 +30 9123 +65 1246 +30 2347 +86 2935".split()
country_set = set([number2country[n] for n in numbers if n.startswith('+')])
print(len(country_set))
# 注意:这里的输出顺序在不同的机器上可能不一样,具体依赖于python的版本和实现。set本身是没有顺序约定的。
print(', '.join(country_set))
4 中国, 新加坡, 秘鲁, 希腊
numbers = "+86 1123 +65 1234 +51 2347 +30 9123 +65 1246 +30 2347 +86 2935".split()
print("numbers: %s" % str(numbers))
print("filtered: %s" % str([n for n in numbers if n.startswith('+')]))
print("filtered and mapped: %s" % ', '.join([number2country[n] for n in numbers if n.startswith('+')]))
country_set = set([number2country[n] for n in numbers if n.startswith('+')])
print(', '.join(country_set))
numbers: ['+86', '1123', '+65', '1234', '+51', '2347', '+30', '9123', '+65', '1246', '+30', '2347', '+86', '2935'] filtered: ['+86', '+65', '+51', '+30', '+65', '+30', '+86'] filtered and mapped: 中国, 新加坡, 秘鲁, 希腊, 新加坡, 希腊, 中国 中国, 新加坡, 秘鲁, 希腊
如何定义类:
class Person:
pass # An empty block
如何使用类产生对象:
p = Person()
print(p)
<__main__.Person instance at 0x7f7fbc419128>
如何定义类的成员方法:
class Person:
def say_hi(self):
print('Hello, how are you?')
p = Person()
p.say_hi()
Hello, how are you?
对象里包含的数据。
可以用.访问。
和方法的主要差别在于。方法需要使用()来调用,而数据不需要。
class Person:
def set_name(self, name):
self.name = name
def say_hi(self):
print('hello, {}, how are you?'.format(self.name))
p = Person()
p.set_name('shell')
print(p.name)
p.say_hi()
shell hello, shell, how are you?
在python中,成员函数使用”self”来指代对象自身。类似于java和C++中的”this”。
在调用时,p.say_hi()即可。不需要传递p.say_hi(self)或者p.say_hi(p)。
在使用时,需要先将self定义为第一个参数。例如def say_hi(self):。
在函数内使用时,对象的成员需要用self.xx的方式使用,例如self.name。
self不是关键词,也不是强制名称。它只是函数的第一个参数。但是为了保持传统,请不要将他改为其他名称。
__init__是对象的第一个方法,用于初始化对象数据。
__init__函数是初始化方法,而非构造方法。
在__init__函数被调用时,self对象已经创建完毕。
在python中,对象的属性可以自由添加删除,不需要先在类里面声明。
一般而言,对象里的所有数据会在__init__方法中赋值,后面可以变更。
但是不在__init__中赋值,后面直接赋值创建属性也是合法的。
class Person:
def __init__(self, name):
self.name = name
def set_name(self, name):
self.name = name
def say_hi(self):
print('hello, {}, how are you?'.format(self.name))
p = Person('Swaroop')
print(p.name)
p.say_hi()
p.set_name('shell')
print(p.name)
p.say_hi()
Swaroop hello, Swaroop, how are you? shell hello, shell, how are you?
定义一个马克杯类,定义一个加水方法和一个喝水方法。
class Mug:
def __init__(self):
self.water = 0
def drink(self, mass):
self.water -= mass
def watering(self, mass):
self.water += mass
类和对象分别拥有成员,例如数据和方法。
对象可以引用类成员,例如p.say_hi()。也可以引用对象成员,或者self.name。
当有重名时优先引用对象成员。
类成员在所有对象间共享,而对象成员只是它自己的。
class Robot:
"""Represents a robot, with a name."""
# A class variable, counting the number of robots
population = 0
def __init__(self, name):
"""Initializes the data."""
self.name = name
print("(Initializing {})".format(self.name))
# When this person is created, the robot
# adds to the population
Robot.population += 1
def die(self):
"""I am dying."""
print("{} is being destroyed!".format(self.name))
Robot.population -= 1
if Robot.population == 0:
print("{} was the last one.".format(self.name))
else:
print("There are still {:d} robots working.".format(
Robot.population))
def say_hi(self):
"""Greeting by the robot.
Yeah, they can do that."""
print("Greetings, my masters call me {}.".format(self.name))
@classmethod
def how_many(cls):
"""Prints the current population."""
print("We have {:d} robots.".format(cls.population))
droid1 = Robot("R2-D2")
droid1.say_hi()
Robot.how_many()
droid2 = Robot("C-3PO")
droid2.say_hi()
Robot.how_many()
print("\nRobots can do some work here.\n")
print("Robots have finished their work. So let's destroy them.")
droid1.die()
droid2.die()
Robot.how_many()
(Initializing R2-D2) Greetings, my masters call me R2-D2. We have 1 robots. (Initializing C-3PO) Greetings, my masters call me C-3PO. We have 2 robots. Robots can do some work here. Robots have finished their work. So let's destroy them. R2-D2 is being destroyed! There are still 1 robots working. C-3PO is being destroyed! C-3PO was the last one. We have 0 robots.
回想一下,上一节你的定义里是否考虑了马克杯的“最大容量”。如果没有,请加上。
思考一下,“最大容量”是对象属性还是类属性,为什么?
class Mug:
capacity = 300
def __init__(self):
self.water = 0
def drink(self, mass):
self.water -= mass
if self.water < 0:
self.water = 0
def watering(self, mass):
self.water += mass
if self.water > self.capacity:
self.water = self.capacity
继承关系分为基类和继承类(也叫父类和子类)。
子类可以继承父类的成员。当子类和父类定义同名成员时,优先引用子类的。
原则上说,父类能做的事,子类一定能做(虽然行为可能有差异)。父类能出现的地方,子类一定能出现。
因为子类拥有父类的所有成员。
继承的写法为:
class Person(Animal):
pass
class SchoolMember:
'''Represents any school member.'''
def __init__(self, name, age):
self.name = name
self.age = age
print('(Initialized SchoolMember: {})'.format(self.name))
def tell(self):
'''Tell my details.'''
print('Name:"{}" Age:"{}"'.format(self.name, self.age))
class Teacher(SchoolMember):
'''Represents a teacher.'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print('(Initialized Teacher: {})'.format(self.name))
def tell(self):
SchoolMember.tell(self)
print('Salary: "{:d}"'.format(self.salary))
class Student(SchoolMember):
'''Represents a student.'''
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print('(Initialized Student: {})'.format(self.name))
def tell(self):
SchoolMember.tell(self)
print('Marks: "{:d}"'.format(self.marks))
t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 25, 75)
# prints a blank line
print()
members = [t, s]
for member in members:
# Works for both Teachers and Students
member.tell()
(Initialized SchoolMember: Mrs. Shrividya) (Initialized Teacher: Mrs. Shrividya) (Initialized SchoolMember: Swaroop) (Initialized Student: Swaroop) () Name:"Mrs. Shrividya" Age:"40" Salary: "30000" Name:"Swaroop" Age:"25" Marks: "75"
请为马克杯增加一个“添满”方法,可以求出需要添多少水到加满,并自动为杯子加水。
class Mug(object):
capacity = 300
def __init__(self):
self.water = 0
def drink(self, mass):
self.water -= mass
if self.water < 0:
self.water = 0
def watering(self, mass):
self.water += mass
if self.water > self.capacity:
self.water = self.capacity
def full(self):
mass = self.capacity - self.water
self.watering(mass)
请定义一个运动杯类,并同样实现“喝水”,“加水”和“添满”方法。
考虑一下,如果“马克杯”和“运动杯”的“喝水”和“添水”行为是不一样的。
那么,“添满”行为是否一样,两边是否可以重用“添满”?
如果可以,怎么做?
class Cup(object):
def __init__(self):
self.water = 0
def full(self):
mass = self.capacity - self.water
self.watering(mass)
class Mug(Cup):
capacity = 300
def drink(self, mass):
self.water -= mass
if self.water < 0:
self.water = 0
def watering(self, mass):
self.water += mass
if self.water > self.capacity:
self.water = self.capacity
class SportBottle(Cup):
capacity = 500
def drink(self, mass):
self.water -= mass
if self.water < 0:
self.water = 0
def watering(self, mass):
self.water += mass
if self.water > self.capacity:
self.water = self.capacity
m = Mug()
m.full()
m.drink(100)
print(m.water)
s = SportBottle()
s.full()
s.drink(100)
print(s.water)
200 400
python3使用input从命令行输入。python2使用raw_input从命令行输入。
两者参数一致,都是一个提示字符串。
在python2中,input是eval(input),有安全性问题。
#Rise to vote, sir.
try:
input = raw_input
except NameError:
pass
def reverse(text):
return text[::-1]
def is_palindrome(text):
return text == reverse(text)
something = input("Enter text: ")
if is_palindrome(something):
print("Yes, it is a palindrome")
else:
print("No, it is not a palindrome")
Enter text: Rise to vote, sir. No, it is not a palindrome
刚刚的回文字符串程序只能检测纯粹的字符串,真正的回文字符检测程序应当能够处理标点,空格和大小写。
例如"Rise to vote, sir."实际上也是回文字符串,但是刚刚的程序判定他不是。
请写出一个新的版本。
简单起见,只需要处理,和.即可。先不用管理各种复杂标点,例如&。
try:
input = raw_input
except NameError:
pass
def reverse(text):
return text[::-1]
def is_palindrome(text):
text = text.replace(',', '')
text = text.replace('.', '')
text = text.replace(' ', '')
text = text.lower()
return text == reverse(text)
something = input("Enter text: ")
if is_palindrome(something):
print("Yes, it is a palindrome")
else:
print("No, it is not a palindrome")
Enter text: Rise to vote, sir. Yes, it is a palindrome
使用open打开文件进行读写。调用方法和C的fopen类似。
import sys
poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f = open('poem.txt', 'w')
f.write(poem)
f.close()
f = open('poem.txt')
while True:
line = f.readline()
if len(line) == 0:
break
sys.stdout.write(line)
f.close()
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
!ls -l poem.txt
-rw-r--r-- 1 shell shell 95 10月 28 20:07 poem.txt
!cat poem.txt
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
以上皆是linux指令,因此不能(也不需要)在windows上执行。windows上对应的行为为dir,type,和del。
Python允许在出现错误的时候,“抛”出这个错误。
错误按照调用顺序依次向上找,找到第一个合适的处理方法对错误进行处理。如果无人处理错误,则程序崩溃。
这种错误处理机制允许调用者对函数深层的错误进行容错,同时中间所有代码对这个过程无需干预。
>>> Print("Hello World")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Print' is not defined
>>> print("Hello World")
Hello World
Print("Hello World")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-111-4a9fe39e6a16> in <module>() ----> 1 Print("Hello World") NameError: name 'Print' is not defined
print("Hello World")
Hello World
python的大多数错误会伴随抛出异常。因此,为了解决日常在使用中碰到的种种问题,我们需要学会如何阅读异常。
异常输出通常和函数的调用顺序相同,和栈的顺序相反。
最上层的调用(最先发生的)在最上面,最后执行到的地方在最下面。最后一个异常行就是异常发生的精确地点。
最后一行是异常的名字和参数。
def layer1():
print('layer1 called')
return layer2()
def layer2():
print('layer2 called')
raise Exception('arg1', 2)
return 1
print(layer1())
layer1 called layer2 called
--------------------------------------------------------------------------- Exception Traceback (most recent call last) <ipython-input-113-22bfd87adf46> in <module>() 8 return 1 9 ---> 10 print(layer1()) <ipython-input-113-22bfd87adf46> in layer1() 1 def layer1(): 2 print('layer1 called') ----> 3 return layer2() 4 5 def layer2(): <ipython-input-113-22bfd87adf46> in layer2() 5 def layer2(): 6 print('layer2 called') ----> 7 raise Exception('arg1', 2) 8 return 1 9 Exception: ('arg1', 2)
常规异常处理代码段需要包含三部分内容:被监管代码体,错误类型和异常处理代码体。
当被监管代码体中发生异常时,异常被向上抛出。
向上寻找处理函数的异常属于定义的这个错误类的子类时,异常处理代码体被调用。
一般来说,除非明确知道确实要捕获所有异常,否则严禁用捕获所有异常来处理某种特定问题。
try:
input = raw_input
except NameError:
pass
try:
number = int(input('Enter something --> '))
except ValueError:
print('Illegal number')
else:
print(number+1)
Enter something --> 10.a Illegal number
def f():
print("I'm ok")
raise Exception('actually not')
try:
f()
except Exception as err:
print(err)
I'm ok actually not
# 必须自Exception中继承
class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
input = raw_input
except NameError:
pass
try:
text = input('Enter something --> ')
if len(text) < 3:
raise ShortInputException(len(text), 3)
except ShortInputException as ex:
print('ShortInputException: The input was {0} long, expected at least {1}'.format(
ex.length, ex.atleast))
else:
print('No exception was raised.')
Enter something --> 1 ShortInputException: The input was 1 long, expected at least 3
finally用于“无论是异常还是正常,以下内容必然被执行”的情况。
多用于清理。
try:
input = raw_input
except NameError:
pass
try:
number = int(input('Enter something --> '))
except ValueError:
print('Illegal number')
else:
print(number+1)
finally:
print('(clean up here)')
Enter something --> 10.a Illegal number (clean up here)
with可以在推出区块时自动关闭文件,而且对异常安全(异常在下一节讲)。
with不仅可以用于文件,而且可以用于很多需要在离开区域时自动关闭的对象,例如锁。
import sys
with open("poem.txt") as f:
for line in f:
sys.stdout.write(line)
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
!rm -f poem.txt
assert 1 == 0, Exception('of course')
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-121-53f9edc366b8> in <module>() ----> 1 assert 1 == 0, Exception('of course') AssertionError: of course
在线文档:
本地文档:随安装版本变化。
两者冲突以本地为准,本地一定对应安装使用的版本。
库查Library Reference,这是最主要部分。
语法特性查Language Reference。
打开文档,请查阅itertools.permutations的意义,参数,返回值,用法,注意要点等信息。并向大家解释。
没有什么固定方法。
在google(注意,不是baidu)上搜索关键词。找一个比较像的官网。找到文档。
可以参考pypi,很多第三方库可以在上面找到。里面往往带有文档地址。
缺点是,pypi上搜出来的重名库太多,很难搞清楚哪个才是你要的。
Python2中有很多固有的设计问题,例如:
这些问题在Python3中逐步给予了修复。
常规来说,修复问题最重要的是“向下兼容,逐步进行”。
然而上述问题几乎全部都是语言本质问题,不对语言进行伤筋动骨的大改是没有办法修复的。
因此Python3的预订是“不和Python2兼容”,这造成了Python社区目前2/3分裂的现状。
Python2和Python3有很多细节差异。但是大致来说,最主要就是上面提到的三项。
在下面这个连接里,收录了Python2和Python3的其他一些细节差异。
Python3带有2to3脚本,可以完成很多项目的迁移。
但是对于某些情况,他仍然不能自动的完成所有工作。
2到3迁移的主要问题在于,目前有很多库,仍然没有完成Python3的迁移工作。这导致使用这些库编写的程序很难在Python3上找到更好的(或者更习惯的)替代产品。
而这件事情是2to3脚本无法自动完成的。
任何能让你不添加额外的麻烦把工作做下来的版本都是好的。
如果Python2和Python3一样好,那么Python3更好。
因为很显然,Python社区已经宣布Python2将停止维护。在可见的时间内,显然都要使用Python3的写法。
如果能够不添加额外麻烦的情况下使用Python3,这可能为将来的维护带来便利。
两套基本系统:
系统中必须安装了setuptools(请咨询管理员安装),或者使用以下指令安装:
Linux:
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
MacOS:
curl https://bootstrap.pypa.io/ez_setup.py -o - | python
进一步资料请参考:https://setuptools.readthedocs.io/en/latest/easy_install.html
功能:用于隔离出一套独立的环境,可以在里面安装各种包,而不对系统造成影响。
使用场景:可以在用户目录中安装包,无需系统权限。也可以隔离多个环境,安装不同版本的不同程序。
限制:virtualenv本身,及其依赖的Python是无法隔离的。
建立环境:virtualenv 目录名
激活环境:进入目录后执行source bin/activate
退出激活环境:deactivate
隔离环境内的安装:pip或者setup.py均可
注意:安装库时需要编译的,系统中必须有编译工具链。
如果在使用virtualenv的同时,也想使用系统中安装的库。那么需要在创建环境时用--system-site-packages参数。
从工程管理角度,我们不推荐这种办法。建议将系统中的所有库在虚环境中再安装一次。
使用virtualenv生成的虚拟环境可以迁移到其他机器上,从而允许将运行环境在多台机器上迁移。但是需要注意以下事项:
Python默认情况下会试图将软件包安装到系统里,如果不具备管理员权限,需要使用virtualenv来安装软件包。
对于Debian/Ubuntu而言,安装软件的第一选择是apt系统。如果找不到包,或版本不对,建议采用virtualenv + pip的方式安装。
对于RHEL/CentOS而言,安装软件的第一选择是yum系统。如果找不到包,或版本不对,建议采用virtualenv + pip的方式安装。
对于Windows/MacOS而言,建议使用virtualenv + pip的方式安装。