python中的TCL-like-for循环

2024-05-16 06:19:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中查找了类似于其他语言Perl/TCL/C++的循环^ {CD1>}。你知道吗

下面的TCL就是for循环

for { set i 0 } { $i < $n } { incr i} {

#I like to increment i value manually too like
if certain condition is true then increment
set i = [expr i+1] # in some cases
print i

我在python中也有类似的方法。在python中,我知道下面是for循环语法

for i in var1
#How to increment var1 index manually in some scenarios

Tags: toin语言forvaluesometclperl
1条回答
网友
1楼 · 发布于 2024-05-16 06:19:42

用途:

for i in range(0, n):
 # Do something

此外,您还可以使用:

i = 0
while i<n:
 # Do something
 i+=1
网友
2楼 · 发布于 2024-05-16 06:19:42

Python的range函数被称为range(b, e, i),它将产生从b开始到e结束的整数,每次递增i。你知道吗

网友
3楼 · 发布于 2024-05-16 06:19:42

Python没有那种类型的for循环,所以使用while循环。你知道吗

i = 0
while i < n:
    # ...
    if some_condition: # Extra increment
        i += 1
    i += 1 # normal increment

相关问题 更多 >