Iterations and loops in Python

Source: Internet
Author: User

Common Loop Structure is a for statement that iterates over multiple items , loops are one of the most important features of Python, and the most common iteration is simply looping through all 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:2
Number:2 Value:4
Number:3 Value:6
Number:4 Value:8
Number:5 value:10
Number:6 Value:12
Number:7 value:14
Number:8 value:16
Number:9 value:18

explain : It is common for each iteration to assign a value in the list to a variable n to perform a loop within an integer range, which results in a Quick Method:

For n in range (1,10): Print ("number:%d value:%d"% (n,2*n));

Output Result:

Number:1 Value:2
Number:2 Value:4
Number:3 Value:6
Number:4 Value:8
Number:5 value:10
Number:6 Value:12
Number:7 value:14
Number:8 value:16
Number: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,9

1,2,3,4,5

0,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 firewood in ', ' Meiran 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 18
Price 1800
Addr Beijing

5, iterate over all the lines of the file

f = open (' E:/work.txt '); #循环输出文件中所有行

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 the value of each loop before the start of the cycle, if True then continue to be false then stop, can be understood as the scope 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 loop: There is no notion 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", ' three '), foreach ($arr as $k = + $v) {echo ' key: '. $k. ' Value: '. $v. ' <br/> ';}

Output Result:
Key:0value:one
Key:1value:two
Key: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 ... loop

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

This article is from the "Hong Dachun Technical column" blog, please be sure to keep this source http://hongdachun.blog.51cto.com/9586598/1762476

Iterations and loops in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.