• Python代码学习


    1.生成四位数字字母验证码,大小写字母随机

      import random    
      if __name__ =="__main__":    #四位数字字母验证码的生成
          checkcode="" #保存验证码的变量
          for i in range(4):
              index=random.randrange(0,4)  #生成一个0~3中的数
              if index!=i and index +1 !=i:
                  checkcode +=chr(random.randint(97,122))  # 生成a~z中的一个小写字母
              elif index +1==i:
                  checkcode +=chr(random.randint(65,90) ) # 生成A~Z中的一个大写字母
              else:
                 checkcode +=str(random.randint(1,9))  # 数字1-9
          print(checkcode)
    #输出为:m47A、8wQ9、vugS

    2.Python统计字符串中数字,字母,汉字的个数

     1  import re
     2  str_test='abcdefgHABC123456中华民族'
     3    
     4   #把正则表达式编译成对象,如果经常使用该对象,此种方式可提高一定效率
     5   num_regex = re.compile(r'[0-9]')
     6   zimu_regex = re.compile(r'[a-zA-z]')
     7   hanzi_regex = re.compile(r'[\u4E00-\u9FA5]')
     8   
     9   print('输入字符串:',str_test)
    10  #findall获取字符串中所有匹配的字符
    11  num_list = num_regex.findall(str_test)
    12  print('包含的数字:',num_list)
    13  zimu_list = zimu_regex.findall(str_test)
    14  print('包含的字母:',zimu_list)
    15  hanzi_list = hanzi_regex.findall(str_test)
    16  print('包含的汉字:',hanzi_list)

    3.判断是否为闰年 (只分闰年和平年,平年有365天,闰年有366天。四年一个闰年被4整除,3年平年)

     1  # 判断是否为闰年
     2   while True:
     3       try:
     4          num=eval(input("请输入一个年份:"))
     5       except:
     6          print('输入错误年份')
     7           continue
     8       if (num %4==0 and num%100 !=0) or num %400==0:
     9           print(num,"是闰年")
    10       else:
    11          print(num,"不是闰年")
     1 #方法二
     2 
     3 import calendar
     4 
     5 year = int(input("请输入年份:"))
     6 check_year=calendar.isleap(year)
     7 if check_year == True:
     8     print ("闰年")
     9 else:
    10     print ("平年")

    4.Python统计字符串中数字,字母,汉字的个数

     1  import re
     2   str_test='abcdefgHABC123456中华民族'
     3    
     4   #把正则表达式编译成对象,如果经常使用该对象,此种方式可提高一定效率
     5   num_regex = re.compile(r'[0-9]')
     6   zimu_regex = re.compile(r'[a-zA-z]')
     7   hanzi_regex = re.compile(r'[\u4E00-\u9FA5]')
     8   
     9   print('输入字符串:',str_test)
    10  #findall获取字符串中所有匹配的字符
    11  num_list = num_regex.findall(str_test)
    12  print('包含的数字:',num_list)
    13  zimu_list = zimu_regex.findall(str_test)
    14  print('包含的字母:',zimu_list)
    15  hanzi_list = hanzi_regex.findall(str_test)
    16  print('包含的汉字:',hanzi_list)

    5.记录显示登录日志实例

     1 import time
     2 
     3 def show_info():
     4     print('''输入提示数字,执行相应操作
     5 0:退出
     6 1:查看登录日志
     7     ''')
     8 
     9 def write_loginfo(username):
    10     """
    11     将用户名和登录时间写入日志
    12     :param username: 用户名
    13     """
    14     with open('log.txt','a') as f:
    15         string = "用户名:{} 登录时间:{}\n".format(username ,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
    16         f.write(string)
    17 
    18 def read_loginfo():
    19     """
    20     读取日志
    21     """
    22     with open('log.txt','r') as f:
    23         while True:
    24             line = f.readline()
    25             if line == '':
    26                 break  # 跳出循环
    27             print(line)  # 输出一行内容
    28 
    29 if __name__ == "__main__":
    30     # 输入用户名
    31     username = input('请输入用户名:')
    32     # 检测用户名
    33     while len(username) < 2 :
    34         print('用户名长度应不少于2位')
    35         username = input('请输入用户名:')
    36     # 输入密码
    37     password = input('请输入密码:')
    38     # 检测密码
    39     while len(passw ord) < 6 :
    40         print('密码长度应不少于6位')
    41         password = input('请输入密码:')
    42 
    43     print('登录成功')
    44     write_loginfo(username)  # 写入日志
    45     show_info()              # 提示信息
    46     num = int(input('输入操作数字:')) # 输入数字
    47     while True:
    48         if num == 0:
    49             print('退出成功')
    50             break
    51         elif num == 1:
    52             print('查看登录日志')
    53             read_loginfo()
    54             show_info()
    55             num = int(input('输入操作数字:'))
    56         else:
    57             print('您输入的数字有误')
    58             show_info()
    59             num = int(input('输入操作数字:'))

    6.求最大公约数和最小公倍数 (辗转相除法)

    最大公约数:指两个或多个整数共有约数中最大的一个

    最小公倍数:两个或多个整数公有的倍数叫做它们的公倍数,其中除0以外最小的一个公倍数就叫做这几个整数的最小公倍数

    二者关系:两个数之积=最小公倍数*最大公约数

     1 a=int(input('输入数字1:'))
     2 b=int(input('输入数字2:'))
     3 s=a*b
     4 while a%b!=0:
     5     a,b=b,(a%b)
     6     print(a)
     7     print(b)
     8 else:
     9     print(b,'is the maximum common divisor最大公约数')
    10     print(s//b,'is the least common multiple,最小公倍数')

    方法二

     1 a=int(input('please enter 1st num:'))
     2 b=int(input('please enter 2nd num:'))
     3 s=a*b
     4 
     5 while a!=b:
     6     if a>b:
     7         a-=b
     8     elif a<b:
     9         b-=a
    10 else:
    11     print(a,'is the maximum common divisor')
    12     print(s//a,'is the least common multiple')
    13 
    14 #运行结果
    15 please enter 1st num:40
    16 please enter 2nd num:60
    17 20 is the maximum common divisor
  • 相关阅读:
    Pytest 系列(28)- 参数化 parametrize + @allure.title() 动态生成标题
    Pytest 系列(27)- allure 命令行参数
    Pytest 系列(26)- 清空 allure 历史报告记录
    Pytest 系列(25)- 标记用例级别 @allure.
    Pytest 系列(24)- allure 环境准备
    基于Python的三种Bandit算法的实现
    博客迁移
    团体程序设计天梯赛2020游记
    P1825 [USACO11OPEN]Corn Maze S
    # JavaScript中的对象转数组Array.prototype.slice.call()方法详解
  • 原文地址:https://www.cnblogs.com/kongsq/p/_LearnPython.html
Copyright © 2020-2023  润新知