Python应用总结

本文介绍了Python编程中的实用操作,包括阿姆斯特朗数的概念及检测,字符串操作,计算每个月的天数,获取日历,获取昨天日期,ASCII码与字符的转换,以及最大公约数和最小公倍数的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python 阿姆斯特朗数

如果一个n位正整数等于其各位数字的n次方之和,则称该数为阿姆斯特朗数。 例如1^3 + 5^3 + 3^3 = 153。1000以内的阿姆斯特朗数: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407。以下代码用于检测用户输入的数字是否为阿姆斯特朗数。

num = int(input("请输入一个数字:"))
sum = 0
n = len(str(num))
temp = num

while temp > 0:
    digit = temp % 10
    sum += digit**n
    temp = temp // 10
if num == sum:
    print(num,"是阿姆斯特朗数")
else:
    print(num,"不是阿姆斯特朗数")

获取指定期间内的阿姆斯特朗数

lower = int(input("最小值: "))
upper = int(input("最大值: "))

for num in range(lower,upper + 1):
   # 初始化 sum
   sum = 0
   # 指数
   n = len(str(num))

   # 检测
   temp = num
   while temp > 0:
       digit = temp % 10
       sum += digit ** n
       temp //= 10

   if num == sum:
       print(num)

Python 实现字符串操作

str = "www.runoob.com"
print(str.upper())          # 把所有字符中的小写字母转换成大写字母
print(str.lower())          # 把所有字符中的大写字母转换成小写字母
print(str.capitalize())     # 把第一个字母转化为大写字母,其余小写
print(str.title())          # 把每个单词的第一个字母转化为大写,其余小写 

str = "runoob.com"
print(str.isalnum()) # 判断所有字符都是数字或者字母
print(str.isalpha()) # 判断所有字符都是字母
print(str.isdigit()) # 判断所有字符都是数字
print(str.islower()) # 判断所有字符都是小写
print(str.isupper()) # 判断所有字符都是大写
print(str.istitle()) # 判断所有单词都是首字母大写,像标题
print(str.isspace()) # 判断所有字符都是空白字符、\t、\n、\r

Python计算每个月天数

通过导入 calendar 模块来计算每个月的天数:

import calendar
monthRange = calendar.monthrange(2016,9)
print(monthRange)

输出的是一个元组,第一个元素是所查月份的第一天对应的是星期几(0-6),第二个元素是这个月的天数。以上实例输出的意思为 2018 年 5 月份的第一天是星期一,该月总共有 31天。

Python获取日历

import calendar
year = int(input("输入年份:"))
month = int(input("输入月份:"))

print(calendar.month(year,mont

Python 获取昨天日期

通过导入 datetime 模块来获取昨天的日期:

import datetime

def getYesterday():
    today = datetime.date.today()
    oneday = datetime.timedelta(days = 1)
    yesterday = today - oneday
    return yesterday

print(getYesterday())

Python ASCII码与字符相互转换

# 用户输入字符
c = input("请输入一个字符: ")
# 用户输入ASCII码,并将输入的数字转为整型
a = int(input("请输入一个ASCII码: "))

print( c + " 的ASCII 码为", ord(c))
print( a , " 对应的字符为", chr(a))

Python 最大公约数算法

# 定义一个函数
def hcf(x, y):
   """该函数返回两个数的最大公约数"""
   # 获取最小值
   if x > y:
       smaller = y
   else:
       smaller = x
   for i in range(1,smaller + 1):
       if((x % i == 0) and (y % i == 0)):
           hcf = i
   return hcf

# 用户输入两个数字
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))
print( num1,"和", num2,"的最大公约数为", hcf(num1, num2))

两个数的最大公约数可以使用 欧几里得算法实现。即两个数的最大公约数等于其中较小的那个数和两数相除余数的最大公约数。代码实现如下:

def gcd(a, b):
  if a == 0:
    a, b = b, a
  while b != 0:
    a, b = b, a % b
  return a

print(gcd(54, 24))

利用辗转相除的方法实现求两个正数的最大公约数,代码如下:

# find the greatest common divisor of two integer

def find_GCD(x, y):
  find_GCD = y
  while (x % y) != 0:
    find_GCD = x % y
    x, y = y, find_GCD
  return find_GCD

int1 = int(input('Input 1st integer: '))
int2 = int(input('Input 2nd integer: '))

print('The greatest common divisor of {} and {} is {}'.format(int1, int2, find_GCD(int1, int2)))

Python 最小公倍数算法

# 定义函数
def lcm(x, y):

   #  获取最大的数
   if x > y:
       greater = x
   else:
       greater = y

   while(True):
       if((greater % x == 0) and (greater % y == 0)):
           lcm = greater
           break
       greater += 1

   return lcm


# 获取用户输入
num1 = int(input("输入第一个数字: "))
num2 = int(input("输入第二个数字: "))

print( num1,"和", num2,"的最小公倍数为", lcm(num1, num2))

求取最小公约数的代码来实现求解两个整数的最大公倍数,代码如下:

# find the Least Common Multiple of two integer

def find_GCD(x, y):
  find_GCD = y
  while (x % y) != 0:
    find_GCD = x % y
    x, y = y, find_GCD
  return find_GCD

def find_LCM(x,y):
    return int(x * y / find_GCD(x, y))

int1 = int(input('Input 1st integer: '))
int2 = int(input('Input 2nd integer: '))

print('The least common multiple of {} and {} is {}'.format(int1, int2, find_LCM(int1, int2)))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值