Python learning notes (5), conditions, loops, and other statements (below)

Source: Internet
Author: User

5.5.5 skip Loop

1 break

Same as C.

>>> from math import sqrt>>> for n in range(99,0,-1):root = sqrt(n)if root == int (root):print nbreak81

2 continue

As in C, do not go into details.

3 while True/break Idioms

Example:

>>> while True:word=raw_input('p;ease enter a word:')if not word: breakprint ' the word was ' +wordp;ease enter a word: a the word was  ap;ease enter a word: b the word was  bp;ease enter a word:  the word was  p;ease enter a word:
5.5.6 else clause in a loop

for n in range(99,0,-1):root = sqrt(n)if root == int (root):print nbreakelse :print " don't find " don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find  don't find 81

5.6 list derivation-lightweight cycle

The list derivation uses other lists to create a new list. It works in a similar way to a for loop and is also simple:

>>> [x*x for x in range(10)][0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Other statements can also be nested.

[x*x for x in range(10)if x%3==0][0, 1, 4, 9, 16, 25, 36, 49, 64, 81][0, 9, 36, 81]>>> [(x,y)for x in range(3)for y in range (3)][(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

5.7 threes

5.7.1 nothing happened


Sometimes, the program does not do anything.

If name = 'l ':

Print 1
Elif name = '2 ':

Pass

Elif:

Print 3


5.7.2 Delete using del

In general, Python deletes objects that are no longer in use.

>>> x= [1,2]>>> x[1, 2]>>> x=None>>> x
In this case, [1, 2] The dictionary cannot be obtained and used. At this time, the Python interpreter (with infinite wisdom) deletes the dictionary directly.

Another method is to use del, which not only removes the reference of an object, but also removesNameItself

>>> x=1>>> del x>>> xTraceback (most recent call last):  File "
 
  ", line 1, in 
  
       xNameError: name 'x' is not defined
  
 

In the following example, both x and y point to the same list. After deleting x, y is still there, because only the name of x is deleted.

>>> x=y=[1,2]>>> x[1, 2]>>> y[1, 2]>>> del x>>> xTraceback (most recent call last):  File "
 
  ", line 1, in 
  
       xNameError: name 'x' is not defined>>> y[1, 2]
  
 

5.7.3 execute and evaluate strings using exec and eval

Sometimes you may need to dynamically create Python code and execute it as a statement or as an expression:

1 exec

The statement for executing a string is exec:

>>> exec "print 'hello,world!' "hello,world!
However, using simple exec statements is by no means a good thing. In many cases, you can provide it with a namespace-where variables can be placed.

You can add in And It is a dictionary that prevents code string namespaces.

>>> from math import sqrt>>> scope={}>>> exec 'sqrt=1' in scope>>> sqrt(4)2.0>>> scope['sqrt']1

This part of content will be further studied in the next chapter.

2. eval

Eval is a built-in function similar to exec. exec statements execute a series of Python statements, while eval calculates Python expressions and returns results. For example, you can use the following code to read a common Python calculator.

eval(raw_input("enter an arithmet expression:"))enter an arithmet expression:6+18*242

Summary:

Print

Import

Assignment

Block

Condition

Assertions

Loop

List Derivation

Pass, del, exec statement

Now that the basic knowledge has been completed, there is no problem with implementing any algorithms you think of. You can also let the program read the parameters and print the results.



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.