第二天Python輸入、if語句等

來源:互聯網
上載者:User

標籤:不能   imp   rand   優先順序   格式化   資料   語句   name   問題   

操作:滑鼠能拖動代碼和注釋

print不換行
print("你好,end="----"")
print("鹿晗,end="----"")
print(i,end="--")

\n加字串前面

字串 %變數 格式化輸出

names = "鹿晗"
print("姓名是:%s"%names)

多個輸出用()

edu = "本科"
address = "順義"
print("學曆是:%s 住在%s"%(edu,address))

%5d %05d 佔位
student_no = 1
print("學號是%5d"%student_no) #佔5位
print("學號是%05d"%student_no) #佔5位,前面用0來補

%.3f 保留小數
price = 30.666
print("價格是:%.3f"%price) # 預設6位小數 %.3f是%0.3f的簡寫,保留三位小數

%% 輸出%
scale = 10
print("資料的比例是%.2f%%"%scale)

input輸入: input(提示資訊) 輸入的都是字串型的

轉換:int(變數) float(變數) str(變數)

字串小數不能轉換成整型
b = "12.33" b = int(b) 報錯
浮點型可轉換成整型
b = 12.33 b = int(b) 沒問題

兩個數互換
a = 10
b = 20
a,b = b,a

// 取商 ** 冪

python異或、按位與

優先順序:()>算數>比較>邏輯>賦值 +=、...、%=、//=算賦值
result = 3 * 5 > 12 and 4 - 2 < 1

if語句判斷,兩個數比較不用浮點型float

if、if else、if elif if

random產生隨機數
import random
n = random.randint(0,2) # [0,2] 0-2產生隨機數

第二天Python輸入、if語句等

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.