Python BASICS (6) -- conditions and loops

Source: Internet
Author: User

1. output with commas

Print to print the expression-whether it is a string or a string of other types after automatic conversion. It is also feasible to print multiple expressions, as long as they are separated by commas:

>>>  ,4242 >>> 1,2,31, 2, 3>>>  1,2,31 2 3>>> (1,2,31, 2, 3)

A space is inserted between each parameter.

2. import one thing as another

When importing functions from a module, you can use

Import somemodule

Or

From somemodule import somefunction

Or

From somemodule import somefunction, anotherfunction, yetanotherfunction

Or

From somemodule import *

If both modules have open functions, you only need to use the first method to import them, and then use the functions as follows:

Module1.open (...)

Module2.open (...)

Alternatively, you can add an as clause at the end of the statement to give a name after the clause, or provide an alias for the entire module:

>>> >>> foobar.sqrt(42.0>>> >>>  math >>> foobar(42.0

3. Multiple values can be assigned at the same time:

>>> x,y,z=1,2,3>>> 1 2 3>>> >>> x,y=>>> 2 1 3

What we do here is sequential unpacking-Unlocking the sequences of multiple values and placing them in the sequence of variables. A more vivid expression is as follows:

>>> values=1,2,3>>>1, 2, 3>>> x,y,z=>>>1>>>2>>>3

4. chained assignment

Chained assignment is a shortcut to assign the same value to multiple variables.

X = y = somefunction ()

The effect is the same as that of the following statement:

Y = somefunction ()

X = y

5. Incremental assignment

>>> x=2>>> x+=1>>> x*=2>>>6>>> fnord=>>> fnord+=>>> fnord*=2>>>

6. Comparison operators:

Used to compare other objects

In Python, the comparison operation and the assignment operation can be connected. Several operators can be used together, for example, 0 <age <100

7. is: identity Operator

The is operator is used to determine the same identity rather than equality.

>>> x=y=[1,2,3>>> z=[1,2,3>>> x==>>> x==>>> x >>> x 

8. while Loop

>>> x=1>>>  x <=100+=10

9. for Loop

>>> words=[,,,,>>>  word >>> >>> numbers=[0,1,2,3,4,5,6,7,8,9>>>  number  number

>>> range(0,101, 2, 3, 4, 5, 6, 7, 8, 9>>> range(101, 2, 3, 4, 5, 6, 7, 8, 9>>>  number  range(1,101 number

10. cyclically traverse dictionary elements

A simple for statement can loop all the keys of the dictionary, just like the processing sequence:

>>> d={:1,:2,:3>>>  key  key,213

The order of the dictionary elements is usually not defined.

The program can iterate two sequences at the same time:

>>> names=[,,,>>> ages=[12,45,32,102>>>  i  names[i], ,ages[i],

The built-in zip function can also be used for parallel iteration. You can "compress" two sequences and return a list of tuples:

>>> names=[,,,>>> ages=[12,45,32,102>>>, 12), (, 45), (, 32), (, 102>>>  name,age  name,,age,

 

 

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.