物聯網平台開發人才平日班

物聯網平台開發人才平日班
【8/30 物聯網平台開發人才養成班】 僅剩最後2位名額,立即下載106年招生簡章!(待業者限定 平日密集培訓)

2015年8月3日 星期一

Python 入門學習快速筆記 (二)


python流程控制



if 

>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
...     x = 0
...     print('Negative changed to zero')
... elif x == 0:
...     print('Zero')
... elif x == 1:
...     print('Single')
... else:
...     print('More')
...
More

for 

>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
...     print(w, len(w))
...
cat 3
window 6
defenestrate 12




>>> for w in words[:]:  # Loop over a slice copy of the entire list.
...     if len(w) > 6:
...         words.insert(0, w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']

Range 函式


>>> for i in range(5):
...     print(i)
...
0
1
2
3
4

a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
     print(i, a[i])

0 Mary
1 had
2 a
3 little
5 lamb


while 

a = ['Mary', 'had', 'a', 'little', 'lamb']
i=0
while i < len(a):
    print(i, a[i])
    i+=1
0 Mary 1 had 2 a 3 little 4 lamb


Random函數

 import random   # 引用亂數module
 num= random.randint(1,100)  # 1~100
 print(num)



沒有留言 :

張貼留言