-
普通四則運算+-*/
取模運算%
地板除(結果保留整數)//
保留小數位數? 例如a=3.34
round(a,1)=3.3
查看全部 -
append()方法總是將元素添加到list的尾部。
insert()方法需要兩個參數,分別是需要插入的位置,以及需要插入的元素。查看全部 -
def func(param1, param2, param3 = None, *args, **kwargs):查看全部
-
注意:ab = s[0:2] # 取字符串s中的第一個字符到第三個字符,不包括第三個字符
查看全部 -
https://docs.python.org/3/library/functions.html?里面有python內置的函數
查看全部 -
1,變量名由大小寫英文字母、數字和下劃線_組成
2,變量不能用數字開頭
3,變量盡量不要和Python關鍵字重合(比如前面學習過的:and、or、not,否則可能導致Python原有關鍵字發揮不出作用)
查看全部 -
is是保留字,不能作為變量名
查看全部 -
因為Python把0、空字符串和None看成False,其他數值和非空字符串都看成True
查看全部 -
num=1, pr1=1*1
num=2, pr2=pr1*2
num=3, pr3=pr2*3
# Enter a code
num = 1
pr = 1
while num <= 10:
? ? pr = pr * num
? ? num = num + 1提交
print(pr)
查看全部 -
num = 10 / 3
print(num) # ==> 3.3333333333333335
# 使用round保留兩位小數
round(num, 2) # ==> 3.33查看全部 -
恰當使用取模運算,可以判斷一個數是否為偶數,當一個數對2取模結果為0時,則這個數為偶數,否則為奇數。
print(3 % 2) # ==> 1 因此3為奇數
print(33 % 2) # ==> 1 因此33為奇數
print(100 % 2) # ==> 0 因此100為偶數ython除了普通除法以外,還有一個特殊的除法被稱為地板除,對于地板除,得到的結果會忽略純小數的部分,得到整數的部分,地板除使用//進行。
10//4 # ==> 2
10//2.5 # ==> 4.0
10//3 # ==> 3查看全部 -
把10用e替代,比如:1.23x10^9就是1.23e9,或者12.3e8,0.000012可以寫成1.2e-5,同學可以自行舉出更多的例子。
and運算是與運算,只有所有都為 True,and運算結果才是 True。
or運算是或運算,只要其中有一個為 True,or 運算結果就是 True。
not運算是非運算,它是一個單目運算符,把 True 變成 False,False 變成 True。
查看全部 -
format處理字符串
組成:字符串模版和模版數據內容,通過大括號{},進行數據匹配
#字符串模版
template = ‘Hello {}’
#模版數據內容
world = ‘World’
result = template.format(world)
print(result) #==> Hello World
1、在模版中 {} 比較多的情況下,可以通過指定模版數據內容的順序
#指定順序
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.2、使用指定對應名字,使得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.查看全部 -
round()函數
用來保留小數點位數,round(計算的數值,保留的位數)
查看全部 -
判斷奇偶: 2%2??====0 3%2??====1
查看全部
舉報