在Python中,我试图做一个可变的增量值,而它比另一个数字小。我知道有可能以(print(x) for x in range(1, 5))
的形式执行for
循环。我的问题是,有没有类似的方式做while
循环这种形式,如x += 1 while x < y
?Python单行while循环
0
A
回答
1
0
您可以将每个语句,像这样一个分号隔开......
x = 0; y = 5 while(x < y): print(x); x=x+1
1
你可以做这样的事情
n = 0
while n < 1000: rn += n if not (n % 3 and n % 5) else 0
你们看到的是一个conditional expression但它以降低可读性为代价
相关问题
- 1. Python:简单while循环
- 2. Python - While循环
- 3. while循环(Python)
- 4. while循环(Python)
- 5. Python的while循环while循环
- 6. While循环的单行
- 7. python 2.7.3 while循环
- 8. python basic while循环
- 9. Python的while循环
- 10. 简单WHILE循环
- 11. while while while循环while循环
- 12. 简单while循环直到Python中断
- 13. 简单的while循环卡在Python 3.3
- 14. While循环用单引号在Python
- 15. Python 3.4:while循环不循环
- 16. while循环python导致无限循环
- 17. python while循环和for循环
- 18. Python中无限循环while循环
- 19. Python双循环? while和for循环?
- 20. 并行while循环
- 21. while循环运行
- 22. 使用while循环进行迭代 - python
- 23. Python While循环(检查行6)
- 24. 帮助与Python while循环行为
- 25. Python中,While循环运行不正常
- 26. PHP while while循环不进行第二个while循环
- 27. while while循环
- 28. while while循环
- 29. Python while循环不停止?
- 30. 退出while循环在Python
您所描述的是一个生成器表达式,仅适用于 – njzk2
好的。这很好。 – Ranger
我的第一印象是你在问[后测循环](https://en.wikipedia.org/wiki/Do_while_loop)。 – pasztorpisti