Python provides some built-in functions for basic object types: cmp (), repr (), str (), type () and () Operators equivalent to repr, this article will share with you the standard built-in functions of Python notes. If you are interested in the knowledge of python built-in functions, learn about them. Python provides some built-in functions for basic object types: cmp (), repr (), str (), type () and ('') Operators equivalent to repr ()
(1) type ()
The usage of type is as follows:
Type (object)
Accept an object as a parameter and return its type. The returned value is a type object.
>>> Type ('R ')
>>> Type (3)
>>> Type (5 ))
(2) cmp ()
The built-in function cmp () is used to compare two objects, obj1 and obj2. If obj1 is greater than obj2, a positive integer is returned. If it is smaller than, a negative integer is returned. If it is equal to, 0 is returned.
>>> A, B = 4, 12
>>> Cmp (a, B)
-1
>>> B = 4
>>> Cmp (a, B)
0
>>> A, B = 'xyz', 'abc'
>>> Cmp (a, B)
23
(3) str (), repr ()
You can easily obtain the object content, type, numeric attributes, and other information in string mode. The str () function makes the string readable, while the string obtained by repr () can be used to obtain the object again.
>>> Str (1)
'1'
>>> Str (2e4)
'2014. 0'
>>> Repr ([0, 1, 2, 3])
'[0, 1, 2, 3]'
Next, let's take a moment to introduce the built-in functions in python.
In python, enter the following command to view the python built-in function dir:
>>> Dir (_ builtins __)
1. str (), repr (), and ''Operators
The built-in functions 'str' (), repr (), and 'can be used to conveniently obtain the object's
Content, type, numeric attribute, and other information. The str () function makes the string readable, while the repr () function produces the character
A string can be used to obtain the object again. Generally, the equation obj = eval_r (repr (obj) is true.
These two functions take an object as its parameter and return an appropriate string. In the following example
Some Python objects to view their string representation.
>>> 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, 9])
'[0, 5, 9, 9]'
>>>
>>> '[0, 5, 9, 9]'
'[0, 5, 9, 9]'
Although str (), repr () and ''operations are very similar in terms of features and functions, in fact repr () and'' do exactly the same thing, they return an object's "official" string representation, that is, in most cases
You can use the evaluate operation (using the eval_r () built-in function) to obtain the object again, but str () is different. Str () is used to generate a readable string representation of an object. Its returned results cannot be used for eval_r () evaluation, but are suitable
Print statement output. Note that not all strings returned by repr () can use the val_r () built-in function to obtain the original object:
>>> Eval_r ('Type (type ))')
File" ", Line 1
Eval_r ('Type (type ))')
^
SyntaxError: invalid syntax
That is to say, repr () outputs are friendly to Python, while str () outputs are friendly to humans. Even so,
In many cases, the output of the three items is still the same.
Core notes: why do we still need ''with repr ''?
In Python learning, you occasionally encounter an operator doing the same thing as a function. This is because functions are more suitable for use than operators in some scenarios. For example, when processing executable objects like functions or calling different functions based on different data items, functions are easier to use than operators. Another example is the multiplication method of the double star number (**) and the built-in function of pow (). x ** y and pow (x, y) both execute the y Power of x.
Note: in fact, the Python community does not encourage the continued use of the ''operator.