Detailed parsing of data types and variables in Python _python

Source: Internet
Author: User
Tags integer division in python

Data type

Computers, as the name suggests, are machines that can do mathematical calculations, so computer programs can naturally handle a variety of values. However, the computer can handle much more than the number, but also can handle text, graphics, audio, video, Web pages and other kinds of data, different data, need to define different data types. In Python, there are several types of data that can be processed directly:
integers

Python can handle integers of any size, including, of course, negative integers, which are represented in the program in exactly the same way as mathematically, for example: 1,100,-8080,0, and so on.

Because the computer uses binary, it is sometimes convenient to represent integers in hexadecimal, hexadecimal with a 0x prefix and 0-9,a-f, for example: 0XFF00,0XA5B4C3D2, and so on.
floating point numbers

A floating-point number is a decimal, known as a float, because the decimal point position of a float is variable in the scientific notation, for example, 1.23x109 and 12.3x108 are equal. Floating point numbers can be written mathematically, such as 1.23,3.14,-9.01, and so on. But for very large or very small floating-point numbers, we must use the scientific notation, the 10 with e substitution, 1.23x109 is 1.23e9, or 12.3e8,0.000012 can be written as 1.2e-5, and so on.

The way integers and floating-point numbers are stored inside the computer is different, and the integer operation is always accurate (is division also accurate?). Yes! , and floating-point operations may have rounding errors.
string

strings are any text enclosed in ' or ', such as ' abc ', ' XYZ ', and so on. Note that the ' or ' itself is only a representation, not part of a string, so the string ' abc ' has only a,b,c these 3 characters. If ' itself is also a character, it can be surrounded by ", such as" I ' m OK "contains the characters I, ', M, space, o,k these 6 characters.

What if the inside of the string contains both ' and contains '? You can use the escape character \ To identify, for example:

' I\ ' m \ ok\ '! '

The string content represented is:

I ' m ' OK '!

Escape character \ Can escape a lot of characters, such as \ n means line break, \ t for tab, character \ itself to escape, so the character is \, can be used in the Python interactive command line print a string to see:

>>> print ' i\ ' m ok. '
I ' m OK.
>>> print ' I\ ' m learning\npython. '
I ' m learning
Python.
>>> print ' \\\n\\ '
\


If there are many characters in the string that need to be escaped, you need to add a lot of \, in order to simplify, Python also allows the use of the "R" "internal" string default does not escape, you can try:

>>> print ' \\\t\\ '
\
>>> print R ' \\\t\\ '
\\\t\\

If there's a lot of line wrapping inside the string, it's hard to read in a row, and for simplicity, Python allows you to use the format of ' ... ' to represent multiple lines of content, and try it yourself:

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

The above is entered in the interactive command line, if written as a program, that is:

print ' ' line1
line2
line3 '

Multi-line string ' ... ' can also be used in front with R, please test yourself.
Boolean value

The Boolean value and Boolean algebra represent exactly the same, a Boolean value of True, false two, or true, or false, in Python, you can directly use True, False to represent a Boolean value (note case), or you can calculate it by Boolean operation:

>>> true
>>> false
false
>>> 3 > 2
True
>>> 3 > 5
False

Boolean values can be operations with and, or, and not.

The and operation is the same as the operation, and only the result of all True,and operations is true:

>>> true and True to
true
>>> true and
false
>>> false and False
False

The or operation is or is an operation, as long as one of the results of the true,or operation is true:

>>> true to True or True
>>> true or False
true
>>> false or False
False

The not operation is a single order operator, which turns true to False,false to true:

>>> not true to
false
>>> not False
true

Boolean values are often used in conditional judgments, such as:

If age >=:
  print ' adult '
else:
  print ' teenager '

Null value

A null value is a special value in Python, represented by none. None is not understood to be 0, because 0 is meaningful, and none is a special null value.

In addition, Python provides lists, dictionaries, and many other data types, and allows you to create custom data types, which we'll continue to talk about later.
variable

The concept of a variable is basically the same as that of a junior algebra, but in a computer program, a variable can be either a number or any data type.

Variables are represented in a program by a variable name, which must be a combination of uppercase and lowercase, numbers, and _, and cannot begin with a number, such as:

A = 1

Variable A is an integer.

t_007 = ' T007 '

Variable t_007 is a string.

Answer = True

Variable answer is a Boolean value true.

In Python, the equal sign = is an assignment statement that can assign any data type to a variable, the same variable can be assigned repeatedly, and can be of different types of variables, such as:

A = 123 # A is the integer
print a
a = ' ABC ' # A becomes a string
print a

This type of variable itself is an invariant language called a dynamic language, which corresponds to a static language. A static language must specify a variable type when defining a variable, and an error will be given if the type does not match when the value is assigned. For example, Java is a static language, and the assignment statement is as follows (//comments):

int a = 123; A is an integer type variable
a = "ABC";//Error: Cannot assign a string to an integer variable

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

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

x = Ten
x = x + 2

If mathematically understood x = x + 2 that is not tenable anyway, in the program, the assignment statement first evaluates the right expression x + 2, gets the result 12, and assigns it to the variable x. Since the value before X is 10, the value of x changes to 12 after the value is assigned.

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

A = ' ABC '

, the Python interpreter did two things:

    1. A string of ' ABC ' was created in memory;
    2. Creates a variable named a in memory and points it to ' ABC '.

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

A = ' ABC '
b = a
a = ' XYZ '
print B

Does the last line print out the contents of variable B, ' ABC ' or ' XYZ '? If you understand mathematically, you will mistakenly get the same as B and a, and it should be ' XYZ ', but actually the value of B is ' ABC ', so let's run the code in one line and we can see what happened:

Executing a = ' abc ', the interpreter creates the string ' abc ' and the variable A and points A to ' abc ':

Executing B = A, the interpreter creates the variable B and points B to the string ' ABC ' that the A points to:

Executing a = ' xyz ', the interpreter created the string ' xyz ' and changed the point of a to ' XYZ ', but B did not change:

So, the final result of printing the variable B is ' ABC '.
Constants

A constant is a variable that cannot be changed, such as a common mathematical constant pi is a constant. In Python, constants are usually represented in all uppercase variable names:

PI = 3.14159265359

But in fact pi is still a variable, Python does not have any mechanism to guarantee PI will not be changed, so, using all uppercase variable names to represent constants is a customary use, if you must change the value of variable pi, no one can stop you.

Finally explain why the division of integers is also accurate, you can try:

>>> 10/3
3

You are not mistaken, integer division is always an integer, even if it is not endless. To do the exact division, just replace one of the integers with floating-point numbers to divide:

>>> 10.0/3
3.3333333333333335

Because integer division takes only the integral part of the result, Python also provides a remainder operation that can be divided by two integers:

>>>% 3
1

Whether the integer is division or the remainder, the result is always an integer, so the result of the integer operation is always accurate.
Summary

Python supports a variety of data types, in which you can think of any data as an "object", while variables are used to point to these data objects in the program, and assigning values to variables is associated with the data and variables.
Your encouragement is the author's greatest motivation for writing

If you think this site's quality of the tutorial is good, after reading that the harvest is very large, the expected wage increase can exceed 30%, may wish to small sponsorship me, let me have the motivation to continue to write high-quality tutorials:

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.