變數宣告:不需要 var、let 或 const。只需命名變數即可。
x = 10 name = "Python"
原型:
資料結構:
numbers = [1, 2, 3] numbers.append(4)
point = (10, 20)
person = {"name": "Alice", "age": 30} person["name"] # Accessing value
unique_numbers = {1, 2, 3, 2}
條件:
if x > 5: print("Greater") elif x == 5: print("Equal") else: print("Lesser")
循環:
for num in [1, 2, 3]: print(num)
i = 0 while i < 5: i += 1
函數定義與傳回語法:
def greet(name): return f"Hello, {name}"
Lambda 函數(如 JS 箭頭函數):
square = lambda x: x * x
列表推導式(建立清單的有效方法):
squares = [x * x for x in range(10)]
生成器(一一產生值):
def generate_numbers(n): for i in range(n): yield i
嘗試/排除區塊:
try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero")
類別定義:
class Animal: def __init__(self, name): self.name = name def speak(self): return f"{self.name} makes a sound"
繼承:
class Dog(Animal): def speak(self): return f"{self.name} barks"
閱讀與寫作:
x = 10 name = "Python"
此摘要應提供有效開始使用 Python 編碼的要點。
以上是JS 開發人員的 Python 基礎知識的詳細內容。更多資訊請關注PHP中文網其他相關文章!