teaching.py
Python/teaching.py
'''
題目:
寫一個函式 is_palindrome(s) 判斷字串是否為回文。
回文意思是正著念、反著念都一樣,例如 "level"、"madam"。
輸入:"level" → 回傳 True
輸入:"hello" → 回傳 False
'''
def is_palindrome(s):
return s == s[::-1]
if __name__ == "__main__":
# 測試程式碼
print(is_palindrome("level")) # True
print(is_palindrome("madam")) # True
print(is_palindrome("racecar")) # True
print(is_palindrome("hello")) # False
Artículos relacionados
01_getting_started.py
01_getting_started.py — python source code from the Python learning materials (Python/01_beginner_level/01_getting_started.py).
Leer artículo →02_core_syntax.py
02_core_syntax.py — python source code from the Python learning materials (Python/01_beginner_level/02_core_syntax.py).
Leer artículo →03_control_flow.py
03_control_flow.py — python source code from the Python learning materials (Python/01_beginner_level/03_control_flow.py).
Leer artículo →04_data_structures.py
04_data_structures.py — python source code from the Python learning materials (Python/01_beginner_level/04_data_structures.py).
Leer artículo →05_functions.py
05_functions.py — python source code from the Python learning materials (Python/01_beginner_level/05_functions.py).
Leer artículo →06_error_handling.py
06_error_handling.py — python source code from the Python learning materials (Python/01_beginner_level/06_error_handling.py).
Leer artículo →