iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 5
1
AI & Data

python 入門到分析股市系列 第 5

[Day05]Python 基礎語法 - function、import

導讀

前言

今天是鐵人競賽第5天,前兩天介紹了Python的基本資料型別、序列結構、程式迴圈。今天要來介紹

  1. 自己定義函式 function
  2. 引用模組 import

function

函式(function):可以指派他們參數,也可以把他們當作引數傳給其他函式,以及回傳值。
語法: def functionName():
一樣用「:」和縮排來表示function區塊

def GetArea(height,width):
    area = width * height
    return area
ret1 = GetArea(6,9)
print('ret1--->',ret1)
# 輸出結果
ret1---> 54
  • 輸入參數名稱
    呼叫function時可以直接輸入參數名稱,這樣可以不用管參數的順序
    以下範例示範GetArea這個function,第一個參數是height,第二個是width,
    可是呼叫時指定參數名稱,就可以不用管順序。
def GetArea(height,width):
    area = width * height
    print('height--->',height)
    print('width--->',width)
    return area
ret1 = GetArea(6,9)
ret2 = GetArea(width=6, height=9)
# 輸出結果
height---> 6
width---> 9
height---> 9
width---> 6
  • 參數預設值
    參數可以使用預設值,(有設值的參數必須是參數串列的最後)
def rightFunction(first, second = 12):
    return first
def wrongFunction(first=12, second):  # 出錯 non-default argument follows default argument
    return first
  • 不定數目參數涵式
    可用在不確定參數會有多少時
def sumNumber(*params):
    total = 0
    for param in params:
        total +=param
    return total

print("2個參數: sumNumber(7,8) = %d" % sumNumber(7,8))
print("3個參數: sumNumber(9,7,8) = %d" % sumNumber(9,7,8))
print("2個參數: sumNumber(7,8,12,11) = %d" % sumNumber(7,8,12,11))
# 輸出結果
2個參數: sumNumber(7,8) = 15
3個參數: sumNumber(9,7,8) = 24
2個參數: sumNumber(7,8,12,11) = 38
  • 將function當作參數傳遞
def run_otherFunc(func):
    func(7,8)
run_otherFunc(GetArea)
# 輸出結果
height---> 7
width---> 8

全域變數和區域變數

若有相同名稱的全域和區域變數,以區域變數為優先
以下範例示範在scope這個function裡面沒有定義變數var2,
可是因為var2是全域變數所以在scope裡面還是可以讀取到var2

def scope():
    var1 = "區域變數"
    print(var1, var2) # 輸出為 區域變數 20
var1 = "全域變數"
var2 =20
scope()
print(var1, var2) # 輸出為 全域變數 20
# 輸出結果
區域變數 20
全域變數 20

python的內建函數

python除了可以自己定義function,還有python內建函數,以下列出比較常用的函數

example1 = abs(-10)
print(example1)  #取得絕對值 輸出 10
example2 = chr(66)
print(example2)  #取得整數66的字元 輸出 B
example3 = divmod(33,6)
print(example3)  #取得33除以6的商數和餘數 輸出 (5, 3)
example4 = float(33)
print(example4)  #轉成浮點數 輸出 33.0
example5 = hex(33)
print(example5)  #轉成16進位 輸出 0x21
example6 = int(22.79)
print(example6)  #轉成整數(無條件捨去) 輸出 22
example7 = oct(33)
print(example7) #轉成八進位 輸出 0o41
example8 = ord("B")
print(example8) #取得Unicode編碼 輸出 66
example9 = pow(2,5)
print(example9) #取得2的5次方 輸出 32
example10 = round(22.79)
print(example10)  #轉成整數(四捨五入) 輸出 23
example11 = sum([11,22,33,44])
print(example11)  #取得list的總和 輸出 110

import package

如果想要引用其他module裡面的function、class、variable。就需要使用import的方式引用進來。
類似要引用Nunpy、Pandas、Matplotlib...
import的方法有四種
1.

   import 套件名稱
套件名稱.函數名稱 # 使用函數的方式
  1. 使用套件的函數可以不必輸入套件名稱
from 套件名稱 import *
函數名稱 # 使用函數的方式
  1. 如果兩個套件有相同的函數名稱,可以使用第三種方法
from 套件名稱 import 涵式1, 涵式2, .....
涵式1 # 使用函數的方式
  1. 將套件名稱取成另外一個簡短的別名
from 套件名稱 as 別名
別名.函數名稱 # 使用函數的方式

上一篇
[Day04]Python 基礎語法 - list、tuple、dict、for、while
下一篇
[Day06]儲存資料 - File、檔案類型(csv、xml、json、yaml)
系列文
python 入門到分析股市30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言