Remarks: Don't copy and paste to run directly, you need to type the code yourself to make progress, pay attention to line indentation
1. Output a number that is divisible by 2 within 100, and the result is to print 5 outputs per line
>>> n=0
>>> for i in range(100):
... if i % 2 == 0:
... print(i,end=" ")
... n+=1
... if n % 5 ==0:
... print("\n")
2. Swap the values of a and b
>>> a=1
>>> b=2
>>> a,b=b,a
>>> print(a,b)
3. The output -1 is true or false
>>> print(bool(-1))
4. Output 2 to the third power
print(2**3)
5. Square root of output 9
import math
print(math.sqrt(9))
6. Calculate the sum of ASCII encoding of az
count=0
for i in range(ord('a'),ord('z')+1):
count=count+i
print(count)
7. The range of output range (10)
print(list(range(10)))
8. Output az reverse display
import string
print(string.ascii_lowercase[::-1])
9. Display the letters in the 2, 4, 6, 8, and 10 digits after a in az
for i in range(2,10,2):
print(chr(ord('a')+i))
10. Count the number of a in the string
方法一:
s='abcdabcd'
print(s.count('a'))
方法二:用字典
>>> result={}
>>> for i in s:
... if i in result.keys():
... result[i]=result[i]+1
... else:
... result[i]=1
... print(result)
11. Remove a from a sentence
方法一:
>>> s="a boy,a girl. glory road is glory."
>>> result=""
>>> for i in s: #遍历字符串
... if i!='a': #不是a,就把字符串拼接到空的变量中
... result+=i
... print("结果是:",result)
方法二:
s=s.replace('a', "")
12. Enter a number to compare the size with a randomly generated integer of 1-10
from pip._vendor.distlib.compat import raw_input
import random
a=int(raw_input("请输入你猜测的数字:\n"))
print("你猜测的数字是:",a)
b=random.randint(1,10)
print("随机数是:",b)
if a>b:
print("猜大了, %s>%s" %(a,b))
elif a==b:
print("猜对了, %s=%s" %(a,b))
else:
print("猜小了, %s<%s" %(a,b))
13. Enter a number and compare it with a randomly generated integer from 1-10 until they are equal
from pip._vendor.distlib.compat import raw_input
import random
b=random.randint(1,10)
while 1: #死循环
a=int(raw_input("请输入你猜测的数字:\n"))
print("你猜测的数字是:",a)
if a > b:
print("猜大了, %s>%s" %(a,b))
elif a==b:
print("猜对了, %s=%s" %(a,b))
break
else:
print("猜小了, %s<%s" %(a,b))
14. Randomly output integers from 1 to 10 until it ends at 3
import random
a=1
while a!=3:
a=random.randint(1,10)
print(a)