好的,以下是一些Python日常常用的20行代码以及命令:
- 打印输出:print("Hello, world!")
- 定义变量:a = 10
- 条件语句:if a > 5: print("a is greater than 5")
- 循环语句:for i in range(5): print(i)
- 列表操作:list = 1, 2, 3 list.append(4) list.remove(2)
- 字符串操作:str = "Hello" str = " world" str.replace("Hello", "Hi")
- 字典操作:dict = {"name": "John", "age": 30} dict"name" = "Mike" dict.pop("age")
- 文件操作:file = open("filename.txt", "r") content = file.read() file.close()
- 函数定义:def add(num1, num2): return num1 num2
- 模块导入:import math print(math.pi)
- 正则表达式:import re pattern = re.compile(r'd ') result = pattern.findall("a1b2c3")
- JSON操作:import json data = {"name": "John", "age": 30} json_data = json.dumps(data) dict_data = json.loads(json_data)
- 数据结构:import collections deque = collections.deque(1, 2, 3) deque.append(4) deque.popleft()
- 时间操作:import datetime now = datetime.datetime.now() print(now.strftime("%Y-%m-%d %H:%M:%S"))
- 网络请求:import requests response = requests.get("http://www.example.com") print(response.text)
- 数据库操作:import sqlite3 conn = sqlite3.connect('example.db') cursor = conn.cursor() cursor.execute("SELECT * FROM table") rows = cursor.fetchall() conn.close()
- 多线程:import threading def worker(): print("Worker") t = threading.Thread(target=worker) t.start()
- GUI编程:import tkinter root = tkinter.Tk() label = tkinter.Label(root, text="Hello, world!") label.pack() root.mainloop()
- 数学计算:import numpy import scipy import matplotlib
- 数据可视化:import seaborn import bokeh import plotly
这些是Python日常常用的一些代码和命令,当然还有很多其他的操作也很常用,具体使用要根据实际需求来决定。