首页
/
每日頭條
/
科技
/
python數據類型8種
python數據類型8種
更新时间:2026-05-02 17:42:41

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
推荐阅读
qq音樂怎麼删除歌單
qq音樂怎麼删除歌單
演示機型:Iphone12&&華為P40&&小米11系統版本:iOS14.4&&EMUI11&&MIUI12.0.7APP版本:qq音樂10.12.51、首先打開一個qq音樂的界面中,點擊我的選項。2、點擊了我的選項之後,點擊管理選項。3、進入到管理的界面中,點擊一個歌單選項。4、勾選上了的歌單之後,點擊删除按鈕。5、就會彈出了的确認框中,點擊删除按鈕。6、即可看到當前歌單删除成功。以上内容适用于
2026-05-02
手機投屏到電視上怎麼操作
手機投屏到電視上怎麼操作
1、投屏方法:無線共享,安卓手機以華為手機為例。首先在電視上打開“無線投屏”或類似Miracast的...
2026-05-02
拒接陌生來電怎麼設置
拒接陌生來電怎麼設置
以Iphone12,ios14為例:在手機桌面找到手機自帶的“設置”按鈕,點擊。進入“設置”後,往下滑,看到一個“勿擾模式”,點擊進入。進入“勿擾模式”後,将“勿擾模式”打開。打開“勿擾模式”後,下面有一個“允許以下來電”,點擊進入
2026-05-02
高速天窗怎麼開
高速天窗怎麼開
1、首先,當打開天窗時,必須選擇合适的時間。等到窗戶擡起後再打開,不要逆風打開。否則,速度快,風力大。它可能會打破天窗。更嚴重的是,天窗被吹走并撞到汽車旁邊或後面的車窗。事故并不像修理汽車那麼簡單,也可能會傷害到人們。2、在汽車低速行駛下式可以打開天窗沒有什麼太多需要注意的,不過當我們的汽車高速行駛下就需要注意這些地方,一種是垂直上升打開後面,還有一種是順着風向打開,最好不要逆着風向打開,逆着風向
2026-05-02
私人二手車過戶流程是什麼
私人二手車過戶流程是什麼
1、來到過戶大廳後,出示相關材料領取一張《XX市舊機動車買賣合同》,雙方各自填寫資料。填好後,拿上以上所有的資料就可以去辦理轉移受理”。2、直接将車開到過戶驗車處,工作人員會對車輛進行檢查、拓号、拆牌和照相,需繳納35元的拓号費。領取車輛照片,貼于檢查記錄表上。這些辦完後,可以将車停到停車場,進入過...
2026-05-02
Copyright 2023-2026 - www.tftnews.com All Rights Reserved