Python learning notes: data type — number type

Time:2020-11-22

Number type

Numerical type, which is usually used to store values, is the most commonly used variable type;
The numeric types in Python include the following types:IntegerLong integerBooleanDouble-precision floatingComplex number

Integer
PS novice note: many people in the process of learning Python will encounter a variety of vexed questions, no one answers easy to give up. For this small editor built a python stack free Q & A. skirt: seven clothes, nine seven seven bar and five (the number of homophony) conversion can be found, do not understand the problem has the old driver to solve, there is the latest Python practical course, no need to go down, supervise each other and make progress together!

It’s equivalent to Clong;
32 bit machine, integer bit width 32 bit:- 2^31 ~2^31 - 1, i.e-2 147 483 648 ~ 2 147 483 627
64 bit machine, integer bit width is 64 bit:- 2^63 ~2^63 - 1, i.e-9 223 372 036 854 775 808 ~ 9 223 372 036 854 775 807

A = 12 ා positive integer
B = - 405 ා negative integer
C = 0x3a ා a positive integer expressed in hexadecimal

Long integer long integer(ාාාාාාාාා3. X has cancelled long type and renamed to int)

Values are followed bylorL(case L, not numbers1Confusion) end.
There is no specified bit width for long integers, and python does not limit the size of long integers. However, in actual development, the memory of the machine is limited, so there is no infinite long integer

a = 3897987L
b = -524327987l
c = 0xA32B9DDL

Boolean Boolean

Judgment use, only two valuesTrueandFalse

a = False
b = True

Double precision floating

It can be expressed in direct decimal or scientific notation. Each floating-point number is 8 bytes.
Floating point values usually have a decimal point and an optional suffixe(both case and case are acceptable).
stayeA positive value can be used between and the index(+)Or negative(-)Indicates positive and negative index.

a = 1.32425
b = -40.
c = 3e8

Complex number

It can refer to a false value or a number that does not indicate a specific quantity.
The complex number consists of real part and imaginary part, and the imaginary part must have suffixjorJ

a = 98.234 + 3J
b = 0 - 23.4j


Operator

Arithmetic operators – only the definitions different from other languages are recorded

Operator meaning
A/B Division: B cannot be 0; integer division is still integer
A//B Division by Division: the result is the integral part of the result a divided by B
A**B Power operation: B power of a
not A Negation: can only be used for boolean type

>>> 5 / 2
2
>>> 5.0 / 2 
2.5
>>> 5.2 // 2 
2
>>> 2 ** 3
8
>>> not True
False

Bit operator

Operator meaning
~A According to the complement rule, the result is-(A+1)
A&B Parallel operation: only two bits are both1Only when1, otherwise0
A|B Or operation: as long as one of the two bits is1Set when1Otherwise, set0
A^B XOR operation: when two bits are in the same phase, the result is set1Otherwise, set0
A>>B A shifts B bit right by bit
A<
A shifts B bit left by bit

>>> ~ 30
-31
>>> 3 & 3
3
>>> 3 & 1
1
>>> 3 ^ 1
2
>>> 3 << 1
6


Built in functions

General function

>>>CMP (43, 12.34) ා compare size: the former small return - 1, the latter small return 1, equal return 0
-1
>>> cmp( 0 , -2)
1
>>> cmp(0x12 , 18)
0
>>>Str (0x20) ා converts parameters to string type and outputs
32
>>>Type (4L) ා 3. X version has cancelled the long type and renamed to int
'long'>
>>> type( 45 + 8j )
'complex'>

Numeric type specific functions

>>> abs( -3 )
3
>>>Coerce (24, - 4.5) ා 3.0 has removed the use of coerce
(24.0 , -4.5)
>>>% 2 is a tuple (bivma), generating a mod
( 2 , 1 )
>>>Round (5) is returned
6
>>>Hex (10) ා converts an element to a string in hexadecimal (oct() octal)
'0xA'
>>>Chr (0x32) ා converts the element to ASCII character, and the elements in brackets are between 0 and 255
'2'
>>>Pow (10,2) ා power operation: B power of a
100


Sequence type cluster

In addition to the basic numerical types, the sequence type cluster in Python plays an important role in Python programming. String in Python(String), tuple(Tuple), list(List)All belong to sequence type cluster.

  • String: a sequence type of characters
  • Tuple: an unmodifiable sequence type of any object
  • List: a modifiable sequence type of any object

Operator

Note: many people will encounter a variety of vexation problems in the process of learning python, and it is easy to give up if no one answers. For this small editor built a python stack free Q & A. skirt: seven clothes, nine seven seven bar and five (the number of homophony) conversion can be found, do not understand the problem has the old driver to solve, there is the latest Python practical course, no need to go down, supervise each other and make progress together!

The text and pictures of this article are from the Internet and my own ideas. They are for study and communication only. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.