Python object type

Source: Internet
Author: User

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

All Python objects have three features: identity, type, and value.

Identity:

Each object has a unique identity to mark itself. The identity of any object can be obtained using the built-in function id. This value can be considered as the memory address of the object.

Type:

The object type determines what type of value the object can save, what operations it can perform, and what rules it follows. You can use the built-in function type () to view the Python object type:

>>> type([1,2<type >>>> type(24<type >>>> type({1,2,3<type >>>> type(<type >

Type () returns an object rather than a simple string.

Value: data items represented by objects

The above three features are assigned a value when an object is created. In addition, the other two features are read-only.

Standard Type/basic data type:

Number, integer, Boolean, long integer, floating point, plural, String, list, tuples, and Dictionary

Other built-in types:

Type, Null object (None), file, set/Fixed Set, function/method, module, Class

None: Python Null Object

Python has a special type, called a Null object or NoneType. It has only one value: None. It does not support any computation or any built-in method. It is similar to the void and None values in C language.

It is very similar to the Null value in C.

None has no useful attributes, 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 has a Boolean value of True or false.

The Boolean value of Null object, any number with a value of 0, or None of Null object is False.

The boolean values of the following objects are False:

  • None
  • False (Boolean)
  • Number of all values 0
  • 0 (integer)
  • (Floating point type)
  • 0L (long integer)
  • 0.0 + 0.0j (plural)
  • "" (Null String)
  • [] (Empty list)
  • () (Null tuples)
  • {} (Empty dictionary)

The Boolean value of the object whose value is not listed above is True.

Standard operators:

Comparison operators are used to determine whether objects of the same type are equal. All built-in types support comparison operations. Comparison operations return True or False values.

>>> 2==2>>> 2.34<=3.44>>> ==>>> >>>> <>>> [3,]==[,3>>> [3,]==[3,

Multiple comparison operations can be performed on the same row. The order of values is from left to right. For example:

>>> 3<4<5 >>> 4>3==3 >>> 4<3<5!=2<7

Comparison operators of standard values:

Object Identity comparison

Each object has a counter and records its own reference times. This number indicates how many variables point to this object

Python provides the is and is not operators to test whether two variables point to the same object.

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

>>> foo2=>>> foo1 >>> foo1  >>> id(foo1)==>>> 

Boolean Type

Boolean operators and, or, and not are all Python keywords. The priority of these operators is as follows:

Standard boolean operators:

>>> x,y=3.1415926,-1024>>> x<5.0>>> (x<5.0>>> (x<5.0)  (y>2.71828>>> (x<5.0)  (y>2.71828>>> (x 

-1 >>> 1 >>> B =-4 >>>>>> a, B =, >>> -1 >>> 1 >>> B >>>View Code

Str () and repr () (and ''operator)

The built-in functions str (), repr (), and quotation mark ('') allow you to conveniently obtain object content, type, numeric attributes, and other information in string mode.

The str () function makes the string readable, while the string obtained by the repr () function can be used to obtain the object again.

>>> Str (1 >>> str (2e10 >>> str ([, 9 >>> repr ([, 9 >>>View Code

Str () and repr () and ''operations are very similar in terms of features and functions. repr () and'' do exactly the same thing, returns the official string of an object. You can use the evaluate operation (using the eval () built-in function) to obtain the object again, but the str () function is different, it can generate a readable string representation of an object. The returned results cannot be used for eval () evaluation, but are suitable for print statement output.

 

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.