习题02


使用enumerate输出元组元素和序号(序号从10开始)

tu = (2, 4, 5, 1, 'rt',)
for ind, items in enumerate(tu, 10):
    print(ind, items)

通过for循环创建301条数据,数据类型不限,如:

alex-1  alex1@live.com  pwd1

alex-2  alex2@live.com  pwd2

alex-3  alex3@live.com  pwd3

 提示用户输入要查看的页码,当输入指定页码时则显示相应页。

注意:

  每页显示10条数据。

  用户输入不是十进制数,则提示输入内容错误。

temple = "alex_{n1}	alex{n2}@live.com	pwd{n3}"
li = []
for i in range(1, 302):
    li.append(temple.format(n1=i, n2=i, n3=i))
while True:
    page = input('请输入页码(1~31):')
    if not page.isdigit():
        print('请输入数字')
    elif int(page) > 31 or int(page) <= 0:
        print("请输入1~31间数字")
    else:
        break
page2 = (int(page) - 1) * 10
for j in range(page2, page2 + 10):
    print(li[j])

九九乘法表

for i in range(1, 10):
    string = ""
    for v in range(1, i + 1):
        string += str(i) + "*" + str(v) + "=" + str(i * v) + "	"
    print(string)
'''
def print(self, *args, sep=' ', end='
', file=None): # known special case of print
"""
print(value, ..., sep=' ', end=' ', file=sys.stdout, flush=False)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
"""
pass
'''

百钱买百鸡

公鸡5,母鸡3,小鸡1/3,每种都买,几种方法

count = 0
for i in range(1, 19):
    for j in range(1, 33):
        for v in range(1, 98):
            if i + j + v == 100 and 5 * i + 3 * j + v / 3 == 100:
                print(i, j, v)
                count += 1
print(count)

利用下划线将,列表元素连成字符串

li = ["alex", "ghjk", "yuio"]
new_li = "_".join(li)
print(new_li)
两个列表,输出第一个列表有,第二个列表没有的元素
l1 = [11, 22, 33,55]
l2 = [22, 33, 44,55]

li = []
#方法一
for i in l1:
  if i not in l2:
    li.append(i)
print(li)

#方法二

length = len(l2)
for item1 in l1:
    count = 0
    for item2 in l2:
        count += 1
        if item1 == item2:
            break
        if count == length and item1 != item2:
            li.append(item1)
print(li)
原文地址:https://www.cnblogs.com/jiangzhch5/p/13246579.html