Iteration and loop in python

Source: Internet
Author: User
A common loop structure is a for statement used to iterate multiple items. a loop is one of the most important functions of python. The most common iteration is to loop a sequence (string, list, or tuples) the common loop structure of all members is a for statement used to iterate multiple items. loop is one of the most important functions of python. The most common iteration is to loop a sequence (string, list, or tuples ).
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: 14number:8 value: 16number:9 value: 18

Explanation: it is very common to assign the value in the list to variable n in each iteration to execute a loop within the integer range. Therefore, a shortcut is provided:

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

The range (I, j) function creates the object range I to the J-1 if the start is omitted, the start value is considered as 0. The third parameter is an optional step value:

a = range(10);  b = range(1,6);  c = range(0,10,2)

Output result

, # Step values

If you are using a version earlier than python3.0, you can try to use the xrange () method python3.0 and rename it range ()
The range method is also used in PHP and similar to that in python.
2 iteration string

A = "Hello World" for c in a: print (c); # print all characters in the string

3. iteration tuples

Name = );

Output result:

Little tornado Chai Jin Mei Yu Gong Zhu Zhe Walker Wu Song Mixing Jiang Long Li Jun

4. iterative Dictionary

Data = {'name': 'Zhang San', 'age': 18, 'add': 'Beijing', 'price': 1800}

Output result:

Name zhang san age 18 price 1800 addr Beijing

5. iterate all lines of the file

F = open ('E:/work.txt '); for line in f: print (line); # loop all rows in the output file

Output result:

'tom',120,132'jon',234,255'jeck',123,678

Loop iteration in php:
1. for loop

for($i=0;$i<=10;$i++){   echo $i . ',';  }


Explanation: $ I = 0 indicates the start value of a loop. $ I <= 10 indicates that a value before the start of each loop is obtained. if it is true, a false value is returned, and the value is stopped. it can be understood as the range of a loop; $ ++ add 1 after each loop, which can be understood as the number of cycles + 1
Output result:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
It can also be used for Loop strings:

$ Str = 'Hello World'; for ($ I = 0; $ I
 
  

Explanation: strlen is the method used in php to obtain the string length.
Output result:
H, e, l, l, o, w, o, r, l, d
2. foreach loop: php does not contain tuples, lists, and dictionaries, but has an array. The foreach statement is used to traverse arrays cyclically.
1. index array: The number index automatically assigned or manually added

$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",'two','three');    foreach($arr as $k=>$v){     echo 'key:'.$k .'value:'.$v.'
'; }

Output result:

key:0value:onekey:1value:twokey:2value:three

2 join array: use a custom key

$ Arr = array ('name' => 'Zhang San', 'age' => 18, 'add' => 'Beijing '); foreach ($ arr as $ k =>v v) {echo $ k. '= '. $ v. ',';}

Output result:
Name = James, age = 18, addr = Beijing
Summary:
1. this section describes the most common method of iteration in python: for... loop
2, for... in iteration is used to print data in the list, tuples, dictionaries, and text cyclically.
3. compare it with the for loop statements and foreach loop statements in PHP.

The above is the content of iterations and loops in python. For more information, see PHP Chinese network (www.php1.cn )!

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.