Python's control structure __python

Source: Internet
Author: User
Tags iterable

Conditional statement:

If Judge condition :

Execute statement ... (Execution when the condition is true)

else:

Execute statement ... (executes when the condition is false.) Else optional)

Note: Any non-null and non-0 values in Python are true. Null (NULL) or zero is false.

Multi-branch Conditional statement:

If Judge condition 1:

Execute statement ...

Elif Judgment Condition 2:

Execute Statement 2 ...

Elif judgment Condition 3:

Execute Statement 3 ...

else:

Execute Statement 4 ...

If more than one condition is required in the judgment statement, you can use or (or) two conditions, one of which is set up to determine the condition. You can use the and (with) two conditions to set up a condition of judgment.

--------------------------------------------------------------------------------------------------------------- -------------------

--------------------------------------------------------------------------------------------------------------- -------------------

Loop statement:

for loop :

For variable in sequence:

statements (s)

For loops are typically used to iterate over objects, and for loops, the for loop works as long as it acts on an iterative object, judging whether an object is an iterative object, judging by the type of iterable of the collections module:

#-*-coding:cp936-*-
from collections import iterable
bloon1=isinstance (' abc ', iterable)  # Can the string ABC iterate C8/>bloon2=isinstance (1234,iterable)  # Integer 1234 can iterate the
print bloon1, "\ n", Bloon2

>>>
True
False

Continue and break:

Continue used to skip this loop: Skip the remaining statements in the current loop, and proceed to the next round of loops

The break is used to jump out of the loop.

EG1:

  for a  in  ' hello ':
      print  a
print "------------------------"
c=[' S ', ' G ', 3,6]
for  b  in  C:
      print  b
with  index  in  range (len (c))
      print  c[ Index

The # len () function returns the length of the list, which is the number of elements. The range () function returns the number of a sequence (a series is generated).

Range (5)--> 0,1,2,3,4

Range (2,10)--> 2,3,4,5,6,7,8,9

Range (2,16,2)--> 2,4,6,8,10,12,14

For...else:

The For statement is nothing different, the Else statement is executed (not terminated by a break) when the whole loop is done properly

eg

For  N  in  range (10,20):
      for  i  in  range (2,n):
            if  n%i==0:
                j=n/i
               Print  '%d  equals  %d  *  %d '  %  (n,i,j)
               break
       else:
               print  N,  ' is a prime number '

--------------------------------------------------------------------------------------------------------------- -------------------

While loop:

While to determine the condition:

Execute statement ...

Continue and break:

Continue used to skip this loop: Skip the remaining statements in the current loop, and proceed to the next round of loops

The break is used to jump out of the loop.

eg:

I=0
while  i<10:
    i+=1
    if  i%2>0:
        continue
    print  i
print----- ----------"    
I=1 while
True:
    print (i)
    i +=1
    if  i>10:
      break

While ... else:

While statements are nothing different, else statements are executed (not terminated by a break) when the entire loop completes normally

eg

Count =0
while  count<5:
      print  count, "less than 5"
      count=count+1
Else:
      print  count, " equals 5 "

--------------------------------------------------------------------------------------------------------------- -------------------

Pass statement:

The pass statement is an empty statement, in order to maintain the integrity of the program structure. Don't do anything, it's generally used as a placeholder statement.

Usage:

Pass

eg:

  while True:
     pass      # waits for keyboard interruption (CTRL + C)



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.