前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python编程 基础练习(二)

Python编程 基础练习(二)

作者头像
叶庭云
修改2021-01-23 14:33:21
7210
修改2021-01-23 14:33:21
举报
文章被收录于专栏:Python进阶之路Python进阶之路

文章目录

1. 随机生成由2个大写字母(前2位)+2个小写字母(第3、4位)+4个数字(第5-8位)组成的密码(字符串)

代码语言:txt
复制
import random

# 大写字母的ASCII码范围
a = [chr(i) for i in range(65, 91)]
# 小写字母的ASCII码范围
b = [chr(j) for j in range(97, 123)]
# 数字
c = [k for k in range(0, 10)]
s = ""
for x in range(1, 9):
    # 前两位 大写字母
    if x <= 2:
        s += random.choice(a)
    # 3、4位 小写字母
    elif 2 < x <= 4:
        s += random.choice(b)
    # 5-8位  数字  转成字符串
    else:
        s += str(random.choice(c))
print(s)

2. 编写函数,输出公式a+aa+aaa+…并计算其结果,其中a为1-9之间的整数,公式的项为n,如a和n分别为3和5时,输出并计算公式 3+33+333+3333+33333。

代码语言:txt
复制
def analysis(a, n):
    s = ""
    for i in range(1, n + 1):
        if i == n:
            s += str(a) * i
        else:
            s += str(a) * i
            s = s + "+"
    print(s)   # 输出公式
    result = sum([int(str(a) * i) for i in range(1, n + 1)])  # 求和
    return result


a = int(input("输入a:"))
n = int(input("输入n:"))
print("计算结果为:{}".format(analysis(a, n)))

3. 统计一段英文字符串中单词"the"的个数,并提取出首字母非t的单词。

代码语言:txt
复制
import re

_str = 'It was this sense of freedom the first innovation entrusted by the west life to the Individualism.' \
       ' On the other hand, American pioneers lost their comfortable life when they were trying to break ' \
       'away from the fetters of the old social order caused by the civilized society. Hence, they had to ' \
       'live by themselves with their independent spirit of pioneering .'

the_count = _str.count("the")  # "the" 的个数
print("单词the的个数:{}".format(the_count))

# 提取英文单词  "\b"表示单词的开头或结尾  返回列表
t_str = re.findall(r'\bt[a-zA-Z]+\b', _str)
all_str = re.findall(r'\b[a-zA-Z]+\b', _str)
print(all_str)  # 所有单词
print(t_str)    # 首字母含t的
no_t_str = []
for i in all_str:
    if i in t_str:  # 首字母是t的  滤掉
        continue
    else:
        no_t_str.append(i)
print(no_t_str)

4. 简单实现抽奖功能

代码语言:txt
复制
import random

rewards ={"一等奖": "汽车一辆", "二等奖": "电视一台", "三等奖": "洗衣液一袋"}
print("一等奖---------->汽车一辆 \n二等奖---------->电视一台 \n三等奖---------->洗衣液一袋")


def getReward(rewards):
    num = random.random()
    if 0 <= num < 0.08:
        reward = "一等奖"
    elif 0.08 <= num < 0.3:
        reward = "二等奖"
    elif 0.3 <= num < 1:
        reward = "三等奖"
    # 返回对应奖项和奖项对应的值
    return reward, rewards[reward]


for i in range(3):
    order = input("按回车键抽奖...")
    k, v = getReward(rewards)
    print("第{}次抽奖结果:{}---->{}".format(i + 1, k, v))

5. 编写函数,接收一个任意字符串,返回其中最长的连续数字子串。

代码语言:txt
复制
import re


def longest_num(s):
    # 匹配多个数字  返回一个列表
    nums = re.findall(r'\d+', s)
    _nums = [int(i) for i in nums]
    return max(_nums)   # 最长的


# 举例输入
# 123 python36a12345snfsig1?!132975..
# abcd12345ed125ss123456789
string = input("输入字符串为:")
result = longest_num(string)
print("最长的连续数字子串为:{}".format(str(result)))

6. 冒泡排序

代码语言:txt
复制
nums = [1, 9, 8, 5, 6, 7, 4, 3, 2]
print("排序前:{}".format(nums))
length = len(nums)
for m in range(length - 1):
    flag = True
    for n in range(length - m - 1):
        if nums[n] > nums[n+1]:
            nums[n], nums[n+1] = nums[n+1], nums[n]
            flag = False
    if flag:
        break

print("排序后:{}".format(nums))

作者:叶庭云 微信公众号:修炼Python CSDN:https://yetingyun.blog.csdn.net/ 本文仅用于交流学习,未经作者允许,禁止转载,更勿做其他用途,违者必究。 觉得文章对你有帮助、让你有所收获的话,期待你的点赞呀,不足之处,也可以在评论区多多指正。

代码语言:txt
复制
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-05-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 随机生成由2个大写字母(前2位)+2个小写字母(第3、4位)+4个数字(第5-8位)组成的密码(字符串)
  • 2. 编写函数,输出公式a+aa+aaa+…并计算其结果,其中a为1-9之间的整数,公式的项为n,如a和n分别为3和5时,输出并计算公式 3+33+333+3333+33333。
  • 3. 统计一段英文字符串中单词"the"的个数,并提取出首字母非t的单词。
  • 4. 简单实现抽奖功能
  • 5. 编写函数,接收一个任意字符串,返回其中最长的连续数字子串。
  • 6. 冒泡排序
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档