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

成都創(chuàng)新互聯(lián)是一家從事企業(yè)網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)站制作、行業(yè)門戶網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)制作的專業(yè)網(wǎng)站設(shè)計(jì)公司,擁有經(jīng)驗(yàn)豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁(yè)設(shè)計(jì)人員,具備各種規(guī)模與類型網(wǎng)站建設(shè)的實(shí)力,在網(wǎng)站建設(shè)領(lǐng)域樹(shù)立了自己獨(dú)特的設(shè)計(jì)風(fēng)格。自公司成立以來(lái)曾獨(dú)立設(shè)計(jì)制作的站點(diǎn)1000+。
用例子寫(xiě)一個(gè)簡(jiǎn)單的 Python 程序。為了演示,我們將使用算術(shù)運(yùn)算符將兩個(gè)數(shù)字相加
簡(jiǎn)單的 Python 程序相加兩個(gè)數(shù)字
這個(gè)簡(jiǎn)單的 Python 程序相加了兩個(gè)數(shù)字,允許用戶輸入兩個(gè)值。接下來(lái),它將把這兩個(gè)數(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 相加兩個(gè)數(shù)字輸出
Please Enter the First Number: 22
Please Enter the second number: 44
The sum of 22 and 44 is 66.0在這個(gè)相加兩個(gè)數(shù)字的簡(jiǎn)單 python 程序示例中,以下語(yǔ)句要求用戶輸入兩個(gè)整數(shù),并將用戶輸入的值存儲(chǔ)在變量 1 和 2 中
number1 = input(" Please Enter the First Number: ")
number2 = input(" Please Enter the second number: ")下一行,我們使用 Python 算術(shù)運(yùn)算符“+”將數(shù)字 1 和數(shù)字 2 相加,然后將該總和賦給 sum。
從下面的語(yǔ)句中,您可以看到,我們使用 float type cast 將用戶輸入值轉(zhuǎn)換為 float。這是因?yàn)椋J(rèn)情況下,用戶輸入的值是字符串類型的,如果我們?cè)趦蓚€(gè)字符串值之間使用+運(yùn)算符,python 會(huì)連接這兩個(gè)值,而不是相加它們
sum = float(number1) + float(number2)下面的 Python 打印語(yǔ)句將輸出 sum 變量(22 + 44 = 66)。
print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))這個(gè) python 程序在加兩個(gè)正整數(shù)的時(shí)候加兩個(gè)數(shù)字效果很好,加正負(fù)整數(shù)怎么樣?讓我們看看
相加兩個(gè)數(shù)字的示例 Python 程序示例 2
負(fù)數(shù)也沒(méi)問(wèn)題!。編寫(xiě)上述 python 相加兩個(gè)數(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注:以上簡(jiǎn)單的程序?qū)⑾拗朴脩簦惠斎胱址底鳛檩斎?/p>
分享題目:Python程序:簡(jiǎn)單相加兩個(gè)數(shù)字
網(wǎng)站網(wǎng)址:http://fisionsoft.com.cn/article/dhochsj.html


咨詢
建站咨詢
