A common loop structure is a for statement that iterates over multiple items, and the loop is one of the most important features of Python, and the most common iteration is simply to loop through all the members of a sequence (string, list, or tuple)
1, Iteration list:
For n in [1,2,3,4,5,6,7,8,9]: print ("number:%d value:%d"% (n,2*n))
Output Result:
Number:1 value:2number:2 value:4number:3 value:6number:4 value:8number:5 value:10number:6 value:12number:7 value:14 Number:8 Value:16number:9 value:18
Explanation: It is common for each iteration to assign a value from a list to a variable n to perform loops in an integer range, resulting in a quick method:
For n in range (1,10): print ("number:%d value:%d"% (n,2*n));
Output Result:
Number:1 value:2number:2 value:4number:3 value:6number:4 value:8number:5 value:10number:6 value:12number:7 value : 14number:8 value:16number:9 value:18
Range of objects created by the range (I,J) function I to j-1 if the start is omitted, the starting value is 0 The third argument is an optional stepping value:
A = range (ten); b = Range (1,6); c = Range (0,10,2)
Output results
0,1,2,3,4,5,6,7,8,91,2,3,4,50,2,4,6,8 #步进值
If you are using python3.0 the following version can try to use the Xrange () method python3.0 after renaming to range ()
The Range method is also used in PHP and similar in python
2 Iteration String
A = "Hello World" for C in a: print (c); #打印出字符串中的所有字符
3, iterating tuples
Name = (' small Cyclone Chai Jin ', ' Meiran gong Zhu Yu ', ' Walker Wu Song ', ' River Dragon Li June '); For NM in name: print (NM);
Output Result:
Small cyclone wood into Meiran male Zhu Yu Walker Wu Song River Dragon Li June
4, Iteration Dictionary
data = { ' name ': ' Zhang San ', ' age ': ' addr ': ' Beijing ', ' Price ': 1800 }
Output Result:
Name Zhang San age 18price 1800addr Beijing
5, iterate over all the lines of the file
f = open (' E:/work.txt '); For line in F: print (line); #循环输出文件中所有行
Output Result:
' Tom ', 120,132 ' Jon ', 234,255 ' Jeck ', 123,678
Loop iterations in PHP:
1,for Cycle
for ($i =0; $i <=10; $i + +) { echo $i. ','; }
Explanation: $i = 0 Loop start value, $i <=10 before each cycle starts, if True then stop for false, can be understood as the range of the loop, $++ after each loop to execute plus 1, can be understood as the number of cycles +1
Output Result:
0,1,2,3,4,5,6,7,8,9,10
can also be used for looping strings:
$str = ' Hello world '; for ($i =0; $i <strlen ($STR); $i + +) { echo $str [$i]. ', '; }
Explanation: Strlen is the method used in the string method in PHP to get the length of a string
Output Result:
H,e,l,l,o,, W,o,r,l,d
2,foreach loops: There is no concept of tuples and lists and dictionaries in PHP, but the notion of arrays is that the foreach statement is used to iterate through the array
1, indexed array: System auto-assigned or manually added digital index
$arr = Array (1,2,3,4,5,6,7); $value = 0; foreach ($arr as $v) { $value + = $v; }
Echo $value;
Output Result:
28
$arr = Array ("One", ' one ', ' three '); foreach ($arr as $k = + $v) { echo ' key: '. $k. ' Value: '. $v. ' <br/> '; }
Output Result:
Key:0value:onekey:1value:twokey:2value:three
2 Associative arrays: Using a custom key
$arr = Array (' name ' = ' Zhang San ', ' age ' =>18, ' addr ' = ' Beijing '); foreach ($arr as $k = + $v) { echo $k. ' = '. $v. ', '; }
Output Result:
Name= Zhang San, age=18,addr= Beijing
Summarize:
1, this section describes the most common method of iteration in Python is for...in ... Cycle
2,for...in iterations are used to loop the data in a printed list, tuple, dictionary, text
3, compared to the FOR Loop statement in PHP and the Foreach Loop statement
The above is the iteration in Python and the content of the loop, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!