个人手写,欢迎指正
更新中。。。
#!/user/bin/env python
#-*- coding:utf-8 -*-
#author:M10
import random
#3-1输入整数,输出百分位及以上的数字
'''
s = input("输入整数哦")
print(s[:-2])
'''
#3-2获取输入字符串,按照空格分割,按行打印
'''
i = input("输入字符串")
s = i.split()
for j in s:
print(j)
'''
#3-3输入数字1-7,输出星期几
"""
i = int(input('input a number between 1-7:'))
if i == 1:
print('monday')
elif i == 2:
print('tuesday')
elif i == 3:
print('wednesday')
elif i == 4:
print('thursday')#后续省略
else:
print('sunday')
"""
#3-4回文数,正反排列相同的数
"""
s = input("input a 5-num")
if s[0]==s[-1] and s[1]==s[-2]:
print('回文')
else:
print('不是回文')
"""
#3-5输入十进制整数,分别输出二进制、八进制、十六进制字符串
"""
s = int(input('a number:'))
print('二进制:{:b}'.format(s))
print('八进制:{:o}'.format(s))
print('十六进制:{:x}'.format(s))
"""
#4-1输入一个年份,判断是否是闰年
"""
i = int(input('input a year:'))
if (i%4==0 and i%100 != 0) or i%400==0 :
print(i,'闰年')
else:
print('不是闰年')
"""
#4-2最大公约数与最小公倍数
"""
a,b = map(int,input('输入两个数,逗号分割:').split(','))
x = a*b
if a>b:
while True:
s = a%b
if s ==0:
print('最大公约数:',b)
print('最小公倍数:',int(x/b))
break
b,a = s,b
elif a<b:
while True:
s = b%a
if s ==0:
print('最大公约数:',a)
print('最小公倍数',int(x/a))
break
a,b = s,a
else:
print('最大公约数:',a)#两数相等
print('最小公倍数',int(x/a))
"""
#4-3统计不同字符个数
"""
z,s,k,q = 0,0,0,0
str = input('输入字符:')
l = len(str)
for i in range(l):
if '0' <= str[i] <='9':
z += 1
elif 'A'<=str[i]<='z':
s += 1
elif str[i] == ' ':
k +=1
else:q+=1
print('整数',z)
print('英文字符',s)
print('空格',k)
print('其他',q)
"""
#4-4猜数有戏续
"""
ran = random.randint(1,1000)
count=0
while True:
try:
i = eval(input('pls input a num:'))
except:
print('类型错误,重新输入')
continue
if type(i) != type(1):
print('类型错误,重新')
continue
else:
count += 1
if i >ran:
print("it's bigger,{}次".format(count))
elif i<ran:
print("it's smaller,{}次".format(count))
else:
print("Right,{}次".format(count))
break
"""
#4-5羊车门问题
#十万次随机实验,不换猜到车的次数为count_n,交换猜到车次数为count
"""
j=1
count_n = 0
count = 0
while j<=100000:
j +=1
i= random.randint(0,2)#随机猜一个数字,0车,12羊
#若猜对车则不换+1,若猜对羊则换+1
if i == 0:
count_n +=1
else:
count +=1
print("don't change",count_n/100000)
print("change",count/100000)
"""
#5-1实现isNum函数
"""
def isNum(s):
if type(s)==type(1+1j) or type(s)==type(1) or type(s)==type(1.1):
return 1
else:
return 0
"""
#5-2实现isPrime函数
"""
def isPrime(a):
try:
if a%1 == 0 and a%2!=0 and a%3 !=0 and a%5 !=0 and a%7!=0:
return True
else:
return False
except:
print('pls input a intger')
print(isPrime(123))
"""
#5-3记数函数
"""
def count(s):
l = len(s)
count_int,count_str,count_sp,count_or = 0,0,0,0
for i in range(l):
if '0'<=s[i]<='9':
count_int +=1
elif 'A'<=s[i]<='z':
count_str +=1
elif s[i]==' ':
count_sp +=1
else:
count_or +=1
print('数字、字母、空格、其他分别为:',count_int,count_str,count_sp,count_or)
count('1232h1 2huuusnu2 u1ue2ue #$ ')
"""
#5-4打印200以内所有素数,空格分开
"""
#b记录整除次数,只能被1和自身整除的是质数
def print_prime():
for j in range(1,201):
b = 0
if j == 1 :
print(1,end=' ')
else:
for i in range(2,j+1):
if j%i ==0:
b += 1
if b<=1:
print(j,end=' ')
print_prime()
"""
#5-5参数为n,获取斐波那契数列第n个数并返回
"""
def Fibonacci(n):
a,b=1,1
i=1
while i<n:
b,a = a+b,b
i +=1
print(a)
Fibonacci(9)
"""
#6-1英文字符频率统计
"""
s = 'ahduh aidhiufhbfuh adnuasdnba'
d={}
for i in s:
if 'a'<=i<='z':
d[i]=d.get(i,0)+1
s = sorted(d,reverse=True)
for i in s:
print(i,d[i])
"""
#6-2中文字符频率统计
"""
s = 'hsua uhduahu阿虎毒啊 阿好速度啊哈shauhdia a d i2 213231huwqusn1 我是'
d = {}
for i in s:
d[i]=d.get(i,0)+1
s = sorted(d,reverse=True)
for i in s:
print(i,d[i])
"""
#6-3随机密码的生成
"""
import string
d = string.digits # 输出包含数字0~9的字符串
lo = string.ascii_lowercase # 包含所有小写字母的字符串
up = string.ascii_uppercase # 包含所有大写字母的字符串
list = list(d)+list(lo)+list(up)
n = len(list)
st=''
for j in range(0,10):
for i in range(0,8):
s = random.randint(0,n-1)
st=st+str(list[s])
print(st)
st = ''
"""
#6-4重复元素的判定
"""
def judge_list(list):
dic = {}
for i in list:
dic[i]=dic.get(i,0)+1
if max(dic.values())>1:
print('pass')
return True
else:
print('no pass')
list=[1,2,3,4,5,6,7,7,7,8,9]
judge_list(list)
list=[1,2,3,4,5,6,7,10,8,123,9]
judge_list(list)
"""
#6-5上题续
"""
def judge_list(list):
l1 = len(list)
s=len(set(list))
if l1==s:
print('pass')
return True
else:
print('no pass')
list1=[1,2,3,4,5,6,7,7,7,8,9]
judge_list(list1)
list=[1,2,3,4,5,6,7,10,8,123,9]
judge_list(list)
"""
#7-1输入一个文件和字符,统计该字符在文件中出现的次数
txt = input('input a txtpath:')
str = input('input a str:')
s=0
f = open(txt,'r')
text = f.read()
for i in text:
if i==str:
s +=1
print(str,'has',s,'in txt')
"""
测试结果如下
input a txtpath:/Users/wangxingfan/Desktop/1.txt
input a strpath:s
s has 402 in txt
"""
#7-2英文文件,大小写字母互换
path = '/Users/wangxingfan/Desktop/1.txt'
f = open(path,'r')
str = f.read()
j=''
for i in str:
if i<'a':
s = i.lower()
j=j+s
elif i>='a':
s = i.upper()
j=j+s
print(j)
print('-------------')
print(str)
f.close()
"""
测试结果如下
aabuhyybnuhybuyyBUAYDABNubqdywubYBYBQWYBDYQBdqbdlhqbdBUBBDEQBSQHUKJBDqbwDHQ,
DNuQDbbqdubhqd,
DnqnwdqdDNQDNQI,
DQWNDQUJDNUndnjwndj
-------------
AABUHYYBNUHYBUYYbuaydabnUBQDYWUBybybqwybdyqbDQBDLHQBDbubbdeqbsqhukjbdQBWdhq,
dnUqdBBQDUBHQD,
dNQNWDQDdnqdnqi,
dqwndqujdnuNDNJWNDJ
"""
#7-3随机生成10*10矩阵并保存csv
import string
import numpy as np
path = '/Users/wangxingfan/Desktop/1.csv'
d = string.digits # 输出包含数字0~9的字符串
lo = string.ascii_lowercase # 包含所有小写字母的字符串
up = string.ascii_uppercase # 包含所有大写字母的字符串
list = list(d)+list(lo)+list(up)
for c in range(0,10):
j = np.random.randint(0, len(list), 10)
a = ''
for i in j:
s = list[i]+','
a = a+s
f = open(path,'a')
f.write(a+'\n')
f.close()
#查看结果
![结果](https://img-blog.csdnimg.cn/20181117214142616.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dhbmd4aW5nZmFuMzE2,size_16,color_FFFFFF,t_70)
#7-4