Python函式練習

def say_hi(name,place):
    message = "我是{},來自{}!!".format(name,place)
    return message
say_hi("Ivan","Taiwan")
'我是Ivan,來自Taiwan!!'

List切割練習

L =[1,2,3,4,5,6,7]
L
[1, 2, 3, 4, 5, 6, 7]
L[0]
1
L[4]
5
L[:]
[1, 2, 3, 4, 5, 6, 7]
L[1:3]
[2, 3]
L[:3]
[1, 2, 3]
L[0:]
[1, 2, 3, 4, 5, 6, 7]

字串切割

str = "Hello"
str[1:]
'ello'
strChinese = "大家好啊"
strChinese[1:3]
'家好'
strChinese[0]
'大'

List快速生成

list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

list的L只能用小寫

List(range(10))
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-29-c3307e3cf0ce> in <module>
----> 1 List(range(10))


NameError: name 'List' is not defined
list(range(2,10))
[2, 3, 4, 5, 6, 7, 8, 9]
list(("ABCDEFG"))
['A', 'B', 'C', 'D', 'E', 'F', 'G']
a = 0
b = 10
c = list(range(a,b))
c
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

for迴圈

L = [1,2,3,4,5]
for i in L:
    print (i)
1
2
3
4
5
for i in range(0,3):
    print(i)
0
1
2
L = ["大","家","好"]
for i in L:
    print(i)
大
家
好
for i in range(1,5):
    print("*"*i)
*
**
***
****

字串中找關鍵字

keyword = "好"
word = "大家好"
keyword in word
True
keyword2 = "你"
keyword2 in word
False
L = [1,2,3,4,5]
2 in L
True
6 in L
False

if條件判斷

a = 10
if(a > 1):
    print("Yes")
Yes
key="好"
key2 = "你"
word = input(">>")
if(key in word):
    print("Yes!")
elif(key2 in word):
    print("Yes!!")
else:
    print("NO")
>>大家好
Yes!
key="好"
key2 = "你"
word = input(">>")
if(key in word):
    print("Yes!")
elif(key2 in word):
    print("Yes!!")
else:
    print("NO")
>>你是誰
Yes!!
key="好"
key2 = "你"
word = input(">>")
if(key in word):
    print("Yes!")
elif(key2 in word):
    print("Yes!!")
else:
    print("NO")
>>我是誰
NO

讀取套件

import numpy as np
np.sin(3)
0.1411200080598672
from numpy import cos
cos(3)
-0.9899924966004454

數據分析標準動作

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
np.pi
3.141592653589793
plt.plot([1,2,3,4,5,6])
[<matplotlib.lines.Line2D at 0x112691b10>]

png

plt.plot(np.random.randn(100))
[<matplotlib.lines.Line2D at 0x113dc4ad0>]

png