n = input()
print(n**32)
2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in ()
1 n = input()
----> 2 print(n**32)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
n = eval(input())#eval() 函数用来执行一个字符串表达式,并返回表达式的值。
print(n**32)
2
4294967296
L = input()
for i in L:
print(i)
hello world!
h
e
l
l
o
w
o
r
l
d
!
print(eval(input()))
3+2
5
num = eval(input())
print(round(num))
"""
In [14]: round(2.355, 2)
Out[14]: 2.35
round() 方法返回浮点数x的四舍五入值
但是不是简单的四舍五入,有兴趣可参考 https://www.runoob.com/w3cnote/python-round-func-note.html
"""
10.24
10
num = eval(input())
num = num / 100
print(round(num))
#下为另一方法,实现方法很多,可以多尝试不同方法,能更好的理解
#num = input()
#print(num[:-2])
1024
10
L = input()
s = ""
for i in L:
if i == ' ' or i == L[-1]:
if i == L[-1]:
s += L[-1]
print(s)
s = ""
else:
s += i
#学习下一章,将会有一个str.split(),可以根据指定字符进行分割,更方便,可以提前了解
hello world!
hello
world!
num_to_days = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期七",]
num = eval(input())
print(num_to_days[num-1])
3
星期三
num = input()#可以当作字符串处理,也可以用类似判断水仙花的方法,代码会更少
for i in range(int(len(num)/2)):
if num[i] != num[-(i+1)]:
print("no")
break
else:#奖励机制:循环完整执行时才会执行,下一章会讲到
print("yes")
hello
no
num = eval(input())
print("{}的二进制数为:{:b},八进制数为:{:o},十六进制数为:{:x}".format(num,num,num,num)) #str.format(),用于控制格式
425
425的二进制数为:110101001,八进制数为:651,十六进制数为:1a9
year = eval(input())
if (year % 4 == 0 and year % 100) or year % 400 == 0:
print("yes")
else:
print("no")
2000
yes
a = eval(input())
b = eval(input())
num1 = a
num2 = b
if b > a:
(a,b) = (b,a)#交换a,b
while a%b:
c = a % b
a = b
b = c
print("{}和{}的最大公约数是:{},最小公倍数为:{}".format(num1,num2,b,num1*num2/b))
8
4
8和4的最大公约数是:4,最小公倍数为:8.0
string = input()
dig = 0
alp = 0
space = 0
other = 0
for i in string:
if i.upper() >= 'A' and i.upper() <= 'Z':
alp += 1
elif i >= '0' and i <= '9':
dig += 1
elif i == ' ':
space += 1
else:
other += 1
print("{}中数字个数:{},字母个数:{},空格个数:{},其他:{}".format(string,dig,alp,space,other))
123,hello world!
123,hello world!中数字个数:3,字母个数:10,空格个数:1,其他:2
import random
target = random.randint(1,1000)
count = 0
while True:
try:
guess = eval(input('请输入猜测的整数(1-1000):'))
count += 1
if guess > target:
print("猜大了!")
elif guess < target:
print("猜小了!")
else:
print("猜对了!")
break
except:
print("输入有误!请重新输入一个整数!")
print("此轮猜测次数为:",count)
请输入猜测的整数(1-1000):i
输入有误!请重新输入一个整数!
请输入猜测的整数(1-1000):100
猜小了!
请输入猜测的整数(1-1000):300
猜小了!
请输入猜测的整数(1-1000):800
猜小了!
请输入猜测的整数(1-1000):900
猜小了!
请输入猜测的整数(1-1000):950
猜大了!
请输入猜测的整数(1-1000):925
猜大了!
请输入猜测的整数(1-1000):910
猜大了!
请输入猜测的整数(1-1000):905
猜对了!
此轮猜测次数为: 9
import random
x=100000
change=0
nochange=0
for i in range(1,x+1):
guess=random.randrange(1,4)
real=random.randrange(1,4)
if guess==real:
nochange=nochange+1
else:
change=change+1
print("不更改选择得到汽车的概率为{}".format(nochange/x))
print("更改选择得到汽车的概率为{}".format(change/x))
不更改选择得到汽车的概率为0.33338
更改选择得到汽车的概率为0.66662
def isnum(string):
try:
if isinstance(eval(string),int) or isinstance(eval(string),float) or isinstance(eval(string),complex):
print("True")
except:
print("False")
string = input('请输入一个字符串:')
isnum(string)
请输入一个字符串:1+1j
True
def isprime(num):
for i in range(2,num):
if num % i == 0:
return False
return True
try:
num = eval(input('请输入一个整数:'))
print(isprime(num))
except:
print('请输入一个整数!')
请输入一个整数:17
True
def printf():
for num in range(1,201):
for i in range(2,num):
if num % i == 0:
break
else: print(num,end = ' ')
printf()
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
def Fibonacci(n):
if n <= 2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
n = eval(input('请输入一个整数:'))
print(Fibonacci(n))
请输入一个整数:6
8
txt = "i love three things,the sun for morning,the moon for night,and you for ever."
txt = txt.lower()
counts = dict()
for i in txt:
counts[i] = counts.get(i,0) + 1
items = list(counts.items())#键值对以元组形式返回
items.sort(key = lambda x:x[1], reverse = True)#以元组中第二个值,即原始dict中的值为排列依据
for i in items:
a, b = i
print('{}出现的次数为{}'.format(a,b))
出现的次数为12
o出现的次数为8
e出现的次数为7
n出现的次数为7
r出现的次数为6
t出现的次数为5
h出现的次数为5
i出现的次数为4
g出现的次数为3
,出现的次数为3
f出现的次数为3
v出现的次数为2
s出现的次数为2
u出现的次数为2
m出现的次数为2
l出现的次数为1
a出现的次数为1
d出现的次数为1
y出现的次数为1
.出现的次数为1
import random
mimalist = [chr(i) for i in range(97,123)] + [chr(i) for i in range(65,91)] + [chr(i) for i in range(49,58)]
print(mimalist)
#以上为列表推导式,其实可以理解成把for 循环压扁。具体用法百度。
mima = [[mimalist[random.randint(0,60)] for i in range(8)] for j in range(10)]
for i in mima:
print(''.join(i))#列表转字符串输出
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9']
CrOEta1h
a8YfBfiL
Lrz9Hpfv
CaJZvbaX
KiDgfTO2
wCOA2mS9
c795COYs
b3OdOdeY
6f22JdIG
Gn3vqHoA
#可以重头到尾找,我这里用序列类型的count操作
def find_repeat(L):
for i in L:
if L.count(i) > 1:
return True
return False
L = input("请输入一个列表")
L = list(L)
print(L)
print(find_repeat(L))
print(L)
请输入一个列表ilovepython
['i', 'l', 'o', 'v', 'e', 'p', 'y', 't', 'h', 'o', 'n']
True
['i', 'l', 'o', 'v', 'e', 'p', 'y', 't', 'h', 'o', 'n']
def find_repeat(L):
new_L = set(L) #集合类型会自动去重
if L != new_L:
return True
return False
L = input("请输入一个列表")
L = list(L)
print(L)
print(find_repeat(L))
print(L)
请输入一个列表ilovepython
['i', 'l', 'o', 'v', 'e', 'p', 'y', 't', 'h', 'o', 'n']
True
['i', 'l', 'o', 'v', 'e', 'p', 'y', 't', 'h', 'o', 'n']
def Count(fname, S, count):
file = fname + '.txt'
f = open(file, 'r', encoding='utf-8')
txt = f.read()
f.close()
print(txt)
count = txt.count(S)
print('“{}”字符在《{}》中出现的的次数是:{}次'.format(S, fname, count))
fname = input('请输入txt文件名称:')
S = input('请输入一个字符:')
count = 0
Count(fname, S, count)
请输入txt文件名称:my
请输入一个字符:the
i love three things.
The sun,the moon,the night.
“the”字符在《my》中出现的的次数是:2次
def transfer(fname):
file = fname + '.txt'
f = open(file, 'r', encoding='utf-8')
txt = f.readlines()
f.close()
new_txt = []
print(txt)
for lines in txt:
for i in lines:
if i >= 'A' and i <= 'Z':
new_txt.append(i.lower())
elif i >= 'a' and i <= 'z':
new_txt.append(i.upper())
else:
new_txt.append(i)
f = open(file, 'w', encoding='utf-8')
for lines in new_txt:
f.writelines(lines)
f.close()
fname = input('请输入txt文件名称:')
transfer(fname)
请输入txt文件名称:my
['i LOVE THREE THINGS.\n', 'tHE SUN,THE MOON,AND YOU.']
还有两道题目推荐文章:https://juejin.im/post/5c07ddcd6fb9a049d7477ab3#heading-4