Python 3 Syntax Notation (vi) conditions, loops and asserts, pass, Del__python

Source: Internet
Author: User
Tags assert

Conditions:

If condition:
Statement block
Elif
Statement block
Else
Statement block


Elif represents else if


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

>>> if 1 < x < 2:
	print (' true ')

	
true

and represents and

>>> if x > 1 and x < 2:
	print (' true ')

	
True


or represents or

>>> x
2
>>> if x = = 2 or x = 3:
	print (x)

	
2

Returns a If B is true, otherwise it returns c A if B else c

>>> ' true ' if 1 < x <2 Else ' False '
true '

While Loop

While condition:

Statement block


Do not need parentheses oh.

>>> x
1.2
>>> while x < 2:
	print (x)
	x + + 0.2

	
1.2
1.4
1.5999999999999999
1.7999999999999998
1.9999999999999998
>>>


Often used:

While True:
    ..... If ...: Break
    ....


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, dictionary, tuple, iterator and so on.

>>> for x in range (0,10):
	print (x*x)

	
0
1
4
9
81

This is for.. else ... Statement
Executes only without a break, or, as long as you don't have a break, it executes


>>> for N in range (99,81,-1):
	root = sqrt (n)
	if root = Int (root):
		print (n)
break else:
	print ("I didn ' t fint it")

	
I didn ' t fint it

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

>>> [x*x for x in range (1,5)]
[1, 4, 9,]
>>> [x**2 to X in range (1,10) if x% 2 ==0]
[4, +]
>>> [(X,y) for X in range (1,3) to Y in range (4,6)]
[(1, 4), (1, 5), (2, 4), (2, 5)]


Assert assert
The following statement is true, otherwise the assertionerror appears

>>> x
1.2
>>> assert x > 1
>>> assert x > 2
traceback (most recent call L AST):
  File "<pyshell#61>", line 1, in <module>
    assert x > 2
assertionerror
>>> Assert x > 2, ' x must bigger than 2 '
traceback (most recent call last):
  File "<pyshell#64>", Line 1, in & Lt;module>
    assert x > 2, ' x must bigger than 2 '
assertionerror:x must bigger than 2

Pass

Pass means there's nothing here, no action.

If your program has unfinished functions and classes, you can add some comments, and then the Code section simply writes a pass, so that the program can run without an error, and later you can continue to refine your program

>>> class Nothing: Pass
	


del
Del deletes only the reference and the name, does not delete the value, that is, Python manages the memory automatically, is responsible for the memory recovery, this is also the python running low efficiency One reason

>>> x = [1,2,3]
>>> y = x    #x and y point to the same list
>>> del x
>>> x
traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    x
nameerror:name ' x ' are not de Fined
>>> y
[1, 2, 3]



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.