日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:簡單相加兩個數(shù)字

創(chuàng)新互聯(lián)Python教程:

成都創(chuàng)新互聯(lián)是一家從事企業(yè)網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)站制作、行業(yè)門戶網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計制作的專業(yè)網(wǎng)站設(shè)計公司,擁有經(jīng)驗豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁設(shè)計人員,具備各種規(guī)模與類型網(wǎng)站建設(shè)的實力,在網(wǎng)站建設(shè)領(lǐng)域樹立了自己獨特的設(shè)計風(fēng)格。自公司成立以來曾獨立設(shè)計制作的站點1000+。

用例子寫一個簡單的 Python 程序。為了演示,我們將使用算術(shù)運算符將兩個數(shù)字相加

簡單的 Python 程序相加兩個數(shù)字

這個簡單的 Python 程序相加了兩個數(shù)字,允許用戶輸入兩個值。接下來,它將把這兩個數(shù)字相加,并將總和賦給變量 sum。

number1 = input(" Please Enter the First Number: ")
number2 = input(" Please Enter the second number: ")

# Using arithmetic + Operator to add two numbers
sum = float(number1) + float(number2)
print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))

Python 相加兩個數(shù)字輸出

 Please Enter the First Number: 22
 Please Enter the second number: 44
The sum of 22 and 44 is 66.0

在這個相加兩個數(shù)字的簡單 python 程序示例中,以下語句要求用戶輸入兩個整數(shù),并將用戶輸入的值存儲在變量 1 和 2 中

number1 = input(" Please Enter the First Number: ")
number2 = input(" Please Enter the second number: ")

下一行,我們使用 Python 算術(shù)運算符“+”將數(shù)字 1 和數(shù)字 2 相加,然后將該總和賦給 sum。

從下面的語句中,您可以看到,我們使用 float type cast 將用戶輸入值轉(zhuǎn)換為 float。這是因為,默認(rèn)情況下,用戶輸入的值是字符串類型的,如果我們在兩個字符串值之間使用+運算符,python 會連接這兩個值,而不是相加它們

sum = float(number1) + float(number2)

下面的 Python 打印語句將輸出 sum 變量(22 + 44 = 66)。

print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))

這個 python 程序在加兩個正整數(shù)的時候加兩個數(shù)字效果很好,加正負(fù)整數(shù)怎么樣?讓我們看看

相加兩個數(shù)字的示例 Python 程序示例 2

負(fù)數(shù)也沒問題!。編寫上述 python 相加兩個數(shù)字程序的替代方法是:

number1 = float(input(" Please Enter the First Number: "))
number2 = float(input(" Please Enter the second number: "))

# Using arithmetic + Operator to add two numbers
sum = number1 + number2
print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))
 Please Enter the First Number: 25
 Please Enter the second number: 91
The sum of 25.0 and 91.0 is 116.0

注:以上簡單的程序?qū)⑾拗朴脩簦惠斎胱址底鳛檩斎?/p>
當(dāng)前標(biāo)題:Python程序:簡單相加兩個數(shù)字
地址分享:http://m.5511xx.com/article/dhochsj.html