日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python学习实例(2)

發布時間:2023/12/13 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python学习实例(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#=================================== #2.2 不同進制間的轉換 #===================================#+++++++++++++++++++++++++++++++++++ #2.2.1. 二進制數轉換為十進制數 #+++++++++++++++++++++++++++++++++++#<程序:2-to-10進制轉換> b=input("Please enter a binary number:") d=0; for i in range(0,len(b)):if b[i] == '1':weight = 2**(len(b)-i-1)d = d+weight; print(d)#<程序:改進后的2-to-10進制轉換> b=input("Please enter a binary number:") d=0; weight=2**(len(b)-1); for i in range(0,len(b)):if b[i] == '1':d = d+weight;weight=weight//2; #‘//’是整數除法 print(d)#+++++++++++++++++++++++++++++++++++ #2.2.2. 十進制數轉換為二進制數 #+++++++++++++++++++++++++++++++++++#<程序:整數的10-to-2進制轉換> x= int(input("Please enter a decimal number:")) r = 0; Rs = []; while(x != 0):r = x% 2x = x//2Rs = [r]+Rs for i in range(0,len(Rs)): #從最高位到最低位依次輸出;Rs[0]存的是最高位, Rs[len(Rs)-1]存的是最低位。print(Rs[i],end='')#<程序:整數的10-to-2進制轉換-遞歸> def convert(x): #把10進制數x轉換為2進制數,并返回結果列表。if x<2: return([x]) #x=0 或 1,所以返回xr= x%2; #r 是2除x的余數return(convert(x//2)+[r]) # 結果=[x//2的二進制,r]num = int(input("Please enter a decimal number:")) Rs= convert(num) for i in range(0, len(Rs)):print (Rs[i],end='')#=====================================================================================#=================================== #2.4. 一切都是邏輯(Logic) #===================================#+++++++++++++++++++++++++++++++++++ #2.4.3. 用邏輯做加法 #+++++++++++++++++++++++++++++++++++#<程序: 全加器> def FA(a,b,c): # Full adderCarry = (a and b) or (b and c) or (a and c)Sum = (a and b and c) or (a and (not b) and (not c)) \or ((not a) and b and (not c)) or ((not a) and (not b) and c)return Carry, Sum#<程序:完整的加法器 Carry Ripple adder> def add(x,y): # x, y are lists of True or False, c is True or False# return carry and a list of x+ywhile len(x) < len(y): x = [False]+x #前面補0while len(y) < len(x): y = [False]+y #前面補0L=[];Carry=Falsefor i in range(len(x)-1,-1,-1): #從最后一位一個個往前加Carry,Sum=FA(x[i],y[i],Carry)L=L+[Sum]return (Carry, L)#<程序:乘法器> def multiplier(x,y): # 求x*yS=[];for i in range(len(y)-1,-1,-1):if y[i] == True: #y[i]是 1,要將x加進到SC, S=add(S,x)if C==True: S=[C]+Sx=x+[False] #每一次x都要向左移一位,后面補0return(S)

?

總結

以上是生活随笔為你收集整理的python学习实例(2)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。