首页
/
每日頭條
/
科技
/
python數據類型8種
python數據類型8種
更新时间:2026-10-27 04:14:36

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
推荐阅读
鍵盤無法輸入怎麼辦
鍵盤無法輸入怎麼辦
1、先打開我們的電腦,然後右擊此電腦;2、之後點擊彈窗中的屬性;3、然後點擊設備管理器;4、之後點開...
2026-10-27
word怎樣給字母打下标
word怎樣給字母打下标
演示機型:華為MateBookX系統版本:win10APP版本:word20161、打開相應word文檔,連續輸入字母和要設置為下标的文字。2、選中要設置為下标的文字,點擊“格式”菜單,選擇“字體”。3、出現“字體”對話框。在“字體”選項卡中,勾選“下标”選項,點擊“
2026-10-27
如何編輯拍一拍内容
如何編輯拍一拍内容
1、微信升級為新版本後,打開微信,打開一個聊天窗口,點擊好友的頭像。2、打開好友個人主頁後,點擊右上...
2026-10-27
蘋果手機充不進電怎麼辦
蘋果手機充不進電怎麼辦
1、蘋果手機無法充電可能是手機充電口不行了,也可能是接觸口的問題。建議使用清潔工具,對USB接觸口進行清潔,再進行測試,最好用酒精擦拭。2、如果測試後還是無法充電,有可能是充電頭與電源插座接觸不良造成的。建議更換插座,或者借别人的數據線來試一下,如果可以充電,說明是數據線壞了。3、非原裝充電器,充電線損壞。這個好辦,問人家要一個原裝的iPhone充電器和充電線一試就知道了。4、電池管理IC損壞。這
2026-10-27
手機卡住了屏幕不動也沒法關機怎麼辦
手機卡住了屏幕不動也沒法關機怎麼辦
1、一般遇到手機突然卡住,也無法去滑動屏幕時那麼這很有可能是手機系統故障死機的原因。2、首先我們可以...
2026-10-27
Copyright 2023-2026 - www.tftnews.com All Rights Reserved