Parse data types and variables in Python in detail, and parse python Data Types

Source: Internet
Author: User
Tags integer division

Parse data types and variables in Python in detail, and parse python Data Types

Data Type

As the name suggests, a computer is a machine that can be used for mathematical computation. Therefore, a computer program can naturally process various numerical values. However, computers can process a variety of data, such as text, graphics, audio, video, and web pages. Different data types need to be defined. In Python, the following types of data can be processed directly:
Integer

Python can process Integers of any size, including negative integers. The Representation Method in the program is exactly the same as that in mathematics, such as 1,100,-8080,0.

Because the computer uses binary, it is convenient to use the hexadecimal notation to represent integers. The hexadecimal notation is indicated by the 0x prefix and 0-9, a-f, for example, 0xff00, 0xa5b4c3d2, and so on.
Floating Point Number

A floating point is also a decimal point. It is called a floating point because the decimal point of a floating point is variable according to the scientific notation. For example, 1.23x109 and 12.3x108 are equal. Floating point numbers can be written in mathematics, such as 1.23, 3.14,-9.01, and so on. However, large or small floating point numbers must be represented by scientific notation, replacing 10 with e. 1.23x109 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5.

Integers and floating-point numbers are stored in computers in different ways. Integer operations are always accurate (is Division accurate? Yes !), Floating-point operations may have rounding errors.
String

A string is any text enclosed by ''or" ", such as 'abc' and" xyz. Note that ''or" "is only a representation, not a part of the string. Therefore, the string 'abc' contains only three characters: a, B, and c. If 'is also a character, it can be enclosed by "". For example, "I'm OK" contains the characters I,', m, space, O, k.

What if the string contains both 'and? It can be identified by escape characters \, for example:

'I\'m \"OK\"!'

The string content is:

I'm "OK"!

Escape Character \ can escape many characters. For example, \ n represents a line break, \ t represents a tab, and the character \ itself also needs to be escaped \, you can use print to print strings in the Python interactive command line:

>>> print 'I\'m ok.'I'm ok.>>> print 'I\'m learning\nPython.'I'm learningPython.>>> print '\\\n\\'\\

If many characters in the string need to be escaped, you need to add a lot of \. For simplicity, Python also allows the use of r'' to indicate that the internal string is not escaped by default. You can try it yourself:

>>> print '\\\t\\'\    \>>> print r'\\\t\\'\\\t\\

If a string contains many line breaks, \ n is not easy to read in a line. To simplify it, Python allows '''... ''' format indicates multi-line content. Try it by yourself:

>>> print '''line1... line2... line3'''line1line2line3

The above is input in the interactive command line. If it is written as a program, it is:

print '''line1line2line3'''

You can add r to the front of the multiline string '''... '''. Please test it yourself.
Boolean Value

The Boolean value is exactly the same as that of the Boolean algebra. A boolean value can only be True or False, either True or False. in Python, you can use True or False to represent a Boolean value (case sensitive). You can also use a Boolean operation to calculate the value:

>>> TrueTrue>>> FalseFalse>>> 3 > 2True>>> 3 > 5False

Boolean values can be calculated using and, or, and not.

The and operation is the same as the operation. Only if all operations are True, and the operation result is True:

>>> True and TrueTrue>>> True and FalseFalse>>> False and FalseFalse

Or is an or operation. If either of them is True, or is True:

>>> True or TrueTrue>>> True or FalseTrue>>> False or FalseFalse

The not operation is not an operation. It is a single-object operator that converts True to False and False to True:

>>> not TrueFalse>>> not FalseTrue

Boolean values are often used in condition judgment, for example:

if age >= 18:  print 'adult'else:  print 'teenager'

Null Value

A null value is a special value in Python, expressed as None. None cannot be understood as 0, because 0 is meaningful, and None is a special null value.

In addition, Python provides multiple data types, such as lists and dictionaries, and allows you to create custom data types. We will continue later.
Variable

The concept of variables is basically the same as that of the equation variables of junior high school algebra. in computer programs, variables can not only be numbers, but also any data type.

A variable is represented by a variable name in the program. The variable name must be a combination of uppercase and lowercase English letters, numbers, and _, and cannot start with a number. For example:

a = 1

Variable a is an integer.

t_007 = 'T007'

The t_007 variable is a string.

Answer = True

The Answer variable is a Boolean value of True.

In Python, equal sign = is a value assignment statement, which can assign any data type to a variable. The same variable can be assigned multiple times and can be of different types, for example:

A = 123 # a is the integer print aa = 'abc' # a is the string print

This variable is a dynamic language and corresponds to a static language. The static language must specify the variable type when defining the variable. If the type does not match during the assignment, an error is returned. For example, if Java is a static language, the value assignment statement is as follows (// represents a comment ):

Int a = 123; // a is an integer type variable a = "ABC"; // error: the string cannot be assigned to an integer variable.

This is why dynamic languages are more flexible than static languages.

Do not equate the equals sign of the value assignment statement with the equals sign of mathematics. For example, the following code:

x = 10x = x + 2

If you understand x = x + 2 in mathematics, it is not true in any case. In the program, the value assignment statement calculates the expression x + 2 on the right. Expected result 12 is displayed, then assign the variable x. Since the value before x is 10, after the value is re-assigned, the value of x is changed to 12.

Finally, it is important to understand the representation of variables in computer memory. When we write:

a = 'ABC'

The Python interpreter does two things:

  1. A 'abc' string is created in the memory;
  2. Create a variable named a in the memory and point it to 'abc '.

You can also assign a value to variable a to variable B. This operation actually points variable B to the data pointed to by variable a, for example, the following code:

a = 'ABC'b = aa = 'XYZ'print b

In the last row, whether the content of variable B is 'abc' or 'xyz '? In a mathematical sense, B and A are the same, and it should be 'xyz', but in fact the value of B is 'abc ', let's execute the code line by line to see what happened:

Run a = 'abc'. The Interpreter creates the string 'abc' and variable a, and points a to 'abc ':

Run B = a. The Interpreter creates variable B and points B to the string 'abc' pointed to by ':

Run a = 'xyz', the interpreter creates the string 'xyz', and changes the direction of a to 'xyz', But B has not changed:

Therefore, the result of printing variable B is 'abc.
Constant

A constant is a variable that cannot be changed. For example, a common mathematical constant π is a constant. In Python, constants are usually represented by variable names in uppercase:

PI = 3.14159265359

But in fact, PI is still a variable. Python does not have any mechanism to ensure that PI will not be changed. Therefore, it is just a habit to use all uppercase variable names to represent constants, if you must change the PI value of the variable, no one will stop you.

Finally, Let's explain why the division of integers is accurate. You can try:

>>> 10 / 33

You are not mistaken. The integer division is always an integer, even if it is not divided. To perform a precise division, you only need to replace an integer with a floating point number for Division:

>>> 10.0 / 33.3333333333333335

Because integer division only takes the integer part of the result, Python also provides a remainder operation to obtain the remainder of the two integers:

>>> 10 % 31

The result is always an integer no matter whether the integer is divided or the remainder. Therefore, the result of the Integer Operation is always accurate.
Summary

Python supports multiple data types. in a computer, any data can be considered as an "object", and variables are used to point to these data objects in a program, assigning a value to a variable is to associate the data with the variable.
Your encouragement is the greatest motivation for writing.

If you think that the tutorial quality of this website is good, you will find a lot of gains after reading it, and the expected salary increase is more than 30%, you may wish to subscribe to me and let me have the motivation to continue writing high-quality Tutorials:

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.