Information for Authors resource now available
17 ปีที่ผ่านมา
import python for everyone in thailand: happycoding()
goodbye
hello
see you later!
import fileinput
for lines in fileinput.FileInput("inputfile.txt", inplace=1): ## edit file in place
lines = lines.replace("hello","hi")
print lines.replace('\n','') ## although this prints the line, the file will be modified.
goodbye
hi
see you later!
>>> import random
>>> random.random() # เป็นการสุ่ม ตัวเลขที่มีทศนิยม (float) ระหว่าง 0.0 <= x < 1.0
0.37444887175646646
>>> random.uniform(1, 10) # เป็นการสุ่ม ตัวเลขที่มีทศนิยม (float)ระหว่าง 1.0 <= x < 10.0
1.1800146073117523
>>> random.randint(1, 10) # เป็นการสุ่ม ตัวเลขจำนวนเต็ม (Integer) ระหว่าง 1 ถึง 10
7
>>> random.randrange(0, 101, 2) # เป็นการสุ่ม ตัวเลขจำนวนเต็ม (Integer) ระหว่าง 1 ถึง 100 แต่จะสุ่มตามเลขที่เพิ่มค่าทีละ 2
26
>>> random.choice('abcdefghij') # สุ่มเลือกตัวอักษร
'c'
>>> items = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(items) # สลับค่าใน list แบบสุ่ม
>>> items
[7, 3, 2, 5, 6, 4, 1]
>>> random.sample([1, 2, 3, 4, 5], 3) # เลือกค่าใน list มา 3 ค่า
[4, 1, 5]
>>>import re
>>>pat = 'Hello'
>>>raw = 'Hello world. Hello you.'
>>>re.compile(pat)
>>><_sre.SRE_Pattern object at 0x01357500>
>>>re.findall(pat,raw)
>>>['Hello', 'Hello']
>>>re.finditer(pat,raw)
>>>
>>>re.match(pat,raw)
>>><_sre.SRE_Match object at 0x01FA7BF0>
>>>re.search(pat,raw)
>>><_sre.SRE_Match object at 0x01FA7BF0>
>>>re.split(pat,raw)
>>>['', ' world. ', ' you.']
>>>re.sub(pat,'Sawasdee', raw)
>>>'Sawasdee world. Sawasdee you.'
>>>re.subn(pat,'Sawasdee',raw)
>>>('Sawasdee world. Sawasdee you.', 2)
>>>re.template(pat)
>>><_sre.SRE_Pattern object at 0x013576A0>
>>> import re
>>> my_pattern = 'a{2}'
>>> raw_string = 'a aa a aa'
>>> re.sub(my_pattern,'-',raw_string)
'a - a -'
>>> my_pattern = 'a{2,4}'
>>> raw_string = 'a aa aaa aaaa aaaaa'
>>> re.sub(my_pattern,'-',raw_string)
'a - - - -a'
>>> my_pattern = '[A-Z]'
>>> raw_string = 'Apple Banana'
>>> re.sub(my_pattern,'-',raw_string)
'-pple -anana'
>>> my_pattern = '[c|b]at'
>>> raw_string = 'cat rat bat'
>>> re.sub(my_pattern,'-',raw_string)
'- rat -'
>>> my_pattern = '(ab)'
>>> raw_string = 'aa ab bb'
>>> re.sub(my_pattern,'-',raw_string)
'aa - bb'
>>> my_pattern = '(ab)+'
>>> raw_string = 'aa ab abab baba'
>>> re.sub(my_pattern,'-',raw_string)
'aa - - b-a'
>>> import re
>>> my_pattern = '.at'
>>> raw_string = 'I love rat and cat'
>>> re.sub(my_pattern,'dog',raw_string)
'I love dog and dog'
>>> my_pattern = '^dog'
>>> raw_string = 'dog dog dog'
>>> re.sub(my_pattern,'cat',raw_string)
'cat dog dog'
>>> my_pattern = 'dog$'
>>> raw_string = 'dog dog dog'
>>> re.sub(my_pattern,'cat',raw_string)
'dog dog cat'
>>> my_pattern = 'ab*'
>>> raw_string = 'aa , abb , abbb , ba , bb , bc'
>>> re.sub(my_pattern,'-',raw_string)
'-- , - , - , b- , bb , bc'
>>> my_pattern = 'ab+'
>>> raw_string = 'aa , abb , abbb , ba , bb , bc'
>>> re.sub(my_pattern,'-',raw_string)
'aa , - , - , ba , bb , bc'
>>> my_pattern = 'ab?'
>>> raw_string = 'aa , abb , abbb , ba , bb , bc'
>>> re.sub(my_pattern,'-',raw_string)
'-- , -b , -bb , b- , bb , bc'
>>>import re
>>>my_pattern = '\AMister'
>>>raw_string = 'Mister python and Mister Thailand'
>>>re.sub(my_pattern,'MR.',raw_string)
'MR. python and Mister Thailand'
>>>import re
>>>s = '100 NORTH BROAD ROAD'
>>>re.sub(r'\bROAD\b', 'RD.', s)
'100 NORTH BROAD RD.'
>>>my_pattern = '\BROAD'
>>>raw_string = 'BROAD ROAD'
>>>re.sub(my_pattern,'RD.',raw_string)
'BRD. ROAD'
>>>my_pattern = '\d'
>>>raw_string = 'credit number 1234-1234-1234'
>>>re.sub(my_pattern,'x',raw_string)
'credit number xxxx-xxxx-xxxx'
>>>my_pattern = '\D'
>>>raw_string = 'credit number 1234-1234-1234'
>>>re.sub(my_pattern,'x',raw_string)
'xxxxxxxxxxxxxx1234x1234x1234'
>>>my_pattern = '\s'
>>>raw_string = 'a b c d e'
>>>re.sub(my_pattern,'_',raw_string)
'a_b_c_d_e'
>>>my_pattern = '\S'
>>>raw_string = 'a b c d e'
>>>re.sub(my_pattern,'_',raw_string)
'_ _ _ _ _'
>>>my_pattern = '\w'
>>>raw_string = 'a!b#c%d&e.'
>>>re.sub(my_pattern,'_',raw_string)
'_!_#_%_&_.'
>>>my_pattern = '\W'
>>>raw_string = 'a!b#c%d&e.'
>>>re.sub(my_pattern,'_',raw_string)
'a_b_c_d_e_'
>>>my_pattern = 'Mister\Z'
>>>raw_string = 'Mister and Mister'
>>>re.sub(my_pattern,'MR.',raw_string)
'Mister and MR.'
>>>import re
>>>s = '100 NORTH BROAD ROAD'
>>>re.sub(r'\bROAD\b', 'RD.', s)
'100 NORTH BROAD RD.'
>>> s = '100 NORTH MAIN ROAD'
>>> s.replace('ROAD', 'RD.')
'100 NORTH MAIN RD.'
>>> s = '100 NORTH BROAD ROAD'
>>> s.replace('ROAD', 'RD.')
'100 NORTH BRD. RD.'
>>>import py_compile
>>>py_compile.compile("a1.py")
>>>import compileall
>>>compileall.compile_dir("dir_name", force=1)
python setup.py install
for i in range(0,5):
print 'Hello python'
print 'Hello python'