跳到主要內容


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-資料庫-mongodb-pymongo

Python 資料庫 mongodb-pymongo 安裝: linux、mac:pip3 install pymongo windows: import pymongo client = pymongo.MongoClient("mongodb://localhost:27017/") db = client['demo_db'] col = db['demo_col'] dict1 = { "name": "ab123ab456g", "day": "1890-04-05" } result = col.insert_one(dict1)  dict2 = [   { "name": "ki", "day": "1666-1-1"},   { "name": "aa", "day": "1222-11-11"},   { "name": "gg-gg", "day": "1333-02-22"},   { "name": "T-T", "day": "1444-03-02"},   { "name": "f-f", "day": "1555-01-01"} ] result = col.insert_many(dict2) result = col.find_one() print(result) results = col.find() for result in col.find(): print(result) results = col.find() query = {'...

語言學習-English-Lights

Song Lyric Title : Lights Singer :Ellie Goulding Album :  Bright Lights Release Date :  2011 I had a way then losing it all on my own I had a heart then but the queen has been overthrown And I'm not sleeping now the dark is too hard to beat And I'm not keeping up the strength I need to push me You show the lights that stop me turn to stone You shine it when I'm alone And so I tell myself that I'll be strong And dreaming when they're gone 'Cause they're calling, calling, calling me home Calling, calling, calling home You show the lights that stop me turn to stone You shine them when I'm alone Noises, I play within my head Touch my own skin and hope they'll still be there And I think back to when my brother and my sister slept In and on my place the only time I feel safe You show the lights that stop me turn to stone You shine it when I'm alone And so I tell myself that I'...

Python-開啟檔案-excel

Python 開啟檔案 excel 安裝: linux、mac :pip3 install openpyxl、pip3 install xlwt、pip3 install xlrd windows    :之後補上 程式碼說明 2007年版後 寫入 from openpyxl import Workbook,load_workbook wb = Workbook() ws = wb.active ws1 = wb.create_sheet('Mysheet') ws2 = wb.create_sheet('Mysheet', 0) ws1.title = 'New Title' print(wb.sheetnames) ws['A4'] = 4 ws.cell(row=4, column=2, value=10) wb.save('demo.xlsx')  讀取 wb_load = load_workbook('demo.xlsx')  sheet_ranges = wb_load['Sheet'] print(sheet_ranges['A4'].value) 2007年版前 寫入 import xlwt from datetime import datetime style0 = xlwt.easyxf('font: name Times New Roman, \ color-index red, bold on',num_format_str='#,##0.00') style1 = xlwt.easyxf(num_format_str='D-MMM-YY') wb = xlwt.Workbook() ws = wb.add_sheet('A Test Sheet') ws.write(0, 0, 124, style0) ws.write(1, 0, datetime.now(), sty...