Basic Python type, Python type

Source: Internet
Author: User

Basic Python type, Python type
Python is a dynamic language that implements interpretation. All errors are generated during runtime. Even if errors and exceptions exist, there will be no errors as long as they are not executed. For example, a non-existent method is called; the type is implicit, that is, the variable type declaration is not required; the type is dynamic, and the type is determined based on the Content pointed to by the variable during runtime, but Python is a strong type language, that is, each variable has a type. Python basic built-in types mainly include numerics, sequences, mapping, files, classes, instances, exceptions. Operations of the types include comparison, true or not, and toString conversion, in Python, str/repr (object) can be converted to a string. in print (object), str () is called implicitly ().
Numerics: integer int, implemented by long in C language, value range-sys. maxint-1 ~ Sys. maxin, no sys. minint long integer long, integer with L/l or out of integer Range, print will carry the suffix L, No Precision Limit, infinite big, so Python is a signed number, there is no float of the unsigned type, which is implemented using double in c, sys. float_info. Therefore, Python does not have single-and double-precision distinguishing between complex and z. real, z. imag

Operation Result Notes
X + y SumXAndY  
X-y DifferenceXAndY  
X * y ProductXAndY  
X/y QuotientXAndY (1)
X // y (Floored) quotientXAndY (4) (5)
X % y RemainderX/y (4)
-X XNegated  
+ X XUnchanged  
Abs (x) Absolute value or magnqueueX (3)
Int (x) XConverted to integer (2)
Long (x) XConverted to long integer (2)
Float (x) XConverted to floating point (6)
Complex (re, im) A complex number with real partRe, Imaginary partIm.ImUlts to zero.  
C. conjugate () Conjugate of the complex numberC. (Identity on real numbers)  
Divmod (x, y) The pair(X // y, x % y) (3) (4)
Pow (x, y) XTo the powerY (3) (7)
X ** y XTo the powerY (7)
Different types of numerics can be mixed, and the rule is similar to c, that is, small scale transformation to a large range, int <long <float <complex division/: the result is always an integer, even if the divisor and divisor are not integers, and the result always tends to be negative infinity,-1/2 =-1 0 0 power: pow () = 1, 0 ** 0 = 1 NaN: not a number, INF: infinite,-inf + inf, float ('nan ') float (' + inf ') float ('-inf ') int (), long () both are downward transformations. The corresponding real int long float can also be selected in the following ways:
Operation Result Notes
Math. trunc (x) XTruncated to Integral  
Round (x [, n]) XRounded to n digits, rounding ties away from zero. If n is omitted, it defaults to 0. Rounding
Math. floor (x) The greatest integral float <=X  
Math. ceil (x) The least integral float> =X  

Boolean: used for condition determination after if/while. True: True if not False. False if not. False if not, False if not. False if not, False if not. Number Type: 0. Empty Container, including empty string '', class _ nonzero _ () or _ len _ return 0 or False instance bool OPERATOR: or and not, follow short-circuit similar to java/c, not has a lower priority than non-Boolean operator. not a = B is equivalent to not (a = B)
Comparison operator: This operator is also used for comparison of all types. It has a higher priority than Boolean operator and supports the expression x <y <z, x <y <z equivalent x <y and y <z and the former y is calculated only once, all follow the short circuit principle; comparison results of different types of objects are False, unless it is a different type of number or string comparison, for example, 0 = 0L, 'abc' = u'abc' returns True
Operation Meaning Notes
< Strictly less  
<= Less than or equal  
> Strictly greater  
> = Greater than or equal  
= Equal  
! = Or <> Not equal (1)
Is Object identity  
Is not Negated object identity  

Bitwise operation: bitwise operations only make sense for integer operations. bitwise operations have lower priority than numeric operators, but are higher than comparison operators ;~ The priority is the same as that of other unary operators (+,-). In the following table, the priority ranges from low to high. A ValueError exception is thrown when a negative number is shifted.
Operation Result Notes
X | y BitwiseOrOfXAndY  
X ^ y BitwiseExclusive orOfXAndY  
X & y BitwiseAndOfXAndY  
X <n XShifted leftNBits (1) (2)
X> n XShifted rightNBits (1) (3)
~ X The bitsXInverted  
Int. bit_length (): Get int bit to indicate long length. bit_length (): gets a long bit string that represents the length of 1, that is, there is no single character string: single quotation marks 'abc' or double quotation marks 'abc "or three consecutive single/double quotation marks ''' indicate multi-line strings. Strings can be understood as constant byte arrays or byte containers, similar to the String in Java, you cannot change the String pointed to by the variable, s = 'abc'; id (s) = id ('abc '). Common Operations on strings: Length: containers are all separated by len (). substrings: Container fragment operators [] 'abcd' [] = 'bc: split/rsplit search/replace: find/rfind does not find return-1; index/rindex does not find throwing ValueError, replace trim: strip/lstrip/rstrip encoding/decoding: only str ('han') can be decoded on str '). decode ('utf-8'), only Unicode encoded u ('han '). encode ('utf-8') case-insensitive conversion: lower/uper judgment: isalnum/isalpha/isdigit/islower/isspace/isupper/startwith/endwith formatting: % + tuple/dict, similar to C sprintf, one parameter '% d' % 1 = '1'; two parameters' % s, % s' % ('A', 'B') = 'a, B '; it refers to the placeholder % (mapping key) + type characters. The ing key must be enclosed by' % (key1) s, % (key2) D' % {'key1 ': 'A', 'key2': 1} = 'a, 1'

Python is easy to get help: help (object): Display help information dir (object): display all methods object. _ doc _: Show document


Python Data Type

CS is a reference.
The difference between reference and pointer should be clear: the former is the memory alias, and the latter is the storage variable of the memory address.
CS is a reference. To return the C language, you must use pointer variables instead of pointer variables.
The pointer should accept id (CS ).
* The pointer accepts cs.

Appendix: actual operation of the ID function
>>> Cd
'Abcdef'
>>> Id (cd)
35255712

A simple question about the python numeric type

Use the built-in function id to identify the returned object:

>>> A = 1
>>> Id ()
31118360L
>>> A = 2
>>> Id ()
31118336L
>>> B = []
>>> Id (B)
44908360L
>>> B. append (1)
>>> Id (B)
44908360L
>>> A = "hello"
>>> Id ()
44919944L
>>> A + = "world"
>>> Id ()
44958608L

It can be seen that for integers and strings, changing the value actually means that the Object id has changed, that is, the variable name is bound to the new object.
For a list, you can change its value.

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.