Python introductory article object type _python

Source: Internet
Author: User
Tags builtin eval logical operators object model readable

Python uses an object model to store data. Constructing any type of value is an object

All Python objects have three attributes: 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 object determines what type of value the object can hold, what it can do, and what rules to follow, and you can use the built-in function type () to view the type of the Python object:

Copy Code code as follows:

>>> type ([1,2])
<type ' list ' >
>>> Type (24)
<type ' int ' >
>>> type ({1,2,3})
<type ' Set ' >
>>> type (' A string ')
<type ' str ' >

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

Value: The data item represented by an object

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

Standard type/Underlying data type:

Numbers, integers, Boolean, long shape, floating-point, plural, string, list, tuple, dictionary

Other built-in types:

types, NULL objects (None), files, collections/fixed sets, functions/methods, modules, classes

None,python null Object

Python has a special type, called a null object or Nonetype, with only one value: None, it does not support any operations, and there is no built-in method, similar to the C-language Void,none type value

is very similar to the null value in C

None has any useful properties, 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 is born with a Boolean true or False value

A null object, any number with a value of 0, or none of the null objects is a Boolean value of False

The following objects have Boolean values that are false:

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

Standard type operator:

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

Copy Code code 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, and evaluated in order from left to right. For example:

Copy Code code 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 are pointing to the object

Python provides the IS and are not operators to test whether two variables point to the same object

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

Copy Code code as follows:

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

Boolean type

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:

Copy Code code 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 built-in functions for these basic object types:

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

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

Copy Code code as follows:

>>> Type (4)
<type ' int ' >
>>> type (' hello! ')
<type ' str ' >
>>> type (Type (4))
<type ' type ' >

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

Copy Code code 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 "operator")

The built-in function str () and repr () or inverted quotation operators (") can easily obtain information about the object's content, type, numeric properties, and so on as a string.

The STR () function gets the string readable and the repr () function gets a string that can often be used to regain the object

Copy Code code 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 feature and function, repr () and "do exactly the same thing, return an official string representation of an object that can be returned by the evaluation operation (using the eval () built-in function), but STR () function is different, it can generate an object's readable string representation, the return result cannot be used for eval () evaluation, but it is good for print statement output.

Standard type operators and BUILTIN 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.