Python基础复习_Key Points
1. 弱类型:不需要为变量指定类型。(C语言是强类型,必须给变量指定类型。如:int,string等。强类型的好处:方便做语法检查)
1 a = 1234 2 print(a) 3 a = ‘abcd’ 4 print(a)
#outcomes:
1234
abcd
2. 变量必须先赋值。Python中变量要先赋值再使用。而C语言中,定义一个变量(int i),i是有一个初始值。
3. Python是通过引用传递变量的。引用变量的地址空间。
4. Python2 和Python3的常见差异
- xrange 在Python2中,xrange主要是解决python循环中的效率问题。与range相比,xrange使用generator来解决空间效率问题。而在python3中,xrange和range的效率是一样的。
- print 在python2中,无需加括号,但在python3中,要加括号。
- 数据类型统一,取消unicode和long
- utf8:python3代码默认utf8
1. none是True和False之外的一种状态。可以等价为false。
2. del是从容器里删除对象。
3. python中只有三种操作:与、或、非。没有&&,||等。
4. 函数是没有返回值的,要返回值,需加return
默认参数:
1 def hello(who = 'world'): 2 print('hello %s!' %(who)) 3 4 hello() 5 hello('sea')
#Outcome:
hello world!
hello sea!
Lambda VS Normal defunction:
1 def g(x): 2 return x*5 3 def f(gf,x): # gf has got the memory address of g(x), no matter what it is called. 4 return gf(x)+100 # gf(x) call g(x) and pass the argument x to g(x). 5 print(f(g,100)) # g -> pass the memory address of g(x) to the argument. 6 print(f(lambda x:x*110,100)) 7 # process: 1. lambda generate a memory address for itself (x*110) 8 #2. call f(g,100) and pass the memory address of lambda to the argument gf. 9 #3. gf(x) call lambda x*110 and pass 100 to x, so the result of lambda equals 11100
#Outcome:
600
11100
1 def f(gf,x,y): 2 return gf(x,y)+100 3 print(f(lambda x,y:x*y,100,200))
#Outcome:
20100
5. while & for
1 totoal = 0 2 i = 1 3 while i <= 100: 4 total += i 5 i += 1 #没有++i或者--i 6 print(total)
#outcome:
5050
for循环只作用用于容器!!
没有这种写法:
for (i = 0; i<100; ++i)
pass
上面这种循环只能用while实现。
1 i = 0 2 while i < 3: 3 j = 0 4 while j <=3: 5 if j == 2: 6 break / continue #只退出当前循环,循环继续执行j=3.。。 7 print(i,j) 8 j += 1 9 i +=1
#outcomes:
Break:
0 0
0 1
1 0
1 1
2 0
2 1
Continue:
0 0
0 1
1. List
用lambda对第一元素做排序
切片
2. String
Mode
‘r’ – Read mode which is used when the file is only being read
‘w’ – Write mode which is used to edit and write new information to the file (any existing files with the same name will be erased when this mode is activated)
'b' - 'b'
appended to the mode ('rb','rb+','wb','wb+') opens the file in binary mode
‘a’ – Appending mode, which is used to add new data to the end of the file; that is new information is automatically amended to the end
‘r+’ – Special read and write mode, which is used to handle both actions when working with a file
Open a file
>>> f = open('workfile', 'w')
Open a file with 'with'
>>> with open('workfile') as f: #If you’re not using the with
keyword, then you should call f.close()
to close the file and immediately free up any system resources used by it.
... read_data = f.read()
>>> f.closed #If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while.
#with as equals:
try:
f = open('workfile','r')
for line in f.readlines():
print(line)
except:
...
finnally:
f.close()
Read a file:
>>> f.read() #Read a whole file.
#Outcome:
'This is the entire file.\n'
>>> f.readline() #Read a line of the file
#Outcome:
'This is the first line of the file.\n'
>>> f.readline()
#Outcome:
'Second line of the file\n'
>>> for line in f: #Read a file line-by-line
... print(line, end='')
#outcomes:
This is the first line of the file.
Second line of the file
Write a file:
f.write('This is a test\n')
#错误处理
1 import logging #在生产环境中,最有效的调试方式。 2 3 ''' 4 作业,自己实现将不同的等级的信息写到不同日志文件。 5 logging.info() 6 logging.debug() 7 ''' 8 9 try: 10 r=10/0 11 except ZeroDivisionError as e: #捕捉异常 12 print(type(e)) 13 print(e) 14 finally: #主要是防止服务端资源泄漏! 15 print('always come here.')