Python學習筆記

2021-06-18 09:02:01 字數 1596 閱讀 3856

1、exec在python3.2中的用法和python2中的區別

>>> from math import sqrt

>>> scope={}

>>> exec 'sqrt=1' in scope

syntaxerror: invalid syntax

>>> exec ('sqrt=1' in scope)

traceback (most recent call last):

file "", line 1, in

exec ('sqrt=1' in scope)

typeerror: exec() arg 1 must be a string, bytes or code object

修改後:

>>> from math import sqrt

>>> scope={}

>>> exec('sqrt=1',scope)

>>> sqrt(4)

2.0>>> scope['sqrt']

12、文件字串

>>> def square(x):

'calculates the square of the number x.'

return x*x

>>> square._doc_

traceback (most recent call last):

file "", line 1, in

square._doc_

attributeerror: 'function' object has no attribute '_doc_'

產生這個錯誤的原因是doc前後需要有兩個下劃線

>>> square.__doc__

'calculates the square of the number x.'

3、6.3.2中的函式

>>> def test():

print('this is printed')

return

print('this is not')

syntaxerror: inconsistent use of tabs and spaces in indentation

不知道為啥要報這個錯誤,知道答案的朋友告訴我一下。

4、>>> def store(data,full_name):

names=full_name.split()

if len(names)==2:names.insert(1,'')

labels='first','middle','last'

for label,name in zip(labels,names):

syntaxerror: unindent does not match any outer indentation level

修改後:

def store(data,full_name):

names=full_name.split()

if len(names)==2:names.insert(1,'')

labels='first','middle','last'

for label,name in zip(labels,names):

python教學筆記 python學習筆記(一)

1.eval 函式 eval是單詞evaluate的縮寫,就是 求.的值的意思。eval 函式的作用是把str轉換成list,dict,tuple.li 1 1,2,3 print eval li 1 di 1 print eval di 1 tu 1 2,4,6 print eval tu 1 執...

python學習筆記

coding utf 8 coding utf 8 應該像八股文一樣在每個指令碼的頭部宣告,這是個忠告 為了解決中文相容問題,同時你應該選擇支援 unicode 編碼的編輯器環境,保證在執行指令碼中的每個漢字都是使用 utf 8 編碼過的。cdays 5 exercise 3.py 求0 100之間...

Python 學習筆記

python 學習筆記 def run print running.def execute method method execute run result running.condition false test yes,is true if condition else no,is false ...