如果一個字符串包含很多需要轉義的字符,對每一個字符都進行轉義會很麻煩。為了避免這種情況,我們可以在字符串前面加個前綴r,表示這是一個 raw 字符串,里面的字符就不需要轉義了。
2023-04-27
# 需要注意的是,not計算的優先級是高于and和or的
# 所以Python解釋器在做布爾運算時,只要能提前確定計算結果,它就不會往后算了,直接返回結果。
所以答案是 hello python ; hello world
# 所以Python解釋器在做布爾運算時,只要能提前確定計算結果,它就不會往后算了,直接返回結果。
所以答案是 hello python ; hello world
2023-04-27
L = [95.5, 85, 59, 66, 72]
N = []
for item in L:
if(item > 60):
N.append(item)
N.sort(reverse=True)
print(N)
N = []
for item in L:
if(item > 60):
N.append(item)
N.sort(reverse=True)
print(N)
2023-04-24
template = 'life is short,{}'
print(template.format('you need Python'))
template = "life is {0},you need {1}"
print(template.format('short','Python'))
print(template.format('you need Python'))
template = "life is {0},you need {1}"
print(template.format('short','Python'))
2023-04-24
最新回答 / 月夜妖華
def func(L): ? ?s = 0 ? ?c = 1 ? ?if isinstance(L, list): ? ? ? ?s = sum(L) ? ? ? ?return s ? ?elif isinstance(L, tuple): ? ? ? ?for F in L: ? ? ? ? ? ?c = c * F ? ? ? ?return c ? ? ? ? ? ?else: ? ? ? ?print("Error")print(func([1, 2, 3]))print(func((1, 2,...
2023-04-22
最贊回答 / 月夜妖華
def square_of_sum(x): ? ?result = 0 ? ?y = [] ? ? ? ?for M in x: ? ? ? ?y.append(M * M) ? ?result = sum(y) ? ?return resultprint(square_of_sum([1, 2, 3, 4, 5]))print(square_of_sum([-5, 0, 5, 15, 25]))你把創建空列表放在循環外就正確了,如果循環一次就創建一個新的列表,那后面的列表會覆蓋前面的列表,第一個列表最后...
2023-04-22
最贊回答 / 程序猿_郭文超
你可以這么理解,假如說你開了一家酒吧,進酒吧的人可以吃喝玩樂,但是你的酒吧為了盈利需要門票,所以每個進去的人都需要買門票,然后門口有一個人驗票后才能入場。從這個例子中 你可以理解為 酒吧 就是一個方法,進去的人 吃喝玩樂指的是方法內你對于進去的人的行為的操作,而門票你可以理解為就是方法的參數也就是你指定的規則。所以定義的方法中需要有方法名,即你酒吧的名字(得讓調用者能找到),然后還需要有對于參數操作即行為的定義,然后參數就是限定的規則。目前python中都是類似于弱類型,像JAVA這種在定義方法的時候回定...
2023-04-21
# Enter a code
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(s2.intersection(s1))
{1, 2, 3, 4, 5}
print(s2.difference(s1))
{8, 9, 6, 7}
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(s2.intersection(s1))
{1, 2, 3, 4, 5}
print(s2.difference(s1))
{8, 9, 6, 7}
2023-04-11