s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(s1&s2)
集 & : x&y,返回一个新的集合,包括同时在集合 x 和y中的共同元素。
并集 | : x|y,返回一个新的集合,包括集合 x 和 y 中所有元素。
差集 - : x-y,返回一个新的集合,包括在集合 x 中但不在集合 y 中的元素。
补集 ^ : x^y,返回一个新的集合,包括集合 x 和 y 的非共同元素。
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
print(s1&s2)
集 & : x&y,返回一个新的集合,包括同时在集合 x 和y中的共同元素。
并集 | : x|y,返回一个新的集合,包括集合 x 和 y 中所有元素。
差集 - : x-y,返回一个新的集合,包括在集合 x 中但不在集合 y 中的元素。
补集 ^ : x^y,返回一个新的集合,包括集合 x 和 y 的非共同元素。
2021-01-30
# Enter a code
a = 'python'
print('hello,', a or 'world')
# 因为a的值是True所以选择a的值输出而不选择后面的world
b = ' '
print('hello,', b or 'world')
# 因为b是False,但是后面world是True,又因为使用的是or语句所以只要有一个是True答案都是True所以输出b的值
a = 'python'
print('hello,', a or 'world')
# 因为a的值是True所以选择a的值输出而不选择后面的world
b = ' '
print('hello,', b or 'world')
# 因为b是False,但是后面world是True,又因为使用的是or语句所以只要有一个是True答案都是True所以输出b的值
2021-01-30
def sub_sum(li):
odd_num_sum = even_num_sum = 0
for i in li:
if i % 2 == 0:
even_num_sum += i
else:
odd_num_sum += i
return odd_num_sum, even_num_sum
odd_num, even_num = sub_sum([1, 2, 3, 4, 5, 6])
print(f'奇数和:{odd_num}, 偶数和:{even_num}')
odd_num_sum = even_num_sum = 0
for i in li:
if i % 2 == 0:
even_num_sum += i
else:
odd_num_sum += i
return odd_num_sum, even_num_sum
odd_num, even_num = sub_sum([1, 2, 3, 4, 5, 6])
print(f'奇数和:{odd_num}, 偶数和:{even_num}')
2021-01-30
def square_of_sum(li):
return sum(i ** 2 for i in li)
result = square_of_sum([1, 2, 3])
print(result)
return sum(i ** 2 for i in li)
result = square_of_sum([1, 2, 3])
print(result)
2021-01-30
s1 = {1, 2, 3, 4, 5}
s2 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
if not s1.isdisjoint(s2):
s_new = s2 - s1
print(s_new)
else:
print('无重合!')
s2 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
if not s1.isdisjoint(s2):
s_new = s2 - s1
print(s_new)
else:
print('无重合!')
2021-01-30
L = ['Alice', 66, 'Bob', True, 'False', 100]
for item in L:
if L.index(item) % 2 != 0:
print(item)
for item in L:
if L.index(item) % 2 != 0:
print(item)
2021-01-30
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
if not s1.isdisjoint(s2):
print(s2&s1)
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
if not s1.isdisjoint(s2):
print(s2&s1)
2021-01-26
# Enter a code
d = {
'Alice': [45],
'Bob': [60],
'Candy': [75],
}
d ['Alice']=[50, 61, 66]
d ['Bob'] =[80, 61, 66]
d ['Candy']=[88, 75, 90]
print(d)
d = {
'Alice': [45],
'Bob': [60],
'Candy': [75],
}
d ['Alice']=[50, 61, 66]
d ['Bob'] =[80, 61, 66]
d ['Candy']=[88, 75, 90]
print(d)
2021-01-24
# Enter a code
T =(100, 69, 29, 100, 72, 99, 98, 100, 75, 100, 100, 42, 88, 100)
print (T.count(100))
T =(100, 69, 29, 100, 72, 99, 98, 100, 75, 100, 100, 42, 88, 100)
print (T.count(100))
2021-01-24