Python Study the next day

Source: Internet
Author: User

Python variables:

In a computer program, a variable can be not only a number, but also any data type.

In a python program, a variable is represented by a variable name, and the variable name must be a combination of uppercase and lowercase English, numeric, and underscore (_) and cannot begin with a number.

In Python, the equals sign = is an assignment statement that assigns any data type to a variable, the same variable can be repeatedly assigned, and can be a variable of different types

A = 123    #  A is an integer print'imooc'   # a becomes a string Print A

This type of variable itself is called Dynamic language, which corresponds to static language.

Static languages must specify the variable type when defining the variable, and if the type does not match, an error is given. For example, Java is a static language, and assignment statements are as follows (//for comments):

int a = 123;  "Mooc"; Error: Cannot assign string to integer variable

Do not equate an equal sign of an assignment statement with a mathematical equal sign. For example, the following code:

x = Ten= x + 2

If mathematically understood x = x + 2 That is not true anyway, in the program, the assignment statement first calculates the right expression X + 2, obtains the result 12, and assigns the variable x. Since the value before X is 10, the value of X becomes 12 after the value is re-assigned.

Finally, it is important to understand the representation of variables in computer memory. When we wrote: a = ‘ABC‘ The Python interpreter did two things:

1. Create a string in memory ‘ABC‘ ;

2. Create a variable named in memory a and point to it ‘ABC‘ .

You can also assign a variable A to another variable B, which actually points the variable B to the data that the variable a points to, such as the following code:

' ABC '  ='XYZ'print b

Does the last line print out the contents of variable b exactly ' ABC ' or ' XYZ '? If you understand mathematically, you will mistakenly conclude that B and a are the same and should be ' XYZ ', but actually the value of B is ' ABC ', and let us execute the code in one line, and we can see exactly what happened:

Execute a = ‘ABC‘ , the interpreter creates the string ' abc ' and variable A, and points a to ' abc ':

Execute b = a , the interpreter creates the variable B and points B to the string ' ABC ' that points to a:

Execution a = ‘XYZ‘ , the interpreter creates the string ' xyz ' and changes the point of a to ' XYZ ', but B does not change:

So, the b result of the last print variable is naturally ‘ABC‘ .

String:

Strings can be represented by ' or ' "" .

What if the string itself contains ? For example, if we want to represent  I‘m OK  a string, we can say it in " " parentheses:

"I ' m OK"

Similarly, if a string is contained " , we can use it as a ‘ ‘ means of enclosing:

' Learn ' Python "in Imooc '

What if the string contains and contains both " ?

At this point, you need to "escape" some special characters of the string, and the Python string is \ escaped.

To represent a stringBob said "I‘m OK".
Because ' and ' can cause ambiguity, so we insert a representation before it that \ this is an ordinary character and does not represent the beginning of the string, so the string can be represented as

' Bob said \ ' i\ ' m ok\ '. '

Note: the escape character \ does not count toward the contents of the string.

The usual escape characters are:

\ n indicates a newline \ t means a tab \ \ means \ character itself

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 to indicate that it is a raw string, and that the characters inside it do not need to be escaped. For example:

R ' \ (~_~)/\ (~_~)/'

However r‘...‘ , the notation cannot represent multiple lines of string, nor can it represent " a string containing and (why?). )

If you want to represent multiple lines of string, you can use the ‘‘‘...‘‘‘ expression:

"Line 1Line 2Line 3"

The above string is represented in exactly the same way as the following:

' Line 1\nline 2\nline 3 '

You can also add the multiline string to a raw string by adding it in front  r  of it:

R ' ' Python is created by ' Guido '. It's free and easy to learn. Let's start learn Python in imooc! '

The string also has an encoding problem.

Because a computer can only handle numbers, if you are working with text, you must convert the text to a number before processing it. The oldest computer was designed with 8 bits (bit) as a byte (byte), so the largest integer that a Word energy saver represents is 255 (binary 11111111 = decimal 255), and 0-255 is used to denote uppercase and lowercase letters, numbers, and some symbols. This Code table is called ASCII encoding, such as the code for capital A is 65, and the lowercase z is encoded as 122.

If you want to express Chinese, obviously a byte is not enough, need at least two bytes, and also can't and ASCII encoding conflict, so, China has developed GB2312 code, used to put Chinese into.

Similarly, other languages such as Japanese and Korean also have this problem. In order to unify all text encoding, Unicode came into being. Unicode unifies all languages into a set of encodings, so there is no more garbled problem.

Unicode usually uses two bytes to represent a character, the original English encoding from a single byte into a double-byte, only need to fill the high byte all 0 can be.

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 '

Note: without u, Chinese will not display properly.

In addition to a single Unicode string  u  , the escape character and multiline notation are still valid, with no difference to the normal string:

Escape:

U ' chinese \ n \ japanese \ Korean '

MultiRow

U ' ' first line second line '

raw+ Multiple lines:

ur ' python's Unicode string supports "Chinese", "Japanese", "Korean" and many other languages "

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.

Python Study the next day

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.