练习结果:
说明:python divmod() 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。
具体代码:
# 计算1~100之间所有整数的和
num = 0
i = 1
while i < 101:
num += i
i += 1
print(num)
# 打印字符A~Z
'''
n = 65
while n <= 90:
l = chr(n)
n += 1
print(l, end=' ')
'''
# 循环输入10个字符,
# 大写转小写,小写转大写,其它字符不变,然后输出
'''
i = 1
while i <= 10:
n = input("请输入一个字符:")
if 65 <= ord(n) <97:
print(chr(ord(n) + 32))
elif 97 <= ord(n) <= 122:
print(chr(ord(n)-32))
else:
print(n)
i += 1
'''
# 将12345转换为54321
'''
m = int(input("请输入整数:"))
n = 0
while m:
m, last = divmod(m, 10)
n = n * 10 + last
print(n,type(n))
'''
# 将12345转换为'12345',不要使用str
'''
n = int(input("请输入:"))
m = ''
while n:
n, last = divmod(n, 10)
l = chr(last+ord('0'))
m = l + m
print(m,end='')
'''
# 将'12345'转换为12345,不要使用int
'''
n = str(input("请输入:"))
m = 0
for i in n:
l = ord(i)-ord('0')
m = m*10+l
print(m,type(m))
'''
遍历列表,打印:我叫name,今年age岁,家住dizhi,电话phone
lt = [
{'name':'小王', 'age':18, 'info':[('phone', '123'), ('dizhi', '广州')]},
{'name':'小芳', 'age':19, 'info':[('phone', '789'), ('dizhi', '深圳')]},
{'name':'小杜', 'age':22, 'info':[('phone', '567'), ('dizhi', '北京')]},
{'name':'小孟', 'age':28, 'info':[('phone', '000'), ('dizhi', '上海')]},
{'name':'小乔', 'age':26, 'info':[('phone', '111'), ('dizhi', '河南')]},
]
'''
'''
lt = [
{'name':'小王', 'age':18, 'info':[('phone', '123'), ('dizhi', '广州')]},
{'name':'小芳', 'age':19, 'info':[('phone', '789'), ('dizhi', '深圳')]},
{'name':'小杜', 'age':22, 'info':[('phone', '567'), ('dizhi', '北京')]},
{'name':'小孟', 'age':28, 'info':[('phone', '000'), ('dizhi', '上海')]},
{'name':'小乔', 'age':26, 'info':[('phone', '111'), ('dizhi', '河南')]},
]
for k in lt:
print('我叫{},今年{}岁,家住{},电话{}\n'.format
(k['name'],k['age'],k['info'][1][1],k['info'][0][1]),end='')
'''
# 打印九九乘法表
for i in range(1,10):
for j in range(1,i+1):
print('{}*{}={}\t'.format(i,j,i*j),end='')
print()
'''
# 从终端输入两个整数m和n,打印m*n的表格,
# 如:2,5,打印如下图形:
# 1 2 3 4 5
# 6 7 8 9 10
m = int(input("请输入一个整数:"))
n = int(input("请再输入一个整数:"))
for i in range(m):
for j in range(n):
print(i * n +(j+1), end=' ')
print()