程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python basic operation

編輯:Python

文章目錄

    • 十進制轉二進制、八進制、十六進制
    • 二進制/八進制/十六進制 轉 十進制
    • Integer to integer list
    • Convert a list of integers to integers
    • 與、非、或、異或、位移
    • 將asciiThe code is converted to the corresponding letter
    • convert letters toascii碼
    • 枚舉 enumerate

十進制轉二進制、八進制、十六進制

bin(5) # '0b101'
oct(5) # '0o5'
hex(5) # '0x5'

二進制/八進制/十六進制 轉 十進制

int('110',2) # 6
int('110',8) # 64+8=72
int('110', 16) # 16*16+16=272 

Integer to integer list

eg. 123 => [1, 2, 3]

a = 123
b = str(a) #'123'
c = list(b) #['1','2','3']
d = list(map(int,c)) #[1, 2, 3]

Convert a list of integers to integers

eg. [1,2,3] => 123

a = [1,2,3]
b = list(map(str,a)) # ['1', '2', '3']
c = ''.join(b) # '123'
d = int(c) # 123

與、非、或、異或、位移

與非或異或左移右移&~|^<<;相當於乘2^k>>;相當於整除2^k5&3=1
(101) | (011) = (001)~5=6
見下面的解釋5|3=7
(101) | (011) = (111)5^3=6
(101) | (011) = (110)5<<2 = 205>>1 = 2

In the computer, the calculation is performed in the form of two's complement,5的是正數,補碼等於原碼:0000 0000 0000 0000 0000 0000 0000 0101, 取非後:1111 1111 1111 1111 1111 1111 1111 1010, Computer operations are performed in two's complement form,However, it is still displayed in the original form on the terminal,Convert the result back to the original code(補碼的補碼,Complementing a negative number is adding the inverse1):1000 0000 0000 0000 0000 0000 0000 0110, 也就是-6.

將asciiThe code is converted to the corresponding letter

chr(97) #'a'

convert letters toascii碼

ord('a') # 97

枚舉 enumerate

numbers=[0,0,3,4]
for i,num in enumerate(numbers):
print(i, num)

0 0
1 0
2 3
3 4

分別是index, 和值


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved