分别打印a与b 加/减/乘/除/幂/商/余数
a = 10
b = 3
print("a+b =",a+b)
print("a-b =",a-b)
print("a*b =",a*b)
print("a/b =",a/b)
print("a**b =",a**b)
print("a/b =",int(a/b))
print("a%b =",a%b)
计算1+2+3+4……+100的和
sum = 0
for i in range(101):
sum+=i
print('第二题结果等于:',sum)
计算1〜100中所有偶数之各
sum = 0
for i in range(0,101,2):
# print(i)
sum+=i
print('1~100中偶像之和等于:',sum)
打印简书ID中所有数字的和
ID='4a577793b99a'
sum = 0
for i in range(len(ID)):
if ID[i].isdigit():
sum+=ID[i]
print("所有数字的和",sum)
将字符串0cd768f4b1a1dac0c512e452726361d9两两分割生成元组及列表
s = '0cd768f4b1a1dac0c512e452726361d9'
t = list(s)
result =[]
for i in range(0,len(t),2):
result.append(t[i]+t[i+1])
print(result)
如何用循环生成下面字典 item={1:'1',2:'2',3:'3'}
item={}
for i in range(1,4):
item[i]='%s'%i
print(item)
打印dict_date中value的年份在1970~1976的key
dict_date = {"Led Zeppelin":1969, "Led Zeppelin II": 1969, "Led Zeppelin III": 1970, "Led Zeppelin IV": 1971, "Houses of the Holy":1973, "Physical Graffiti": 1975, "Presence":1976, "In Through the Out Door":1979, "Coda":1982}
for k in dict_date.keys():
if dict_date[k]>=1970 and dict_date[k]<=1976:
print(dict_date[k])
编写一个用来交换 a 与 b 值的函数
逻辑题,结果是什么?如何分析解答这道题?
list_a = [i for i in range (10) if i % 2]
print(list_a)
#range(10) 即 0~9;对2取模为True,打印出来
编写一个用来交换字典 key 与 value 的函数(重复的放入 value list 中)
def change_num(dict_1):
s = dict_1.items()
d = {}
for x, y in s:
d.setdefault(y,[]).append(x)
return d
D = {"a":1,"b":1,"c":2}
d = change_num(D)
通过代码实现将以上所有代码及结果输出到同一文件中(首行需标注)
from sys import argv
script, file_name, new_file = argv
content = open(file_name)
new = open(new_file, 'w')
o = 'line1: this is copy script file.'
y = content.read()
new.write(o+'\n'+y)
网友评论