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

成都創(chuàng)新互聯(lián)公司主營嵩明網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App制作,嵩明h5小程序制作搭建,嵩明網(wǎng)站營銷推廣歡迎嵩明等地區(qū)企業(yè)咨詢
在這個簡單的 python 程序中,我們需要使用一個臨時變量來交換數(shù)字。這是一個初級 python 程序。
要理解這個例子,您應(yīng)該了解以下 Python 編程主題:
- Python 語法
- Python 數(shù)據(jù)類型
- Python 安裝和設(shè)置
如何在 python 中交換變量?
這里的這個 python 程序的例子是給初學(xué)者的;我們用一個臨時變量來交換兩個變量。例如:如果 x=a 和 y=b 那么我們做互換,結(jié)果就是 x=b 和 y=a 。這是一個使用臨時變量進行交換的簡單邏輯。
為了在 python 編程語言中應(yīng)用這個邏輯,我們必須使用一個臨時變量。這個臨時變量用來存儲 x 的值,我們把 y 的值應(yīng)用到 x 上,然后把 temp 的值應(yīng)用到 y 上 temp = x,x = y,y = temp 現(xiàn)在我們一步一步的破解代碼。
算法
步驟 1:使用簡單 python 程序中的函數(shù)輸入接受用戶輸入,并將該值存儲到變量中。
第二步:創(chuàng)建一個臨時變量 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)站名稱:Python程序:交換兩個變量
文章路徑:http://m.5511xx.com/article/djheodp.html


咨詢
建站咨詢
