python3 循环语句 for语句和while语句

  • range 函数.是python中创建范围的内置函数。
    范围类似于切片。它们包含起始位置(这里为0),但不包含结束位置(这里为10)。还可以指定范围,如range(1,5)
>>> list(range(5))
[0, 1, 2, 3, 4]

>>> list(range(1,5))
[1, 2, 3, 4]
  • for循环 可用于条件为真时,反复执行代码。
for i in range(5):   #for语句中,range(10)可以代表循环10次
 print("hello")        #也可以表示从0到9
for i in range(5):
 print("hello", i)
➜  pythoncode python3 holly_03_17.py
hello
hello
hello
hello
hello
hello 0
hello 1
hello 2
hello 3
hello 4
numbers = [1,2,3,4,5]
for number in numbers:
	print(number)
➜  pythoncode python3 holly_03_17.py
1
2
3
4
5
  • while循环
x = 1
while x <=5:
	print(x)
	x += 1   # X += 1等价于 X=X+1
  pythoncode python3 holly_03_17.py
1
2
3
4
5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值