BD_學習Python簡明教程

linux系統:ubuntu16.04

python :2.7.12

1、Python 2.7.12跟Python3.6.1使用help幫助的區別

即:Python2的help參數需加引號,Python3兼容Python2

2、轉義字符&自然字符串

>>> print "this is a per\
... son"
this is a person
>>> print r"this is a per\
... son"
this is a per\
son
>>> 

3、python 自動級連

>>> print 'what\'s' ' your name?'
what's your name?
>>> print 'what\'s'' your name?'
what's your name?
>>> 

4、在vim var.py文件時,一不小心按到ctrl+s,使文件發生了異常。再次打開該文件提示:

E325: 注意
發現交換文件 ".var.py.swp"
            所有者: zh    日期: Mon Apr 22 16:55:03 2019
            文件名: ~zh/var.py
            修改過: 是
            用戶名: zh      主機名: ubuntu16-04
           進程 ID: 4536
正在打開文件 "var.py"
              日期: Mon Apr 22 17:14:23 2019
      比交換文件新!

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    如果是這樣,請用 ":recover" 或 "vim -r var.py"
    恢復修改的內容 (請見 ":help recovery")。
    如果你已經進行了恢復,請刪除交換文件 ".var.py.swp"
    以避免再看到此消息。

交換文件 ".var.py.swp" 已存在!
以只讀方式打開([O]), 直接編輯((E)), 恢復((R)), 刪除交換文件((D)), 退出((Q)), 中>
止((A)): 

解決方案:

請參看:https://blog.csdn.net/ljihe/article/details/52231000

分析:異常退出,linux會針對該文件生成一個swp文件。

若不需恢復還原舊文件內容的打算,直接將該文件刪掉即可,從而可以從新編輯var.py文件。注意,該文件,用ll命令是看不到的。

5、while 表達式:

while 表達式:
    語句塊
else:
    語句

while 語句結束,else語句必執行,前提是while的終止跟break無關;

若break語句終止裏while循環,則else語句不執行。

注意:else可選

6、for語句

for xx in xxxx:
    語句塊
else:
    語句塊

else語句可選,for循環執行完之後,纔會執行else語句,前提是while的終止跟break無關;

若break語句終止裏while循環,則else語句不執行。

7、global的使用

zh@ubuntu16-04:~/python_simple$ python func_global.py 
x is 50
Changed local x to 2
value of x is 2
zh@ubuntu16-04:~/python_simple$ cat func
func_global.py  func_local.py   func_param.py   function.py
zh@ubuntu16-04:~/python_simple$ cat func_global.py 
#!/usr/bin/python
#Filename:func_global.py
def func():
	global x
	print "x is",x
	x=2
	print "Changed local x to",x
x=50
func()
print "value of x is",x

zh@ubuntu16-04:~/python_simple$ 

8、函數返回值:return

函數沒有明確返回值的情況:

zh@ubuntu16-04:~/python_simple$ python function_return1.py 
None
zh@ubuntu16-04:~/python_simple$ cat function_return1.py 
#!/usr/bin/python
#Filename:func_return1.py
def someFunction():
	pass
print someFunction()

zh@ubuntu16-04:~/python_simple$ 

9、文檔字符串__doc__屬性的使用:

zh@ubuntu16-04:~/python_simple$ python func_doc.py 
5 is maximum
Prints the maximum of two numbers.

	The two values must be integers.
zh@ubuntu16-04:~/python_simple$ cat func_doc.py 
#!/usr/bin/python
#Filename:func_doc.py
def printMax(x,y):
	'''Prints the maximum of two numbers.

	The two values must be integers.'''
	x=int(x)
	y=int(y)
	if x>y:
		print x,'is maximum'
	else:
		print y,'is maximum'
printMax(3,5)
print printMax.__doc__

zh@ubuntu16-04:~/python_simple$ 

9、help()的使用:只是取函數的__doc__屬性,然後展示

zh@ubuntu16-04:~/python_simple$ python func_doc.py 
5 is maximum
Prints the maximum of two numbers.

	The two values must be integers.
help()的使用:

zh@ubuntu16-04:~/python_simple$ cat func_doc.py 
#!/usr/bin/python
#encoding:utf-8
#Filename:func_doc.py
def printMax(x,y):
	'''Prints the maximum of two numbers.

	The two values must be integers.'''
	x=int(x)
	y=int(y)
	if x>y:
		print x,'is maximum'
	else:
		print y,'is maximum'
printMax(3,5)
print printMax.__doc__

print "help()的使用:"
help(printMax)


zh@ubuntu16-04:~/python_simple$ 

 

Help on function printMax in module __main__:

printMax(x, y)
    Prints the maximum of two numbers.
    
    The two values must be integers.
(END)

10、import的含義

import 模塊名

含義:代表輸入模塊,當執行該語句的時候,它在sys.path變量中所列目錄中尋找sys.py模塊,當找到這個文件,該模塊的主塊將被運行。最後該模塊將可以被使用。

若想要使用該模塊中的變量。可以使用from 模塊名 import 變量名

11、注意對象的引用跟copy的區別

切片操作符來取得拷貝;

將一個對象賦給一個變量的時候,這個變量僅僅引用那個對象

12、

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章