Python entry object type and python entry object

Source: Internet
Author: User
Tags python list

Python entry object type and python entry object

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:

Copy codeThe Code is 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 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.

Copy codeThe 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 row. The order of values is from left to right. For example:

Copy codeThe Code is as follows:
>>> 3 <4 <5 # equivalent to (3 <4) and (4 <5)
True
>>> 4> 3 = 3 # equivalent to (4> 3) and (3 = 3)
True
>>> 4 <3 <5! = 2 <7
False

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)

Copy codeThe Code is as follows:
>>> Foo2 = foo1
>>> Foo1 is foo2
True
>>> Foo1 is not foo2
False
>>> Id (foo1) = id (foo2)
True
>>>

Boolean Type

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

Standard boolean operators:

Copy codeThe 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 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 () accepts an object as a parameter and returns its type. Its return value is a type object.

Copy codeThe Code is as follows:
>>> Type (4)
<Type 'int'>
>>> Type ('Hello! ')
<Type 'str'>
>>> 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, the behavior is similar to the strcmp () function in C, and the comparison is performed between objects.

Copy codeThe Code is as follows:
>>> A, B =-4, 12
>>> Cmp (a, B)
-1
>>> Cmp (B,)
1
>>> B =-4
>>> Cmp (a, B)
0
>>> A, B = 'abc', 'xyz'
>>> Cmp (a, B)
-1
>>> Cmp (B,)
1
>>> B = 'abc'
>>> Cmp (a, B)
0

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.

Copy codeThe Code is as follows:
>>> Str (4.53-2j)
'(4.53-2j )'
>>> Str (1)
'1'
>>> Str (2e10)
'2014. 0'

>>> Str ([0, 5, 9, 9])
'[0, 5, 9, 9]'
>>> Repr ([0, 5, 9])
'[0, 5, 9, 9]'
>>> '[, 9]'
'[,]'

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.

Standard operators and built-in functions


How to Understand the instances of class objects in python programming?

Class is the generalization of a class of things, such as people.
The data type includes built-in strings, numbers, plural numbers, and other custom classes.
Objects and instances are specific things in the class, such as men, women, and others. Here men and women can also be one type, such as older men, young man.
Remember that a class is a collective term of a type of thing, and an instance (or object) is a specific thing.
For reference only.
Example:
Class Person:
'''Basic attributes of a person: name, age, and Gender '''
Def _ init _ (self, name, age, sex ):
Self. name = name
Self. age = age
Self. sex = sex

Class Man (Person ):
Def _ init _ (self, name, age ):
Super (Man, self). _ init _ (name, age, 'male ')

Class Woman (Person ):
Def _ init _ (self, name, age ):
Super (Woman, self). _ init _ (name, age, 'female ')

What type of objects can be in the python list, and can it be a set of custom functions?

Yes

Def makeActions ():
Acts = []
For I in range (5 ):
Acts. append (lambda x, I = I: I ** x)
Return acts
Acts = makeActions ()
Print acts [0] (2)
Print acts [1] (2)
Print acts [2] (2)
 

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.