跳到主要內容


Python-變數

Python

資料型態概念說明:

參考資料型態概念-變數內容

Python的資料型態分為無型態、布林數、整數、複數、浮點數、字串六種,None為變數沒存任何狀態,在整數上有四種形式分別為十進位、二進位、八進位、十六進位,在浮點數有兩種表示法分別為小數形式、科學記號形式,字串預設為utf8格式,支援ascii、big5等,如果遇到檔案不是標準的格式建議搜尋一下看是否支援。
Python又有容器資料型態,又可看為資料結構有串列、字典、Tuple、Set四種。
  • 無型態:None
  • 布林數:True、False
  • 整數:10、0b01、0o10、0xAA
  • 複數:1+1j
  • 浮點數:1.1、1e+10
  • 字串:'string'
  • 串列:List = [1,2,3]
  • 字典:{1:1,2:2,:3:3},{1:'1',2:'2',:3:'3'},{'1':'1','2':'2',:'3':'3'}
  • Tuple:(1,2,3),('1','2','3')
  • Set:{'a','b'},{1,2}

基本語法

在直譯器下可以直接使用以下語法進行測試
None
True
False
10
0b10
0o10
0xAA
1+1j
1.1
1e+10
'string'

再用type函式進行檢視各種型態
type(None)
type(True)
type(False)
type(10)
type(0b10)
type(0o10)
type(0xAA)
type(1+1j)
type(1.1)
type(1e+10)
type('string')
type("string')
type('''string string string''')

變數定義

  • 無型態:a = None
  • 布林數:a = True
  • 整數:a = 10
  • 複數:a = 1+1j
  • 浮點數:a = 1.1
  • 字串:a = 'string'
在用type函數去測試a變數的型態

數值運算

      整數、複數、浮點數都適用。

  1. 加法:1+2
  2. 乘法:3*2
  3. 減法:2-1
  4. 除法:2/3
  5. 餘數:3%2
  6. 次方運算:2**2
  7. 除法商取整數:3//2


邏輯運算

  1. and:True and True
  2. or  :True or False
  3. not:not True

比較運算

  1. 大於:2 > 1
  2. 大於等於:2 >= 2
  3. 等於: 2 == 2
  4. 小於: 2 < 1
  5. 小於等於: 2 <= 1

位元運算

a=0b00110011
b=0b00111100

  1. and:bin(a&b)
  2. or  :bin(a|b)
  3. not:bin(~a)
  4. xor:bin(a^b)
  5. shift to left:bin(a<<2)
  6. shift to right:bin(2>>a)

指派運算

      指派運算就是賦予變數數值的運算,如數字運算指派或是位元運算指派可以減少程式撰寫內容,以加法為例如原本a=0,而a+1並存入a,寫成 a = a + 1,可以縮減成 a+=1,其他運算也是使用相同寫法,
  1. 等於指派:a = 2
  2. 數字運算指派: a += 2
  3. 位元運算指派: a >>= 2

字串運算

  • 字串串連:str1 + str2
  • 字串重複:str ** 2
  • 獲取字串特定位置資料: str[0]、str[1:4]、str[-1]>
串列
用中括號包著,可以當作堆疊、佇列,中間使用逗號分隔。

字典

用大括號包著,中間使用逗號分隔,index可以數字、字串。

Tuple

用小括號包著,中間使用逗號分隔。

Set

其儲存內容不能夠重複,用大括弧包著,中間使用逗號分隔,可以用集合概念來運算。

容器資料型態的運算

  • 判斷資料是否存於容器內 : 'a' in list1
  • 判斷資料是否不存於容器內:'a' not in list1

變數定義

在Python這個語言是不需要先行定義變數的的性態的,如要存整數只要a=2即可,也是所謂的泛型

資料型態轉換

數值轉換


float->int:nt(3.14)

int->floatflaot(3)
negative->positiveabs(-3)
int->complex:complex(3)
flaot->complex:complex(3.14)

資料結構轉換

list -> dict
list -> tuple
list -> set

list4 =[['a','we'],['b','are'],['c','a'],['d','family']]
dict2 = dict(list4)
print(dict2)

list5 = [2,3,4,3,43,'232']
tup1 = tuple(list5)
tup2 = tuple(list4)
print(tup1)
print(tup2)

list1 = [2,3,44,3,3,3,33,3,3,23]
set1 = set(list1)
print(set1)
dict -> list
dict -> tuple

dict4 = {'a':1,'b':2,'c':3}
list8 = list(dict4.keys())
print(list8)
list9 = list(dict4.values())
print(list9)


dict5 = {'age':23,'name':'zhangsan','school':'ssdsd','money':230.2323}
tup5 = tuple(dict5)
print(tup5)
tup6 = tuple(dict5.values())
print(tup6)



tuple -> list

tuple -> dict
tuple -> set

tup7 = ('232','23232',2323,'weasdfasd')
list9 = list(tup7)
print(list9)
tup9 = (('a',1),('b',2),('c',3),('d',4))
dict6 = dict(tup9)
print(dict6)
tup8 = ('2323','sdfsdf','2323','sdds','wwe','2323',23,23.33)
set6 = set(tup8)
print(set6)


set -> tuple

set -> list



set4 = set('asdfasdfasdf323')
tup3 = tuple(set4)
print(tup3)
set5 = set([2,3,43,43,433,323,2,43])
tup4 = tuple(set5)
print(tup4)


set2 = set('asdfasdfs2323sdfasdf')
list6 = list(set2)
print(list6)


 投影片-slideshare:Python_變數
 影片-youtube:Python_變數
 程式碼-Github:Python_變數  
下一單元:Python-條件敘述

留言

這個網誌中的熱門文章

Python-開啟檔案

Python 開啟檔案 基本概念說明 參考 程式語言概念-常見檔案型態 基本常見檔案類型 二進位檔 文字檔 CSV XML JSON html excel word 圖片 音源 影片 以下是處理該類型檔案對應函式或模組 這邊內建函數的意思是讀取之後能直接處理。 檔案類型 內建函數 標準模組 非標準模組 二進位檔 open() None - 文字檔 open() None - CSV None csv - XML None xml - JSON None json - html None html - excel None None 非 windows excel api windows excel api word None None 非 windows word api windows word api 圖片 None None pypng 音源 None wave - 影片 None None moviepy 二進位檔程式碼範例 二位元的定義在Python官網的資料型態沒有定義,但是還是可以使用的需要用函式轉換才能夠出現,分別用bytes、bytearray兩種,在使用前可以先盡到直譯器上,用help指令查訊該function的功能,以下是其內容。 在前面先講bytes跟bytearray用法,後續再講數字、字串轉成bytes的方法,最後才是進行二進位檔案讀寫。 bytes 再把資料料轉換時輸入內容可分成一下種類 整數 字串 可迭代資料:迭代內容一定要是數字 buffer:這邊不是示範,因為寫python沒用過 bytes(1) bytes(2) bytes(3) bytes(4) bytes('str'.encode('utf8')) bytes('str'.encode('ascii')) bytes([0,1,255]) bytes((2)) bytes((1,2)) bytes({1,2}) 程式碼說明 bytes()中代入數字是告知bytes數量,如:bytes(1)就是一個bytes量 bytes()中代入字串時,附加編...

Python-設計模式-共享模式

Python 設計模式 共享模式 class Font:     def __init__(self):        self.Size = 0        self.Type = ''     def printAll(self):        print(self.Size, self.Type)  class FontFacotry:     def Word(self, Size=3, Type='1'):        F = Font()        F.Size = Size        F.Type = Type        return F  FontSize = [1,2,3] FontType = ['1','2','3'] Facotry = FontFacotry()  F1 = Facotry.Word(FontSize[0],FontType[0])  F1.printAll()  F2 = Facotry.Word( FontSize[1],FontType[1] ) F2.printAll()  F3 = Facotry.Word( FontSize[2],FontType[2] ) F3.printAll() 程式碼說明 font 定義類別 fontFacotry物件生成工廠 fontsize用來儲存font大小的外部空間 fonttype用來儲存font種類的外部空間

語言學習-English-The Scientist

Song Lyric Title :  The Scientist Singer :Coldplay Album :A Rush of blood to the head Release Date :  2002 Come up to meet you Tell you I'm sorry You don't know how lovely you are I had to find you Tell you I need you Tell you I set you apart Tell me your secrets And ask me your questions Oh let's go back to the start Running in circles, coming up tails Heads on a science apart Nobody said it was easy It's such a shame for us to part Nobody said it was easy No one ever said it would be this hard Oh take me back to the start I was just guessing at numbers and figures Pulling your puzzles apart Questions of science, science and progress Do not speak as loud as my heart Tell me you love me Come back and haunt me Oh and I rush to the start Running in circles, chasing our tails Coming back as we are Nobody said it was easy Oh it's such a shame for us to part Nobody said it was easy No one...