Python实验考试

简介: Python实验考试

一、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)
目录
相关文章
|
3月前
|
Python
277: 程序设计C 实验二 题目五 统计二进制数中的1的个数(python)
277: 程序设计C 实验二 题目五 统计二进制数中的1的个数(python)
|
3月前
|
Python
557: 程序设计C 实验四 题目三 字符串交叉插入(python)
557: 程序设计C 实验四 题目三 字符串交叉插入(python)
|
2月前
|
数据处理 索引 Python
【Python学习篇】Python实验小练习——文件操作(十一)
【Python学习篇】Python实验小练习——文件操作(十一)
42 1
|
2月前
|
存储 缓存 算法
【Python学习篇】Python实验小练习——循环结构(八)
【Python学习篇】Python实验小练习——循环结构(八)
23 1
|
2月前
|
存储 算法 数据安全/隐私保护
【Python学习篇】Python实验小练习——高级数据结构(五)
【Python学习篇】Python实验小练习——高级数据结构(五)
48 1
|
3月前
|
存储 数据采集 数据挖掘
Python数据分析实验一:Python数据采集与存储
Python数据分析实验一:Python数据采集与存储
156 1
|
3月前
|
存储 算法 安全
Python编程实验六:面向对象应用
Python编程实验六:面向对象应用
72 1
|
10天前
|
存储 Python
【python】python标准化考试系统[单项选择题 简易版](源码)【独一无二】
【python】python标准化考试系统[单项选择题 简易版](源码)【独一无二】
|
1月前
|
机器学习/深度学习 TensorFlow API
Keras是一个高层神经网络API,由Python编写,并能够在TensorFlow、Theano或CNTK之上运行。Keras的设计初衷是支持快速实验,能够用最少的代码实现想法,并且能够方便地在CPU和GPU上运行。
Keras是一个高层神经网络API,由Python编写,并能够在TensorFlow、Theano或CNTK之上运行。Keras的设计初衷是支持快速实验,能够用最少的代码实现想法,并且能够方便地在CPU和GPU上运行。
|
2月前
|
索引 Python
Python考试基础知识
Python考试基础知识
18 1