Presentation of data in Python. The concept of the object. Identity, type, value of the object. The functions id(), type(). Operators is, is not
- 1. Presentation of data in the programming language Python. Object concept
- 2. How can you present data as objects?
- 3. What are the benefits of using built-in Python objects in comparison with your own designed objects?
- 4. What are the characteristics of each object?
- 5. What is the identity of the object?
- 6. How to determine the value of identity for this object? The id() function. Example
- 7. How to compare two objects for identity? Operators is and is not. Examples
- 8. What is an object type in Python?
- 9. How to get object type? The function type(). Examples
- 10. How are the values of objects formed? Assigning values to objects. Examples
- Related topics
Search other websites:
1. Presentation of data in the programming language Python. Object concept
Data in Python are represented as objects or relations between them. An object is a memory area that contains some data value and a set of operations associated with it. An object is an abstraction used to represent data in the Python language. An object is a fundamental concept in the Python programming language (and not only in Python). The object is synonymous with the word instance, which contains some data value. Any operations (add, subtract, etc.) in Python are performed on objects.
2. How can you present data as objects?
Data in the form of objects can be represented in two ways:
- embedded objects that are provided by the Python language;
- own created objects using Python language constructions or extension libraries.
3. What are the benefits of using built-in Python objects in comparison with your own designed objects?
Using embedded objects provides the following benefits:
- programs development is simplified. No extra resources are spent on developing your own complex objects. Enough to use the existing powerful built-in tools of the Python language;
- reduces the number of errors on the development of your own objects, since the program code of embedded objects has been reliably tested;
- high enough speed from processing collections, lists, tables, etc. is provided. This is explained by the fact that the program code of embedded objects is maximally optimized;
- the ability to develop your own complex objects based on the built-in classes of the Python language or C interfaces. This, in turn, facilitates the extensibility of already existing code;
- embedded objects are implemented in such a way that they are a standard and immutable part of the Python language. This gives an advantage in convenience and high efficiency of their use in programs. Own developed objects tend to change from one case to another.
4. What are the characteristics of each object?
Each object has such characteristics as:
5. What is the identity of the object?
The object’s identity is a unique and constant integer (constant) number, which is set for this particular object. An object’s identity is set only once when an object is created. The identity of a given object never changes after its creation. The identity of the object is associated with the address of the object in memory.
6. How to determine the value of identity for this object? The id() function. Example
The id() function is designed to get the value of the object’s identity. The function returns an integer that is unique and constant for this object.
>>> a=5 >>> id(a) 1752381760 >>> pa=id(a) >>> pa 1752381760 >>> id(5) 1752381760 >>> id(6) # integer value 6 is also an object 1752381776 >>> id(5) # integer value 5 is also an object 1752381760 >>> id(a) # object a is identical to object 5 1752381760
In the above example, the identity value is defined for objects with the names a, pa, integer 6, integer 5. As you can see from the example, numbers are also objects. Identity values for different names may be the same.
7. How to compare two objects for identity? Operators is and is not. Examples
With the help of the operators is and is not you can compare the value of objects on identity.
Example. The values of different objects on identity are compared using the is and is not operators.
>>> # comparison of objects on identity >>> a=3 >>> b=5 >>> a is b False >>> >>> a=5 >>> a is b True >>> >>> a 5 >>> b 5 >>> >>> b = 7 >>> a is b False >>> a is not b True
8. What is an object type in Python?
In Python, every object has a specific type. The type of the object determines:
- set (set) of operations that are supported by this object;
- possible values for objects of this type.
After creating an object of a certain type, the type of this object is considered unchanged. However, in Python, there are means to change the type of the object.
9. How to get object type? The function type(). Examples
Using the type() function, you can get the type of a specific object.
Example 1. Getting information about the type of an integer object
>>> a=10 >>> ta = type(a) >>> ta <class 'int'>
As can be seen from the example, the integer object a, which received the value 10, has the type
Example 2. Getting information about the type of object that contains floating point values.
>>> b = 3.85 >>> tb = type(b) >>> tb <class 'float'>
Example 3. Getting information about the type of object c, which is a string of characters
>>> c = "abcd" # string of characters >>> c 'abcd' >>> tc = type(c) >>> tc <class 'str'> >>> type(c) <class 'str'>
10. How are the values of objects formed? Assigning values to objects. Examples
To change the value of an object, you need to execute the assignment operator ‘=’. The assignment operator is used to set the object to some value.
In general, assigning a value to an object is as follows.
objName = value
here value – the value that is assigned to the object named objName.
The assignment operator in Python has several variations and is described in another topic:
An example of setting values for simple objects.
>>> x=5 >>> y=8 >>> z = x*y # use of an assignment operation in an expression >>> x 5 >>> y,z (8, 40) >>> x,y,z = 9,12,23 # assignment of three values at once >>> x,y,z # output x, y, z values simultaneously (9, 12, 23) >>> M=[5,12] >>> M 5 >>> M 12
- Assignment operator. Assignment forms. Examples. Positional assignment of tuples, lists
- Working with the Python editor. General issues. Interactive and program modes