Readability counts.
The Zen of Python, Tim Peters
def function_name(輸入, 參數, ...):
"""
Docstrings
"""
# 做些什麼事
return 輸出
# Define
def get_abs(x):
"""
取得 x 的絕對值。
"""
if x < 0:
return -x
else:
return x
# Use
print(help(get_abs))
print(get_abs(-5556))
print(get_abs(5566))
Help on function get_abs in module __main__: get_abs(x) 取得 x 的絕對值。 None 5556 5566
def divide(x, y):
"""
將輸入的兩個數字相除
"""
return x / y
print(divide(5566, 0))
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-3-400a2a8f876f> in <module> 5 return x / y 6 ----> 7 print(divide(5566, 0)) <ipython-input-3-400a2a8f876f> in divide(x, y) 3 將輸入的兩個數字相除 4 """ ----> 5 return x / y 6 7 print(divide(5566, 0)) ZeroDivisionError: division by zero
try...except...
處理錯誤與例外¶def safe_divide(x, y):
"""
將輸入的兩個數字相除
"""
try:
return x / y
except:
return "Something went wrong..."
print(safe_divide(5566, 0))
Something went wrong...
*args
: for list-like arguments
def get_fahrenheit(c):
return c*9/5 + 32
get_fahrenheit(18)
64.4
get_fahrenheit(18, 20, 22)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-6-07d7bb62e8ca> in <module> ----> 1 get_fahrenheit(18, 20, 22) TypeError: get_fahrenheit() takes 1 positional argument but 3 were given
def get_fahrenheits(*args):
fahrenheits = []
for c in args:
fahrenheits.append(c*9/5 + 32)
return fahrenheits
print(get_fahrenheits(18))
print(get_fahrenheits(18, 20, 22))
[64.4] [64.4, 68.0, 71.6]
get_mean(*args)
回傳 *args
所組成之數列的平均數¶def get_mean(*args):
summation = sum(args)
length = len(args)
x_bar = summation / length
return x_bar
print(get_mean(1, 3, 5, 7, 9))
print(get_mean(3, 4, 5, 6, 7))
print(get_mean(3))
5.0 5.0 3.0
get_std(*args)
回傳 *args
所組成之數列的樣本標準差¶print(get_std(1, 3, 5, 7, 9))
print(get_std(3, 4, 5, 6, 7))
print(get_std(3))
3.1622776601683795 1.5811388300841898 Please input at least 2 numbers.
def
更簡潔的語法來定義函數¶def squared(x):
return x**2
squared(2)
4
lambda
函數¶FUNCTION_NAME = lambda arg0, arg1, ...: USING arg0, arg1
squared = lambda x: x**2
squared(2)
4
my_abs = lambda x: -x if x < 0 else x
print(my_abs(-2))
print(my_abs(2))
2 2
map()
filter()
def get_fahrenheit(c):
return c*9/5 + 32
temp_c = [18, 20, 22]
temp_f = map(get_fahrenheit, temp_c)
list(temp_f)
[64.4, 68.0, 71.6]
# map()
temp_c = [18, 20, 22]
temp_f = map(lambda x: x*9/5 + 32, temp_c)
list(temp_f)
[64.4, 68.0, 71.6]
# filter()
temp_c = [-10, 18, 20, -5, -3]
below_zero = filter(lambda x: x < 0, temp_c)
list(below_zero)
[-10, -5, -3]
enumerate()
:同時取用一個 iterable 中的 index 與 valuezip()
:同時取用多個 iterables 中的 values# enumerate():同時取用一個 iterable 中的 index 與 value
the_avenger_movies = ["The Avengers", "Avengers: Age of Ultron", "Avengers: Infinity War", "Avengers: Endgame"]
for i, val in enumerate(the_avenger_movies):
print("復仇者聯盟第{}集:{}".format(i+1, val))
復仇者聯盟第1集:The Avengers 復仇者聯盟第2集:Avengers: Age of Ultron 復仇者聯盟第3集:Avengers: Infinity War 復仇者聯盟第4集:Avengers: Endgame
# zip():同時取用多個 iterables 中的 values
release_years = [2012, 2015, 2018, 2019]
the_avenger_movies = ["The Avengers", "Avengers: Age of Ultron", "Avengers: Infinity War", "Avengers: Endgame"]
for y, movie in zip(release_years, the_avenger_movies):
print("{} 上映年份 {}".format(movie, y))
The Avengers 上映年份 2012 Avengers: Age of Ultron 上映年份 2015 Avengers: Infinity War 上映年份 2018 Avengers: Endgame 上映年份 2019
# loop construction
squared_list = []
for i in range(10):
squared_list.append(i**2)
print(squared_list)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# list comprehension
squared_list = [i**2 for i in range(10)]
print(squared_list)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
# list comprehension with if
even_numbers = [i for i in range(10) if i % 2 == 0]
print(even_numbers)
[0, 2, 4, 6, 8]
# list comprehension with if-else
is_even_numbers = [True if i % 2 == 0 else False for i in range(10)]
print(is_even_numbers)
[True, False, True, False, True, False, True, False, True, False]
kilometers = [1.6, 3, 5, 10, 21.095, 42.195]
print(miles)
[0.994192, 1.86411, 3.1068499999999997, 6.213699999999999, 13.10780015, 26.21870715]
map()
filter()
enumerate()
zip()
# map()
temp_c = [18, 20, 22]
temp_f = map(lambda x: x*9/5 + 32, temp_c)
print(type(temp_f))
print(temp_f)
<class 'map'> <map object at 0x10aaa3610>
list(temp_f)
[64.4, 68.0, 71.6]
list(temp_f)
[]
# filter()
temp_c = [-10, 18, 20, -5, -3]
below_zero = filter(lambda x: x < 0, temp_c)
print(type(below_zero))
print(below_zero)
<class 'filter'> <filter object at 0x10aaa1950>
list(below_zero)
[-10, -5, -3]
list(below_zero)
[]
map()
將公里的距離都轉換成英里¶kilometers = [1.6, 3, 5, 10, 21.095, 42.195]
print(miles)
print(list(miles))
<generator object <genexpr> at 0x10aac90d0> [0.994192, 1.86411, 3.1068499999999997, 6.213699999999999, 13.10780015, 26.21870715]
# enumerate()
the_avenger_movies = ["The Avengers", "Avengers: Age of Ultron", "Avengers: Infinity War", "Avengers: Endgame"]
enumerate_generator = enumerate(the_avenger_movies)
print(type(enumerate_generator))
print(enumerate_generator)
<class 'enumerate'> <enumerate object at 0x10aac1a00>
list(enumerate_generator)
[(0, 'The Avengers'), (1, 'Avengers: Age of Ultron'), (2, 'Avengers: Infinity War'), (3, 'Avengers: Endgame')]
list(enumerate_generator)
[]
# zip()
release_years = [2012, 2015, 2018, 2019]
the_avenger_movies = ["The Avengers", "Avengers: Age of Ultron", "Avengers: Infinity War", "Avengers: Endgame"]
zip_generator = zip(release_years, the_avenger_movies)
print(type(zip_generator))
print(zip_generator)
<class 'zip'> <zip object at 0x10aadc3c0>
list(zip_generator)
[(2012, 'The Avengers'), (2015, 'Avengers: Age of Ultron'), (2018, 'Avengers: Infinity War'), (2019, 'Avengers: Endgame')]
list(zip_generator)
[]
.format()
pi = 3.14159
print("圓周率的值為: {}".format(pi))
圓周率的值為: 3.14159
pi_str = "圓周率"
pi = 3.14159
print("{}取兩位小數為: {:.2f}".format(pi_str, pi))
print("{}整數部分是 {:.0f}".format(pi_str, pi))
圓周率取兩位小數為: 3.14 圓周率整數部分是 3
.title()
.upper()
.lower()
use_the_force = "Luke, use the Force!"
print(use_the_force.title())
print(use_the_force.upper())
print(use_the_force.lower())
Luke, Use The Force! LUKE, USE THE FORCE! luke, use the force!
.rstrip()
.lstrip()
.strip()
use_the_force = """
Luke, use the Force!
"""
use_the_force
'\n \nLuke, use the Force!\n \n'
print(use_the_force.rstrip())
print(use_the_force.lstrip())
print(use_the_force.strip())
Luke, use the Force! Luke, use the Force! Luke, use the Force!
.replace()
skywalker = "Anakin Skywalker"
print(skywalker)
print(skywalker.replace("Anakin", "Luke"))
Anakin Skywalker Luke Skywalker
.split()
use_the_force = "Luke, use the Force!"
print(use_the_force.split())
print(use_the_force.split(","))
['Luke,', 'use', 'the', 'Force!'] ['Luke', ' use the Force!']
episode_ix_opening_crawl = """
A long time ago in a galaxy far, far away....
The dead speak! The galaxy has heard a mysterious broadcast, a threat of REVENGE in the sinister voice of the late EMPEROR PALPATINE.
GENERAL LEIA ORGANA dispatches secret agents to gather intelligence, while REY, the last hope of the Jedi, trains for battle against the diabolical FIRST ORDER.
Meanwhile, Supreme Leader KYLO REN rages in search of the phantom Emperor, determined to destroy any threat to his power....
"""
def get_word_frequency(long_str):
long_str_split = long_str.split()
word_frequency = {}
for i in long_str_split:
if i not in word_frequency.keys():
word_frequency[i] = 1
else:
word_frequency[i] += 1
return word_frequency
episode_ix_opening_crawl = """
A long time ago in a galaxy far, far away....
The dead speak! The galaxy has heard a mysterious broadcast, a threat of REVENGE in the sinister voice of the late EMPEROR PALPATINE.
GENERAL LEIA ORGANA dispatches secret agents to gather intelligence, while REY, the last hope of the Jedi, trains for battle against the diabolical FIRST ORDER.
Meanwhile, Supreme Leader KYLO REN rages in search of the phantom Emperor, determined to destroy any threat to his power....
"""
print(get_word_frequency(episode_ix_opening_crawl))
{'A': 1, 'long': 1, 'time': 1, 'ago': 1, 'in': 3, 'a': 3, 'galaxy': 2, 'far,': 1, 'far': 1, 'away....': 1, 'The': 2, 'dead': 1, 'speak!': 1, 'has': 1, 'heard': 1, 'mysterious': 1, 'broadcast,': 1, 'threat': 2, 'of': 4, 'REVENGE': 1, 'the': 6, 'sinister': 1, 'voice': 1, 'late': 1, 'EMPEROR': 1, 'PALPATINE.': 1, 'GENERAL': 1, 'LEIA': 1, 'ORGANA': 1, 'dispatches': 1, 'secret': 1, 'agents': 1, 'to': 3, 'gather': 1, 'intelligence,': 1, 'while': 1, 'REY,': 1, 'last': 1, 'hope': 1, 'Jedi,': 1, 'trains': 1, 'for': 1, 'battle': 1, 'against': 1, 'diabolical': 1, 'FIRST': 1, 'ORDER.': 1, 'Meanwhile,': 1, 'Supreme': 1, 'Leader': 1, 'KYLO': 1, 'REN': 1, 'rages': 1, 'search': 1, 'phantom': 1, 'Emperor,': 1, 'determined': 1, 'destroy': 1, 'any': 1, 'his': 1, 'power....': 1}