如何将Python的for循环转换为while循环?

How to convert a Python for loop to while loop?

以下是一个简单的for循环,遍历一个范围

1
2
for x in range(5):
  print (x)

要转换为while循环,我们在循环开始之前将计数变量初始化为0,并在每次迭代中将其递增1(只要小于5)

1
2
3
4
x=0
while x<5:
  x=x+1
  print (x)