Published 5月 29, 2018 by with 0 comment

快快樂樂學Python - 09

Python Lab

今天學調用函數


1. Python有個內建的函數庫, 有大量的函數可以直接拿來使用.
Built-in Functions:
https://docs.python.org/3/library/functions.html


2. abs (): 就是數學上的絕對值, 一個數的絕對值永遠非負, 沒有負號.
>>> abs (10)
10
>>> abs (-10)
10
>>> abs (-1.1)
1.1
>>>


3. max (): 接收任意多個數, 並返回最大值.
>>> max (-2, -1, 0, 1, 2)
2
>>>


4. int (): 將數值轉成整數.
>>> int (1)
1
>>> int (1.9)
1
>>> int (-1.9)
-1
>>> int (a)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'a' is not defined
>>>


5. float (): 允許浮點數(帶小數點的數)
>>> float (1)
1.0
>>> float (1e3)
1000.0
>>> float (1e-3)
0.001
>>>


6. 可以把函數指向一個變量
>>> a = abs
>>> a (-1)
1
>>>


Example
# -*- coding: utf-8 -*-
#20170516
#09_01.py
#將輸入的十進位數字轉成2進位和16進位
a = int (input ('Input a number: '))
b1 = bin (a)
h1 = hex (a)
print (a,'Binary values: ',b1)
print (a,'Hexadecimal values: ',h1)


最初發表 / 最後更新: 2017.05.16 / 2018.05.29

0 comments:

張貼留言