Python Exercises 1-10, python1-10

Source: Internet
Author: User

Python Exercises 1-10, python1-10

1. There are four numbers: 1, 2, 3, and 4. How many three numbers can be composed of different and non-repeated numbers? What is each?
2. the bonus paid by the enterprise is based on the profit. If the profit (I) is less than or equal to 0.1 million yuan, the bonus can be raised by 10%; if the profit is higher than 0.1 million yuan, if the profit is lower than 0.2 million yuan, the portion lower than 0.1 million yuan will be charged by 10%, and the portion higher than 0.1 million yuan, can be increased to 7.5%; 0.2 million to 0.4 million, higher than 0.2 million yuan, can be increased to 5%; 0.4 million to 0.6 million is higher than 0.4 million yuan, can be increased to 3%; between 0.6 million and 1 million, if the price is higher than 0.6 million yuan, the price can be increased to 1.5%. If the price is higher than 1 million yuan, the price of more than 1 million yuan is increased by 1%, and the current month's profit is input from the keyboard, how many bonuses should be paid?
3. An integer. After 100 is added, it is a full number of workers, and 168 is a full number of workers. What is this number?
4. Enter a day of a certain year to determine the day of the year?
5. Input three integers x, y, and z. Output these three numbers from small to large.
6. Fibonacci series.
7. Copy data from one list to another.
8. output the 9*9 multiplication table.
9. Pause one second output.
10. Pause the output for one second and format the current time.

1. There are four numbers: 1, 2, 3, and 4. How many three numbers can be composed of different and non-repeated numbers? What is each?
Method 1:
1 l = []2 for i in range(1,5):3     for j in range(1,5):4         for m in range(1,5):5             if len({i,j,m}) == 3:6                 l.append(i * 100 + j * 10 + m)7 print(l)8 print(len(l))
1 [123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]2 24
Method 2:
1 from itertools import permutations2 3 l = []4 for i in permutations([1, 2, 3, 4], 3):5     l.append(i)6 7 print(l)8 print(len(l))
1 [(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]2 24

2. the bonus paid by the enterprise is based on the profit. If the profit (I) is less than or equal to 0.1 million yuan, the bonus can be raised by 10%; if the profit is higher than 0.1 million yuan, if the profit is lower than 0.2 million yuan, the portion lower than 0.1 million yuan will be charged by 10%, and the portion higher than 0.1 million yuan, can be increased to 7.5%; 0.2 million to 0.4 million, higher than 0.2 million yuan, can be increased to 5%; 0.4 million to 0.6 million is higher than 0.4 million yuan, can be increased to 3%; between 0.6 million and 1 million, if the price is higher than 0.6 million yuan, the price can be increased to 1.5%. If the price is higher than 1 million yuan, the price of more than 1 million yuan is increased by 1%, and the current month's profit is input from the keyboard, how many bonuses should be paid?
Method 1:
1 # This is the weakest method, but I think of it .. 2 a = [0.01, 0.015, 0.03, 0.05, 0.075, 0] 3 B = [0.1,] 4 x = int ('sales profit is: ') 5 6 if x> = a [0]: 7 bonus = (x-a [0]) * B [0] + 400000 * B [1] + 200000 * B [2] + 200000 * B [3] + 100000 * B [4] + 100000 * B [5] 8 print ('Commission is: ', bonus) 9 elif a [0]> x> = a [1]: 10 bonus = (x-a [1]) * B [1] + 200000 * B [2] + 200000 * B [3] + 100000 * B [4] + 100000 * B [5] 11 print ('Commission is: ', bonus) 12 elif a [1]> x> = a [2]: 13 bonus = (x-a [2]) * B [2] + 200000 * B [3] + 100000 * B [4] + 100000 * B [5] 14 print ('Commission: ', bonus) 15 elif a [2]> x> = a [3]: 16 bonus = (x-a [3]) * B [3] + 100000 * B [4] + 100000 * B [5] 17 print ('Commission: ', bonus) 18 elif a [3]> x> = a [4]: 19 bonus = (x-a [4]) * B [4] + 100000 * B [5] 20 print ('Commission: ', bonus) 21 elif a [4]> x> = a [5]: 22 bonus = (x-a [5]) * B [5] 23 print ('Commission: ', bonus) 24 elif x <0: 25 print ('x must be greater than 0 ')
1. The sales profit is: 2000012 Commission: 17500.05
Method 2:
1 # refer to others' 2 a = [1000000,600 000, 400000,200 000, 100000, 0] 3 B = [0.01, 0.015, 0.03, 0.05, 0.075, 0.1] 4 count = 0 5 while count <5: 6 x = int (input ('sale profit is: ') 7 sum = 0 8 for n in range ): 9 if x> a [n]: 10 tmp = (x-a [n]) * B [n] # calculate the percentage of this level. 11 sum + = tmp12 x = a [n] # the subsequent percentage of each level is calculated as 13 print (sum) 14 count + = 1

 

3. An integer. After 100 is added, it is a full number of workers, and 168 is a full number of workers. What is this number?

Method 1:

Import mathx =-99 # This value comes to mind when looking at others' answers. x + 100 must be greater than 0 while x <10000: if math. pow (int (math. sqrt (x + 100), 2) = x + 100: if math. pow (int (math. sqrt (x + 100 + 168), 2) = x + 100 + 168: # simple column equations. No one will... Mdzz
# print(x) x += 1

Method 2: After reading other people's ideas, I feel that I am mentally retarded.

'''1, x + 100 = n2, x + 100 + 168 = m22, calculation equation: m2-n2 = (m + n) (m-n) = 1683, set: m + n = I, m-n = j, I * j = 168, at least one of I and j is an even number 4, available: m = (I + j)/2, n = (I-j)/2, I and j are either even or odd. 5. We can infer from 3 and 4 that both I and j are even numbers greater than or equal to 2. 6. Because I * j = 168, j> = 2, 1 <I <168/2 + 1. 7. calculate all the numbers of I in a loop. ''' I = 1 # obtain the maximum range of while (I + 1) * (I + 1)-I * I) <= 168: I + = 1 # cyclic test and print for j in range (1, I): for k in range (1, I): if (k * k-j * j) = 168: print (k * k-268)

 

4. Enter a day of a certain year to determine the day of the year?

Method 1:

Import datetimeY = int (input ('year: ') m = int (input ('month:') d = int (input ('day :')) days = datetime. datetime (Y, m, d)-datetime. datetime (Y, 1, 1) + datetime. timedelta (1) # Minus January 1 n = int (str (days ). split ('') [0]) print (n) year: 2003 month: 12 day: 2336

Method 2:

Import time # D = input ("Enter the year in the format of XXXX-XX-XX:") D = '2017-4-3 'd = time. strptime (D, '% Y-% m-% D '). tm_yday # print ("the {} day of this year! ". Format (d) print (time. strptime (D, '% Y-% m-% D') # or use the datetime module import datetimeprint (datetime. date (classification 4, 3 ). timetuple (). tm_yday) print (datetime. date (classification 4, 3 ). timetuple ())
93time.struct_time(tm_year=2017, tm_mon=4, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=93, tm_isdst=-1)93time.struct_time(tm_year=2017, tm_mon=4, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=93, tm_isdst=-1)

 

5. Input three integers x, y, and z. Output these three numbers from small to large.

Method 1

L = [a,b,c]L.sort()print('\n'.join(l))

Method 2:

1 # Best Practice 2 # a = input ('a = ') 3 # B = input (' B = ') 4 # c = input ('C = ') 5 a = 10 6 B = 20 7 c = 30 8 9 l = [] 10 11 l. append (a) 12 if B <a: 13 l. insert (0, B) 14 if c <B: 15 l. insert (0, c) 16 elif c> a: 17 l. append (c) 18 else: 19 l. insert (1, c) 20 else: 21 l. append (B) 22 if c <a: 23 l. insert (0, c) 24 elif c> B: 25 l. append (c) 26 else: 27 l. insert (1, c) 28 29 print ('\ n '. join (l ))

Method 3:

# Bubble Method a = [, 7] n = len (a) for I in range (0, n): for j in range (I, n ): if (a [I]> = a [j]): a [I], a [j] = a [j], a [I] print ()
[1, 2, 3, 4, 5, 5, 7]

6. Fibonacci series.
Method 1:
l = [0,1]def f(n):    for x in range(n):        l.append(l[x]+l[x+1])f(20)print(l)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946]

Method 2:

# Use recursive def fib (n): if n = 1 or n = 2: return 1 return fib (n-1) + fib (n-2) # output 10th Fibonacci series print (fib (20 ))

7. Copy data from one list to another.
a = list(range(20))b = a[:]print(a)print(b)print(id(a))print(id(b))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19][0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]20300079445842030007104200

8. output the 9*9 multiplication table.
1 # Baidu first describes what the multiplication table looks like and how to arrange the 2''' 3 multiplication table as follows: 4 1*1 = 1 5 2*1 = 2 2*2 = 4 6 3*1 = 3 3*2 = 6 3*3 = 9 7 4*1 = 4 4*2 = 8 4*3 = 12 4*4 = 16 8 5*1 = 5 5*2 = 10 5*3 = 15 5*4 = 20 5*5 = 25 9 6*1 = 6 6*2 = 12 6*3 = 18 6*4 = 24 6*5 = 30 6*6 = 3610 7*1 = 7 7*2 = 14 7*3 = 21 7*4 = 28 7*5 = 35 7*6 = 42 7*7 = 4911 8*1 = 8 8*2 = 16 8*3 = 24 8*4 = 32 8*5 = 40 8*6 = 48 8*7 = 56 8*8 = 6412 9*1 = 9 9 9*2 = 18 9*3 = 27 9*4 = 36 9*5 = 45 9*6 = 54 9*7 = 63 9*8 = 72 9*9 = 8113 ''' 14 formula = '{} * {B} = {c} '15 16 s = ''17 for I in range): 18 print (s) 19 s = ''20 for j in range (1, I + 1): 21 s + = formula. format (a = I, B = j, c = I * j) + ''22 # print (formula. format (a = I, B = j, c = I * j ))
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. Pause one second output.
import timefor n in range(1,10):    print(n)    time.sleep(1)

10. Pause the output for one second and format the current time.
import timefor i in range(5):    struct_time = time.localtime(time.time())    t = time.strftime('%Y-%m-%d %H:%M:%S',struct_time)    t = time.strftime('%Y-%m-%d',struct_time)    print(t)    time.sleep(1)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.