The object type of the Python introductory article

Source: Internet
Author: User
Tags logical operators
Python uses the object model to store data. Constructs any type of value that is an object

All Python objects have three properties: Identity, type, value

Identity:

Each object has a unique identity to flag itself, and any object's identity can be obtained using the built-in function ID (). This value can be considered to be the memory address of the object

Type:

The type of the object determines what type of value the object can hold, what it can do, and what rules to follow, using the built-in function type () to see the type of the Python object:

The code is as follows:


>>> type ([+])

>>> Type (24)

>>> type ({A-i})

>>> type (' A string ')

Type () returns an object instead of a simple string.

Value: The data item represented by the object

The above three attributes are assigned when the object is created, except that the other two features are read-only

Standard type/base data type:

Numbers, integers, booleans, long shapes, float, plural, string, list, tuple, dictionary

Other built-in types:

Type, null object (None), file, collection/fixed collection, function/method, module, class

Null object for None,python

Python has a special type, called a null object or Nonetype, with only one value: None, which does not support any operations or any built-in methods, similar to the values of the C-language void,none type

Very similar to the null value in C

None has a useful property, and its Boolean value is always False

Boolean value

All standard objects can be used for Boolean testing and can be compared between objects of the same type. Each object inherently has a Boolean true or False value

Null object, any number with a value of 0, or null object none of the Boolean values are false

The following objects have a Boolean value of false:

None
False (Boolean type)
All values are 0 of the number
0 (integral type)
(floating point type)
0L (Long integer type)
0.0+0.0j (plural)
"" (empty string)
[] (Empty list)
() (Empty tuple)
{} (Empty dictionary)
The Boolean value of the object that is not the value listed above is True

Standard type operators:

Comparison operators are used to determine whether objects of the same type are equal, all built-in types support comparison operations, and the comparison operation returns a Boolean value of TRUE or False

The code is as follows:


>>> 2==2
True
>>> 2.34<=3.44
True
>>> ' abc ' = = ' xyz '
False
>>> ' xyz ' > ' abc '
True
>>> ' xyz ' < ' abc '
False
>>> [3, ' abc ']==[' abc ', 3]
False
>>> [3, ' abc ']==[3, ' abc ']
True

Multiple comparison operations can be performed on the same line, with the order of evaluation from left to right. For example:

The code is as follows:


>>> 3<4<5 #等价于 (3<4) and (4<5)
True
>>> 4>3==3 #等价于 (4>3) and (3==3)
True
>>> 4<3<5!=2<7
False

Standard type value comparison operators:

Object Identity Comparison

Each object is born with a counter that records its own number of references. This number indicates how many variables point to the object

Python provides the IS and is not operator to test whether two variables point to the same object

A is B equivalent to ID (a) ==id (b)

The code is as follows:


>>> foo2=foo1
>>> Foo1 is Foo2
True
>>> Foo1 is not Foo2
False
>>> ID (foo1) ==id (FOO2)
True
>>>

Boolean type

The Boolean logical operators, And,or,not, are all Python keywords, and the precedence of these operators is from highest to lowest in the following order:

Standard Type Boolean operators:

The code is as follows:


>>> x,y=3.1415926,-1024
>>> x<5.0
True
>>> Not (x<5.0)
False
>>> (x<5.0) or (y>2.71828)
True
>>> (x<5.0) and (y>2.71828)
False
>>> not (x is Y)
True

Standard type built-in functions

Python provides some of the built-in functions for these basic object types:

CMP (), repr (), str (), type (), and the SLR ("') operator equivalent to the repr () function

Type () takes an object as a parameter and returns its type. Its return value is a type object.

The code is as follows:


>>> Type (4)

>>> type (' hello! ')

>>> type (Type (4))

CMP () is used to compare two objects obj1 and obj2, if Obj1 obj2 returns 1, if Obj1==obj2 returns 0, behaves like the strcmp () function in C, the comparison is made between objects

The code is as follows:


>>> a,b=-4,12
>>> CMP (A, B)
-1
>>> CMP (B,A)
1
>>> b=-4
>>> CMP (A, B)
0
>>> a,b= ' abc ', ' XYZ '
>>> CMP (A, B)
-1
>>> CMP (B,A)
1
>>> b= ' abc '
>>> CMP (A, B)
0

STR () and repr () (and the "operator")

The built-in function str () and the repr () or the inverse quote operator ("') can easily be used as strings to obtain information about the object's content, type, numeric properties, and so on.

The STR () function gets a good readability of the string, and the string obtained by the repr () function can often be used to regain the object

The code is as follows:


>>> Str (4.53-2J)
' (4.53-2j) '
>>> Str (1)
' 1 '
>>> Str (2E10)
' 20000000000.0 '

>>> STR ([0,5,9,9])
' [0, 5, 9, 9] '
>>> repr ([0,5,9,9])
' [0, 5, 9, 9] '
>>> ' [0,5,9,9] '
' [0,5,9,9] '

STR () and repr () and ' operations are very similar in terms of features and functions, repr () and ' doing things exactly the same, returning an official string representation of an object that can be re-obtained by an evaluation operation (using the eval () built-in function), but STR () The function is different, it can generate a good-readability string representation of an object, the returned result cannot be used for eval () evaluation, but is well suited for print statement output.

Standard type operators and built-in functions

  • Related Article

    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.