日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

在Python中以二进制格式输入数字

發(fā)布時(shí)間:2023/12/1 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Python中以二进制格式输入数字 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Syntax to convert binary value to an integer (decimal format),

將二進(jìn)制值轉(zhuǎn)換為整數(shù)(十進(jìn)制格式)的語法,

int(bin_value, 2)

Here,

這里,

  • bin_value should contain the valid binary value

    bin_value應(yīng)該包含有效的二進(jìn)制值

  • 2 is the base value of the binary number system

    2是二進(jìn)制數(shù)系統(tǒng)的基值

Note: bin_value must contain only binary digits (0 and 1), if it contains other than these digits a "ValueError" will return.

注意 : bin_value必須僅包含二進(jìn)制數(shù)字(0和1),如果它不包含這些數(shù)字,則將返回“ ValueError”

將給定的二進(jìn)制值轉(zhuǎn)換為整數(shù)(十進(jìn)制)的程序 (Program to convert given binary value to integer (decimal))

# function to convert given binary Value # to an integer (decimal number) def BinToDec(value):try:return int(value, 2)except ValueError:return "Invalid binary Value"# Main code input1 = "11110000" input2 = "10101010" input3 = "11111111" input4 = "000000" input5 = "012"print(input1, "as decimal: ", BinToDec(input1)) print(input2, "as decimal: ", BinToDec(input2)) print(input3, "as decimal: ", BinToDec(input3)) print(input4, "as decimal: ", BinToDec(input4)) print(input5, "as decimal: ", BinToDec(input5))

Output

輸出量

11110000 as decimal: 240 10101010 as decimal: 170 11111111 as decimal: 255 000000 as decimal: 0 012 as decimal: Invalid binary Value

Now, we are going to implement the program – that will take input the number as an binary number and printing it in the decimal format.

現(xiàn)在,我們將實(shí)現(xiàn)該程序–該程序?qū)⑤斎霐?shù)字作為二進(jìn)制數(shù)字并以十進(jìn)制格式打印。

程序以二進(jìn)制格式輸入數(shù)字 (Program to input a number in binary format)

# input number in binary format and # converting it into decimal formattry:num = int(input("Input binary value: "), 2)print("num (decimal format):", num)print("num (binary format):", bin(num)) except ValueError:print("Please input only binary value...")

Output

輸出量

RUN 1: Input binary value: 11110000 num (decimal format): 240 num (binary format): 0b11110000RUN 2: Input binary value: 101010101010 num (decimal format): 2730 num (binary format): 0b101010101010RUN 3: Input binary value: 1111111111111111 num (decimal format): 65535 num (binary format): 0b1111111111111111RUN 4: Input binary value: 0000000 num (decimal format): 0 num (binary format): 0b0RUN 5: Input binary value: 012 Please input only binary value...

Recommended posts

推薦的帖子

  • Read input as an integer in Python

    在Python中將輸入讀取為整數(shù)

  • Read input as a float in Python

    在Python中以浮點(diǎn)形式讀取輸入

  • Parse a string to float in Python (float() function)

    解析要在Python中浮動(dòng)的字符串(float()函數(shù))

  • How do you read from stdin in Python?

    您如何從Python的stdin中讀取信息?

  • Asking the user for integer input in Python | Limit the user to input only integer value

    要求用戶在Python中輸入整數(shù)| 限制用戶僅輸入整數(shù)值

  • Asking the user for input until a valid response in Python

    要求用戶輸入直到Python中的有效響應(yīng)

  • Input a number in hexadecimal format in Python

    在Python中以十六進(jìn)制格式輸入數(shù)字

  • Input a number in octal format in Python

    在Python中以八進(jìn)制格式輸入數(shù)字

  • How to get the hexadecimal value of a float number in python?

    如何在python中獲取浮點(diǎn)數(shù)的十六進(jìn)制值?

  • Convert an integer value to the string using str() function in Python

    使用Python中的str()函數(shù)將整數(shù)值轉(zhuǎn)換為字符串

  • Convert a float value to the string using str() function in Python

    使用Python中的str()函數(shù)將浮點(diǎn)值轉(zhuǎn)換為字符串

  • Input and Output Operations with Examples in Python

    使用Python中的示例進(jìn)行輸入和輸出操作

  • Taking multiple inputs from the user using split() method in Python

    使用Python中的split()方法從用戶獲取多個(gè)輸入

  • Fast input / output for competitive programming in Python

    快速輸入/輸出,可在Python中進(jìn)行有競(jìng)爭(zhēng)力的編程

  • Precision handling in Python

    Python中的精確處理

  • Python print() function with end parameter

    帶有結(jié)束參數(shù)的Python print()函數(shù)

翻譯自: https://www.includehelp.com/python/input-a-number-in-binary-format.aspx

總結(jié)

以上是生活随笔為你收集整理的在Python中以二进制格式输入数字的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。