python基础知识
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
''' @author:wusen time :2020-02-28 10:12:41 ''' # ============================================================================= # Ctrl + 1: 注释/反注释 # # Ctrl + 4/5: 块注释/块反注释 # '''和"""都可以注释 # ============================================================================= #获取当前时间 import datetime now_time=datetime.datetime.now() time=now_time.strftime("%Y-%m-%d %H:%M:%S") print(time) #变量 变量区分大小写 #global animal="动物" 在函数内部创建为全局变量 animal1,animal2,animal3="小狗","小猫","小羊" print(animal3) #数据类型转换 animal4="4" #强制转换 print(type(int(animal4))) #int float complex复数 num1=2j print(num1) print(type(num1)) #随机数 random import random print(random.randrange(1,10)) #'''和"""也是多行字符串位置 str1="""2020年 加油! """ print(str1) #bool(7>6)布尔类型 #运算符 #列表可以表示数组(python没有数组) fruit=["香蕉","apple",12] print(fruit[1]) print(len(fruit)) fruit.append("wusen") fruit.pop(1) fruit.remove(12) print(fruit) #元组 #不可修改。 #元组之间可以相加 name=("wusen","吴森",12,12) print(name.count(12)) #集合 是无序的,不可修改,可以增加,删除 animal={"wusen","吴森","3可贼",12} animal.add(14) #字典 sex={ "wusen":"男", "xiaohong":"女", "xiaobai":12, "xiaohei":12 } print(sex.get("xiaobai")) city=("邢台","石家庄","保定","邯郸","衡水","廊坊","秦皇岛","唐山") j=range(1,100,3) arr={0:0,} for i in range(1,20): arr[i]=j[i] for i ,j in arr.items(): print(i,j) for i in arr: print(i,arr[i]) i=0 j=1 while i<9: i=i+1 j=1 while j<=i: # print("%dX%d=%d" %(i,j,i*j),end=" ") print((i,j),end="") j=j+1 print("") #函数 def def demoprint(a): print("第",a,"遍新年快乐") for i in range(1,10): if i!=7: demoprint(i) else: continue else: print("结束") #lambda函数 sum=lambda a,b:a+b sum(1,2) def my_f(a): return lambda b:a*b my=my_f(5) my(50) #类 class people: def __init__(self,name,age): self.name=name self.age=age def myname(self): print("My name is",self.name) I=people("吴森",24) I.myname() import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="123456" ) print(mydb) #数据库(大小写好像没事) import mysql.connector mydb=mysql.connector.connect( host="localhost", user="root", passwd="123456" ) mycursor=mydb.cursor() mycursor.execute("CREATE DATABASE mydatabase") #输出数据库 import mysql.connector mydb=mysql.connector.connect( host="localhost", user="root", passwd="123456" ) mycursor=mydb.cursor() mycursor.execute("SHOW DATABASES") #print(mycursor) for x in mycursor: print(x) import mysql.connector mydb=mysql.connector.connect( host="localhost", user="root", passwd="123456", database="mydatabase" ) mycursor=mydb.cursor() #mycursor.execute("CREATE TABLE customers(name VARCHAR(255), address VARCHAR(255))") # ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY mycursor.execute("ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY") mycursor.execute("show tables") for x in mycursor: print(x) import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", passwd="123456", database="mydatabase" ) mycursor = mydb.cursor() sql = "INSERT INTO customers (name, address) VALUES (%s, %s)" val = ("John", "Highway 21") mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "record inserted.") import mysql.connector mydb=mysql.connector.connect( host="localhost", user="root", passwd="123456", database="mydatabase" ) mycursor=mydb.cursor() sql="INSERT INTO customers (name,address) VALUES (%s,%s)" val=("曹操","东汉末年") mycursor.execute(sql,val) mydb.commit() import mysql.connector mydb=mysql.connector.connect( host="localhost", user="root", passwd="123456", database="mydatabase" ) mycursor=mydb.cursor() mycursor.execute("SELECT * FROM CUSTOMERS") myresult=mycursor.fetchall() for x in myresult: print(x) #批量插入 def myinsert(vals): import mysql.connector mydb=mysql.connector.connect( host="localhost", user="root", passwd="123456", database="mydatabase" ) mycursor=mydb.cursor() sql="INSERT INTO customers (name ,address) VALUES(%s,%s)" for val in vals: mycursor.execute(sql,val) mydb.commit() import openpyxl wb=openpyxl.load_workbook("E:\data.xlsx") sheets = wb.sheetnames print(sheets, type(sheets)) ws=wb["Sheet1"] print(ws["A1"].value) i=1 while i<=ws.max_row: s="A"+str(i) print(ws[s].value) i=i+1 for x in ws[3]: print(x) col_range = ws["A":"B"] row_range = ws[1:4] for col in col_range: # 打印BC两列单元格中的值内容 for cell in col: print(cell.value) for row in row_range: # 打印 2-5行中所有单元格中的值 for cell in row: print(cell.value) print("A"+str(1)) s=input("请输入:") print(s) #发邮件 #email构建邮件 #smtplib发送邮件 #!/usr/bin/python # -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText from email.header import Header sender = 'from@runoob.com' receivers = ['2096706824@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 mail_msg = """ <p>Python 邮件发送测试...</p> <p><a href="http://www.runoob.com">这是一个链接</a></p> """ message = MIMEText(mail_msg, 'html', 'utf-8') message['From'] = Header("菜鸟教程", 'utf-8') message['To'] = Header("测试", 'utf-8') subject = 'Python SMTP 邮件测试' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message.as_string()) print "邮件发送成功" except smtplib.SMTPException: print "Error: 无法发送邮件" import smtplib server = smtplib.SMTP() server.connect(host, port) server.login(username, password) server.sendmail(from_addr, to_addr, msg.as_string()) server.quit() #成功发送 import smtplib from email.mime.text import MIMEText from_addr='2096706824@qq.com' password="nmckgeilsckzcghg" to_addr="2376502463@qq.com" smtp_server="smtp.qq.com" msg=MIMEText("python","plain","utf-8") server=smtplib.SMTP() server.connect(smtp_server,25) server.login(from_addr,password) server.sendmail(from_addr,to_addr,msg.as_string()) server.quit() print("结束") #发送邮件 import smtplib from email.mime.text import MIMEText from_addr="2096706824@qq.com" password="nmckgeilsckzcghg" to_addr="2376502463@qq.com" smtp_server="smtp.qq.com" msg=MIMEText("python","plain","utf-8") i=1; while i<10: server=smtplib.SMTP() server.connect(smtp_server,25) server.login(from_addr,password) server.sendmail(from_addr,to_addr,msg.as_string()) server.quit() i=i+1
-----------------------------------------------------------------------------------------------------------------------------------------