大家好,我是濤哥。
python基本數據類型
數字類型
各進位的表示與轉換
序列
str字符串
列表(list)
元組(tuple)
序列總結
集合(set)
字典(dict)
轉義字符
知識點總結
首先需要你的電腦安裝好了Python環境,並且安裝好了Python開發工具。
數字類型python中的數據類型==僅有int和float兩種==(沒有如short,long,double之分)
具體運算中數據的類型這裡查看各種情況的數據類型運用到了python中的==type函數==
print(type(1))
print(type(-1))
print(type(1.1111))
print(type(1+1))
print(type(1+1.0)) # 由於1.0為float類型,python將1+1.0自動轉化為float類型
print(type(1*1))
print(type(1*1.0))
# python中的除法使用'/'結果為float類型,使用"//"為int類型
print(type(2/2))
print(type(2//2))
print(type(1//2)) # 與其他語言類似python中整除會忽略小數點後數字運行結果:
==小結:==
1、只要式子中出現了浮點型數字(小數)最終整體類型都會變成float類型2、使用'/'結果為float類型,使用"//"為int類型(第一點優先級最高)布爾類型:bool==python中,布爾類型也是數字的一種==
不同情況下的布爾類型# bool類型包括 True和 False兩種
print(type(True))
print(type(False))
# 將bool類型轉換為int類型
print(int(True))
print(int(False))
# python中0為假,非0為真(無論進位)
print(bool(1))
print(bool(0))
print(bool(2.2))
print(bool(0b10))
# 對字符串取布爾值
print(bool('abc'))
print(bool(''))
# 對列表取布爾值
print(bool([1,2,3]))
print(bool([]))運行結果:
==小結==
數字中:0為False,其他均為True;在其他類型中:空為False,非空為True各進位的表示與轉換 進位的表示# 二進位標識符為 0b,列印輸出其代表的十進位數
print(0b10)
print(0b11)
# 八進位標識符為 0o,列印輸出其代表的十進位數
print(0o10)
print(0o11)
# 十六進位標識符為 0x,列印輸出其代表的十進位數
print(0x10)
print(0x1F)
# 輸入數字默認為十進位
print(10)結果:
==小結==:需牢記各種進位的表示形式
進位的轉換轉換為十六進位(hexadecimal):hex()# 轉換為二進位
print(bin(10))
print(bin(0o7))
print(bin(0xE))
# 轉換為八進位
print(oct(0b111))
print(oct(0x777))
# 轉換為十進位
print(int(0b111))
print(int(0o777))
# 轉換為十六進位
print(hex(888))
print(hex(0b111))
print(hex(0o7777))運行結果:
序列 str字符串字符串類型表示為單/雙引號內的內容,由於英文語句中可能出現單引號的情況(Let's go),此時可使用雙引號擴起字符串中的內容print("Let't go")
print('Let't go') # 其中此語句會報錯運行結果:
字符串的運算# 字符串的運算
print("he"+"llo")
print("hello"*3)結果
獲取單個字符在str後添加[i](i代表想要獲取str中的位置下標),可獲取指定位置字符# 輸出指定位置的字符
print("hello world"[0])
print("hello world"[1])
print("hello world"[2])
print("hello world"[-1])
print("hello world"[-2])結果
其中==i可為負數==,代表獲取倒數第i位的數字
獲取指定區間內的字符串使用『:』對起始位置和末尾位置進行連接,如:[m:n+1]表示截取str中下標為==m~n+1==的字符串進行截取(==n+1位置取開區間==)[m:]則表示由下標為m的位置一直截取到末尾# 截取指定區域的字符串
print("hello world"[0:5])
print("hello world"[-5:11])
print("hello world"[-5:])
列表(list)列表存儲的數據類型# 列表可存儲的類型
print(type([1, 2, 3, 4, 5]))
print(type(["hello", 1, False]))
print(type([[1, 2], [3, 4], [True, False]])) # 嵌套列表
讀取列表中的元素# 讀取列表中的元素
print(["hello", "world"][0:]) # 和str類型的讀取方式相同
列表的運算# 列表的運算(和str的運算相似)
print(["hello", "world"] + ["hello", "world"])
print(["hello", "world"] * 3)
元組(tuple)元組的基本使用方法,存儲數據的規則和列表相同,它們的區別主要在以下幾點:列表是動態數組,它們可變且可以重設長度(改變其內部元素的個數)。元組是靜態數組,它們不可變,且其內部數據一旦創建便無法改變。元組緩存於Python運行時環境,這意味著我們每次使用元組時無須訪問內核去分配內存。對元組的基本操作# 元組存儲是數據類型
print(type((1, 2, 3, 4, 5)))
print(type((1, 2, "hello", [1, 2, 3], True)))
# 獲取指定位置元素
print((1, 2, 3, 4)[2])
# 獲取指定區域元素
print((1, 2, 3, 4)[1:])
print(type((1, 2, 3, 4)[1:])) # 返回類型為tuple
# 元組的運算
print((1, 2, 3, 4)+(5, 6))
print((1, 2, 3, 4)*2)
序列總結提取指定區間元素方法:序列後面緊跟[m : n](m,n為區間下標,n為開區間)==序列這種類型對其中的元素操作方法完全相同==
集合(set) 集合中的數據是==無序的==,故==不能用下標==進行訪問集合的運算# 求兩個集合的差集
print({1, 2, 3, 4, 5, 6} - {2, 3}) # '-'為求差集的符號
# 求兩個集合的交集
print({1, 2, 3, 4, 5, 6} & {2, 3}) # '&'為求交集的符號
# 求兩個集合的併集
print({1, 2, 3, 4, 5, 6} | {5, 6, 7}) # '-'為求差集的符號>>> set()
字典(dict) 字典和集合定義的區別:dict:{key1:value1,key2:value2}set:{value1,value2....}字典中的鍵==不能重複也不可變==(如list列表就是可變類型)# 字典類型的輸入格式
print(type({1: 1, 2: 2, 3: 3}))
# 字典的使用
print({1:"Hello", 2:"world"}[2])
轉義字符 知識點總結