Data type
A computer is a machine that can do mathematical calculations as the name implies, so a computer program can handle a variety of values. However, the computer can handle far more than the numerical value, but also can deal with text, graphics, audio, video, web 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:
Integer
Python can handle integers of any size, including, of course, negative integers, which are represented in the program in the same way as mathematically, for example:,,, 1
100
, and so on -8080
0
.
Because the computer uses binary, it is sometimes convenient to use hexadecimal notation for integers, and hexadecimal is 0x
represented by prefixes and 0-9,a-f, for example: 0xff00
,, and 0xa5b4c3d2
so on.
Floating point number
Floating-point numbers, which are decimals, are called floating-point numbers because, when represented by scientific notation, the decimal position of a floating-point number is variable, for example, 1.23x109 and 12.3x108 are equal. Floating-point numbers can be written in mathematical notation, such as,, 1.23
3.14
, and -9.01
so on. But for very large or very small floating-point numbers, it must be expressed in scientific notation, the 10 is replaced with E, 1.23x109 is 1.23e9
, or 12.3e8
, 0.000012 can be written 1.2e-5
, and so on.
Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.
String
strings are arbitrary text enclosed in "or", for example ‘abc‘
, and "xyz"
so on. Note that the "or" "itself is only a representation, not part of a string, so the string ‘abc‘
is a
only b
, c
this 3 characters. If it ‘
is also a character, it can be enclosed in "", for example, the "I‘m OK"
containing character is I
,,, a space, which is ‘
m
O
K
6 characters.
What if the inside of a string contains ‘
and contains both "
? Can be identified by an escape character \
, such as
' i\ ' m \ "Ok\"! '
The string content represented is:
I'm "OK"!
An escape character \
can escape many characters, such as a \n
newline, a \t
tab, and the character itself, \
so \\
the character that is represented is \
You can use the print string in Python's interactive command line to see:
' i\ ' m OK. ' I ' m OK. ' i\ ' m learning\npython. ' I ' M Learning Python. ' \\\n\\ '
If there are a lot of characters in the string that need to be escaped, you need to add a lot \
, in order to simplify, Python also allows to use r‘‘
‘‘
A string that represents the inside of the default is not escaped, you can try it yourself:
' \\\t\\ ' \ >>> print R'\\\t\\'\\\t\\
If there is a lot of line wrapping inside the string, \n
writing in a row is not good to read, in order to simplify, Python allows ‘‘‘...‘‘‘
a format to represent multiple lines of content, you can try it yourself:
" " line1 ... line2 line3 " " Line1line2line3
The above is entered in the interactive command line, if written as a program, that is:
" " line1 Line2line3 " "
Multi-line strings ‘‘‘...‘‘‘
can also be used in front of each other r
, please test yourself.
Boolean value
A Boolean value is exactly the same as a Boolean algebra, with a Boolean value of only True
False
two values, either, True
or, False
in Python, you can directly use True
and False
represent a Boolean value (note case). It can also be computed by Boolean operations:
>>> truetrue>>> falsefalse32True3 5False
Boolean values can be used and
, or
and not
operations.
and
Operations are and operations, and only all are True
, the result of the and
operation is True
:
>>> True and Truetrue>>> True and Falsefalse>>> False and Falsefalse
or
An operation is or an operation, as long as one of them is True
, the result of the or
operation is True
:
>>> True or TrueTrue>>> True or FalseTrue>>> False or FalseFalse
not
An operation is a non-operation, which is a single-mesh operator that turns True
into False
False
True
:
>>> not TrueFalse>>> not FalseTrue
Boolean values are often used in conditional judgments, such as:
if age >= 18: print ‘adult‘else: print ‘teenager‘
Null value
A null value is a special value in Python, None
denoted by. None
cannot be understood as 0
, because 0
it is meaningful, and None
is a special null value.
In addition, Python provides a variety of data types such as lists, dictionaries, and also allows you to create custom data types, which we'll continue to talk about later.
Variable
The concept of variables is basically the same as the equation variables in junior algebra, but in computer programs, variables can be not only numbers, but also arbitrary data types.
Variables are represented by a variable name in the program, and the variable name must be a combination of uppercase and lowercase English, a number, and _
cannot start with a number, such as:
a = 1
a
A variable is an integer.
t_007 = ‘T007‘
t_007
A variable is a string.
Answer = True
Answer
A variable is a Boolean value True
.
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, for example:
123 # a是整数print aa = ‘ABC‘ # a变为字符串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; // a是整数类型变量a = "ABC"; // 错误:不能把字符串赋给整型变量
This is why dynamic languages are more flexible than static languages.
Do not equate an equal sign of an assignment statement with a mathematical equal sign. For example, the following code:
x = 10x = x + 2
If mathematically understood x = x + 2
that is not tenable in any case, in the program, the assignment statement evaluates the right expression first x + 2
, obtains the result 12
, and assigns the variable x
. Since x
the previous value is 10
, the value becomes after the value is re-assigned x
12
.
Finally, it is important to understand the representation of variables in computer memory. When we write:
a = ‘ABC‘
, the Python interpreter did two things:
A string is created in memory ‘ABC‘
;
A variable named in memory is created a
and pointed to ‘ABC‘
.
You can also assign a variable a
to another variable, which actually refers to the data that the variable points to b
b
a
, such as the following code:
‘ABC‘b = aa = ‘XYZ‘print b
The last line prints out b
the contents of the variable. ‘ABC‘
‘XYZ‘
If it is mathematically understood, it will be wrong to draw the b
same, and it a
should be ‘XYZ‘
, but b
the actual value is ‘ABC‘
, let us execute the code in one line, we can see exactly what happened:
Execution a = ‘ABC‘
, the interpreter creates a string ‘ABC‘
and a variable a
, and a
points to ‘ABC‘
:
Constant
So-called constants are immutable variables, such as the usual mathematical constants π is a constant. In Python, constants are typically represented in all uppercase variable names:
PI = 3.14159265359
But in fact PI
it is still a variable, Python does not have any mechanism to ensure that it PI
will not be changed, so, using all uppercase variable names to represent constants is only a customary usage, and if you must change PI
the value of the variable, no one can stop you.
Finally explain why the division of integers is also accurate, you can try:
>>> 10 / 33
You are not mistaken, integer division is always an integer, even if it is not endless. To do the exact division, simply replace one of the integers with a floating-point number to do the division:
>>> 10.0 / 33.3333333333333335
Because integer division takes only the integer part of the result, Python also provides a remainder operation that gives the remainder of dividing two integers:
>>> 10 % 31
The result is always an integer, regardless of whether the integer divides or takes the remainder, so the result of the integer operation is always accurate.
Summary
Python supports a variety of data types, within a computer, you can think of any data as an "object", and variables are used in the program to point to these data objects, the variable is assigned to the data and variables to associate.
Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 0013747381369301852037f35874be2b85aa318aad57bda000
Five Python basic data types and variables