變數宣告
宣告函式(function)
呼叫內建的print函式
利用tab空白來分段
回到原本的排版結束宣告
呼叫剛剛建立的say_hello
並傳入參數'NISRA'字串
name = 'BZ'
def say_hello( where ):
print 'Hello' , name + '@' + where + '!'
say_hello( 'NISRA' )
>>> say_hello('NISRA')
hello BZ@NISRA!
n = 0
f = 9.99
str = '一串字串'
fruit = 'apple'
fruit = 0
#print '我被註解了O_Q'
print '我沒被註解!'
#print '我又被註解了Q_Q'
n=9
m=10
''' print '從這行開始'
n=0
m=1
print '一直到這邊' '''
print n, m
9 10
if
<CONDITION>: #做某些事
elif <ANOTHER CONDITION>:
#做其他事 else:
#做剩下的事
while <CONDITION>:
#一直做某些事
>>> 2 < 1
False
>>> 'abc' is not 'qoo'
True
>>> 'a' in 'apple'
True
>>> "a" is 'a'
True
if True: print 'hello'
^^^^四個空格
if True: print 'hello'
(^T )一個TAB
for i in range(10):
print i, #拿掉,會變成換行
Output:
0 1 2 3 4 5 6 7 8 9 10
for c in 'hello': print c
for fruit in ['apple','melon','banana']:
print fruit
c = 0
if True:
while c<20:
for x in range(40):
if x<22:
c = x
print x,
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
str = "Hello, I'm a STRING"
len(str) #length of str
>>> str[4]
'e'
>>> str[7:10]
"I'm"
>>> c[-1] 往回數
's'
>>> c[-6:-1] 一樣是往回數,從第-6到第-1個 'Thank'
>>> c[11:] 第11之後的所有項目(字) 'Thanks'
>>> c[:9] 第9之前的所有項目(字) '123456789'
>>> c[0:11:2] 從0到11之間,「間隔2」的所有項目 '135790'
>>> c[::-1] 所有字串倒著取回來 'sknahT01987654321'
>>>count_list[0]
1
>>>count_list[:10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> li=[8,1,9,10,60,14,5,6]
>>> print sorted(li)
[1, 5, 6, 8, 9, 10, 14, 60]
>>> print sum(li)
113
>>> print max(li)
60
#pip install PIL
import urllib2
response = urllib2.urlopen('http://python.org/')
html_src = response.read()
讀入模組
輸入網址
開啟連結
讀取原始碼
找到密碼在原始碼中的位置
用slice取得原始碼中的密碼
密碼是用get方式輸入回傳
所以一樣透過網址+參數
印出結果
import urllib2
url = 'http://bz.weco.net:1337/'
res = urllib2.urlopen(url)
html_src = res.read()
pw_start = html_src.find('<password>')+10
pw_end = html_src.find('</password>')
pw = html_src[pw_start:pw_end]
login = urllib2.urlopen(url+'?pw='+pw)
answer = login.read()
print answer