Python 3 conditions, loops and assert, pass, Del

Source: Internet
Author: User

Conditions:

If condition:
Statement block
Elif
Statement block
Else
Statement block


Elif indicates else if


This is actually legal!!! 1 < x < 2!!!

[Python]View Plaincopy print?
    1. >>> If 1 < x < 2:
    2. print (' True ')
    3. True


and represents and

[Python]View Plaincopy print?
    1. >>> if x > 1 and x < 2:
    2. print (' True ')
    3. True


or represents or

[Python]View Plaincopy print?
    1. >>> x
    2. 2
    3. >>> if x = = 2 or x = = 3:
    4. print (x)
    5. 2

Returns a If B is true, otherwise returns C

A If B else c

[Python]View Plaincopy print?
    1. >>> ' True ' if 1 < x <2 else ' False '
    2. ' True '

While loop

While condition:

Statement block


No brackets required!

[Python]View Plaincopy print?
    1. >>> x
    2. 1.2
    3. >>> while x < 2:
    4. print (x)
    5. x + = 0.2
    6. 1.2
    7. 1.4
    8. 1.5999999999999999
    9. 1.7999999999999998
    10. 1.9999999999999998
    11. >>>


Often used:

[Python]View Plaincopy print?
    1. While True:
    2. ....
    3. If ...:
    4. Break
    5. ....


For loop

For something in XXXX:

Statement block


That is, for each element in XXXX, execute some block of statements, XXXX can be a list, a dictionary, a tuple, an iterator, and so on.

[Python]View Plain Copy print?
    1. >>> for x in range (0,10):   
    2.     print (x*x)   
    3. &NBSP;&NBSP;
    4.       
    5. 0&NBSP;&NBSP;
    6. 1&NBSP;&NBSP;
    7. 4&NBSP;&NBSP;
    8. 9&NBSP;&NBSP;
    9. 16&NBSP;&NBSP;
    10. 25&NBSP;&NBSP;
    11. 36&NBSP;&NBSP;
    12. 49&NBSP;&NBSP;
    13. 64&NBSP;&NBSP;
    14. 81&NBSP;&NBSP;

This is for: else ... Statement
Execute only without break, or, as long as you do not break, it will execute


[Python]View Plaincopy print?
  1. >>> for n in range (81,-1):
  2. root = sqrt (n)
  3. if root = = Int (root):
  4. print (n)
  5. Break
  6. Else
  7. print ("I didn ' t fint it")
  8. I didn ' t fint it


But you should use the list derivation as much as possible, as it is more convenient and clear

[Python]View Plaincopy print?
  1. >>> [x*x for x in range (1,5)]
  2. [1, 4, 9, + ]
  3. >>> [x**2 for x in range (1,Ten) if x 2 = =0] /c5>
  4. [4, +, + , + ]
  5. >>> [(x, y) for x in range (1,3) for y in range (4,6)]
  6. [(1, 4), (1, 5), (2, 4), (2, 5)]


Assert assert
The following statement is true, otherwise assertionerror appears

Used to check a condition, and if it is true, do nothing. If it is false, it throws a Asserterror and contains an error message.

For example:

py> x =23py> assertx > 0"x is not zero or negative"py> assertx%2==0"x is not an even number"Traceback (most recent call last):File"", line 1inAssertionError: x isnotan even number
#常用在代码开头的注释 assert target  in (x, y, z) if target  = = x:      run_x_code() elif target  = = y:      run_y_code() else :      assert target  = = z      run_z_code()

Pass

Pass means there's nothing here, no action done.

If your program has unfinished functions and classes and so on, you can add some comments first, then the Code section just write a pass, so the program can run without error, and later you can continue to refine your program

[Python]View Plaincopy print?
    1. >>> class Nothing:
    2. Pass
    3. >>>


Del
Del deletes only references and names, does not delete values, that is, Python automatically manages memory and is responsible for memory recycling, which is one reason why Python is running less efficiently.

[Python]View Plaincopy print?
    1. >>> x = [1,2,3]
    2. >>> y = x #x and y point to the same list
    3. >>> del x
    4. >>> x
    5. Traceback (most recent):
    6. File "<pyshell#41>", line 1, in <module>
    7. X
    8. Nameerror:name ' x ' is not defined
    9. >>> y
    10. [1, 2, 3]

Python 3 conditions, loops and assert, pass, Del

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.