Python basic loop structure programming (below)

Python basic loop structure programming (below)

Original  July 14, 2017 19:54:47
  • 196

First, the for statement 1. 
The format of the for statement is generally as follows: 
for loop index value in sequence:  the execution process of the
loop body 
for statement is: each time it loops, it is judged whether the loop index is still in the sequence, if so, take out the value and provide it to Used when the statement inside the loop body; if not, the loop ends. 
2. The for statement is used for sequence type 
lists, tuples, and strings are all sequences. The sequence type has the same access mode: each element of it can be obtained by specifying an offset, and multiple elements can be obtained by slicing.

   例:创建一个由分数构成的列表,求出所有分数的平均分。
   方法一:使用python中内置函数sum()求和,然后再求平均分。
  • 1
  • 2
  • 3
>>>score=[70,90,78,85,97,94,65,80]
>>>score
[70,90,78,85,97,94,65,80]
>>>aver=sum(score)/8.0
>>>aver
82.375
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
方法二:使用for循环计算列表元素之和。
  • 1
  • 2
score=[70,90,78,85,97,94,65,80]
sum=0
print '所有分数的值为: '
for i in range(len(score)):
    print score[i] ,
    sum+=socre[i]
aver=sum/8.0
print 'aver= ', aver
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
运行结果:
所有分数的值为:
70 90 78 85 97 94 65 80 
aver=82.375
  • 1
  • 2
  • 3
  • 4
  • 5

How to use the range() function 
(1) The complete syntax of the range() function requires 2 or 3 integer parameters: range(start,end,step) 
The range() function will produce a series containing all arithmetic differences A list of k, the range of k is start<=k<=end, k increments step each time, step cannot be zero, otherwise an error will occur. Defaults to 1. 
(2) The range() function also has two simple syntax formats: range(end), range(start,end) 
When a parameter is provided, start defaults to zero, step defaults to 1, and range() returns from 0 to end sequence. 
When 2 arguments are provided, step defaults to 1, and range() returns a sequence from start to end.

3. The for statement is used with counting loops 
(1) single-layer loop 
(2) double-layer loop nested 
loops The nesting of loops refers to a loop that completely includes another complete loop, that is, the loop body also includes a loop statement. while loops and for loops can be nested within each other.

Example: Ninety-nine multiplication table

for i in range(1,10,1):
    for j in range(1,i+1,1):
        print j, "*", i, "=",i*j, '\t',     #最后一个,表示输出之后不换行
    print '\n'
  • 1
  • 2
  • 3
  • 4
程序运行结果:
1*1=1
1*2=2  2*2=4
1*3=3  2*3=6   3*3=9
1*4=4  2*4=8   3*4=12   4*4=16
1*5=5  2*5=10  3*5=15   4*5=20   5*5=25
1*6=6  2*6=12  3*6=18   4*6=24   5*6=30   6*6=36
1*7=7  2*7=14  3*7=21   4*7=28   5*7=35   6*7=42   7*7=49
1*8=8  2*8=16  3*8=24   4*8=32   5*8=40   6*8=48   7*8=56    8*8=64
1*9=9  2*9=18  3*9=27   4*9=36   5*9=45   6*9=54   7*9=63    8*9=72    9*9=81
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Second, the break statement 
Python provides a statement to end the loop early – the break statement. This statement can be used when a condition is triggered during the loop and the loop needs to be stopped immediately. The break statement can be used in while and for loops.

3. The continue statement 
The continue statement provided in python is similar to the traditional continue statement in other high-level languages. It can be used in the while statement and the for statement. Its function is: when the continue statement is encountered, the program will terminate the current loop, And ignore the statement after continue, then go back to the top of the loop and continue with the next loop.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325645151&siteId=291194637