python基础
自变量
读写文件
简单的文件写入
Text = 'This is my first test.\n This is next line.\n This is last line'
print(test)
my_file = open('my file.txt', 'w')
my_file.wirte(text)
my_file.close()
追加写入
append_text = '\n This is appended line'
print(test)
my_file = open('my file.txt', 'a')
my_file.wirte(append_text)
my_file.close()
读取文件
file = open('my file.txt', r)
content = file.read()
print(content)
#读取一行
content = file.readline() #逐行读入
#全部读入
content = file.readlines() #content放入list
class类
class Calculator:
price = 18 #属性
def add(self, x, y): #self 可以调用自身的属性 功能
result = x + y
print(result)
calcul = Calculator()
calcul.price
calcul.add(10, 10)
类 init 功能
如果有 init
类的时候调用的时候必须带上需要的参数
class Calculator:
price = 18 #属性
def __init__(self, name, price, high, width, weight):
self.name = name
self.price = price
self.h = hight
self.wi = width
self.we = weight
def add(self, x, y): #self 可以调用自身的属性 功能
result = x + y
print(result)
calcul = Calculator(‘tony’, 12, 23, 23, 23)
calcul.price
calcul.add(10, 10)
input
a_input = input() #return string
print(a_input)
a_input = int(input()) #装成整数
元组和列表
a_tuple = (12,3,5,15,6)
another_tuple = 2,4,6,7,8
a_list = [12,3,67,7,82]
for content in a_list: # 遍历列表
print(content)
for content in another_tuple: # 遍历元组
print(content)
for index in range(len(a_List)):
print('index=', index, 'number in list=', a_List[index])
for index in range(len(a_tuple)):
print('index=', index, 'number in list=', a_tuple[index])
列表功能
a = [1,2,3,4,5]
a.append(0) #在末尾加入0
print(a)
a.insert(1, 0) #insert(pos, val) 在 pos 中添加 val
a.remove(2) #remove(val) 移除第一次出现的 val
print(a[0]) #打印第 0 位
print(a[-1]) #打印最后一位
print(a[0:3]) #打印0-3位 [start : end]
print(a[3:4])
print(a[-3:]) #打印最后三位
a.index(2) # index(val) 返回第一次出现val的值
a.count(2) # count(val) 返回统计val的个数
a.sort() #升序
a.sort(reverse=True) #降序
多维列表
a [1,2,4,5,6] #一维
multi_dim_a = [[1,2,3], [2,3,5], [3,4,5]]
字典
d = {'apple':1, 'pear':2, 'orange':3}
print(d['apple']) #取值
del d['apple'] #删除元素
d['b'] = 20
#字典里面可以有函数、字典和列表
d = {'apple':1, 'pear':{1:3, 3:'a'}, 'orange':3}
print(d['pear'][3])
模块
import 内部模块
import timeprint(time.localtime())
#m如果模块很长、可以用一个简称
import time as tprint(t.time())
#导入功能,可以直接使用该功能
from time import time localtime
print(time())
print(localtime())
#导入time所有功能
from time import *
print(time())
print(localtime())
导入自己模块
m1.py
def printdata(data):print(data)
test.py
和 m1.py
在同一个目录下
import m1m1.printdata('test')
错误处理
file = open('eee', r) #没有该文件会报错,提示找不到文件
try:
file = open('eee', 'r')
except Exception as e:
print(e)
try:
file = open('eee', 'r')
except Exception as e:
print('there is no file named as eeee')
response = input('do you want ti create a new file')
if response == 'y':
file = open('eeee', w)
else:
pass
else:
file.wite('eeeee')
zip lambda map
a = [1, 2, 3]
b = [4, 5, 6]
list(zip(a, b)) #对应位合并
for i, j in zip(a, b):
print(i, j)
fun2 = lambda x, y:x + y
def fun1(x, y):
return (x + y)
map(fun1, [1], [2])
list(map(fun1, [1, 3], [2, 5])) # map(fun1 [参数x], [参数y])
浅复制 和 深复制
a = [1, 2, 3]
b = a #深复制
b[0] = 11
a[1] = 22 # a 和 b 同时会随之改变
a = [1, 2, 3, [123, 23]]
c = copy.copy(a) #浅复制
a[3][0] = 123
print(c) # c 会随之改变, 但 a[1] = 4 c 不会随之改变d = copy.deepcopy(a) #任何都不会被引用
多线程
pickle
a_dict = {'da':111, 2:[23,1,4], '23':{1:2, 'd':'sad'}}#存放
file = open('pickle_example.pickle', 'wb')
pick.dump(a_dict, file) #将a_dist 保存到 file 中file.clear()#读写file = open('pick_example.pickle', 'rb')
a_dict1 = pickle.load(file)
file.close()
print(a_dict1)
with open('pick_example.pickle', 'rb') as file:
a_dict1 = pickle.load(file)
print(a_dict1)
set
char_list = ['a', 'a', 'a', 'b', 'b', 'c', 'c']
print(set(char_list))
sentence = 'Welcome Back to This Tutorial'
print(set(sentence))
unique_char = set(char_list)unique_char.add('x') # 加入元素unique_char.claer() #清除
setunique_char.remove('x') #去除某个元素
unique_char.discard('y') #去掉某一元素 ,区别于remove 就是如果元素本身不存在 remove 会进行报错, 而discard 不会报错
set1 = unique_char
set2 = {'a', 'e', 'i'}
print(set1.difference(set2)) #找set1中set2 没有的东西print(set1.intersection(set1)) #找交集