Python pycharm learning 循环语句的使用,列表数据录入
文章编号:Python学【2020】001号
日期:2020-02-10
NO.1
for x in range(4):
for y in range(3):
print(f"({x},{y})")
print out
(0,0)
(0,1)
(0,2)
(1,0)
(1,1)
(1,2)
(2,0)
(2,1)
(2,2)
(3,0)
(3,1)
(3,2)*
No.2
numbers = [5,2,5,2,2]
for x_count in numbers:
print('*'* x_count)
No.3
numbers = [5,2,5,2,2]
for x_count in numbers:
output = ''
for count in range(x_count):
output += 'x'
print(output)
print out
xxxxx
xx
xxxxx
xx
xx
No.4
names = ['Jhon','Bob','Mosh','Sarah','Mary']
print(names[0])
print out
Jhon
No.5
```python
names = ['Jhon','Bob','Mosh','Sarah','Mary']
#注释:start index from 2 to the end
print(names[2:])
print out
[‘Mosh’, ‘Sarah’, ‘Mary’]
names = ['Jhon','Bob','Mosh','Sarah','Mary']
# redefine arrays data that index is 0
names[0] = 'Jho'
print(names)
print out
[‘Jho’, ‘Bob’, ‘Mosh’, ‘Sarah’, ‘Mary’]
No.6 Write a program to find the largest number in a list Solution
numbers = [3,6,2,9,4,10]
max =numbers[0]
for number in numbers:
if number > max:
max = number
print(max)
print out
10
No.7
#2D list(two dimentional list)(square bracket)
…
note:
matrix = [
[1,2,3]…1st row
[4,5,6)…2nd row
]
matrix[0][1],0row,1column
…
No.1
matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
print(matrix[0][1])
#print out
2
No.2
matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
#append data
matrix[0][1]=20
print(matrix)
#print out
[1,20,3],
[4,5,6],
[7,8,9]
No.3
matrix = [
[1,2,3],
[4,5,6],
[7,8,9]
]
for row in matrix:
for item in row:
print(item)
#print out
1
2
3
4
5
6
7
8
9