L = [75, 92, 59, 68, 99]
sum = 0
for i in L:
sum += i
print(sum)
print(sum/5)
print(sum/len(L))
sum = 0
for i in L:
sum += i
print(sum)
print(sum/5)
print(sum/len(L))
2024-02-13
age = 2
if age>=18:
print('adult')
elif age>=6 and age<18:
print('teenager')
elif age>=3 and age<6:
print('kid')
else:
print('baby')
if age>=18:
print('adult')
elif age>=6 and age<18:
print('teenager')
elif age>=3 and age<6:
print('kid')
else:
print('baby')
2024-02-13
age =30
if age<18:
print('teenager')
else:
print('adult')
if age<18:
print('teenager')
else:
print('adult')
2024-02-13
age = 19
if age>18:
print('adult')
print('dondonqiang age {}'.format(age))
else:
print('minor')
if age>18:
print('adult')
print('dondonqiang age {}'.format(age))
else:
print('minor')
2024-02-13
template = '{l} {i} {s}, {y} {n} {p}'
l = 'Life'
i = 'is'
s = 'short'
y = 'you'
n = 'need'
p = 'Python'
result = template.format(l,i,s,y,n,p)
print(result)
result = template.format(l='Life',i='is',s='short',y='you',n='need',p='Python')
print(result)
l = 'Life'
i = 'is'
s = 'short'
y = 'you'
n = 'need'
p = 'Python'
result = template.format(l,i,s,y,n,p)
print(result)
result = template.format(l='Life',i='is',s='short',y='you',n='need',p='Python')
print(result)
2024-02-08
template = '{}'
con = 'Life is short,you need Python'
result = template.format(con)
print(result)
template = '{0} {1} {2},{3} {4} {5}'
result = template.format('Life','is','short','you','need','Python')
print(result)
con = 'Life is short,you need Python'
result = template.format(con)
print(result)
template = '{0} {1} {2},{3} {4} {5}'
result = template.format('Life','is','short','you','need','Python')
print(result)
2024-02-08
print(r'''
"To be, or not to be": that is the question.Whether it's nobler in the mind to suffer.
''')
"To be, or not to be": that is the question.Whether it's nobler in the mind to suffer.
''')
2024-02-08
print("special string:',\",\,\\\,\\n,\\t")
print("special string:',\",\,\\\\,\\n,\\t")
print("special string:',\",\,\\\\,\\n,\\t")
2024-02-08
>>> a='python'
>>> print('hello,',a or 'world')
hello, python
解释:短路运算 a or 'world',a为True结果一定为True,直接返回a
>>> b=''
>>> print('hello,',b or 'world')
hello, world
解释:python讲空字符串看作False,b or 'world',b为False,结果取决于'world',直接返回‘world’
>>> print('hello,',a or 'world')
hello, python
解释:短路运算 a or 'world',a为True结果一定为True,直接返回a
>>> b=''
>>> print('hello,',b or 'world')
hello, world
解释:python讲空字符串看作False,b or 'world',b为False,结果取决于'world',直接返回‘world’
2024-02-07
def func(x):
if isinstance(x, list):
sum = 0
for item in x:
sum += item
return sum
elif isinstance(x, tuple):
sum = 1
for item in x:
sum *= item
return sum
L = [1, 2, 3,4]
t = tuple(L)
print(func(L),func(t))
if isinstance(x, list):
sum = 0
for item in x:
sum += item
return sum
elif isinstance(x, tuple):
sum = 1
for item in x:
sum *= item
return sum
L = [1, 2, 3,4]
t = tuple(L)
print(func(L),func(t))
2024-02-04
def sub_sum(L):
a = 0
b = 0
for num in L:
if(num % 2 == 0):
a += num
else:
b += num
return b,a
L = [1,2,3,4,5,6]
print(sub_sum(L))
a = 0
b = 0
for num in L:
if(num % 2 == 0):
a += num
else:
b += num
return b,a
L = [1,2,3,4,5,6]
print(sub_sum(L))
2024-02-04
def square_of_sum(L):
sum = 0
for itme in L:
sum = sum + pow(itme, 2)
return sum
L = [1, 5, 10]
print(square_of_sum(L))
sum = 0
for itme in L:
sum = sum + pow(itme, 2)
return sum
L = [1, 5, 10]
print(square_of_sum(L))
2024-02-04
s1 = set([1, 2, 3, 4, 5])
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
if not s1.isdisjoint(s2):
for item in s1:
if item in s2:
print(item)
s2 = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
if not s1.isdisjoint(s2):
for item in s1:
if item in s2:
print(item)
2024-02-04
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
S = set([1, 3, 5, 7, 9, 11])
for item in L:
if item in S:
S.remove(item)
print(S)
S = set([1, 3, 5, 7, 9, 11])
for item in L:
if item in S:
S.remove(item)
print(S)
2024-02-04