Python (5) assignment, expression, and print statement

Source: Internet
Author: User

Assignment Statement

The vast majority of assignment statements are simple, but some features should be specifically remembered:

<1> the value assignment statement creates an object reference value.

The Python value assignment statement stores the object reference value in the element of the variable name or data structure. The value assignment statement always creates an object reference value, instead

Copying objects, so Python variables are more like pointers than data storage areas.

<2> the variable name is created when the value is assigned for the first time.

Once a value is assigned, the variable name is replaced by the referenced value whenever it appears in the expression.

<3> variable names must be assigned a value before being referenced.

Otherwise, an exception may occur.

<4> some operations when performing implicit value assignment

All assignment environments Bind Variable names and object reference values at runtime.

Common assignment statements

<1> spam = 'spam' # basic form

<2> spam, ham = 'yum ', 'yum' # values of tuples

<3> [spam, ham] = 'yum ', 'yum' # list assignment

<4> a, B, c, d = 'spam' # sequence assignment

<5> spam = ham = 'spam' # multi-objective Assignment

<6> spam = spam + N # enhanced value assignment

Common expression statements

Foo (x, y) # function call

Foo. f (x) # method call

Var # directly print the variable value

Expression statements are usually used to execute methods that can be modified in the original place.

If L = L. append (1) is used, L is None.

Print operation

The print operations differ greatly in python versions 2. x and 3. x, making it unusable without modification.

Printing in 2.x indicates that the statement has its own specific syntax.

Printing in 3.x is a built-in function that uses keyword parameters to represent a specific mode.

3.0 print function

Print ([object,...] [, sep = ''] [, end = '\ n'] [, file = sys. stdout])

The items in square brackets are optional, and the values after '=' have default values. This built-in function separates one or more sep strings.

The text representation of the object, followed by end, is printed to the file stream. Where,

Sep is a string inserted between the text of each object. If it is not passed, it is a space by default.

X = 1 y = 2 z = 3

Print (x, y, z)-> 1 2 3 # The default sep interval is a space.

Print (x, y, z, sep = '')-> 123 # There is no space between the replacement objects with an empty string in sep

End is a string at the end of the printed text. The default value is \ n.

Print (x, y, z, end = '')-> 1 2 3 # after printing, the end is a null string and will not go to the next line.

Print (x, y, z, end = ''); print (x, y, z)-> 1 2 31 2 3 # next to the last print

File specifies the file to which the text will be sent. The default value is sys. stdout, which will be displayed on the screen.

Print(x,y,z,file=open('test.txt ', 'w') into test.txt

Print(open('test.txt '). read ())

1 2 3

You can also modify the value assignment file stream of sys. stdout for redirection.

Import sys

Sys. stdout = open('log.txt ', 'A ')

In the subsequent program, all the printlinks in any place will be written at the end of log.txt. The default print statement only transmits text to sys. stdout. write.

Method.

Reference < >

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.