Python variables and data type notes

Source: Internet
Author: User

Data type

Can print different binary operations directly

The print statement can also be followed by multiple strings, separated by a comma "," and connected to a string of outputs.

String

    • 1. Strings can be represented by "or".
    • 2. What if the string itself contains '? For example, we want to represent the string I ' m OK, at this point, you can use "" to denote:
    • 3. "I ' m OK" Similarly, if the string contains ", we can use '" in the enclosed expression: ' Learn "Python" in Imooc '
    • 4. What if the string contains both ' and contains '? At this point, you need to "escape" some special characters of the string, and the python string is escaped with \.

Raw string

If a string contains many characters that need to be escaped, it can be cumbersome to escape each character. To avoid this, we can prefix the string with R, which means that it is a raw string, and that the characters inside it don't need to be escaped.

multi-line string " ... " "

" " Line 1Line 2Line 3 ""
#和这个相同 ' line 1\nline 2\nline 3 '

You can also add the RAW keyword before the multiline string.

Unicode string

Because a computer can only handle numbers, if you are working with text, you must convert the text to a number before processing it. Unicode unifies all languages into a set of encodings, so there is no more garbled problem.

Because Python was born earlier than the Unicode standard, the earliest Python only supported ASCII encoding, and the normal string ' ABC ' was ASCII-encoded inside python.

Python later added support for Unicode, with a Unicode-represented string expressed in u ' ... ' , for example:

Print u'  Chinese ' 

Unicode strings are no different from normal strings except for one more u, and the escape character and multiline notation are still valid:

Escape:

U' Chinese \ n japanese \ Korean '

MultiRow

U' ' first line second line '

If the Chinese string encounters unicodedecodeerror in a Python environment, this is because there is a problem with the format saved by the. py file. You can add comments on the first line:

# -*-coding:utf-8-*-

The purpose is to tell the Python interpreter to read the source code with UTF-8 encoding. Then save with notepad++ as ... and select UTF-8 format to save.

Integers and floating-point numbers

    1. Integer operation result is still integer
    2. Floating-point arithmetic is still a floating-point number
    3. The result of mixed integers and floating-point numbers is floating-point numbers

If we want to calculate the exact result of 11/4, we need to use floating-point calculation

11.0/4    #  ==> 2.75

Boolean-Value cloth

Only true and false two types of values

Boolean types have the following operations: with or not

Type can also do and, or, and not with other data types

Python sees 0, empty string ' ' and none as False, and other numeric and non-empty strings are considered True

A = Trueprintand'a=t'or'a=f  '  #A and ' a=t ' return ' a=t '#'a=t ' or ' a=f ' return ' a=t '#  So the result is ' a=t '

An important rule of and and or operations: short-circuit calculation

1. When calculating A and B, if a is false, then according to the algorithm, the whole result must be false, so it returns a; If A is True, the entire result must depend on B, so it returns B.

2. When a or B is calculated, if a is true, the whole result must be true, according to the or algorithm, and therefore return a; If A is False, the entire result must depend on B and therefore return B.

Raw strings and multiple lines of string

Python variables and data type notes

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.