一、字符串、列表、元组、字典操作
"""
===================
==== 字符串操作 ====
===================
"""
Str = 'helloWorld!'
print(Str[1:3])
print(Str[:5])
print(Str[4:])
print(Str * 4)
print(Str + "yes")
"""
=======================
==== 列表、元组操作 ====
=======================
"""
List = ['L', 'O', 'V', 'E']
print(List[2])
print(List)
"""
增加元素几种方式
添加结果显示分别为:
['L', 'O', 'V', 'E', '!']
['L', 'i', 'O', 'V', 'E', '!']
['L', 'i', 'O', 'V', 'E', '!', 'm', 'e']
"""
List.append('!')
print(List)
List.insert(1, 'i')
print(List)
List.extend("me")
print(List)
"""
删除元素几种方式
"""
List.pop(1)
print(List)
List.remove("V")
print(List)
"""
修改元素
"""
List[3] = "H"
print(List)
"""
元组与列表类似,唯一的区别是元组创建后不可以改变元素
"""
array = ("1", "2", "3")
"""
=======================
==== 字典 ====
=======================
"""
dist = {"name": "liuBei", "sex": "男", "age": 20}
print(dist["name"])
print(dist)
for key in dist:
print(dist[key])
dist["name"] = "gunYu"
二、类
"""
============
==== 类 ====
============
"""
"""
单下划线、双下划线、头尾双下划线说明:
__foo__: 定义的是特殊方法,一般是系统定义名字 ,类似 __init__() 之类的。
_foo: 以单下划线开头的表示的是 protected 类型的变量,即保护类型只能允许其本身与子类进行访问,不能用于 from module import *
__foo: 双下划线的表示的是私有类型(private)的变量, 只能是允许这个类本身进行访问了。
"""
class User:
__name = "liuBei"
__age = 14
__birth = "2010"
def __init__(self, name, age, birth):
self._User__name = None
self.__name = name
self.__age = age
self.__birth = birth
def setName(self, name):
self.__name = name
def setAge(self, age):
self.__age = age
def setBirth(self, birth):
self.__birth = birth
def getName(self):
return self.__name
def getAge(self):
return self.__age
def getBirth(self):
return self.__birth
def __str__(self):
return 'name:%s age:%d birth:%s' % (self.__name, self.__age, self.__birth)
user = User("liLa", 12, "2013")
str = User.getName(user)
print(str)
print(user)
print(user._User__age)
三、文件操作
"""
==================
==== 文件操作 =====
==================
"""
import os
text = open("hello.txt", "r+")
texts = text.read()
print("读取的文件内容为:" + texts)
position = text.tell()
print("当前位置", position)
find = text.seek(2, 0)
textr = text.read()
print(textr)
text.close()
四、其他
import time
import calendar
inputs = input("请输入")
print(inputs)
localTime = time.asctime(time.localtime(time.time()))
print(localTime)
times = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(times)
calendars = calendar.month(2020, 6)
print(calendars)
def prints(str):
print(str)
return
prints("你好")
sums = lambda arg1, arg2: arg1 + arg2
print(sums(10, 20))
content = dir(time)
print(content)
五、数据库操作(mysql)
import mysql.connector
cnn = mysql.connector.connect(user='root',
password='123456',
host='127.0.0.1',
database='login'
)
print("连接数据库成功")
cursor = cnn.cursor()
sql = """
select *from user
"""
try:
cursor.execute(sql)
results = cursor.fetchall()
List1 = []
List2 = []
for result in results:
print(result)
Id = result[0]
userName = result[1]
passWord = result[2]
List2.append("Id=" + str(Id))
List2.append("userName=" + userName)
List2.append("passWord=" + passWord)
List1.insert(-1, List2)
print(List1)
except:
print("没有获取数据")
cursor.close()
cnn.close()
六、多线程
import threading
import time
class MyThread(threading.Thread):
def __init__(self, ThreadId, name):
threading.Thread.__init__(self)
self.ThreadId = ThreadId
self.name = name
def run(self):
flag = 5
while flag:
time.sleep(2)
print(str(self.ThreadId) + self.name + time.ctime(time.time()))
flag -= 1
myThread1 = MyThread(1, "A")
myThread2 = MyThread(2, "B")
myThread1.start()
myThread1.join()
myThread2.start()
myThread2.join()
七、网络编程
- 服务端
import socket
"""
==== 服务端 ====
"""
sockets = socket.socket()
host = socket.gethostname()
post = 8083
sockets.bind((host, post))
sockets.listen(5)
while True:
fd, addr = sockets.accept()
print("地址:", addr)
data=fd.recv(1024)
print("recv:",data.decode())
fd.send("你好客户端".encode())
print("关闭连接")
break
fd.close()
- 客户端
import socket
client = socket.socket()
host = socket.gethostname()
post = 8083
client.connect((host, post))
while True:
msg = "你好呀服务器!"
client.send(msg.encode('utf-8'))
data = client.recv(1024)
print('data:', data.decode())
break
client.close()