python的變數

來源:互聯網
上載者:User

標籤:nbsp   please   個數   1.0   一個   ror   inpu   erro   else   

變數是電腦記憶體中的一塊地區,變數可以儲存規定範圍內的值,而且值可以改變。 python下變數是對一個資料的引用 變數的命名-變數名由字母,數字,底線組成。-變數不能以數字開頭-不可以使用關鍵字-a a1 _a 變數的賦值-是變數的聲明和已定義流程 a = 1id(a) 運算子與運算式Python運算子包括-賦值運算子-算術運算子-關係運算子-邏輯運算子運算式是將不同的資料(包括變數,函數)用運算子號按一定規則串連起來的一種式子。 x = 2type(x)顯示 int x = ‘2‘type(x)顯示 str x = 2x +=2x顯示 4 x -=3x顯示 1 x *=4x顯示 4 x  /=4x顯示 1 3 + 4顯示 7 ‘a‘+‘b‘顯示 ‘ab‘ 3 - 4顯示 -1 3 * 4顯示 12 4 / 3顯示 1 4.0 / 3顯示 1.33333333333 4.0 // 3顯示 1.0  表示整除的部分 4 // 3顯示 1 4 % 3取餘 2**3顯示 8 2的3次方 2**10顯示 1024 2的10次方 1 > 2顯示 false 1 < 2顯示 true 1 >= 2顯示 false 1 == 1顯示true 1== 2顯示 false 1 != 2顯示 true 邏輯運算子and 邏輯與 : true and falseor 邏輯或 : false or truenot 邏輯非 : not true 1 == 2 and 2 > 1顯示 false 1 == 1 and 2 > 1 顯示 true 1 == 2 or 2 > 1顯示 true not 1 == 2顯示 true 寫一個四則運算器-要求從鍵盤讀取數字input()與raw_input()區別 input("Please input: ")輸入 123 ,顯示 123輸入‘abc‘  顯示 ‘abc‘輸入 abc 會報錯 raw_input("Please input: ")輸入 abcdjf ,顯示 ‘abcdjf‘輸入 234 顯示 ‘234’ vim 3.py #!/usr/bin/python num1 = input("Please a number: ")num2 = input("Please a number: ") print "%s + %s = %s" %(num1,num2,num1+num2)print "%s - %s = %s" %(num1,num2,num1-num2)print "%s * %s = %s" %(num1,num2,num1*num2)print "%s / %s = %s" %(num1,num2,num1/num2) 顯示:python 3.pyPlease a number: 4Please a number: 54 + 5 = 94 - 5 = -14 * 5 = 204 / 5 = 0  簡單計算機 [[email protected] day01]# cat cal.py#!/usr/bin/python def counter():    while True:        num1 = int(raw_input("please input num1:"))        oper = raw_input("please input oper:")        num2 = int(raw_input("please input num2:"))         if oper == "+":            print "%s + %s = %s" %(num1,num2,(num1+num2))        elif oper == "-":            print "%s - %s = %s" %(num1,num2,(num1-num2))        elif oper == "*":            print "%s * %s = %s" %(num1,num2,(num1*num2))        elif oper == "/":            if num2 == 0:                print ‘error / zero‘                break            else:                print "%s / %s = %s" %(num1,num2,(float(num1)/float(num2)))        exit = raw_input("input q exit:")        if exit.upper() == "Q":             break; counter()  

python的變數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.