directory

  • Introduction to the Python bin function
  • The Python bin function syntax
  • Use the Python bin function
  • Four. Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

Introduction to the Python bin function

The bin function is a Python built-in function that returns a binary representation of an int or long int.

Some of you may ask:

1. Integer (int) or long integer (long int) difference ** : The difference between the two is that the value range is different, for example: **

  • Integer value range: -32768 to 32767.
  • The value of long integer ranges from -2147483648 to 2147483647. If a number is greater than 32767, the value can only be long integer.

2. What is binary: Binary data is numbers represented by 0 and 1. Its base is 2, the carry rule is “every two into one”, the borrowing rule is “borrow one when two”, specific can be Baidu, relatively simple;

3. Binary calculation: from right to left, the first digit represents 2 to the 0 power, the second digit represents 2 to the 1 power, and the NTH digit represents 2 to the n-1 power. You can interpret 1 as yes and 0 as none. For example, 01101 = 1*2^0 + 0*2^1 + 1*2^2 + 1*2^3 + 0*2^4 = 13.

If __name__ = = "__main__" : value = 1 (2, 0) + 0 * * pow pow (2, 1) (2, 2) + 1 + 1 * pow (2, 3) + 0 * * pow pow (2, 4) print # 13 (value)Copy the code

The Python bin function syntax

Parameter: num - Integer or long integer. No other type is supported; Return value: Returns the binary representation of an integer or long integer; ''' bin(number)Copy the code

Use the Python bin function

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python bin function. py @time :2021/04/28 07:37 @Motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated unrelentingly! "" "if __name__ = = "__main__" : print (bin) (1) print (bin (10)) print (bin) (20) # error writing, does not support floating-point number # TypeError: 'Float' object cannot be interpreted as an integer # bin(2.3) 'Copy the code

Code analysis: The first two characters “0b” indicate that the data type is binary, followed by “0b” binary data. From right to left, the first character represents 2 to the 0 power, the second character represents 2 to the 1 power, and the NTH character represents 2 to the n-1 power. 1 is understood as yes, and 0 is understood as none. This allows the reverse derivation of binary to decimal data;

Four.Guess you like

  1. The Python for loop
  2. The Python string
  3. The Python list
  4. The Python tuple tuple
  5. Python dictionary dict
  6. Python conditional derivations
  7. Python list derivations
  8. Python dictionary derivations
  9. Python function declarations and calls
  10. Python variable argument *argc/**kargcs
  11. Python anonymous function lambda
  12. Python return logic determines expressions
  13. Python string/list/tuple/dictionary conversions
  14. Python local and global variables
  15. The Python type function is different from the isinstance function
  16. Python is differs from ==
  17. Python mutable and immutable data types
  18. Shallow and deep copies of Python

Python bin is a Python bin function

This article is published by the blog – Ape Say Programming Ape Say programming!