1、乘法表
#! -*- coding:utf-8 -*- for i in range(1,10):
for j in range(1,i+1):
print('%d*%d=%d\t' % (i, j, i*j)),
print'\r'
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
2、回文数
#! -*- coding:utf-8 -*- num = raw_input('请输入一个整数: ') for i in range(len(num)/2):
if num[i] == num[-i-1]:
print('%s 是一个回文数' % num)
else:
print('%s 不是一个回文数' % num)
3、阶乘
#! -*- coding:utf-8 -*- def f(n):
if n < 0:
print('负数没有阶乘')
elif n == 0:
return 1
elif n == 1:
return 1
else:
return n*f(n-1) def jiesum(n):
sum = 0
for i in range(1, n+1):
sum += f(i)
return sum number = int(raw_input('请输入一个整数: '))
result = f(number)
sum = jiesum(number)
print('%d 的阶乘是%d' % (number, result))
print('%d 的阶乘和是%d' % (number, sum))
4、字符串去重
#! -*- coding:utf-8 -*- # 字符串去重
str = 'zhang jingjing'
lst = [] for i in str:
if i not in lst:
lst.append(i)
print ''.join(lst) # 把字符串sign替换成空格 str1 = 'title=华为招聘会&time=20190305&sign=zhang&name=jing'
lst1 = []
for i in str1.split('&'):
if i.startswith('sign='):
lst1.append('sign= ')
else:
lst1.append(i)
print '&'.join(lst1)
5、冒泡排序法
#! -*- coding:utf-8 -*-
# list = [1,5,4,78] # n个数比n-1轮 # 1 - n-1-1 # i n-i-1
num = raw_input('输入一组数: ')
# print(num) str
list = num.split(',') # list
# list = list(num)
print(list)
for i in range(len(list)-1):
for j in range(len(list)-i-1):
if list[j]>list[j+1]:
list[j],list[j+1] = list[j+1],list[j]
print(list)
6、自动生成扑克牌,输入1随机发一张牌,输入2随机发2张牌。。。输入0停止发牌并把之前的牌全部返回
#! -*- coding:utf-8 -*-
import random def auto():
pokers = []
poker = []
list1 = ['R', 'B', 'F', 'M']
list2 = ['A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'K', 'Q']
for i in list1:
for j in list2:
poker.append(i)
poker.append(j)
pokers.append(poker)
poker = []
pokers.append(['bighero'])
pokers.append(['smallhero'])
# random.shuffle(pokers)
return pokers def fapai(poker, n):
if n != 0:
return random.sample(poker, n)
else:
print('请输入一个整数') pokers = auto()
print(pokers)
result = []
while True:
number = int(raw_input('请输入一个整数: '))
if number != 0:
rand1 = fapai(pokers, number)
print(rand1)
for i in rand1:
result.append(i)
else:
print(result)
break
学习路上的同志共勉!