current position:Home>Python and binary
Python and binary
2022-01-31 17:22:16 【laufing】
Decimal and binary
Decimal system ---> Binary system , except 2 The remainder is inverted
Like the decimal system 5, Represented as binary 101 python Realization :
d = 5
bin(5)
#"0b101"
# Convert the current binary data , To integer
int("0b101",base=2) # 5
Copy code
The fractional part , Then take 2 Round and align
Like the decimal system 0.5, Represented as binary 0.1
Binary system ----> Decimal system , Just weighted sum
Such as binary 101, Its decimal system is expressed as 1*2^2^+0*2^1^+1*2^0^=5
Decimal and octal
Decimal system ----> octal , except 8 The remainder is inverted
Like the decimal system 8 , Expressed in octal 0o10
d = 8
oct(d)
#0o10
int("0o10",base=8)
Copy code
octal ---> Decimal system , Similarly, weighted summation
Decimal system ----> Hexadecimal ,same
d = 17
hex(d)
#'0x11'
int("0x11",base=16) # Hexadecimal to decimal , To sum by weight
#1*16^1 + 1*16^0
Copy code
Binary representation of characters
The character has a corresponding ASCII clock , The corresponding decimal number , Change to binary
# Get the binary of English characters
bin(ord("a"))
#'0b1100001', One byte
chr
Copy code
1byte = 8bit 1Kb = 1024bytes 1Mb = 1024Kb ...
Chinese and binary
Chinese characters have corresponding GBK/GB2312/GB18030 Encoding table , Corresponding decimal system , Change to binary .
A Chinese character takes up two bytes , Now the mainstream is GBK code .
Universal coding method Unicode, Correspond all characters to binary , But each character takes == Two bytes == Of memory , Than ASCII Double the code , Temporarily stored in memory can also , Saving to disk or network transmission is a waste of space .
So there is a utf-8 The coding ,Unicode Transform Format, Save disk 、 Space during transmission .
Data in memory -->Unicode code ----2bytes/per char
Data on disk -->UTF-8----1byte for english, 3bytes for chinese
linux Type... On the command line locale, View supported encoding formats
py2 And py3 The coding
py2 In memory is ASCII code , If you use Chinese, you need to declare
linux/macOS The system uses utf-8
#!/usr/bin/python2
# -*- coding:utf-8 -*-
print(" chinese ")
Copy code
windows System use gbk code
py3 In memory is Unicode code
hash Hash
Put different inputs , Hash calculation yields a fixed length output .
characteristic : Irreversible Fast calculation , Even if the file is big , It won't affect the calculation speed too much The type of data entered , Must be immutable
purpose : Password encryption File check
hash("xxxxx")
from hashlib import md5
m = md5()
m.update("xxx".encode())
m.hexdigest()
Copy code
python Based on hash Data type of
dict/set Why is dictionary query fast ? In the dictionary , Every key Have to go through hash Calculation , For all the key Of hash Value to sort . Looking for some key The value of , First calculate its hash value , According to its hash Values are in the already ordered hash Binary search in value sequence . fast
According to the query hash value , Get the corresponding value The index of .
Why? set Automatic weight removal ? When saving value , Calculating the hash value , Then store the data in the corresponding position , If there is no data at this location , The insert ; Otherwise do not insert .
copyright notice
author[laufing],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311722147700.html
The sidebar is recommended
- Python - convert Matplotlib image to numpy Array or PIL Image
- Python and Java crawl personal blog information and export it to excel
- Using class decorators in Python
- Untested Python code is not far from crashing
- Python efficient derivation (8)
- Python requests Library
- leetcode 2047. Number of Valid Words in a Sentence(python)
- leetcode 2027. Minimum Moves to Convert String(python)
- How IOS developers learn Python Programming 5 - data types 2
- leetcode 1971. Find if Path Exists in Graph(python)
guess what you like
-
leetcode 1984. Minimum Difference Between Highest and Lowest of K Scores(python)
-
Python interface automation test framework (basic) -- basic syntax
-
Detailed explanation of Python derivation
-
Python reptile lesson 2-9 Chinese monster database. It is found that there is a classification of color (he) desire (Xie) monsters during operation
-
A brief note on the method of creating Python virtual environment in Intranet Environment
-
[worth collecting] for Python beginners, sort out the common errors of beginners + Python Mini applet! (code attached)
-
[Python souvenir book] two people in one room have three meals and four seasons: 'how many years is it only XX years away from a hundred years of good marriage' ~?? Just come in and have a look.
-
The unknown side of Python functions
-
Python based interface automation test project, complete actual project, with source code sharing
-
A python artifact handles automatic chart color matching
Random recommended
- Python crawls the map of Gaode and the weather conditions of each city
- leetcode 1275. Find Winner on a Tic Tac Toe Game(python)
- leetcode 2016. Maximum Difference Between Increasing Elements(python)
- Run through Python date and time processing (Part 2)
- Application of urllib package in Python
- Django API Version (II)
- Python utility module playsound
- Database addition, deletion, modification and query of Python Sqlalchemy basic operation
- Tiobe November programming language ranking: Python surpasses C language to become the first! PHP is about to fall out of the top ten?
- Learn how to use opencv and python to realize face recognition!
- Using OpenCV and python to identify credit card numbers
- Principle of Python Apriori algorithm (11)
- Python AI steals your voice in 5 seconds
- A glance at Python's file processing (Part 1)
- Python cloud cat
- Python crawler actual combat, pyecharts module, python data analysis tells you which goods are popular on free fish~
- Using pandas to implement SQL group_ concat
- How IOS developers learn Python Programming 8 - set type 3
- windows10+apache2. 4 + Django deployment
- Django parser
- leetcode 1560. Most Visited Sector in a Circular Track(python)
- leetcode 1995. Count Special Quadruplets(python)
- How to program based on interfaces using Python
- leetcode 1286. Iterator for Combination(python)
- leetcode 1418. Display Table of Food Orders in a Restaurant (python)
- Python Matplotlib drawing histogram
- Python development foundation summary (VII) database + FTP + character coding + source code security
- Python modular package management and import mechanism
- Django serialization (II)
- Python dataloader error "dataloader worker (PID XXX) is killed by signal" solution
- apache2. 4 + Django + windows 10 Automated Deployment
- leetcode 1222. Queens That Can Attack the King(python)
- leetcode 1387. Sort Integers by The Power Value (python)
- Tiger sniffing 24-hour praise device, a case with a crawler skill, python crawler lesson 7-9
- Python object oriented programming 01: introduction classes and objects
- Baidu Post: high definition Python
- Python Matplotlib drawing contour map
- Python crawler actual combat, requests module, python realizes IMDB movie top data visualization
- Python classic: explain programming and development from simple to deep and step by step
- Python implements URL availability monitoring and instant push