-
浮點數可以表達整數的結果,但是整數不能表達浮點數的結果。
取模運算:余數?
地板除,得到的結果會忽略純小數的部分,得到整數的部分,地板除使用//進行。
round()函數來處理,這里先了解round的調用方式,使用兩個參數,第一個是需要保留小數點位數的數值,第二個是保留的位數。
num = 10 / 3
print(num) # ==> 3.3333333333333335
# 使用round保留兩位小數
round(num, 2) # ==> 3.33查看全部 -
兩種format的方式打印字符串Life is short, you need Python
str1 = 'Life is {0}, you need {1}'
result1 = str1.format('short','python')
print(result1)
str2 = 'Life is {m1}, you need {m2}'
word1 = 'short'
word2 = 'python'
result2 = str2.format(m1=word1, m2=word2)
print(result2)
以上兩種方式,運行結果都是:Life is short,you need python
查看全部 -
a = 'python'
print('hello,', a or 'world')
預計結果:hello,python
a or 'world', a為True,返回結果True,'python',所以運行結果為hello,Python
b = ''
print('hello,', b or 'world')
預計結果:hello,world
b or 'world',b為False,返回結果取決于‘world’,‘world’為真,返回結果為真,‘world’,所以運行結果為hello,world
查看全部 -
這個語法很容易理解,但是任務里面有一個知識點,就是使用for語法計算數組的平均值時,要先對sum賦值(最好是0或0.0),這樣才能正確計算
查看全部 -
/**/1 a=:56
rf 1/49 & 68:b=1:0
查看全部 -
# coding=utf-8
#遞歸函數
#def he(n):
#? ? if n == 1:
#? ? ? ? return 1
#? ? return n + he(n-1)
? ??
#res = he(100)
#print(res)
#循環函數
def loop(n):
? ? sum = 0
? ? i = 0
? ? while i <= n:
? ? ? ? sum = sum + i
? ? ? ? i +=1
? ? return sum
? ??
res = loop(100)
print(res)
查看全部 -
def sub_sum(l):
? ? sum_e = 0
? ? sum_o = 0
? ? for i in l:
? ? ? ? if i%2 == 0:
? ? ? ? ? ? sum_o += i
? ? ? ? else:
? ? ? ? ? ? sum_e += i
? ? return sum_o, sum_e
l = [1,2,3,4,5,6,7,8,9]
result = sub_sum(l)
sum_o = result[0]
sum_e = result[1]
print("所有奇數項的和 = {}".format(sum_e))
print("所有偶數項的和 = {}".format(sum_o))
查看全部 -
r'...'表示法不能表示多行字符串,也不能表示包含'和?"的字符串。
可以在多行字符串前面添加r,把這個多行字符串也變成一個raw字符串:
查看全部 -
\n表示換行
\t?表示一個制表符
\\表示?\?字符本身查看全部 -
數據類型:整型,浮點型,字符型,布爾型查看全部
-
三元表達式結構
value_if_true if condition else value_if_false
舉例子:print(f"{student}: {score}" if score is not None else f"{student}: None")
其中,f"{student}: {score}"為符合條件的真值(True), f"{student}: None"為不符合條件的假值(False)
查看全部 -
not的優先級大于and和or。查看全部
-
不會報錯的刪除:set.discard()
清除所有元素:set.clear()
子集:s1.issubset(s2)????# s1是s2的子集?
超集:s1.issuperset(s2)????# s1是s2的超集?
判斷是否重合:s1.isdisjoint(s2)????# s1和s2無重復元素?
查看全部 -
如果remove的元素不在set里面的話,那么將會引發錯誤。
查看全部 -
set不能包含重復的元素,在傳入set()的list中,包含了重復的元素,但是打印的時候,相同的元素只保留了一個,重復的元素都被去掉了,這是set的一個重要特點。
set元素是區分大小寫的,必須大小寫完全匹配。
查看全部
舉報