python中的print()

python中的print函数有个end参数特别有用:

1 for i in range(1, 10):
2     for j in range(1, i + 1):
3         print(f'{j} * {i} = { i * j}')
4     print()

结果:

 1 1 * 1 = 1
 2 
 3 1 * 2 = 2
 4 2 * 2 = 4
 5 
 6 1 * 3 = 3
 7 2 * 3 = 6
 8 3 * 3 = 9
 9 
10 1 * 4 = 4
11 2 * 4 = 8
12 3 * 4 = 12
13 4 * 4 = 16
14 
15 1 * 5 = 5
16 2 * 5 = 10
17 3 * 5 = 15
18 4 * 5 = 20
19 5 * 5 = 25
20 
21 1 * 6 = 6
22 2 * 6 = 12
23 3 * 6 = 18
24 4 * 6 = 24
25 5 * 6 = 30
26 6 * 6 = 36
27 
28 1 * 7 = 7
29 2 * 7 = 14
30 3 * 7 = 21
31 4 * 7 = 28
32 5 * 7 = 35
33 6 * 7 = 42
34 7 * 7 = 49
35 
36 1 * 8 = 8
37 2 * 8 = 16
38 3 * 8 = 24
39 4 * 8 = 32
40 5 * 8 = 40
41 6 * 8 = 48
42 7 * 8 = 56
43 8 * 8 = 64
44 
45 1 * 9 = 9
46 2 * 9 = 18
47 3 * 9 = 27
48 4 * 9 = 36
49 5 * 9 = 45
50 6 * 9 = 54
51 7 * 9 = 63
52 8 * 9 = 72
53 9 * 9 = 81

下面代码:

1 for i in range(1, 10):
2     for j in range(1, i + 1):
3         print(f'{j} * {i} = { i * j}', end='\t')
4     print()

结果为:

1 1 * 1 = 1    
2 1 * 2 = 2    2 * 2 = 4    
3 1 * 3 = 3    2 * 3 = 6    3 * 3 = 9    
4 1 * 4 = 4    2 * 4 = 8    3 * 4 = 12    4 * 4 = 16    
5 1 * 5 = 5    2 * 5 = 10    3 * 5 = 15    4 * 5 = 20    5 * 5 = 25    
6 1 * 6 = 6    2 * 6 = 12    3 * 6 = 18    4 * 6 = 24    5 * 6 = 30    6 * 6 = 36    
7 1 * 7 = 7    2 * 7 = 14    3 * 7 = 21    4 * 7 = 28    5 * 7 = 35    6 * 7 = 42    7 * 7 = 49    
8 1 * 8 = 8    2 * 8 = 16    3 * 8 = 24    4 * 8 = 32    5 * 8 = 40    6 * 8 = 48    7 * 8 = 56    8 * 8 = 64    
9 1 * 9 = 9    2 * 9 = 18    3 * 9 = 27    4 * 9 = 36    5 * 9 = 45    6 * 9 = 54    7 * 9 = 63    8 * 9 = 72    9 * 9 = 81

看看哪种更合适吧

posted @ 2023-01-02 21:48  叕叒双又  阅读(173)  评论(0编辑  收藏  举报