当前位置:网站首页>Loop structure of program (for loop)
Loop structure of program (for loop)
2022-07-06 01:13:00 【Leifeng】
Traversal cycle :for loop
Traversal cycle refers to the cycle operation mode formed by traversing a certain structure :
for < Loop variable > in < Traversal structure >:
< Sentence block >
Every time I cycle , Extract one of the elements from the traversal structure , Put it in the previous loop variable , Execute the following statement block , After performing , Then extract the next element from the traversal structure , When all elements in the traversal structure are put into the loop variable , And all have been executed circularly , So the program exits .
Extract elements one by one from the traversal structure , Put it in a loop variable , Then execute the following statement block
By reserved words for and in form , Complete traversal of all elements ends
Each cycle , The resulting element is placed in a loop variable , And execute the statement block once
The traversal structure used is a structure that can contain multiple elements , The so-called traversal structure is not a special type , It contains many types .
Count cycle (N Time ):
for i in range(N):
< Sentence block >
His execution effect is to traverse the following statement block , perform N Content of secondary statement block .
Traversal by range() Function produces a sequence of numbers , Create a cycle .
for Sentences can be compared with range() Function collocation ,range() Function can generate an iteratable object composed of integers ( It can be simply understood as supporting the use of for Loop traversed objects ),range(N) The function generates a sequence of numbers , This sequence contains N Elements , Each element is an integer , Namely 0,1 Until N-1
Count cycle N Time , We can use this variable in the middle i, It is also possible not to use variables i, about for in range It's a cycle N Time , As for not using intermediate loop variables, it is decided according to the logic of the program .
The code example is as follows :
for i in range(5):
print(i)
The operation interface is as follows :

Count cycle ( Specific times ):
The format is as follows :
for i in range(M,N,K):
from M Start , Less than N, To N Previous integer , With K Step length .
The code of example 1 is as follows :
for i in range(1,6):# from 1 Start , To 6 Previous integer 5
print(i)The operation interface is as follows :

The code of example 2 is as follows :
for i in range(1,6,2):# from 1 Start , To 6 Previous integer 5, With 2 Step length .
print(i)The operation interface is as follows :

String traversal loop :
for c in s:
< Sentence block >
s Is string , Traverses each character of the string , Create a cycle .
c Represents each character in the string , It can take each character out of the string in order and put it in c in , And for each extracted character , Execute the statement block once , And create a cycle .
The code example is as follows :
for c in "Python123":
print(c,end=",")The operation interface is as follows :

List traversal loop :
for item in ls:
< Sentence block >
ls It's a list , Traverse every element of it , Create a cycle
The code example is as follows :
for item in [123,"PY",456]:
print(item,end=",")The operation interface is as follows :
Tuple traversal loop :
for i in ( Elements ):
print(i)
The code is as follows :
for i in ('python',1,5,6):
print(i)
The operation interface is as follows :

Dictionary traversal loop :
Output all elements in the following format :
dic={' ' : ,' ': }
for item in dic.items():
print(item)
The code is as follows :
dic={'python':1,'age':27,5:6}
for item in dic.items():
print(item)
The operation interface is as follows :

Output all keys in the following format :
dic={' ' : ,' ': }
for item in dic.keys():
print(item)
The code is as follows :
dic = {'python': 1, 'age': 27, 5: 6}
for item in dic.keys():
print(item)The operation interface is as follows :

Output the values corresponding to all keys :
dic={' ' : ,' ': }
for item in dic.values():
print(item)
The code is as follows :
dic = {'python': 1, 'age': 27, 5: 6}
for item in dic.values():
print(item)The operation interface is as follows :

The traversal loop uses reserved words for in A cycle formed , It can assign the current loop variable to each element in the traversal structure , And form a cycle .
all for in Such a traversable structure , As long as the following variables or this data type , It is a data type composed of multiple elements , You can use for in The way , Traverse every element of it , And according to the number of elements , Form a corresponding cycle .
边栏推荐
- 几百行代码实现一个 JSON 解析器
- 直播系统代码,自定义软键盘样式:字母、数字、标点三种切换
- Mysql--- query the top 5 students
- VMware Tools installation error: unable to automatically install vsock driver
- 黄金价格走势k线图如何看?
- Construction plan of Zhuhai food physical and chemical testing laboratory
- Opinions on softmax function
- Vulhub vulnerability recurrence 75_ XStream
- What is the most suitable book for programmers to engage in open source?
- vSphere实现虚拟机迁移
猜你喜欢
![[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)](/img/52/021931181ad3f4bef271b4e98105c2.jpg)
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)

VMware Tools安装报错:无法自动安装VSock驱动程序

Who knows how to modify the data type accuracy of the columns in the database table of Damon

After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?

Keepalive component cache does not take effect

The population logic of the request to read product data on the sap Spartacus home page
![[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)](/img/dd/bffe27b04d830d70f30df95a82b3d2.jpg)
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)

Some features of ECMAScript

ADS-NPU芯片架构设计的五大挑战

Kotlin basics 1
随机推荐
The basic usage of JMeter BeanShell. The following syntax can only be used in BeanShell
Getting started with devkit
Three methods of script about login and cookies
Is chaozhaojin safe? Will it lose its principal
Recursive method to realize the insertion operation in binary search tree
在产业互联网时代,将会凭借大的产业范畴,实现足够多的发展
Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization
视频直播源码,实现本地存储搜索历史记录
Zhuhai's waste gas treatment scheme was exposed
1791. Find the central node of the star diagram / 1790 Can two strings be equal by performing string exchange only once
Cglib dynamic agent -- example / principle
FFT learning notes (I think it is detailed)
The inconsistency between the versions of dynamic library and static library will lead to bugs
I'm interested in watching Tiktok live beyond concert
[groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
Dynamic programming -- linear DP
servlet(1)
Daily practice - February 13, 2022
JMeter BeanShell的基本用法 一下语法只能在beanshell中使用
View class diagram in idea
