所有的非10進制數字都要以字串的型式表達,而數字一律是10進制表示。
因此進制轉換就只是字串轉數字或數字轉字串的差別。以下就針對這兩種狀況來說明。
● 字串轉數字
>>> int ('0x10', 16) #必須告訴直譯器字串是哪種進制
16
bloggerads
2017年8月14日 星期一
2017年8月13日 星期日
Python : Tkinter (Button, Label, Entry, Listbox)
Tkinter 並不是一個很強大的GUI library, 但優點是, 他是Python官方支援的module, 且大部分的OS都有預先安裝它<Note 1>。 之後陸續出現的第三方GUI library相信都是由他啟發的。因此如果不是開發太複雜的GUI tool, 又想了解這個歷史悠久的library, 那就值得來了解他。
話不多說,直接上code:
step 1. 先來創一個空的視窗
from Tkinter import *
win = Tk()
win.title("Tk Gui")
win.resizable(width=False, height=False)
win.geometry("500x250")
win.mainloop()
話不多說,直接上code:
step 1. 先來創一個空的視窗
from Tkinter import *
win = Tk()
win.title("Tk Gui")
win.resizable(width=False, height=False)
win.geometry("500x250")
win.mainloop()
Python : 調用模組的語法比較
以調用random模組中randint函數,印出隨機數字0~10來作範例
1. 函數呼叫透過: 模組.函數()
import random
print random.randint(0,10)
print random.randint(0,10)
2. 直接呼叫函數(), 但必須告訴直譯器調用模組中的哪個函數
from random import randint
print randint(0,10)
3. 直接呼叫函數(), 直接告訴直譯器要引用模組中所有的函數。 會有函數名稱衝突的風險!
from random import *
print randint(0,10)
4. 在script中定義模組的新名稱, 呼叫方式: 模組新名稱.函數()
import random as newname
print newname.randint(0, 10)
print randint(0,10)
4. 在script中定義模組的新名稱, 呼叫方式: 模組新名稱.函數()
import random as newname
print newname.randint(0, 10)
2017年8月11日 星期五
Python : tuple, list, set, dict 整理與比較
python常用的四種container types: tuple, list, set dictionary
tuple: ()
- save in order
- read only
list: []
- save in order
set: {}
dictionary: {key:value}
NOTE:
t = tuple([1,2,3])
>>>t[0]
1
Python : Use dictionary to simulate multi-dimension array
Since there is no array in Python. Use dictionary instead.
Follows are the example to initialize a 2x2 array
>>>a={}
>>>for i in range(4):
. . . a[i/2, i%2] = i
>>> a
{ (0,1):1, (1,0):2, (0,0):0, (1,1):3 }
>>>a[0,1]
1
Follows are the example to initialize a 2x2 array
>>>a={}
>>>for i in range(4):
. . . a[i/2, i%2] = i
>>> a
{ (0,1):1, (1,0):2, (0,0):0, (1,1):3 }
>>>a[0,1]
1
2017年8月9日 星期三
Python : string related
### Demonstrate join
>>> list = ['Nice', 'to', 'meet', 'you']
>>> ''.join(list)
Nicetomeetyou
>>> ' '.join(list)
Nice to meet you
>>>'_'.join(list)
Nice_to_meet_you
>>> list = ['Nice', 'to', 'meet', 'you']
>>> ''.join(list)
Nicetomeetyou
>>> ' '.join(list)
Nice to meet you
>>>'_'.join(list)
Nice_to_meet_you
2017年8月6日 星期日
Python : Exception Handling
以parsing文字檔的內容來做例外處理的範例, parsing過程中其它列的內容可能和原本預設格式不同, 此時就會有例外產生:
with open('a.txt', 'r') as f:
badLine = 0
goodLine = 0
totalLine = 0
for line in f:
try:
d1, d2, d3 = line.split()
except:
badLine += 1
else:
goodLine += 1
finally:
totalLine += 1
print('totalLine : {}, badLine: {}, goodLine: {}'.format(str(totalLine), str(badLine), str(goodLine)))
with open('a.txt', 'r') as f:
badLine = 0
goodLine = 0
totalLine = 0
for line in f:
try:
d1, d2, d3 = line.split()
except:
badLine += 1
else:
goodLine += 1
finally:
totalLine += 1
print('totalLine : {}, badLine: {}, goodLine: {}'.format(str(totalLine), str(badLine), str(goodLine)))
2017年8月4日 星期五
Python : class method / static method / method
This is a good example for showing the difference between class method, static method and method in Python.
class Demo: y=1 # class (or static) variable def __init__(self,z): self.z = z @classmethod def class_method(cls, x): return x + cls.y @staticmethod def static_method(x): return x #cannot invoke cls.z def method(self, x): return x + self.z print Demo.class_method(2) # show 3 print Demo.static_method(2) # show 2 #print Demo.method(2) #Error, must declare first demo = Demo(2) print demo.method(2) # show 4
2017年8月3日 星期四
Python : dictionary
Follows are the Dictionary example. Key and value can be string or number.
● Initial a dictionary
>>> d={'a':'Martin', 'b':'John'}
● Add new item
>>> d['c']='Mary'
● Add items
>>> d.update({'d':'Megan', 'e':'May'})
● Check item in a dictionary (return True/False)
>>> 'd' in d
True
>>> 'f' in d
False
● Enumerate the items in a dictionary
>>> for id, name in d.items():
... print id, name
● Delete an item
>>> del d['a']
● Initial a dictionary
>>> d={'a':'Martin', 'b':'John'}
● Add new item
>>> d['c']='Mary'
● Add items
>>> d.update({'d':'Megan', 'e':'May'})
● Check item in a dictionary (return True/False)
>>> 'd' in d
True
>>> 'f' in d
False
● Enumerate the items in a dictionary
>>> for id, name in d.items():
... print id, name
● Delete an item
>>> del d['a']
訂閱:
文章 (Atom)