网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

如何用PYTHON里LIST和FOR LOOPS

时间:2024-10-14 22:25:41

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
© 2025 小知经验
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com