-
\n表示換行
\t?表示一個制表符
\\表示?\?字符本身查看全部 -
not計算的優先級是高于and和or的
Python把0、空字符串和None看成False,其他數值和非空字符串都看成True
查看全部 -
變量名由大小寫英文字母、數字和下劃線_組成
變量不能用數字開頭
變量盡量不要和Python關鍵字重合(比如前面學習過的:and、or、not,否則可能導致Python原有關鍵字發揮不出作用)
查看全部 -
在index.py中,使用函數可以直接使用,不需要打出“=”
查看全部 -
".+代碼“后面的代碼必須是函數
"_+代碼"其實就是給變量名
查看全部 -
所謂“set()沒有順序”,是指無論原本的set()順序是什么,打出來后的順序都是一樣的。
查看全部 -
Python的字符串切片
s = 'ABC'
a = s[0] # 第一個
b = s[1] # 第二個
c = s[2] # 第三個
print(a) # ==> A
print(b) # ==> B
print(c) # ==> C
s = 'ABCDEFGHIJK'
abcd = s[0:4]
# 取字符串s中的第一個字符到第五個字符,不包括第五個字符
print(abcd) # ==> ABCD
cdef = s[2:6]
# 取字符串s中的第三個字符到第七個字符,不包括第七個字符
print(cdef) # ==> CDEF查看全部 -
Python的字符串format
# 指定{}的名字w,c,b,i
template = 'Hello {w}, Hello {c}, Hello , Hello {i}.'
world = 'World'
china = 'China'
beijing = 'Beijing'
imooc = 'imooc'
# 指定名字對應的模板數據內容
result = template.format(w = world, c = china, b = beijing, i = imooc)
print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.查看全部 -
字符串format由兩個部分組成,分別是字符串模板和模板數據內容組成,通過大括號{},可以把模板數據內容鑲嵌到字符串模板的相應位置
# 字符串模板
template = 'Hello {}'
# 模板數據內容
world = 'World'
result = template.format(world)
print(result) # ==> Hello World- 如果模板中{}比較多,容易混亂,在format可以指定數據模板的內容順序
- # 指定順序 template = 'Hello {0}, Hello {1}, Hello {2}, Hello {3}.'
- result = template.format('World', 'China', 'Beijing', 'imooc')
- print(result) # ==> Hello World, Hello China, Hello Beijing, Hello imooc.
- # 調整順序 template = 'Hello {3}, Hello {2}, Hello {1}, Hello {0}.'
- result = template.format('World', 'China', 'Beijing', 'imooc')
- print(result) # ==> Hello imooc, Hello Beijing, Hello China, Hello World.
查看全部 -
a = 'python'
print('hello,', a or 'world')
b = ''
print('hello,', b or 'world')and與運算
如果a是錯誤的(False)的,根據或運算法則,整個結果必定為False,所以返a(and法則,必須兩個都為true,才會返回true,其中一個不為true,就為false)
如果a是正確的(true)的,根據或運算法則,整個結果必定取決于b,所以選b
or或運算
如果a是正確的,根據或運算法則,整個結果為a
如果a是錯誤的,根據或運算法則,整個結果為b
查看全部 -
word = 'Life is {b1}, you need {a1}.'
acc = 'short'
bcc = 'python'
result = word.format(b1 = acc, a1 = bcc)
print(result)
查看全部 -
r'...'表示法不能表示多行字符串,也不能表示包含'和?"的字符串
r'...'表示法不能表示多行字符串,也不能表示包含'和?"的字符串
查看全部 -
如果字符串既包含'又包含"怎么辦?
這個時候,就需要對字符串中的某些特殊字符進行“轉義”,Python字符串用\進行轉義。
要表示字符串Bob said "I'm OK"
由于'和"會引起歧義,因此,我們在它前面插入一個\表示這是一個普通字符,不代表字符串的起始,因此,這個字符串又可以表示為'Bob said \"I\'m OK\".'
注意:轉義字符?\不計入字符串的內容中。
查看全部 -
fotmat函數的形式
假設
Life is {0} , you need {1}
template.format('short','Python')
0 = short 1=Python?
是下面一行內已經擁有了順序,讓上面那一行去輸出,而不是上面那一行的數字決定下一行的順序,是一種調用的關系,而不是一種定義的關系
查看全部 -
def func(P):
? ??
? ? if isinstance(P, list):
? ? ? ? sum = 0
? ? ? ? for i in P:
? ? ? ? ? ? sum+=i
? ? if isinstance(P, tuple):
? ? ? ? sum=1
? ? ? ? for i in P:
? ? ? ? ? ? sum=i*sum? ? ? ?
? ? ? ? ? ??
? ? return sum
? ??
L1=[1,2,3,4]
print(func(L1))
查看全部
舉報