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

成都創(chuàng)新互聯(lián)公司主營(yíng)嵩明網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App制作,嵩明h5小程序制作搭建,嵩明網(wǎng)站營(yíng)銷推廣歡迎嵩明等地區(qū)企業(yè)咨詢
在這個(gè)簡(jiǎn)單的 python 程序中,我們需要使用一個(gè)臨時(shí)變量來(lái)交換數(shù)字。這是一個(gè)初級(jí) python 程序。
要理解這個(gè)例子,您應(yīng)該了解以下 Python 編程主題:
- Python 語(yǔ)法
- Python 數(shù)據(jù)類型
- Python 安裝和設(shè)置
如何在 python 中交換變量?
這里的這個(gè) python 程序的例子是給初學(xué)者的;我們用一個(gè)臨時(shí)變量來(lái)交換兩個(gè)變量。例如:如果 x=a 和 y=b 那么我們做互換,結(jié)果就是 x=b 和 y=a 。這是一個(gè)使用臨時(shí)變量進(jìn)行交換的簡(jiǎn)單邏輯。
為了在 python 編程語(yǔ)言中應(yīng)用這個(gè)邏輯,我們必須使用一個(gè)臨時(shí)變量。這個(gè)臨時(shí)變量用來(lái)存儲(chǔ) x 的值,我們把 y 的值應(yīng)用到 x 上,然后把 temp 的值應(yīng)用到 y 上 temp = x,x = y,y = temp 現(xiàn)在我們一步一步的破解代碼。
算法
步驟 1:使用簡(jiǎn)單 python 程序中的函數(shù)輸入接受用戶輸入,并將該值存儲(chǔ)到變量中。
第二步:創(chuàng)建一個(gè)臨時(shí)變量 temp ,并將 x 的值換成 temp 。
第三步:將 y 的值換成 x ,然后將溫度的值換成 y 。
第四步:用格式化的方法打印交換后的 x 和 y 的值。
格式方法在二次方程 python 程序中描述。
Python 源代碼
x = input('Enter value of x: ')
y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x
x = y
y = temp
print('after swapping the X is: {}'.format(x))
print('after swapping the Y is: {}'.format(y))
輸出
Enter value of x: a
Enter value of y: b
after swapping the X is: b
after swapping the y is: a 網(wǎng)頁(yè)名稱:Python程序:交換兩個(gè)變量
瀏覽地址:http://www.dlmjj.cn/article/djheodp.html


咨詢
建站咨詢
