1. 3位整数逆序

num = input("请输入一个三位整数:")

print(num[::-1])

2. if - else 训练

sex = input('请输入您的性别(F 或 M): ')
age = int(input("请输入您的年龄: "))

if sex == "M":
    if age < 30:
        print('young')
    elif age <= 36:
        print('marriageable age')
    else:
        print('old')

elif sex == 'F':
    if age < 25:
        print('young')
    elif age <= 30:
        print('marriageable age')
    else:
        print('old')

else:
    print('wrong')

3. if-else 和 计算能力 训练

#Python学习培训交流群:778463939

my_str = input("What's the temperature? ")

if my_str[-1] in ['F', 'f']:
    C = round((eval(my_str[0:-1]) - 32) / 1.8, 1)

    print(f"The converted temperature is {C}C.")


elif my_str[-1] in ['C', 'c']:
    F = round(1.8 * eval(my_str[0:-1])   32, 1)

    print(f"The converted temperature is {F}F.")

else:
    print("input error my dear.")

4. 循环系统: 文档读写能力

with open("movie.txt") as f_in:
    with open("out.txt") as f_out:
        for line in f_in:
            # 假定每排数据信息, 切分符是 ','
            line_list = line.split(',')
            # 时间 Lasting 是最终一列
            if line_list[-1] < 90:
                # 将这家银行编号写到 out.txt
                f_out.write(str(line_list[0]))

5.循环系统训练: 亲密无间数

#Python学习培训交流群:778463939

x = int(input("请输入一个正整数: "))

for a in range(2, x):
    b = 0
    for i in range(1, a):
        if a % i == 0:
            b  = i
    r = 0
    for j in range(1, b):
        if b % j == 0:
            r  = j
    if r == a and a < b:
        print(a, b)

6.循环系统: 判断推理

n = int(input('请输入较大 军力总数: '))

for i in range(n   1):
    if (i % 3 == 2) and (i % 5 == 1) and (i % 7 == 0):
        print(i, end=' ')

7.列表推导式

num = int(input("请输入一个 1-100 间的整数: "))

print([i for i in range(1, num   1) if i % 2 != 0])

8.正则表达式

# 搭配: 首写为 '数据或下划线, 其他英文字母为 英文字母,数据,下划线'

import re

my_str = input("请输入您即将申请注册的登录名: ")

ret = re.match(r"[a-z_]?[a-z\d\s_]*", my_str)

print(ret.group())
print(True) if ret else print(False)

9.词典按值排列

import operator

int(input("输入您要解决的整数数量: "))
num_lst = list(map(int, input("请在一行键入, 两二间用 空格符 分隔").split()))

my_dict = {}
for num in num_lst:
    if num not in my_dict:
        my_dict[num] = 1
    else:
        my_dict[num]  = 1

items = sorted(my_dict.items(), key=operator.itemgetter(1), reverse=True)
for i, j in items:
    print(str(i)   ' '   str(j))
文章来源于网络,如有侵权请联系站长QQ61910465删除
本文版权归趣快排www.sEoguruBlog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ✈61910465