python資料類型

來源:互聯網
上載者:User

標籤:表達   聲明   資料   port   vim   函數名   ack   and   hello   

變數以及類型

變數:儲存程式運行中的資料,變數有3個要素:變數名、變數類型、變數值。python屬於弱類型語言,不需要聲明變數類型。

[[email protected] python]# ipython3In [1]: a=1  //變數名=變數值;在堆記憶體中的一個地區存了一個值為1,記憶體分為堆記憶體和棧記憶體,棧記憶體的是引用。指向堆記憶體中的值。In [4]: b=88In [5]: c=a+bIn [7]: cOut[7]: 89In [8]: aOut[8]: 1In [9]: type(a)Out[9]: intIn [10]: type(c)Out[10]: intIn [11]: str="hello"In [12]: strOut[12]: ‘hello‘In [13]: x,y=1,2In [14]: xOut[14]: 1In [15]: yOut[15]: 2In [16]:In [16]: type(str)Out[16]: str

變數類型

 

標識符:自訂的一些符號和名稱,標識符是自己定義的,如變數名、函數名等。

規則:由字母、底線和數字組成,且不能以數字開頭;不能包含一些有特殊意義的符號,如#.&!()等;同時區分大小寫。

大小寫規則:

駝峰命名法:如小駝峰:userName / userLoginFlag  大駝峰:UserName;類的名字一般首字母大寫。

底線規則:user_name user_login_flag

保留字/關鍵字:在電腦中有特殊含義的單詞。

In [1]: import keywordIn [2]: keyword.kwlistOut[2]: [‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘, ‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘nonlocal‘, ‘not‘, ‘or‘, ‘pass‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]In [3]: 

輸出和輸出格式

普通的輸出,print()

In [4]: print(‘hello‘,‘world‘,‘zyj‘,‘hello‘) //輸出多個變數hello world zyj helloIn [5]: print(111+222) //輸出運算式333In [6]: exit[[email protected] python]#vim 3.py   1 a=35  2 print("my age is:%d"%a )  //變數替換[[email protected] python]# python3 3.py my age is:35[[email protected] python]#vim 3.py    1 a=35  2 b=10  3 print("my age is:%d"%a+b)[[email protected] python]# python3 3.py Traceback (most recent call last):  File "3.py", line 3, in <module>    print("my age is:%d"%a+b) //錯誤行TypeError: must be str, not int  //錯誤類型[[email protected] python]#vim 3.py    1 a=35  2 b=10  3 print("my age is:%d"%(a+b)) //運算式需要用括弧括起來[[email protected] python]# python3 3.py my age is:45[[email protected] python]#vim 3.py   1 a=35  2 b=10  3 my_name="zyj"  4 print("my age is:%d,my name is:%s" %(a+b,my_name)) //如果需要多個變數替換也要加小括弧,並且每個變數用逗號隔開,%d代表整數類型替換,%s代表所有的類型變數替換,可以只記住%s[[email protected] python]# python3 3.py my age is:45,my name is:zyj
[[email protected] python]
#vim 3.py money=2print("I have money:%4d",%money) //輸出為4位,預設位空格[[email protected] python]# python3 3.py I have money: 2[[email protected] python]#vim 3.py money=2print("I have money:%04d" %money) //不夠4位前面用0填充[[email protected] python]# python3 3.py I have money:0002

 

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.