Python Basic Statement (5) continuously updated

Source: Internet
Author: User

We've seen the print statement, import statement, and assignment statement in the previous pages. This time, it's more about understanding more statements.

Statement (1) print with comma output

The print------is automatically converted to a string and output, whether it be a string or any other type, if you need to print more than just a comma separated.

Print '  'Zhao'is  Zhao

The print parameter does not make up a tuple, and if you want to become a tuple, you need to enclose it in parentheses.

Print Print (1, 2, 3)

If there is a comma at the end of the print statement, the next print statement will be printed on the same line.

Print ' Hello ' , Print ' Python '

Output

Hello Python
Statement (2) Import

When you want to invoke the module function (functions), you first have to import the (import). The way to import has

    1. Import Module
    2. From module Import function
    3. From module import function,anotherfunction,yetfunction
    4. From module Import *

If all two modules have a function of the same name, such as a set function, it is possible to import it using the first method. Such as

Import Module Import Anothermodulemodule.set () anothermodule.set ( )

You can also alias the module. Such as

Import module as fishfish.eat ()

Aliases the function. Such as

 from Import function as Fishfunfishfun ()

For those functions with the same name, you can use this as

 from Import function as Function1  from Import function as function2

Note that some modules are arranged hierarchically, (one module inside of another module) and the detailed content will be updated later.

Statement (3) Assignment

Multiple variable assignments called sequence unpacking or recursive unpacking

>>> A, B, C = 1, 2, 3>>> a,b,c (1, 2, 3)

Swap variables (or multiple variables)

Print A, B1 2>>> A, B = B, aprint  A, b2 1

This feature can be leveraged when a function or method returns a tuple, sequence, or iterative object.

>>> words = {'one': 1,'both': 2,'three  ': 3}>>> k,v = words.popitem ()>>> k,v (' three ', 3)

Note that when the sequence is unpacked, the assignment variable on the left must be the same as the number of elements on the right, otherwise an assignment exception will be thrown.

>>> A, B, C =Traceback (most recent  ):"<stdin>"  in <module>2 values to unpack

New assignment unpacking features updated in Python3.0: Use the * operator to save the arguments in the form of a list.

>>> A, b, *c = 1,2,3,4,5,6,7,8,9>>> a,b,c (1, 2, [3, 4, 5, 6, 7, 8, 9])

Assignment of a variable to a multivariable-------chained value

>>> A, B = 1,2>>> A, B (1, 2)

Expression operators (+ 、-、/, *, |) Placed on the left side of the assignment operator (=)--------------increment assignment

First, the following two statements are equal

A = a + 1+ = 1

The second statement is the increment assignment, which can be used for other types as well.

' Hello '   'print*= 2print str

Output

Hello Word!hello word! Hello word!
Statement (4) statement block

A statement block is a set of statements in a conditional statement and a loop statement that requires a space or tab character to indent a statement to create a statement block.

Use: Represents the beginning of the current statement block, which indicates the end of the current statement block when a fallback statement block or a closed statement block is indented. Such as

A = 1if a = = 1    :print'A is equal to 1'else     Print'A is not equal to 1'
Statement (5) Conditional statement

Boolean value

The following values will be Python-read false:

False None  "" ()  [] {}

The others are interpreted as true (true).

Python Basic Statement (5) continuously updated

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.