# Python——入门 | 常用python实现

list = [56,12,1,8,354,10,100,34,56,7,23,456,234,-58]
def MP_paixu():
for i in range(len(list)-1):
for j in range(len(list)-1-i):
for list[j] > list[j+1]:
list[j],list[j+1] = list[j+1],list[j]

return list

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

def cals(*number):
sum = 0
for n in number:
sum = sum + n * n
return sum

def fac():
num = int(input("请输入一个数字"))
factorial = 1
#查看数字是负数，0或正数
if num < 0 :
print("抱歉，负数没有阶乘")
elif num == 0 :
print("0的阶乘为1")
else:
for i in range(1 , num +1):
factorial = factorial * i
print("%d的阶乘为%d"%(num,factorial))

i = [d for d in os.listdir('.')]

L = ['Hello','World','IBM','Apple']
l = [s.lower() for s in L]

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

print(print_dir())

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

filepath = "C:\Program Files\Internet Explorer"
show_dir(filepath)

def show_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 = "C:\Program Files\Internet Explorer"
show_dir(filepath)

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 * %d = %d \t'%(i,j,i*j),end='')

num = ['harden','lampard',3,34,45,56,76,87,78,46,3,3,3,87686,98,76]
#获取出现3的次数
for i in range(num.count(3)):
# 获取首次3出现的坐标
ele_index = num.index(3)
#修改3为3a
num[ele_index] = "3a"

L = ['harden','lampard']
for i in range(len(L)):
print("Hello,%s"%L[i])

list1 = [2,3,4,8,9,5,6]
list2 = [5,6,10,11,17,12]
list3 = list1 + list2
print(set(list3))
print(list(set(list3)))

num = float(input('请输入一个数字'))
num_sqrt = num ** 0.5
print(num_sqrt)

def is_number(s):
try:
float(s)
return True
except ValueError:
pass

try:
import unicodedata
unicodedata.numeric(s)
return True
except(TypeError,ValueError):
pass

return False

num = int(input("输入一个数字："))
if (num % 2) == 0:
print("偶数")
else:
print("奇数")

import calendar
year = int(input("输入一个年份："))
check_year = calendar.isleap(year)
if check_year == True:
print("闰年")

x = int(input('请输入需要对比大小数字的个数：\n'))
num = [int(input('请输入第%d各对比的数字：\n'%i)) for range(1,x+1)]

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

posted @ 2019-01-23 09:56  澄心元素  阅读(...)  评论(...编辑  收藏