Python3學習(2)

來源:互聯網
上載者:User

標籤:numbers   --   finally   turn   運算式   tuple   rom   資料   字串   

字串賦值引用特性
同一個字串賦值給不同的變數,所有變數都是同一個對象

>> s = "abc"
>> s1 = "abc"
>> id(s)
34707248

>> id(s1)
34707248

>> id("abc")
34707248

>> s is s1
True

變數賦值

>> a = b = c = 3
>> a,b,c
(3, 3, 3)

>> a,b,c = 1,2,3
>> a,b,c
(1, 2, 3)

變數特性
變數可以重新賦值,變數儲存的是值的引用,即值在記憶體中的地址,當變數被重新賦值後變數指向的地址就會變;會指向一個新的對象;

>> a = 5
>> id(a)
499805328
>> id(5)
499805328
>> a = 1000
>> id(a)
34452592

交換兩個變數的值

>> a,b = b,a

其他語言:

>> a,b = 1,2
>> temp = a
>> a = b
>> b = temp
>> a,b
(2, 1)

查看保留字,關鍵字模組keyword

>> import keyword
>> print(keyword.kwlist)
[‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘,
‘else‘, ‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘nonloc
al‘, ‘not‘, ‘or‘, ‘pass‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]

>> keyword.iskeyword("yield")
True

一行寫多個運算式,”;”

>> a = 1;b = 2;c = 3

代碼換行

>> a = 3\
... +3
>> a

判斷字元類型

>> isinstance(s,str)
True
>> isinstance(s,(str,bytes))
True

help 和 dir 命令
help可以查看對象的使用方法
dir 可以查看模組或對象包含的屬性和方法

python3中的資料類型
Numbers 數字 ,python3中沒有long
--int
--float
--complex
str 字串

list 列表

tuple 元組

dict 字典
set 集合
注釋
單行注釋用#
多行注釋用三引號”” ”””

Python3學習(2)

聯繫我們

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