首页
/
每日頭條
/
科技
/
python數據類型8種
python數據類型8種
更新时间:2026-02-12 16:23:27

python數據類型8種?>>> type(1 1.0) <class 'float'> >>> type(4/2) <class 'float'> >>> type(24//2) <class 'int'> >>> 4/2 2.0 >>> 4//2 2 >>> >>> 1//2 0,今天小編就來說說關于python數據類型8種?下面更多詳細答案一起來看看吧!

python數據類型8種(python編程基本數據類型)1

python數據類型8種

(一)數字整形和浮點型
  • 整數:int (python沒有數字範圍short long)
  • 浮點型:float(python沒有單精度float和雙精度double之分)

>>> type(1 1.0) <class 'float'> >>> type(4/2) <class 'float'> >>> type(24//2) <class 'int'> >>> 4/2 2.0 >>> 4//2 2 >>> >>> 1//2 0

  • / 是除法,得到浮點數
  • // 是整除,是得到整數
(二)計算機基礎知識 10、2、8、16進制的表示和轉換

>>> 0b10 2 >>> 0o10 8 >>> 0x10 16

  • 它們之間如何進行轉換

>>> bin(0b100) '0b100' >>> oct(0o100) '0o100' >>> int(100) 100 >>> hex(0xf) '0xf'

(三) 數字布爾類型和複數
  • bool 布爾類型:表示真True和假False
  • complex 複數

>>> type(True) <class 'bool'> >>> type(False) <class 'bool'>

  • 為什麼bool歸在數字裡面呢?

>>> int(False) 0 >>> int(True) 1 >>> bool(0) # 0 表示false False >>> bool(1) True >>> bool(-1) True >>> bool(2) True

  • 空值表示false

>>> bool(0x0) False >>> bool("") False >>> bool([]) False >>> bool({}) False >>> bool(()) False >>> bool(None) False

  • 複數的表示 j

>>> 36j 36j >>> type(36j) <class 'complex'>

(四) 字符串單引号和雙引号
  • str 字符串
  • 如何表示字符串:單引号、雙引号、三引号

>>> type('2') <class 'str'> >>> type(1) <class 'int'>

  • 為什麼需要這麼多引号,如果我的it‘s 需要一個引号,外部則需要雙引号

>>> "it's good" "it's good"

  • 如果隻想要單引号,使用轉義字符

>>> 'it\'s good' "it's good"

(五) 多行字符串
  • 一行字符79,需要使用三引号解決多行

>>> """ ... ni hao ... hello ... hi""" '\nni hao\nhello\nhi'

  • \n 表示回車
  • 加入轉義字符,還是會原本的輸出

>>> """ nihao \n hi""" ' nihao \n hi'


  • 使用print,可以把轉義字符輸出

>>> print(""" nihao \n hi""") nihao hi

  • 字符串換行,使用\ 編輯,不會報錯

>>> 'ni\ ... hao' 'nihao'

(六) 轉義字符
  • 特殊字符
  • 無法看到的字符

\n 換行,\t 橫向制表符, \r 回車

  • 與語言本身語法有沖突的字符

\' 單引号

  • 如果想把\n進行輸出,需要加上\進行轉義

>>> print("hello \n world") hello world >>> print("hello \\n world") hello \n world

(七) 原始字符串
  • 輸出文件路徑,可以使用\, 或者r(使用r值後,字符串變成了原始字符串)

>>> print("c:\north\no.py") c: orth o.py >>> print("c:\\north\\no.py") c:\north\no.py >>> print(r"c:\\north\\no.py") c:\\north\\no.py

(八) 字符串運算
  • 字符串的基本操作方法

>>> "hello" "world" 'helloworld' >>> "hello"*2 'hellohello'

  • 獲取字符串中的字符,使用[],序号從0開始,右邊從-1開始(表示從末尾開始查找的第一個)

>>> "hello"[0] 'h' >>> "hello"[1] 'e' >>> "hello"[-1] 'o'

  • 獲取一組字符,不取尾巴

>>> "hello world"[0:5] 'hello' >>> "hello world"[0:-3] 'hello wo'

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
手機字體太小看不清楚怎麼辦
手機字體太小看不清楚怎麼辦
現在手機屏幕開始越做越大,但是屏幕上的字體似乎沒有變化。對于視力不好,或者年紀稍長的人來說。看手機上的文字都是比較困難的。其實手機上的字體可以放大,那麼接下來就跟着我一起這樣做吧!手機自帶其實手機本身也自帶字體放大功能,簡單設置一下就可以。...
2026-02-12
iphone不激活怎麼看是否是翻新機
iphone不激活怎麼看是否是翻新機
iphone不激活怎麼看是否是翻新機?平時我們在入手了新的iPhone蘋果手機後,很多小夥伴都會有這樣的顧慮,就是新入手的手機會不會是翻新機呢?估計這是很多小夥伴都會擔心害怕的地方吧,畢竟自己花了那麼多錢,肯定是不想自己買來的是二手的翻新機...
2026-02-12
迷你世界創造黑科技
迷你世界創造黑科技
迷你世界可謂是當下最熱門的手機沙盒遊戲,在這你可以根據自己的想法,創造一切東西。比如你喜歡童話世界,那麼就可以創造一個城堡。如果你喜歡武俠,那麼就可以創造出一個江湖。不同的人不同的想法,自然也誕生了不少地圖。對于玩家而言,這些地圖在一定程度...
2026-02-12
電腦顯卡散熱不好怎麼解決
電腦顯卡散熱不好怎麼解決
電腦顯卡散熱不好怎麼解決?顯卡是發熱量最大的電腦配件,就獨立顯卡而言,一般溫度在40至60攝氏度之間屬于正常範圍,不過有些高端顯卡或是超頻版顯卡在玩遊戲時,溫度可能漲到80攝氏度,如果室内溫度再高點,溫度繼續上升,就可能會燒毀顯卡,我來為大...
2026-02-12
凍結表頭怎樣添加續表
凍結表頭怎樣添加續表
我們在工作中,制作的表格往往數據行很多,在查看時,向下滾動鼠标滾輪時,表頭會越來越向上,直到消失,這樣就不便于我們查看表頭标題對應的數據,那很有沒有好的辦法能夠解決這一問題呢,答案是肯定的,今天小編就和大家分享利用Excel凍結窗口功能來固...
2026-02-12
Copyright 2023-2026 - www.tftnews.com All Rights Reserved