當前位置:生活全書館 >

學習教育

> 詳解python的循環

詳解python的循環

<link rel="stylesheet" href="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4f61d3594de/a4ea273683c8.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4f61d3594de/a4ea303194c0eaf695827b59325e.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><style>pre{overflow-x: auto}</style>

詳解python的循環

range函數的使用

作爲循環遍歷的對象

詳解python的循環 第2張

第一種創建方式

r=range(10)print(r)#range(0,10)print(list(r))

默認從零開始,默認步長爲1

range(0, 10)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]第二種創建方式

指定了初始值1,到10結束,不包含10,默認步長爲1

'''第二種創建方式,給了兩個參數(小括號中給了兩個數)'''r=range(1,10)print(list(r))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

第三種創建方式

最後一位數爲步長

r=range(1,10,2)print(list(r))
[1, 3, 5, 7, 9]

判斷指定的數有沒有在當前序列中

r=range(1,10,2)print(10 in r)
False

循環結構

詳解python的循環 第3張

總結

本篇文章就到這裏了,希望能夠給你帶來幫助,也希望您能夠多多關注好二三四的更多內容!

標籤: Python
  • 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zh-hant/xuexijiaoyu/kzpyn6.html