Basic use Python cycle (for in, while)

Python loop in two ways:

One is for ... in loop: mainly used to traverse the tuple, list;

One is the while loop: as long as the conditions are met, the continuous cycle, the loop exits when the condition is not satisfied.

# ! / Usr / bin / Python 
# - * - Coding: utf-8 - * - 
    # when there is a Chinese program, notes that the use of utf-8 encoding explain 

    # for ... in loop, followed by the list or tuple of each element in the iteration out 
list = [ 'lingyunmu ' , 25, 'Computer ' ]
 for K in list:
     Print (K)
     # traverse the list del (list)
 Print ( ' * ' * 40 ) 
SUM = 0
 for X in [. 1 , 2,3,4,5,6,7,8 ]: 
    SUM = SUM + X
 Print (SUM)


del (SUM)
 Print ( ' * ' * 40 ) 

SUM1 = 0 
Y = List (Range (999 ))
     #range method, is from 0 to generate a start sequence 999 of less than 
# Print (Y) 
for X in Y: 
    SUM1 SUM1 + = X
 Print (SUM1)
 del (SUM1)
 del (Y)
 Print ( ' * ' * 40 ) 

SUM = 0 
n- = 99
 the while n-> 0: 
    SUM = SUM +the n- 
    the n- = the n-- 2
 Print (SUM)
     # the while as long as the conditions are met, continuous cycle, the loop exits when the condition is not satisfied.

Reading and fitness there is always a way

Guess you like

Origin www.cnblogs.com/Renqy/p/11584012.html