您的位置:首页 > 编程语言 > Python开发

几个常用python实现

2019-02-17 21:08 85 查看
[code]# ================冒泡排序================ #
lis = [56, 12, 1, 8, 354, 10, 100, 34, 56, 7, 23, 456, 234, -58, 0]
def sortport():
for i in range(len(lis)-1):
for j in range(len(lis)-1-i):
if lis[j] > lis[j+1]:
lis[j], lis[j+1] = lis[j+1], lis[j]

return lis
# ======================================== #
# ================计算x的n次方================ #
def power(x,n):
# return x ** n
s = 1
while n > 0:
n -= 1
s *= x
return s
# ============================================ #
# ================计算平方和================ #
def calc(*numbers):
summary = 0
for n in numbers:
summary += n * n
return summary
# ========================================== #
# ================计算阶乘================ #
def factorial(n):
result = n
for i in range(1,n):
result *= i
return result
# ========================================== #
# ================列出当前目录下的所有文件和目录名================ #
# d for d in os.listdir('.')]
# ================把一个List中的所有字符串变成小写================ #
L = ['Hello', 'World', 'IBM', 'APPle']
[s.lower() for s in L]
# ========================================== #
# ================把原字典的键值对颠倒并生产新的字典================ #
dict1 = {"A":"a","B":"b","C":"c"}
dict2 = {y:x for x,y in dict1.items()}
print(dict2)
# ================================================================ #
# ================打印九九乘法表================ #
for i in range(1,10):
for j in range(1, i+1):
print('%d x %d = %d \t' % (i, j, i*j), end='')
print()
# ================合并去重复================ #
list1 = [2,3,45,6,8,9,5,6]
list2 = [5,6,10,17,11,2,9]
list3 = list1 + list2
print(list3)  # 不去重只是两个列表的组合
print(list(set(list3)))
# ================判断闰年================ #
import calendar
year = int(input('请输入年份:'))
check_year = calendar.isleap(year)
if check_year == True:
print('%d是闰年' % year)
else:
print('%d是平年' % year)
# ================斐波那契数列================ #
# 方法一
def fibo(n):
a, b = 0, 1
while b <= n :
print(b, end=' ')
a, b = b, a+b

n = int(input('请输入n: '))
fibo(n)
'''方法二'''
# def fib1(n):
#     n1 = 1
#     n2 = 1
#     n3 = 1
#     if n < 1:
#         print('输入错误! ')
#         return -1
#     while (n-2) > 0:
#         n3 = n1 + n2
#         n1 = n2
#         n2 = n3
#         n -= 1
#     return n3
# n =int(input('输入n: '))
# print(fib1(n))
"""方法三"""
# def fib3(n):
#     if n < 1:
#         print('输入有误! ')
#         return -1
#     if n == 1 or n == 2:
#         return 1
#     else:
#         return fib3(n-1) + fib3(n-2)
# n = int(input('输入n: '))
# result = fib3(n)
# if result != -1:
#     print(result)

'''递归与迭代实现的结果相等,但是效率会相差很大'''

# ================进制转换================ #
dec = int(input('输入数字:'))
print("十进制为:", dec)
print("转换为二进制为:", bin(dec))
print("转换为八进制为:", oct(dec))
print("转换为十六进制:", hex(dec))
# ================最大公约数================ #
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 = i
return hcf_i
# ================最小公倍数================ #
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

 当字典用。

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: