Python中For循环

Posted 四叶草134

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python中For循环相关的知识,希望对你有一定的参考价值。

1.

for i in range(10):
    print(i)

输出结果

F:pypyProjectvenvScriptspython.exe F:/py/pyProject/venv/while.py
0
1
2
3
4
5
6
7
8
9

Process finished with exit code 0

2.

age_of_oldboy = 56

for i in range(3):
    guess_age =int(input ("guess age:"))
    if guess_age == age_of_oldboy:
        print ("you got it")
        break
    elif guess_age > age_of_oldboy:
        print("think bigger!!!")
    else:
        print("think smaller!!")

else:
     print("you have tried many times...")

输出结果

F:pypyProjectvenvScriptspython.exe F:/py/pyProject/venv/while.py
guess age:23
think smaller!!
guess age:45
think smaller!!
guess age:78
think bigger!!!
you have tried many times...

Process finished with exit code 0

3 for循环隔1个打印出1个,

for i in range(0,10,2):
    print(i)

输出结果为

F:pypyProjectvenvScriptspython.exe F:/py/pyProject/venv/while.py
0
2
4
6
8

Process finished with exit code 0

以上是关于Python中For循环的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 这个简单的代码片段显示了如何使用有符号整数在C中完成插值。 for()循环确定要插入的范围

在 Activity 内部,如何暂停 for 循环以调用片段,然后在按钮单击片段后恢复循环以重新开始

如何在Django视图中使用for循环返回每次迭代[关闭]

如何使用引导程序和 for 循环在 django 中创建电影片段?

for循环结构

codeblock代码片段