python-01
For loop
- Print 4 rows 4 columns rectangle
1 | l = 4 |
Print traiangle
1
2
3
4row = int(input('Type a number'))
for i in range(1, row+1):
print((row - i) * " ", end="") # 打印空格
print(i * "* ") # 打印星号
Print upside-down triangle
1
2
3
4l = 4
for i in range(1, l + 1):
print((i - 1) * " ", end="") # 打印空格
print((4 - i + 1) * "* ") # 打印星号Diamond
1 | row = int(input('Type a number')) |
While loop
- Number guessing
1 | import random |
正序乘法表
1
2
3
4
5
6
7
8i = 1
while i <= 9:
j = 1
while j <= i:
print('%d*%d = %2d\t' % (i, j, i * j), end='')
j += 1
i += 1
print()逆序乘法表
1
2
3
4
5
6
7
8i = 9
while i >= 1:
j = 1
while j <= i:
print('%d*%d = %2d\t'%(j, i, i * j), end='')
j += 1
i -= 1
print()triangle
1
2
3
4
5
6
7
8
9
10
11
12row = 1
while row <= 5:
j = 1
while j <= 5 - row:
print(' ', end='')
j += 1
k = 1
while k <= 2 * row - 1:
print('*', end='')
k += 1
print()
row += 1
- pramid
1 | ''' |
- Rectangular Spotted Graph
1 | i = 1 |
Function
- Pass info from
1 | def greet_user(username): |