In [1]: #欢迎来到“只读课堂”!
In [2]: #下面我们来讲讲循环
In [3]: #循环有:for 循环 - 遍历循环, while 循环 - 条件循环
In [4]: costs = [3,4,12,23,43,100]
In [5]: #如果我要把这个清单整理好,要用“花费。。。花费。。。”打印出来:
In [6]: for cost in costs:
...: print('消费{}元'format(cost))
File "<ipython-input-6-c06e3a352828>", line 2
print('消费{}元'format(cost))
^
SyntaxError: invalid syntax
In [7]: #上面少了个点
In [8]: for cost in costs:
...: print('消费{}元'.format(cost))
...:
消费3元
消费4元
消费12元
消费23元
消费43元
消费100元
In [9]: #它就,可以很完整地展示
In [10]: #可以再规整点儿:
In [11]: for cost in costs:
...: print('消费{}元'.format(str(cost).center(10)))
...:
消费 3 元
消费 4 元
消费 12 元
消费 23 元
消费 43 元
消费 100 元
In [12]: #————————————————————————————————————————————————————————————————
In [13]: #生成一个长度为20地随即列表
In [14]: import random
In [15]: random.randint(1,10)
Out[15]: 8
In [16]: #它就会随即返回一个1-10之间随即地一个数
In [17]: random.randint(1,10)
Out[17]: 10
In [18]: #生成随即一个长度为20地列表:
In [19]: import random
...:
...: random_numbers = []
...: while len(random_numbers) <= 20:
...: random_numbers.append(random.randint(1,10))
...: print(random_numbers)
...:
[8, 10, 2, 3, 4, 5, 2, 3, 5, 1, 2, 9, 8, 3, 8, 1, 10, 2, 3, 4, 10]
In [20]: import random
...:
...: random_numbers = []
...: while len(random_numbers) <= 20:
...: random_numbers.append(random.randint(1,10))
...: print(random_numbers, len(random_numbers))
...:
[5, 9, 8, 8, 5, 5, 5, 1, 10, 3, 3, 1, 1, 6, 3, 5, 6, 9, 2, 2, 8] 21
In [21]: import random
...:
...: random_numbers = []
...: while len(random_numbers) < 20:
...: random_numbers.append(random.randint(1,10))
...: print(random_numbers, len(random_numbers))
...:
[10, 2, 8, 7, 1, 8, 4, 1, 4, 3, 8, 10, 5, 9, 4, 8, 10, 6, 7, 4] 20
In [22]: #如果那里是<=20,就会有个21,所以我们改成了<20 ↑
In [23]: #————————————————————————————————————————————————————————————————
In [24]: #编程建议:只要能使用for循环,就不要使用while循环。
In [25]: #一定要写import random
In [26]: import random
In [27]: #它跟模块是一样的,只要上面已经有导入过这个模块,下面就不用再写。
In [28]: random_numbers = []
In [29]: for i in range(20)
In [30]: File "<ipython-input-29-165a5f730222>", line 1
for i in range(20)
^
SyntaxError: invalid syntax
list(range(20))
File "<ipython-input-30-32922625a043>", line 1
File "<ipython-input-29-165a5f730222>", line 1
^
SyntaxError: invalid syntax
In [31]: #这样就不行,如果打印一行:
In [32]: random_numbers = []
In [33]: for i in range(20):
...: print(i)
...:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
In [34]: #对于之前的例子也是一样的:
In [35]: random_numbers = []
...: for i in range(20)
...: random_numbers.append(random.randint(1,10))
...: print(random_numbers, len(random_numbers))
File "<ipython-input-35-65362b7b9dad>", line 2
for i in range(20)
^
SyntaxError: invalid syntax
In [35]:
In [36]: random_numbers = []
...: for i in range(20):
...: random_numbers.append(random.randint(1,10))
...: print(random_numbers, len(random_numbers))
File "<ipython-input-36-2a5ae58edc18>", line 2
for i in range(20):
^
SyntaxError: invalid character in identifier
In [37]: random_numbers = []
...: for i in range(20):
...: random_numbers.append(random.randint(1,10))
...: print(random_numbers, len(random_numbers))
...:
[1, 1, 6, 3, 2, 10, 4, 10, 5, 1, 7, 5, 4, 6, 7, 9, 2, 8, 2, 10] 20
In [38]: #之前两个打错了啊,不要在意
In [39]: #为什么我要建议能用for,就不要while?
In [40]: #:因为写很多代码的时候,很容易将while循环写成死循环。
In [41]: #但是有些同学会说,我小心一点,不就行了吗?
In [42]: #:“小心一点”就意味着多花时间和精力。
In [43]: #————————————————————————————————————————————————————————————————
In [44]: #什么时候必须用while循环?
In [45]: #:当循环的条件跟数量没有关系时,只能用while循环。
In [46]: #————————————————————————————————————————————————————————————————
In [47]: #举个例子:往空列表中添加随即数,知道添加的数为9,则终止。
In [48]: #直到添加的数为9
In [49]: random_numbers = []
In [50]: while 9 not in random_numbers
File "<ipython-input-50-d6db7c96a19f>", line 1
while 9 not in random_numbers
^
SyntaxError: invalid syntax
In [51]: random_numbers = []
...: while 9 not in random_numbers:
...: random_numbers.append(random.randint(1,10))
...:
In [52]: print(random_numbers, len(random_numbers))
...:
...:
...:
...:
[6, 5, 4, 8, 10, 10, 9] 7
In [53]: #它就是说,本次随即生成第7次才会出现数字9。
In [54]: #————————————————————————————————————————————————————————————————
In [55]: #这次课程我们来点好玩的:留一个课后问题,大家自己去想想:
In [56]: print('\n问题:a = [1,2,3], b = 1, c = (b in a),大家猜测一下,c是一个什么类型,它是不是一个元组呢?\n')
问题:a = [1,2,3], b = 1, c = (b in a),大家猜测一下,c是一个什么类型,它是不是一个元组呢?
In [57]: print("\n本次“只读课堂”的python教程就到这了,欢迎继续收看!\n")
本次“只读课堂”的python教程就到这了,欢迎继续收看!