Python introductory programming questions: 1~10 (answer)
Tip: It is best to think first, write first, and then read the answer ^_^
1.
for i in range(1, 5):
for j in range(1,5):
for k in range(1, 5):
if i != j and j!= k and i != k:
print(i,j,k)
2.
i = int(input('净利润:'))
arr = [1000000,600000,400000,200000,100000,0]
rat = [0.01,0.015,0.03,0.05,0.075,0.1]
r = 0
for idx in range(0,6):
if i > arr[idx]:
r += (i - arr[idx]) * rat[idx]
print((i-arr[idx]) * rat[idx])
i = arr[idx]
print(r)
3.
import math
for i in range(10001):
if math.sqrt(i + 100) == int(math.sqrt(i + 100)) and math.sqrt(i + 268) == int(math.sqrt(i + 268)):
print(i)
4.
def leapyear(n):
return True if (n % 4 == 0 and n % 100 != 0) or n % 400 == 0 else False
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 ]
year, month, day = [int(x) for x in input('input year/month/day: ').split('/')]
day2 = sum(days[:month - 1]) + day
if leapyear(year) and month > 2:
day2 += 1
print(day2)
5.
L = []
a = int(input('Please enter the first integer: '))
b = int(input('Please enter the second integer: '))
c = int(input('Please enter the third Integer: '))
L.append(a)
L.append(b)
L.append(c)
L.sort()
print(L)
6.
#Fibonacci sequence.
def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
return a
7.
#Output 9*9 multiplication formula table
for i in range(1, 10):
for j in range(1, i+1):
print('%s * %s = %s' %(i, j, i * j), end = ' ')
print()
8.
import math
leap = 1
h = 0
for m in range(101, 201):
k = int(math.sqrt(m + 1))
for i in range(2, k+1):
if m % i == 0:
leap = 0
break
if leap == 1:
print('%-4d' %m, end = '')
h += 1
if h % 10 == 0:
print()
leap = 1
print('The total is %d' % h)
9.
from math import sqrt #Determine
whether n is prime
def isprime(n):
if n <= 1:
return 0
m = int(sqrt(n))+1
for x in range(2,m):
if n%x = = 0:
return 0
return 1 #Use
recursion to decompose n and print prime factors
def bprime(n):
if isprime(n):
print(n)
else:
x = 2
while x <= int(n/2):
if n %x == 0:
print(x)
return bprime(n/x)
x = x + 1
10.
x2 = 1
for day in range(9,0,-1):
x1 = (x2 + 1) * 2
x2 = x1
print(x1)
It is very difficult to learn python by myself. I really hoped that someone could answer my questions and learn with other friends. Therefore, if you need python learning materials, learning roadmaps, python learning e-books, etc., or if you encounter any problems on the way to learning, you can see here~ The python teacher in the group will teach you free live teaching every night to take you to learn Python. Python learning exchange qq group: 196872581