Python Study Notes (1)-special symbols

Source: Internet
Author: User
Tags floor division

Special symbols

 

Some symbols have special definitions in Python compared with C/C ++ and Java.

 

_: Value of the last expression

 

Example:

 

 

>>> 4/2 <br/> 2 <br/> >>>_< br/> 2 <br/> Print _ + 10 <br/> 12 

 

%: String format Operator

 

Example:

 

>>> Print '% s is Number % d! '% ('Python', 1) <br/> python is number 1! 

 

>>: Output redirection

 

Example:

>>> Logfile = open ('/tmp/mylog.txt', 'A') <br/> Print> logfile, 'fatal error: Invalid input! '<Br/> logfile. Close ()

 

 

#: Comments from # To the end of the line.

 

//: For Python 3 +, '/' indicates the real division, '//' indicates the floor Division (for 2. for Version X, you need to import _ future _ division)

 

# Python version 2.5 <br/> 5/2 <br/> 2 <br/> 5 // 2 <br/> 2 <br/> from _ future _ import Division <br/> 5/2 <br/> 2.5 <br/> 5 // 2 <br/> 2

 

+, *: For strings, '+' indicates string connection, and '*' indicates string duplication.

 

Example:

>>> 'Hello' + 'World! '<Br/>' Hello world! '<Br/>>>> 'hello' * 2 <br/> 'hellohello'

 

[Begin: end]: The operation symbol of the slice. value range: [begin, end). If you do not enter begin, it indicates all elements before the end (excluding the end itself). If you do not enter end, it indicates the begin and all its elements. The index starts from 0. In particular, the last index can be represented by-1.

 

Example:

>>> Val = [1, 2, 3, 4] <br/> Val [0] <br/> 1 <br/> Val [-1] <br/> 4 <br/>> Val [0: -1] <br/> [1, 2, 3] <br/> Val [0:] <br/> [1, 2, 3, 4] <br/> Val [:-1] <br/> [1, 2, 3] <br/> Val [:] <br/> [1, 2, 3, 4]

 

''': Three consecutive quotation marks, which are used as the start and end of a string. A string can span multiple rows and contain special symbols such as line breaks and tabs. When an HTML or SQL statement is required, using ''' makes the code more concise and clear.

 

Cursor.exe cute (''' <br/> insert into warning_type <br/> (type_id, type_name, urgency, description) <br/> values (0, 'test info ', 0, 'test info'); <br/> ''')

 

U: the string is preceded by the U symbol, indicating that the string is a unicode string.

 

>>> S = 'hello' <br/> type (s) <br/> <type 'str'> <br/> S = u'hello' <br/> type (s) <br/> <type 'unicode '>

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.