Python

171 阅读1分钟

Python版本下载地址:mirrors.huaweicloud.com/python/

语法基础

第一部分

# 打印数字
print(333)

面向对象

# 类
class Mysql:  
    host = ''  # 属性
    port = ''  
    username = ''  
    password = ''  
    
  # 构造函数
    def __init__(self,host,port,username,password):  
        self.host = host  
        self.port = port  
        self.username = username  
        self.password = password  
  
  #set方法
    def setHost(self,host):  
        self.host = host  
  
  #get方法
    def getHost(self):  
        return self.host  
 
m = Mysql("127.0.0.1",port=3306,username='root',password='Yy132323')  
print(m.getHost())  
m.setHost('192.168.1.1')  
print(m.getHost())

第三方库下载

    pip -V # 查看pip版本
    pip list # 列出第三方库
    pip install -i https://pypi.douban.com/simple/ pandas #从国内下载第三库
    pip install --upgrade pip # 升级pip
    pip show pandas # 查看库的版本

pip的仓库地址

https://pypi.tuna.tsinghua.edu.cn/simple/ # 清华大学
http://pypi.mirrors.ustc.edu.cn/simple/ # 中国科技大学
http://pypi.douban.com/simple/ 豆瓣
http://mirrors.aliyun.com/pypi/simple/ 阿里
http://pypi.hustunique.com/ 华中科技大学

PyMysql库操作数据库

import pymysql

conn = pymysql.connect(  
host='127.0.0.1',  
port=3306,  
user='root',  
password='111111',  
db='lanyue3d142'  
)  
cursor = conn.cursor()  
cursor.execute('select * from cfg_buff')  
result = cursor.fetchall()  
print(result)

Python编译打包

pip install -i https://pypi.douban.com/simple/ pyinstaller #安装
info.py修改为info.pyw #.py修改为.pyw
pyinstaller -F -w info.pyw # -F打包成独立运行程序,-w打包gui 编译打包

遍历目录查找文件

import os


for root, dirs, files in os.walk("C:/projects"): #root(文件路径) dirs(下层子目录和文件)files(文件)
    for file in files:
        if file.endswith(".xlsx"):
            print(os.path.join(root, file))  #路径,文件