一 、python计数器函数
from collections import Counter
list=[‘红’,‘蓝’,‘红’,‘绿’]
allset=Counter(list)
将统计表转化为字典
print(dict(allset))
二、获取键盘输入进行输出
a = input(“input:”)
print(a)
三、python连接数据库
import pymysql
try: con=pymysql.connect(host=‘localhost’,user=‘root’,password=‘***’,port=3306,database=‘mysql’)
print(‘数据库连接成功’)
except:
print(‘数据库连接失败’)
四、python连接数据库进行增删改差
import pymysql
conn = pymysql.connect(host=‘localhost’,user=‘root’,password=‘mayan1993’,port=3306,database=‘test’)
#获取游标
cursor = conn.cursor()
#查询
try:
cursor.execute(“select * from student”)
data=cursor.fetchone()
print(data)
except:
conn.rollback()
print(‘数据查询失败’)
#插入
try:
cursor.execute(“insert into student(id,name,sex) values(2,‘Amma’,‘女’)”)
conn.commit()
print(“插入成功”)
except:
conn.rollback()
print(‘数据插入失败’)
#修改
try:
cursor.execute(“update student set name=‘duo’ where id=2”)
conn.commit()
print(“修改成功”)
except:
conn.rollback()
print(‘数据修改失败’)
#删除
try:
cursor.execute(“delete from student where id=2”)
conn.commit()
print(“删除成功”)
except:
conn.rollback()
print(‘数据删除失败’)
conn.close()