首页
/
每日頭條
/
科技
/
python數據類型8種
python數據類型8種
更新时间:2026-06-21 21:26:44

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
推荐阅读
聯想thinkserver工作站
聯想thinkserver工作站
據IDC數據顯示,過去一年,中國邊緣計算服務器市場爆發式增長了266.3%,這意味着當前中國企業的IT架構正在邁向一個雲網融合、混合多雲、邊緣計算等多架構并存的全新階段。為幫助企業更好的應對數字化轉型下的架構變革,7月18日聯想正式發布全新...
2026-06-21
智能居家防護系統
智能居家防護系統
防爆分析小屋主要是實現用戶現場集中控制、調度指揮、工藝分析、儀表監控的防爆處理,具有其它防爆結構不可比拟的優越性。防爆分析小屋室内常見安保系統01可燃氣體檢測器需選用檢測探頭和信号變送器一體化結構的産品,并帶就地報警和遠傳接點輸出。02有毒...
2026-06-21
CentOS7搭建vsftp服務器
CentOS7搭建vsftp服務器
今天簡單配置匿名訪問及用戶驗證的相關配置,基于虛拟用戶的配置敬請期待下一篇搭建vsftp服務器首先要确保虛拟機的防火牆和seLinux防火牆都是關閉的一、ftp文件傳輸服務1.FTP連接的端口号21:傳輸控制使用,進行身份驗證授權訪問20:...
2026-06-21
手機玻璃蓋闆怎麼樣
手機玻璃蓋闆怎麼樣
手機玻璃蓋闆怎麼樣?2022年已半,各大手機廠商的中高低産品線基本都有更新,但廠商們的核心宣傳點大多集中在外觀、影像、性能、續航、顯示上,畢竟這些大項才真正關乎到産品的價格定位和能力水平,我來為大家科普一下關于手機玻璃蓋闆怎麼樣?以下内容希...
2026-06-21
7.1寸大屏手機推薦
7.1寸大屏手機推薦
現在買手機是一件讓人很糾結的事,買貴買便宜了都怕吃虧,想花更多的錢買更好的手機,隻為解決不用經常換機的煩惱,如果你正好有遊戲需求,又想長期不換機,推薦你這幾款手機,幾乎零差評。一加10Pro與哈蘇合作的一加丨哈蘇影像系統是一加10Pro的主...
2026-06-21
Copyright 2023-2026 - www.tftnews.com All Rights Reserved