python小练习

1、python内置函数接收用户输入字符串,并将其输出。

encoding:utf-8

str1 = raw_input('请输入字符串:'.decode('utf-8'));
print(str1)
str2 = raw_input("string:")
print str2

2、排序三个数
nums = [0,0,0]
inex = [0,1,2]
for i in inex:
nums[i] = int(raw_input('please input a number:'))
if nums[0]>nums[1]:
nums[0],nums[1] = nums[1],nums[0]
if nums[1]>nums[2]:
nums[1],nums[2] = nums[2],nums[1]
if nums[0]>nums[2]:
nums[0],nums[2] = nums[2],nums[0]
for i in nums:
print i

3、n的阶乘之和

def factorial(n):
if n==1:
return n
else:
return n*factorial(n-1)
intnum = int(raw_input('请输入一个整数:'.decode('utf-8')))

numberSum=0
for i in range(1,intnum+1):
numberSum +=factorial(i)
print(numberSum)

4、输出1000以内的水仙花数

for i in range(100,1000):
a = i//100
b = i//10%10
c = i%10
if aaa+bbb+ccc == i:
print i

5、三角形

triangle1 = [" *"," ***"," *****","*******"]
for i in triangle1:
print i

6、菱形

triangle2 = [" *"," ***"," *****","*******"]
triangle3 = [" *****"," ***"," *"]
for i in triangle2:
print i
for i in triangle3:
print i

7、空心菱形
triangle4 = [" *"," * *"," * "," *"]
triangle5 = [" * *"," * *"," *"]
for i in triangle4:
print i
for i in triangle5:
print i

8、实现一个列表的冒泡排序

List = [20,30,40,3,6,47,25,77,15]
length = len(List)
for i in range(0,length):
for j in range(0,length-i-1):
if List[j] > List[j+1]:
temp = List[j+1]
List[j+1] = List[j]
List[j] = temp
for i in List:
print i

9、实现列表的去重

List = [1,2,3,4,4,2,6,3,3,2]
length = len(List)
for i in range(length):
for j in range(i+1,length):
if List[i] == List[j]:
List[i] = 0
List1 = []
for i in range(len(List)):
if List[i] != 0:
List1.append(List[i])
print List1

===============
1.两个数的四则运算函数
def calculate(num1,operator,num2):
if operator == '+':
return num1+num2
elif operator == '-':
return num1-num2
elif operator == '':
return num1
num2
elif operator == '/':
return num1/num2
else:
print('error')
print(calculate(5,'+',6))

2.f(x) = f(x-1) +f(x-2)

def functionTest1(n):
num1 = 1
num2 = 1
num3 = 0
for i in range(2,n):
num3 = num1 + num2
num1 = num2
num2 = num3
return num3
print(functionTest1(100))

3.形状类

class Shape(object):
def init(self):
print('father')
def calPerimeter():
return
def calArea():
return
class Circular(Shape):
def init(self,radius):
self.radius = radius
def calPerimeter(self):
return 2self.radius3.14
def calArea(self):
return self.radiusself.radius3.14
class Rectangle(Shape):
def init(self,length,width):
self.length = length
self.width = width
def calPerimeter(self):
return (self.length+self.width)2
def calArea(self):
return self.length
self.width
c = Circular(3)
print(c.calPerimeter())
print(c.calArea())
r = Rectangle(3,4)
print(r.calArea())
print(r.calPerimeter())

4.打印n排杨辉三角(n<15)

def yanghui(n):
print(1)
for i in range(2,n+1):
s = 1
print(1),
for j in range(1,i-1):
s=(i-j)*s//j
print("%d "%s),
print(1)
yanghui(10)

======================
1.提取列表各个字符串的数字,一行代码解决

import re
l = ['15pb','213sdfj','saf3hj21','ad32f1']
print([re.findall('\d',j) for j in l]) #一行代码

2.VS工程清理工具

import os
stufix = [".ilk",".pdb",".obj",".log",".pch",".tlog",".lastbuildstate",".sdf",".idb",".ipch",".res",".o",".lst",".knl",".img",".bin",".db",".exe",".suo"]
def del_file(path):
for i in os.listdir(path):
path_file = os.path.join(path,i)
if os.path.isfile(path_file):
text = os.path.splitext(path_file)
if text[1] in stufix:
os.remove(path_file)
else:
del_file(path_file)
del_file('C:\Users\Tangos\Desktop\MFC001')

3.函数装饰器,能够为存在的函数添加处理名称错误的异常处理

def exceptions(func):
def add_excep(*args, *keyargs):
try:
return func(
args, **keyargs)
except NameError,e:
print('Error: %s' % func.name)
print(repr(e))
return add_excep

@exceptions
def funcTest():
eva('3') #eval()

if name == 'main':
funcTest()

你可能感兴趣的:(python小练习)