Python是一種高級編程語言,它被廣泛地用于Web開發、科學計算、數據分析、AI、機器學習等領域。以下是一些Python的知識點:
# 1.基本語法 # 變量 x = 5 y = "Hello World" # 輸出 print(x) print(y) # 2.數據類型 a = 5 # int b = 5.0 # float c = 1 + 2j # complex d = "Hello World" # str e = True # bool # 列表 myList = ["apple", "banana", "cherry"] # 元組 myTuple = ("apple", "banana", "cherry") # 集合 mySet = {"apple", "banana", "cherry"} # 字典 myDict = {"name": "John", "age": 36} # 3.控制流 # if語句 x = 5 if x >3: print("x is greater than 3") elif x == 3: print("x is equal to 3") else: print("x is less than 3") # 循環 myList = ["apple", "banana", "cherry"] for x in myList: print(x) while x >0: print(x) x -= 1 # 4.函數 def myFunction(a,b): return a + b x = myFunction(1,2) print(x) # 5.模塊 # 導入模塊 import math x = math.sqrt(64) print(x) # 創建自己的模塊 # 創建一個名為mymodule.py的文件 def myFunction(): print("Hello from a function in mymodule") # 導入模塊 import mymodule mymodule.myFunction() # 6.異常處理 try: x = 5 / 0 except ZeroDivisionError: print("division by zero") except: print("something went wrong") # 7.文件處理 # 打開文件 f = open("myfile.txt", "r") # 讀取文件內容 print(f.read()) # 關閉文件 f.close() # 8.正則表達式 import re txt = "The rain in Spain" x = re.search("^The.*Spain$", txt) if x: print("YES! We have a match!") else: print("No match")