Python practice example

Posted by ben2468 on Wed, 04 Dec 2019 02:09:53 +0100

7. Copy data from one list to another.

Program analysis: use list [:].

#python3.7

#For simple lists (i.e. all basic elements in the list)
a1 = [1,2]
b1 = a1[:]
print(b1)

#It is not suitable for a list containing a list. If the list to be copied contains a list, only references to that list will be copied
a2 = [1,[2,3]]
b2 = a2[:]
a2[1].append(4)
print(a2)
print(b2)
#here b2 It's just copied a2 in[2,3]Quotation 

//Result: 
[1, 2] 
[1, [2, 3, 4]] 
[1, [2, 3, 4]]

 

8. Output 9 * 9 multiplication table.

Program analysis: considering the branch and column, 9 lines and 9 columns in total, i control line and j control column.

#python3.7
#Bottom left triangle format output 99 multiplication table
for i in range(1,10):
    for j in range(1,i+1):
        print('%d*%d=%d' % (i,j,i*j),end=' ')
    print(' ')

//Result:
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*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81

 

9. Pause output for one second.

Program analysis: use the sleep() function of the time module.

import time

myD = {1:'a', 2:'b'}
for key, value in dict.items(myD):
    print(key, value)
    time.sleep(1)
#Pause for one second before output

 

10. Pause the output for one second and format the current time.

#python3.7

import time

#Output current time
print(time.strftime('%Y-%M-%D %H:%M:%S', time.localtime(time.time())))

time.sleep(1)

#Output next second time
print(time.strftime('%Y-%M-%D %H:%M:%S', time.localtime(time.time())))

//Result:
2019-45-01/24/19 13:45:40
2019-45-01/24/19 13:45:41

 

11. Classical question: there is a pair of rabbits. From the third month after birth, a pair of rabbits will be born each month. From the third month after birth, a pair of rabbits will be born each month. If the rabbits do not die, what is the total number of rabbits each month?

Program analysis: the law of rabbit logarithm is sequence 1,1,2,3,5,8,13,21

#python3.7

f1 = 1
f2 = 2
for i in range(1,22):
    print('%121d %121d' % (f1, f2))
    if (i % 3) == 0:
        print('')
    f1 = f1 + f2
    f2 = f1 + f2

 

12. Judge how many prime numbers are between 101-200, and output all prime numbers.

Program analysis: the method of judging prime number: use a number to remove 2 to sqrt (this number). If it can be divided by an integer, it indicates that this number is not a prime number, otherwise it is a prime number.

#python3.7

h = 0
leap = 1
from math import sqrt
from sys import stdout
for m in range(101, 201):
    k = int(sqrt(m + 1))
    for i in range(2, k + 1):
        if m % i == 0:
            leap = 0
            break
    if leap == 1:
        print('%-4d' % m)
        h += 1
        if h % 100 == 0:
            print('')
    leap = 1
print('The total is %d' % h)

//Result:
101 
103 
107 
109 
113 
127 
131 
137 
139 
149 
151 
157 
163 
167 
173 
179 
181 
191 
193 
197 
199 
The total is 21

 

 

 

reference material:

1. 100 Python examples: http://www.runoob.com/python/python-100-examples.html

2. Copy data from one list to another. : https://blog.csdn.net/weixin_38889645/article/details/76718640

Topics: Python