如何用PYTHON里LIST和FOR LOOPS

2025-11-05 07:31:55

1、打开JUPYTER NOTEBOOK,新建一个空白的PY文档。

如何用PYTHON里LIST和FOR LOOPS

2、fruit = ["apple", "peach", "orange", "banana"]

for special_fruit in fruit:

    print(special_fruit)

新建一个列表,用FOR LOOPS简单地打印出来。

如何用PYTHON里LIST和FOR LOOPS

3、fruit = ["apple", "peach", "orange", "banana", "pear"]

for special_fruit in fruit:

    if special_fruit == "orange":

        print("Eat it.")

        break

    print(special_fruit)

如果我们只为了找到其中某一数据,那么可以用BREAK来停止继续的操作。

如何用PYTHON里LIST和FOR LOOPS

4、fruit = ["apple", "peach", "orange", "banana", "pear"]

for special_fruit in fruit:

    if special_fruit == "orange":

        print("Eat it.")

        continue

    print(special_fruit)

但是有时候找到数据我们需要继续运行,那么就可以用CONTINUE。

如何用PYTHON里LIST和FOR LOOPS

5、fruit = ["apple", "peach", "orange", "banana", "pear"]

for special_fruit in fruit:

    for index in "12345":

        print(index, special_fruit)

如果要在列表关键词前加数字也是可以的。

如何用PYTHON里LIST和FOR LOOPS

6、fruit = ["apple", "peach", "orange", "banana", "pear"]

do_it = ["eat the ", "cook the ", "slice the ", "water the ", "take the "]

for special_fruit in fruit:

    for dododo in do_it:

        print(dododo, special_fruit)

同时用上两组列表,这样就可以打印出来合并的句子。

如何用PYTHON里LIST和FOR LOOPS

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢