1.數據類型總括 ( c1.py )
# 代碼:是現實世界在計算機世界中的映射
# 寫代碼:就是將現實世界中的事物用計算機語言來描述
# 序列:str、list、tuple 序列是有序的
# 集合 set 集合是無序的
# Python基本數據類型
# Number, eg:1,1.1,-1,-1.1,0
# bool eg:False,True
# 字符串 str eg:'hello',"hello"
# 列表 list eg:[1,2],['he']
# 元組 tuple eg:(1,2)
# 集合 set eg:{1,2,3}
# 字典 dict eg:{key1:value1,key2:value2}
# 數字類型:
# int (整型) float (浮點型) complex (複數) bool (布爾類型,表示真和假)
# 非零表示bool真,只有零表示bool假
# 數據類型 type
print(type(1)) #int
print(type(1.1)) #float
# 加減,爲整型都是整型
print(type(1+1)) #int
print(type(1-1)) #int
# 整型 加(或減) 浮點型 爲浮點型
print(type(1+1.0)) #float
print(type(1-1.0)) #float
print(type(1*1)) #int
# 除法爲浮點型
print(type(1/1)) #float
# 整型 乘(或除) 浮點型 爲浮點型
print(type(1*1.0)) #float
print(type(1/1.0)) #float
# 如果兩個整型相除還想得到一個整型,用兩個斜槓即可 //
# 單斜槓 (除法,自動轉爲浮點型)
# 雙斜槓 (整除)
print(type(1/1)) #float 1.0
print(type(1//1)) #int 1
print(type(1//2)) #int 0
# complex (複數) 用 j 表示
print(36j)
# ~~~~~~~~~~~~~~~~數據類型小結~~~~~~~~~~~~~~~~~
# Python的基本數據類型
# 1.數字(Number)
# 1.1整型 int
# 1.2浮點型 float
# 1.3布爾型 bool
# 1.4複數 complex
# 2.組
# 2.1序列 (有序,可以用下標索引來訪問,切片操作(0:5))
# 2.1.1字符串 str (不可變)
# 2.2.2列表 list
# 2.2.3元組 tuple (不可變)
# 2.2集合 set 無序,沒有索引,不能切片
# 2.3字典 dic key:value鍵值對是其基本概念
2.進制間的轉換 (c2.py)
# 1.進制
# 10進制:滿10進1 0,1,2...8,9,10,11
# 2進制:滿2進1 0,1,10,11,100 (十進制的10和二進制的10不同)
# 8進制:滿8進1 0,1...6,7,10,11 (十進制的10和八進制的10不同)
# 16進制:滿16進1 0,1...9,A,B,C,D,E,F,10 (十進制的10和十六進制的10不同)
# 2.不同進制之間的轉換
# 2.1各個進制數的表示
# 2.1.1二進制 (數字前邊加0b)
print(0b10)
print(0b11)
# 2.1.2八進制 (數字前邊加0o)
print(0o10)
print(0o11)
# 2.1.3十六進制 (數字前邊加0x)
print(0x10)
print(0x11)
print(0x1F)
# 2.2進制間的轉換
# 2.2.1將別的進制的數轉爲二進制 bin()
print(bin(10)) #十進制轉10爲二進制
print(bin(0o7)) #八進制轉0o7爲二進制
print(bin(0xE)) #十六進制轉0xE爲二進制
# 2.2.2將別的進制的數轉爲十進制 int()
print(int(0b11)) #二進制轉0b11爲十進制
print(int(0o77)) #十六進制轉0o77爲十進制
# 2.2.3將別的進制的數轉爲十六進制 hex()
print(hex(888)) #十進制轉0b11爲十六進制
print(hex(0o777)) #八進制轉0o777爲十六進制
# 2.2.3將別的進制的數轉爲八進制 oct()
print(hex(0b111)) #二進制轉0b11爲八進制
print(hex(0x777)) #十六進制轉0b11爲八進制
3.bool (c3.py)
# bool (布爾類型)
# 其它類型都有一種類型與之對應
# True False (首字母大寫,否則會報錯)
print(type(True))
print(type(False))
print(int(True))
print(int(False))
# 非零表示bool真,只有零表示bool假
print(bool(1))
print(bool(1.1))
print(bool(-1))
print(bool(0))
# 字符串
print(bool('abc'))
print(bool(''))
# 列表
print(bool([1,2,3]))
print(bool([]))
# 集合
print(bool({1,2,3}))
print(bool({}))
# None
print(bool(None))
4.字符串 str (c4.py)
# 字符串 str
# 1.對於換行,可用成對的三個引號(單雙都可以)
# ss1 = '''
# hello
# hello
# '''
# ss2 = 'hello\nhello'
# ss3 = '''hello\nhello'''
# ss4 = 'hello\nhello'
# print(ss1)
# print(ss2)
# print(ss3)
# print(ss4)
# 2.轉義字符
# 2.1 \n : 換行 ; \r : 回車 ; \' :單引號 ; \t : 橫向製表符
# print('hello\\nhello')
# print('let\'s go')
# 2.2問磁盤路徑,這裏的\n需要轉義
# print('C:\normal\normal')
# 2.2.1 轉義字符
# print('C:\\normal\\normal')
# 2.2.2 如果路徑比加長,每個都加轉義字符斜槓,那麼會使字符串特別長,這裏還可使用 r (原樣輸出)
# 當一個字符串前邊加了 r 之後,他就不是一個普通字符串了,而是一個原始字符串
# print(r'C:\normal\normal')
# 3.字符串的運算
# 3.1 加:拼接 ; 乘:重複(乘幾就表示重複幾次)
# print('hellow '+'world')
# print('hellow'*3)
# 3.2 通過 [] 下標的方式,可以訪問字符串中的某個字符
# 正向:從0開始;
# 反向:從-1開始; [-n]:從末尾數第n項
# print(len('hello world'))
# print(('hello world')[0])
# print(('hello world')[4])
# print(('hello world')[-1])
# print(('hello world')[6])
# print(('hello world')[-5])
# 3.3截取一段字符
# (中括號包裹兩個數字,兩個數字之間通過冒號隔開) 通過 [起始位置:終了位置]
# 注意:終了位置(應該再往後加一位,例如下例,到截取字符的下一位)
# 負數表示 步長
# 省略冒號後邊:表示從冒號前邊的那個開始一直到結束
# 省略冒號前邊:表示從冒號後邊的那個數的前一位開始一直到最開始的位置
# print(('hello world')[0:5]) # hello
# print(('hello world')[0:-1]) # hello worl
# print(('hello world')[6:12]) # world
# print(('hello world')[6:]) # world
# print(('hello world')[-5:]) # world
# print(('hello pyton ruby #c java node')[6:]) # pyton ruby #c java node
# print(('hello pyton ruby #c java node')[-4:]) # node
# print(('hello pyton ruby #c java node')[:4]) # hell
# print(('hello pyton ruby #c java node')[:-4]) # hello pyton ruby #c java
# 3.4原始字符串 r/R (大小寫都可以)
# print(r'C:\\Windows\node\nopad')
# print(R'C:\\Windows\node\nopad')
5.列表 list (c5.py)
# 組 (在Python中表示組的概念的方式有多種)
# 列表 list
# 1.定義
# 組裏的各個成員可以是 數字,字符串,bool,還可以是列表 (不限於這幾種,還可加入其它類型)
# a1 = [1,2,3,4]
# print(type(a1))
# a2 = [[1,'hellow'],'hello',1,True]
# print(type(a2))
# 2.操作
# 2.1通過下標訪問 (參考字符串的方式)
# 2.1.1 如果下標只是一個數字,得到的是一個字符串
# 2.1.2 如果下標是兩個數字(用冒號隔開的),得到的是一個列表,即使改列表只有一個元素
# a3 = ['新月打擊','蒼白之瀑','月之降臨','月神衝刺']
# print(a3[2]) # 月之降臨
# print(a3[0:2]) # ['新月打擊', '蒼白之瀑']
# print(a3[-1:]) # ['月神衝刺']
# 2.2運算
# 2.2.1 加法(兩個列表合併爲一個列表)
# 2.2.2 乘法(兩個列表不能相乘;但列表和數字相乘,表示重複次數)
# 2.2.3 減法(兩個列表不能相減,會報錯)
a4 = ['新月打擊','蒼白之瀑','月之降臨','月神衝刺']
a5 = ['虛弱','點燃']
print(a4 + a5) # ['新月打擊', '蒼白之瀑', '月之降臨', '月神衝刺', '虛弱', '點燃']
print(a5*3) # ['虛弱', '點燃', '虛弱', '點燃', '虛弱', '點燃']
# 組的
a6 = [['巴西','墨西哥','喀麥隆','羅克地亞'],[],[],[],[]] # 列表的嵌套
6.元組 tuple (c6.py)
# 元組 tuple
# 1.各個元素 也可以爲 數字,字符串,bool
# a1 = (1,2,3,4)
# a2 = (1,'-1',False)
# print(type(a1))
# print(type(a2))
# 2.訪問方式
# 2.1通過[]下標的方式
a3 = (1,2,3)
a4 = (4,5,6)
# print(a3[0])
# 2.2中括號中兩個數字的方式
# print(a3[0:3])
# 2.3兩個元組相加
# print(a3+a4)
# 2.4元組乘以一個整數 (重複整數次數)
# print(a3*3)
# 3.序列 :str、list、tuple (一些共有的特性) 序列是有序的
# 3.1訪問 (可以銅鼓切片的方式 切片還可有第三個參數)
# print('hello world'[2])
# print([1,2,3,4,5][2])
# print([1,2,3,4,5][0:3])
# print([1,2,3,4,5][-1:])
# print('hello world'[0:8:2])
# 3.2操作,有 + 和 *
# 3.3判斷一個數是否在某個list中, 使用 in(在) not in(不在) 操作符 ,得到一個bool值結果
# print(1 in [1,2,3,4,5])
# print(1 not in [1,2,3,4,5])
# 3.4獲取列表的長度 使用 len() 函數
# print(len([1,2,3,4,5,6]))
# print(len('hello world'))
# 3.5求一個最大值,使用 max() 函數 ;求 最小值,使用 min() 函數 (這時比較的是 ASCII 值的大小 ,ASCII值通過 ord() 函數獲得 )
# print(max([1,2,3,4,5,6]))
# print(min([1,2,3,4,5,6]))
# print(max('helloworld'))
# print(min('helloworld'))
# print(ord('d'))
# print(ord('w'))
7.set (c7.py)
# 集合是無序的
# set
# 不支持下標訪問,不支持切片
# 1.定義:通過 {} 包裹
a1 = {1,2,3,4,5,6,}
# print(type(a1))
# 2.特性
# 2.1 無序 (不支持下標索引訪問,不支持切片)
# print(a1[2]) #報錯
# print(a1[0:2]) #報錯
# 2.2 不重複
# a2 = {1,1,1,2,2,2,3,3,3,}
# print(a2) #{1, 2, 3}
# 2.3支持長度判斷 len()
# print(len(a1))
# 2.4 支持 in 和 not in
# print(1 in a1)
# print(1 not in a1)
# 2.5支持 求兩個集合的 差集 (-)
# print({1,2,3,4,5,6}-{3,4}) #{1, 2, 5, 6}
# 2.6支持 求兩個集合的 交集 (&)
# print({1,2,3,4,5,6}&{3,4}) #{3, 4}
# 2.7求兩個集合的 並集 (|)
# print({1,2,3,4,5,6}|{3,4,7}) #{1, 2, 3, 4, 5, 6, 7}
# 2.8定義一個空的集合 並不能直接通過 一個空的花括號,而是應該通過set關鍵字
print(type({})) #dict
print(type(set())) #set
8.字典 dict (c8.py)
# 字典 dict {key1:value1,key2:value2}
# 1.定義
# 1.1很多個key和value,集合類型,字典有點像set,是無序的
# a1 = {1:1,2:2}
# print(type(a1)) #dict
# 1.2對於字典的value:str,int,float,list,set,dict
# 1.3對於字典的key可以:int,str (必須爲不可變類型)
# 1.4鍵名爲list報錯,鍵名爲tuple(元組)可以
# print({[1,2]:'hello'})
# print({(1,2):'hello'})
# 1.5空的字典,用{}花括號表示,空的set(集合)須用set()來定義
a2 = {'Q':'新月打擊','W':'蒼白之瀑','E':'月之降臨','R':'月神衝刺'}
# 2.操作
# 2.1不能通過下標索引訪問,不能通過切片,是無序的
# 2.2通過key來訪問value的形式,不能有相同的鍵key
# print(a2['Q'])
# a2 = {'Q':'新月打擊','Q':'蒼白之瀑','E':'月之降臨','R':'月神衝刺'}
# print(a2)
# print(a2['Q'])
# a3 = {'1':'新月打擊',1:'蒼白之瀑'}
# print(type(a3))
# 2.3這裏的1是鍵值而非下標索引,數字和字符串是不同的
# print(a3[1])
# print(a3['1'])
(備註:以上內容來自七月老師的學習筆記,僅作爲學習使用)