• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

9道python基础练习题

python 搞java代码 3年前 (2022-05-21) 17次浏览 已收录 0个评论

1. 3位整数逆序

<code class="language-python">num = <a href="https://www.gaodaima.com/tag/input" title="查看更多关于input的文章" target="_blank">input</a>("请输入一个三位整数:")

<a href="https://www.gaodaima.com/tag/print" title="查看更多关于print的文章" target="_blank">print</a>(num[::-1])
</code>

www#gaodaima.com来源gao@dai!ma.com搞$代^码网搞代码

2. if – else 练习

<code class="language-python">sex = input("请输入您的性别(F 或 M): ")
<a href="https://www.gaodaima.com/tag/age" title="查看更多关于age的文章" target="_blank">age</a> = 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")
</code>

3. if-else 和 数学计算 练习

<code class="language-python">#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.")
</code>

4. 循环: 文件读写

<code class="language-python">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]))
</code>

5.循环练习: 亲密数

<code class="language-python">#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)
</code>

6.循环: 逻辑判断

<code class="language-python">n = int(input("请输入最大兵力人数: "))

for i in range(n + 1):
    if (i % 3 == 2) and (i % 5 == 1) and (i % 7 == 0):
        print(i, end=" ")
</code>

7.列表推导式

<code class="language-python">num = int(input("请输入一个 1-100 间的整数: "))

print([i for i in range(1, num + 1) if i % 2 != 0])
</code>

8.正则表达式

<code class="language-python"># 匹配: 首字母为 "数字或下划线, 其余字母为 字母,数字,下划线"

import re

my_str = input("请输入您将要注册的用户名: ")

ret = re.match(r"[a-z_]?[a-zds_]*", my_str)

print(ret.group())
print(True) if ret else print(False)
</code>

9.字典按值排序

<code class="language-python">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))
</code>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:9道python基础练习题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址