自学30个常用python实现

 
1、 Python实现冒泡排序
# coding=utf-8
# Python实现冒泡排序
def bubble_sort(array):
    for i in range( len(array)-1):
        for j in range( len(array)-1-i):
            if array[j] > array[j+1]:
                array[j], array[j+1] = array[j+1], array[j]
    return array
 
 
if __name__ == '__main__':
    array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
    print(bubble_sort(array))
    #[5, 7, 10, 15, 17, 21, 24, 27, 30, 36, 45, 50]

"""
参考资料:
https://blog.csdn.net/weixin_43790276/article/details/104033622
"""
View Code
 2、计算x的n次⽅的⽅法
# 2、计算x的n次⽅的⽅法


def power(x, n ):
    s = 1
    while n > 0:
        n -= 1
        s = s * x
    return s


x = 2
a = power(x,4)
print(a)
View Code

3、计算a*a + b*b + c*c + ……

# 3、计算a*a + b*b + c*c + ……

def calc(*numbers):
    sum = 0
    for n in numbers:
        sum = sum + n*n
    return sum


if __name__ == '__main__':
    array = calc(1,2,3,4)
    print( array)
# 30
View Code
 4、计算阶乘 n!(用递归实现)
# 4、计算阶乘 n!(用递归实现)


def fact(num):
    if num < 0:
        return print("抱歉负数没有阶乘!")
    if num ==0:
        print("0 的阶乘为 1")
        return 1
    elif num==1:
        # print("1 的阶乘为 1")
        return 1
    elif num > 1:
        return num*fact(num-1)


"""
def factorial(n):
 return n*factorial(n-1) if n>1 else 1
print(factorial(5))
"""


if __name__ == '__main__':
    a=fact(6)
    print(a)  #720
View Code
5、列出当前⽬录下的所有⽂件和⽬录名
# 5、列出当前⽬录下的所有⽂件和⽬录名
import os

ml = [d for d in os.listdir('.')]
print(ml)
View Code
6、把⼀个list中所有的字符串变成⼩写
# 6、把⼀个list中所有的字符串变成⼩写
L = ["Hello", "World", "IBM", "Apple"]

def trans_lower(array):
    a = []
    for i in range( len(array) ):
        a.append( array[i].lower() )
    return a

if __name__ == '__main__':
    a = trans_lower(L)
    print(a)
    b = [s.lower() for s in L]
    print(b)
View Code
7、输出某个路径下的所有⽂件和⽂件夹的路径
#7、输出某个路径下的所有⽂件和⽂件夹的路径
import os

def print_dir():
    filepath = input("请输入一个路径:")
    if filepath == "":
        print("请输入正确的路径")
    else:
        for i in os.listdir(filepath):  #获取目录中的文件及子目录列表
            print(os.path.join(filepath,i))  #把路径组合起来

print( print_dir() )
View Code

 8、打印输出某个路径及其⼦⽬录下的所有⽂件的绝对路径

#8、打印输出某个路径及其⼦⽬录下的所有⽂件的绝对路径

import os
def show_dir(filepath):
    for i in os.listdir(filepath):
        path = (os.path.join(filepath, i))
        print(path)
        if os.path.isdir(path):  #isdir()判断是否是目录
            show_dir(path)       #如果是目录,使用递归方法

filepath = "C:\Program Files\Microsoft VS Code"
show_dir(filepath)
View Code
9、输出某个路径及其⼦⽬录下所有以.html为后缀的⽂件
# 9、输出某个路径及其⼦⽬录下所有以.html为后缀的⽂件
import os
def print_dir(filepath):
    for i in os.listdir(filepath):
        path = os.path.join(filepath, i)
        if os.path.isdir(path):
            print_dir(path)
        if path.endswith(".html"):
            print(path)

filepath = "F:\PycharmProjects"
print_dir(filepath)
View Code
10、把原字典的键值对颠倒并⽣产新的字典
#  10、把原字典的键值对颠倒并⽣产新的字典


dict1 = {"A":"a","B":"b","C":"c"}
dict2 = { v:k for k,v in dict1.items()}
print(dict2)
View Code

 

 

 

posted @ 2022-02-11 10:42  Marlon康  阅读(77)  评论(0编辑  收藏  举报