如何用PYTHON里LIST和FOR LOOPS

 时间:2026-02-14 11:19:52

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

  • 如何运用PYTHON的嵌套FOR LOOPS循环
  • 如何复制PYTHON里的列表list
  • 如何用PYTHON里LIST中sort()方法
  • python里lambda的使用
  • python里面re.sub()函数的使用方法
  • 热门搜索
    始作俑者的意思 隅是什么意思 买什么手机 老人过生日送什么好 索取的意思 乌梢蛇的功效与作用 date是什么意思翻译 u盘买什么牌子的好 为虎作伥的意思 商砼是什么意思