一、Numpy数据分析
import numpy as np
import random
list=[]
for i in range(1,11):
n=random.randint(10,99)
list.insert(i,n)
arr1=np.array(list)
m1=np.max(arr1); n1=np.min(arr1); a1=np.average(arr1)
print("一维数组:")
print(arr1)
print("数据分析:最大值是=",m1,end=' '); print("最小值是=",n1,end=' '); print("平均值是=",a1)
print("-----------------------------------------------------")
list1=[]
for i in range (1,26):
nn=random.randint(10,99)
list1.insert(i,nn)
arr2=np.array(list1)
arr2.shape=(5,5)
m2=np.max(arr2); n2=np.min(arr2); a2=np.average(arr2)
print("二维数组:")
print(arr2)
print("数据分析:最大值是=",m2,end=' '); print("最小值是=",n2,end=' '); print("平均值是=",a2,end=' ')
二、类的单继承
class person():
name=' '
age=0
def __init__(self,n,a):
self.name=n
self.age=a
def speak(self):
print("my name is",self.name,", I am",self.age,"years old,")
class teacher(person):
__profession=' '
__title=' '
__course=' '
def __init__(self,n,a,p,t,c):
person.__init__(self,n,a)
self.__profession=p
self.__title=t
self.__course=c
def speak(self):
print('my name is %s, I am %d years old,\nAnd my profession is %s,\nmy title is %s,\nmy master course is %s.\nthank you!'%(self.name,self.age,self.__profession,self.__title,self.__course))
s=teacher('python',18,'computer science and technology','associate professor','Python programming desiging')
s.speak()
三、TXT文件操作
t=open("test.txt","w+")#以读写模式打开
for i in range (5):#默认从0开始,不到5,即0,1,2,3,4
t.write("python"+str(i+1)+"\n") #写入
t.close() #关闭文件,保证文件写入后得到保存
t=open("test.txt","r")
print("读5个字符是:"+t.read(5)) #读
t.seek(0)
print("读第一行字符串是: "+t.readline(),end='')#读一行
t.seek(t.tell()*2)
print("读第三行字符串是:"+t.readline())#读一行
t.seek(0)
print("读所有行字符串是:")
s=t.readlines()#以列表形式返回
#print(s)
for line in s:
print(line[:-1],end=' ') #不读入换行符'\n',以空格隔开
t.close()
四、CSV文件操作
import csv
list1=[[1,2,3],[4,5,6],[7,8,9]]
#print(list1)
t=open("test.csv","w+",newline='')#无newline='' 会导致行数据间有一空行
c=csv.writer(t,dialect="excel")#编码风格
for i in range(len(list1)):
c.writerow(list1[i])
t.close()
c1=csv.reader(open("test.csv",encoding="utf-8"))
for row in c1:
print(row)