Several interesting points about Python

Source: Internet
Author: User

1. Variable "_"

 

In interactive mode, the value of the last expression is assigned to the variable._. In this way, we can use it as a desktop calculator for continuous computing, for example:

>>>Tax = 12.5 / 100>>>Price = 100.50>>>Price * Tax12.5625>>>Price + _113.0625>>>Round(_, 2)113.06

This variable is read-only for users. Don't try to assign a value to it-you will only create an independent local variable with the same name, which shields the magic effect of the built-in variables of the system.

2. Three quotation marks

A string can be enclosed in one pair of three quotation marks:'"""'Or'''''. There is no need to escape the lines in the three quotation marks. They are already included in the string.

 
Print("""\Usage: thingy [Options]-H display this usage message-H hostname to connect""")

 

The output is as follows:

 
Usage: thingy [Options]-H display this usage message-H hostname to connect

 

3. original string

 

If we generate an "original" string,\ NThe sequence is not escaped, And the backslash at the end of the line and the line break in the source code are all part of the data in the string. Therefore, the following example:

 
Hello = R "this is a rather long string containing \ n\Several lines of text much as you wowould do in C ."Print(Hello)

Printed:

 
This is a rather long string containing \ n \ several lines of text much as you wowould do in C.

 

4. "+" and "*" of the string "*"

 

The string can be+Operator join (stick together), can be*Repeat:

>>>Word = 'Help' + 'A'>>>Word'Helpa'>>>'<' + Word*5 + '>''<Helpahelpahelpahelpahelpa>'

 

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.