你可能不知道的Python(一)

1.Python關鍵字

import keyword
print(keyword.kwlist)
>>>>
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

2.有趣的import

>>> import __hello__
Hello world!

In [1]: import this 
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

>>> import antigravity

會自動打開一個網頁


3.萬物皆對象

# 省略號(...)也是
In [2]: ...                                                              
Out[2]: Ellipsi
In [3]: type(...)                                                        
Out[3]: ellipsis
# 轉布爾爲True
In [4]: bool(...)                                                     
Out[4]: True

可以使用…代替pass


4. and與or

如果or表達式中所有的值都爲真,Python會選擇第一個值
and的話,會選擇最後一個

>>> 5 or 6
5
>>> (True and 9)
9

5.解釋器提示符(>>>和…)

>>> for i in range(2):
...     print(i)
... 
0
1
# 有沒有想過爲什麼是>>>和...  , 可不可以修改呢
>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '
>>> sys.ps3
>>> sys.ps1="--hello python>>> "
--hello python>>> sys.ps2 = '0**0'
--hello python>>> for i in range(2):
0**0                    print(i)
0**0
0
1

你可能不知道的Python(二)

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