Python Basics (5)---integral type, string, list, tuple, dictionary built-in methods and file operation introduction

Source: Internet
Author: User

For Python, all things are objects, objects are created based on classes, objects inherit properties of classes, methods, and so on.

1.int

First, let's look at what functions the next int contains

#python3.xdir (int)#[' __abs__ ', ' __add__ ', ' __and__ ', ' __bool__ ', ' __ceil__ ', ' __class__ ', ' __delattr__ ', ' __dir__ ', ' __divmod__ ', ' __ Doc__ ', ' __eq__ ', ' __float__ ', ' __floor__ ', ' __floordiv__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __getnewargs_ ' _ ', ' __gt__ ', ' __hash__ ', ' __index__ ', ' __init__ ', ' __int__ ', ' __invert__ ', ' __le__ ', ' __lshift__ ', ' __lt__ ', ' __mod__ ', ' __mul__ ', ' __ne__ ', ' __neg__ ', ' __new__ ', ' __or__ ', ' __pos__ ', ' __pow__ ', ' __radd__ ', ' __rand__ ', ' __rdivmod__ ', ' __ reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __rfloordiv__ ', ' __rlshift__ ', ' __rmod__ ', ' __rmul__ ', ' __ror__ ', ' __round__ ' , ' __rpow__ ', ' __rrshift__ ', ' __rshift__ ', ' __rsub__ ', ' __rtruediv__ ', ' __rxor__ ', ' __setattr__ ', ' __sizeof__ ', ' __str_ _ ', ' __sub__ ', ' __subclasshook__ ', ' __truediv__ ', ' __trunc__ ', ' __xor__ ', ' bit_length ', ' conjugate ', ' denominator ', ' From_bytes ', ' imag ', ' numerator ', ' real ', ' to_bytes ']#Python 2.xdir (int)#[' __abs__ ', ' __add__ ', ' __and__ ', ' __class__ ', ' __cmp__ ', ' __coerce__ ', ' __delattr__ ', ' __div__ ', ' __divmod__ ', ' __ Doc__ ', ' __float__ ', ' __floordiv__ ', ' __format__ ', ' __getattribute__ ', ' __getnewargs__ ', ' __hash__ ', ' __hex__ ', ' __ Index__ ', ' __init__ ', ' __int__ ', ' __invert__ ', ' __long__ ', ' __lshift__ ', ' __mod__ ', ' __mul__ ', ' __neg__ ', ' __new__ ', ' __ Nonzero__ ', ' __oct__ ', ' __or__ ', ' __pos__ ', ' __pow__ ', ' __radd__ ', ' __rand__ ', ' __rdiv__ ', ' __rdivmod__ ', ' __reduce__ ', ' __reduce_ex__ ', ' __repr__ ', ' __rfloordiv__ ', ' __rlshift__ ', ' __rmod__ ', ' __rmul__ ', ' __ror__ ', ' __rpow__ ', ' __rrshift ' __ ', ' __rshift__ ', ' __rsub__ ', ' __rtruediv__ ', ' __rxor__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __sub__ ', ' __ Subclasshook__ ', ' __truediv__ ', ' __trunc__ ', ' __xor__ ', ' bit_length ', ' conjugate ', ' denominator ', ' imag ', ' numerator ', ' Real ']

# __abs__ ()  absolute output num = 1= num.__abs__()print=-1= Num.__abs__()print(Result)
__abs__ () absolute output
1 num = -12 result = num.__add__(2)print(result)#  Print Results output 1
__add__ Addition
1 num = result = num.__and__(2)print(result)# print output is 0#  0 0 0 0 0 1 0 1    5#  0 0 0 0 0 0 1 0    2# The same bit 1 is 1, because there is no same bit, so 5 & ; 2 Results of 0
__and__ and & Operations
1#The following result outputs are true2 num = 11 3Print(Num.__bool__())#True4 5 num =-11 6Print(Num.__bool__())#True7 8#The following result output is False9 num =010Print(Num.__bool__())#Falsenum =Nonenum =False14Print(Num.__bool__())#False
__bool__ Boolean value
# The divmod function allows you to divide an int type object by another int object to get a list of two elements. # The left side of the list is the value of the rounding , the second element is the remainder of the modulo  = 9= num.__divmod__(2)print(result)#  Output (4,1)
__divmod__ Division taking the whole modulus
num = 2= num.__eq__(3)print(result)# prints false  #2 = = 3   result is false  = num.__eq__(2)print(result)  # print result is true#  2 = = 2 result is true __eq__ = = comparison operator
__eq__ = = comparison operator
num = 9print (num.__float__())# Prints the result as 9.0
__float__ conversion to floating point number
num = Int (181= num.__floordiv__(9)print(result)#  PrintOut# floor except//take the whole __floordiv__ floor except //
__floordiv__ Floor except//
num = Int (181= num.__getattribute__("bit_length")Print  (Result)# Print Output <built-in method bit_length of int object at 0x100275020>#  indicates that the data type num exists bit_length This property, which can be used to determine whether an object has some property
__getattribute__ Get Object Properties
num = Int (181)print(num.__ge__(111))# print output True# because 181 is greater than 111, the result is true, and the property is used to determine the method that is greater than or equal to the property itself, and the result returns True, otherwise false
__ge__ comparison Operation >=
num = 181print (int.__invert__(num))# printout -182= -180 Print (int.__invert__(num))# print output 179= -181print (int. __invert__ (num)) # Print Output
__invert__ Non-operation
num = -181= num.__le__(111)print(result)# print output is true  # when the descendant parameter is compared to the object itself, the result is true if the object is less than or equal to the parameter of the descendant, otherwise false
__le__ less than or equal to
num = -181= num.__lshift__(1)print(result)# printout output is-362   , i.e. -181 * (2**1)  = num.__lshift__(2)print(result)# printed output is-724 , -181* (2**2)# When an incoming parameter is greater than or equal to 0 o'clock and the object itself cannot be 0, the first argument itself is an exponential power of 2, and then the result is multiplied by the object itself to move the final result to the left
__lshift__ left shift operation
num = -181print (num.__lt__(One-to-one# Prints the result to True# The General object is smaller than the passed in parameter, The result is true, otherwise the result is false
__lt__ less than
num = -181print (num.__mod__(3# Prints The result is 2, because 181 divided by 3 equals 60 and the remainder is 2, so the result is 2 
__mod__ modulo Operation
num = 181print (num.__mul__(2))# printout results of 362, which is the result of 181*2
__mul__ multiplication Operation
num = -181print (int.__neg__# Prints a result of 181, that is-(-181) and the result is 181
__neg__ unary operation subtraction
num = 181print (num__ne__(181))# Prints the result to falseprint(Num. __ne__ (one)) # Print result is true # All incoming parameters are not equal to the object itself, the result is true, otherwise false
__ne__ Not equal to comparison
num = print(num.__or__(7))# printout output is  0 0 0 1 0 0 1 0 18      #  0 0 0 0 0 1 1 1       7#  0 0 0 1 0 1 1 1       a decimal or an operation where the same bit is true, that is 1, the result is true, i.e. 1, then the final result is 23.
__or__ or | operations
num = 9print (num.__pow__(2))# The printout output is 81, which is 9**2
__pow__ Power Operation
num = 6print(num.__rdivmod__(3))# Returns the result (0,3) to the left is the remainder and the right is the result of an integer division
__rdivmod__ and Divmod return the opposite result
# python 2.7num = 1print (num.__sizeof__())# Prints the output as 24 bytes, Indicates that an int type defaults to 24 byte size in memory #python3.5num = 1print(num.__sizeof__( )# The printout results in 28 bytes, indicating that an int type data consumes 24 bytes in memory by default
__sizeof__ Compute data type takes up memory size
num = Int (1111= num.__str__()print(type result)#  Print output to <class ' str ' ># convert int to str data type
convert __str__ int to str
num = Int (9)print (num.__sub__(2))# print output is 7 # The object itself is subtracted from the incoming parameter, resulting in a final return value
__sub__ Subtraction Operation
num = oneprint (num.__truediv__(3))# prints out the result is 3.6666666666666665#  The returned data type is float, floating point type
__truediv__ really apart
num = tenprint(num.__xor__(6))#  0 0 0 0 1 0 1 0   c15> 0 0 0 0 0 1 1 0  6#  0 0 0 0 1 1 0 0  # The same bit compares, all 0 is False, 1 is false, one true false 
__xor__ xor ^ Operation
num = 5print(Num.bit_length ())# printout results 3#  0 0 0 0 0 1 0 1   # length 3 bit 
bit_length Display the placeholder length of the data
num = 2.3-2.5j= num.real       # Real part of the complex print (Result)   # printout 2.3 result = Num.imag    # The imaginary part of the complex number print (result)   # printout 2.5j = Num.conjugate () # Returns the complex's conjugate complex print (result) #          Print output (2.3+2.5j)
conjugate
num = 5print(num.__format__(")")#  Indicates 5 front speech with 20 spaces
__format__ formatted output
Print (Int.from_bytes (bytes=b'1', byteorder='little' # print output  , convert character 1 to decimal   
from_bytes character Conversion decimal
num = 2= num.to_bytes (5,byteorder='little')print(Result )# print output B ' \x02\x00\x00\x00\x00 ' for in result:      Print(i)# printout 2\n0\n0\n0\n0#\ n = Enter
convert to_bytes int to byte

Python Basics (5)---integral type, string, list, tuple, dictionary built-in methods and file operation introduction

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.