Python variables, character encodings and operations

Source: Internet
Author: User
Tags arithmetic

One, variable

I believe that we are not very unfamiliar with the variables, we are from the primary equation when the x and Y are variables. So what is a python variable?

Python variables are actually similar to the variables of the equation of reconciliation.

>>>a=1>>><class'int'>>>>a=1.0 <class'float'>>>>2131613099568

C language variables, you must first declare the type of the variable, and the Python variable can be based on its value to pay it, and dynamically modify the type. So what's the use of variables?

We can think of variables as a label, just like your name, and what if you don't have a name. If someone wants to know you, how do you introduce yourself. There is a mechanism in the Python interpreter, when a data does not have a variable name

Or if the tag corresponds to it, Python will delete it, which can reduce memory. Python assigns the data stored by the variable to one of its memory addresses. We can access this function through the ID ().

Let's look at an example like this.

  

>>>a=1>>>1>>>b=a>>>1>>>a=6>>>  6>>>1

We can understand that with both A and B variables pointing at 1, when a variable points to another number 6 o'clock, B still points to 1. So the result will come.

Two, character encoding

We first understand what is the character encoding, we know that the calculation is binary to store data, the computer is more stupid, so it can only recognize 0 and 1, then we are now a lot of English symbols, how to store various Chinese characters?

The computer was first invented by Americans, so they designed a set of coding formats. A byte (8-bit) is used to denote letters and characters in English, which is the ASCII code.

ASCII code can represent up to 255 characters This is obviously enough for the English, but the computer is now very popular, the world so many languages so many symbols, ASCII code is not enough, then how to solve it?

Taking Chinese characters, I have accumulated more than 5,000 years of Chinese culture. Chinese people's ingenuity, has launched a number of GB2312,GBK, such as some can support the Chinese encoding format. But if that's the only thing,

So if I write a program in China, get Japan to run, then it will become garbled, so need a uniform rules. Unicode was born, and Unicode was called the Universal code, supporting the language and symbols of the country.

Each character is the same word, and two bytes. The advent of Unicode solves a lot of problems, but in this case the Americans do not want to, originally their English only a byte can be, and now have to use a word, so very

Waste. Then they launched the UTF-8, in the UTF-8 English characters for a byte, and Chinese into 3 bytes, always feel good pit.

  

  

So for Python, python2 special Pit, a lot of people in character encoding ate a loss, because Python2 appear earlier, so its default encoding is ASCII code, for Python3 is very good, it torture is utf-8,

This solves the problem by coding this big hole.

  

>>>Import sys>>>print (sys.getdefaultencoding ())  'utf-8  '

The conversions in different encodings in Python are also easy.

  

The transcoding in Python, we have to go through Unicode, for example, we want to convert the Utf-8 encoding into GBK encoding, we should first decode, in the code. Vice versa.

  

# first decoded into Unicode, encoded into GBKa=' I love China!  'a=a.decode ('utf-8'). Encode ('gbk')  # Then A's code is GBK.
View Code

Three, arithmetic

Arithmetic operations:

  

  

>>>a=3+2>>>5>>>a=5-6>>>-1>>>a=5*6>>>  30>>>a=5/2>>>2.5>>>a=45%10>>>5>>>a=2** 3>>>8>>>a=8/3>>>2
View Code

Comparison operation:

  

>>>3==3 true>>>4!=3 true# This syntax is problematic. >>>3>4 False>>>3<4 true>>>4>=3 true>> >4<=3 False
View Code

Assignment operation:

  

>>>a=3>>>3>>>a+=3>>>6>>>a-=3>>>  3>>>a*=3>>>9>>>a/=3>>>3.0>>>a%=2>> >1.0>>>a**=8>>>1.0>>>a//=2>>>0.0
View Code

Logical operation:

It is false as long as it is greater than 0 or not empty.

  

What is a logical short circuit:

Take and for, only the right and the same time is true when the result will be true, if the first is false, then the second will not judge, directly return the first data.

Similarly, or there will be.

>>>'and# The first is an empty string, so the first one is false, so the first character is returned.   The first one is 3, so the first one is true, so go directly to the first one.   not 3 flase
View Code

Member Operations:

  

inch [3,5,6] True  not inch [3,5,6] False
View Code

Identity operation:

  

>>>a=3>>>b=a>>>1720525584>>> is a True is not a False
View Code

Bit operations:

>>>bin (6)#Change decimal number to binary '0b110'>>>bin (5) '0b101'>>>6 & 5#The binary number of each bit is the same as the following4>>>bin (4) '0b100'>>>15 |4 15>>>15^4 11>>>~4#equivalent to the number before the sign minus one, the table has a problem. -5>>>4<<2#100b=>10000b16>>>4>>2#100b=>1b1
View Code

Order of Operations:

  

Python variables, character encodings and operations

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.