The data type

As the name suggests, a computer is a machine that can do mathematical calculations, so it stands to reason that computer programs can handle all kinds of numbers. However, computers can process far more than numerical values, but also can process text, graphics, audio, video, web pages and other data, different data, need to define different data types. In Python, there are several types of data that can be handled directly:

The first group of letters is: Mengy, the second group is: 7762, and she will arrange to learn them in order. , whether you are big bull or small white, is want to change or want to enter the industry can come to understand progress together to learn! There are development tools, lots of dry goods and technical information to share!

The integer

Python can handle integers of any size, including negative integers, as mathematically written, such as 1,100, -8080, 0, and so on.

Because computers use binary, it is sometimes convenient to use hexadecimal integers, which are represented by the 0x prefix and 0-9, a-f, such as 0xFF00, 0xA5b4C3d2, and so on.

Floating point Numbers

Floating point numbers, which are decimals, are called floating point numbers because in scientific notation, the decimal position of a floating point number is variable; for example, 1.23×109 is equal to 12.3×108. 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, you have to use scientific notation, substituting 10 for e, 1.23×109 for 1.23e9, or 12.3E8, 0.000012 for 1.2E-5, and so on.

The first group of letters is: Mengy, the second group is: 7762, and she will arrange to learn them in order. , whether you are big bull or small white, is want to change or want to enter the industry can come to understand progress together to learn! There are development tools, lots of dry goods and technical information to share!

Integers and floating-point numbers are stored differently inside a computer, and integer operations are always exact. Yes!) Floating-point operations can be rounded off.

string

A string is any text enclosed in “” or “”, such as

‘abc’

.

“xyz”

And so on. Note that ” or “” is only a representation in and of itself, not part of a string, therefore, a string

‘abc’

only

a

.

b

.

c

These three characters. if

It is also a character, so it can be enclosed in “”, for example

“I’m OK”

The contained characters are

I

.

.

m

, the blank space,

O

.

K

These six characters.

If the string contains both

Contain,

How to do? Escape characters can be used

\

To identify, for example:

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

The contents of the string represented are:

I'm "OK"!

Escape character

\

Can escape many characters, such as

\n

Means newline,

\t

Represents TAB characters

\

It has to escape itself, so

\

The character represented is

\

Print the string on Python’s interactive command line:

>>> print 'I\'m ok.'I'm ok.

If a string has a lot of characters that need to be escaped, add a lot

\

For simplicity, Python also allows use of

r”

said

‘ ‘

Internal strings are not escaped by default, you can try yourself:

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

If there are many line breaks inside the string, use

\n

It’s hard to read on a single line, so Python allows it for simplicity

“‘… ‘ ‘ ‘

The format represents multiple lines of content, you can try it yourself:

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

The above is the interactive command line input, if written as a program, is:

print '''line1

Multiline string

“‘… ‘ ‘ ‘

I could also add it out front

r

Use, please test yourself.

Boolean value

Boolean values and Boolean algebra are exactly the same representation, a Boolean value is only

True

,

False

Two values, either

True

, either

False

In Python, you can use it directly

True

,

False

Represents a Boolean value (case sensitive), which can also be computed by a Boolean operation:

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

Boolean values can be used

and

,

or

and

not

Operation.

and

An operation is and an operation only if everything is

True

.

and

The result of the operation is

True

:

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

or

An operation is an or operation as long as one of them is

True

.

or

And the result of that is

True

:

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

not

The operation is not an operation, it is a unary operator, the

True

become

False

.

False

become

True

:

>>> not TrueFalse>>> not FalseTrue

Booleans are often used in conditional judgments, such as:

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

A null value

A null value is a special value in Python

None

Said.

None

Not to be understood as

0

Because the

0

Is meaningful, and

None

Is a special null value.

In addition, Python provides lists, dictionaries, and other data types, as well as the ability to create custom data types, which we’ll cover later.

variable

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

A variable in a program is represented by a variable name, which must be uppercase, numeric, and

_

And cannot start with a number, such as:

a = 1

variable

a

It’s an integer.

t_007 = 'T007'

variable

t_007

It’s a string.

Answer = True

variable

Answer

Is a Boolean value

True

.

In Python, the equal sign

=

An assignment statement can assign any data type to a variable, the same variable can be repeatedly assigned, and can be different types of variables, for example:

A = 123 # a print a

Languages in which the variable itself is of variable type are called dynamic languages, as opposed to static languages. Static languages must specify the type of a variable when defining it, and an error will be reported if the assignment does not match the type. For example, if Java is a static language, the assignment statement looks like this (// represents a comment) :

int a = 123; // a is an integer type variable a = "ABC"; // Error: cannot assign strings to integer variables

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

Do not equate the equal sign of an assignment statement with the equal sign of mathematics. For example:

x = 10x = x + 2

If you think about it mathematically

x = x + 2

That’s not true anyway. In a program, assignment evaluates the expression on the right first, okay

x + 2

, get the result

12

, and assign to the variable

x

. Due to the

x

The previous value was zero

10

, after reassigning,

x

The value of the into

12

.

Finally, it is important to understand how variables are represented in computer memory. When we write:

a = 'ABC'

When, the Python interpreter does two things:

  1. One is created in memory

    ‘ABC’

    String;

  2. Creates a file in memory named

    a

    Variable and point it to

    ‘ABC’

    .

You can also put a variable

a

Assign to another variable

b

, this operation is actually a variable

b

Pointing to the variable

a

To point to data, such as the following code:

a = 'ABC'b = a

The last line prints out the variable

b

What is the content of

‘ABC’

or

‘XYZ’

? If you understand it mathematically, you get the wrong answer

b

and

a

Same thing, it should be

‘XYZ’

But in fact

b

The value is

‘ABC’

Let’s run the code line by line to see what happens:

perform

a = ‘ABC’

The interpreter creates the string

‘ABC’

And variables

a

And put the

a

Point to the

‘ABC’

:

perform

b = a

The interpreter creates the variable

b

And put the

b

Point to the

a

String to point to

‘ABC’

:

perform

a = ‘XYZ’

, the interpreter creates the string ‘XYZ’ and puts

a

Change the direction of

‘XYZ’

, but

b

Has not changed:

So, finally, print the variable

b

Of course

‘ABC’

.

constant

A constant is a variable that cannot be changed. For example, the mathematical constant π is a constant. In Python, constants are usually represented by all-caps variable names:

PI = 3.14159265359

But in fact

PI

Is still a variable, and Python has no mechanism to guarantee it at all

PI

Will not be changed, so using all uppercase variable names for constants is just a common usage if you must change the variable

PI

No one can stop you.

To finally explain why integer division is accurate, try:

>>> 10 / 33

You read that right. Integer division is always an integer, even if you can’t divide it all. To do the exact division, simply replace one of the integers with a floating-point number and do the division:

>>> 10.0 / 33.3333333333333335

Since integer division only takes the integer part of the result, Python also provides a remainder operation that yields the remainder of dividing two integers:

> > > 10% 31

Whether you divide an integer or take a remainder, the result is always an integer, so the result of an integer operation is always accurate.

The first group of letters is: Mengy, the second group is: 7762, and she will arrange to learn them in order. , whether you are big bull or small white, is want to change or want to enter the industry can come to understand progress together to learn! There are development tools, lots of dry goods and technical information to share!

summary

Python supports a variety of data types. Inside the computer, any data can be considered an “object”, and variables are used to refer to these data objects in the program. Assigning values to variables associates data with variables.