数据分析Python习题系列

喜欢的话,关注我,谢谢点赞

chapter1

title-1

自定义字符串,完成字符串的索引/切片/find/count/replace/split/乘法/连接操作,并写出输出结果

>>> str='hello'
>>> print("第二个字符是'{:}'".format(str[1]))
第二个字符是'e'
>>> print('{:}'.format(str[0:2]))
he
>>> strFind='lo'
>>> print(str.find(strFind))
3
>>> sub='l'
>>> print(str.count(sub))
2
>>> print(str.replace("llo","666"))
he666
>>> print(str.split('o',1))
['hell', '']
>>> str*3
'hellohellohello'
>>> p=' python'
>>> str+p
'hello python'

title-2

打印99乘法表

# 九九乘法表
for i in range(1, 10):
    for j in range(1, i+1):
        print('{}x{}={}\t'.format(j, i, i*j), end='')
    print()

Xnip2021-03-28_22-20-01

title-3

定义一个函数,函数的参数是分数,把分数转化成A(2%)B(10%)C(70%)D(15%)E(3%)五个等级

def fun(score):
  if score<60:
    print("E")
  elif score<70:
    print("D")
  elif score<80:
    print("C")
  elif score<90:
    print("B")
  else:
    print("A")

>>> fun(10)
>>> E
>>> fun(98)
>>> A
a=eval(input())
if a<60:
  print("E")
elif a<70:
  print("D")
elif a<80:
  print("C")
elif a<90:
  print("B")
else:
  print("A")
a=eval(input())
if a>=90:
  print("A")
elif 90>a>=80:
  print("B")
elif 80>a>=70:
  print("C")
elif 70>a>=60:
  print("D")
elif a<60:
  print("E")

chapter2

1、列表和元组的操作和转换

def listMethod():
    print()
    # 列表
    List = ['北1', '清1', 2021, 2023]
    print(List)
    # 列表索引
    print("list[0]: ", List[0])
    # 列表切片
    print("list[0:2]: ", List[0:2])
    # 列表增加元素
    List.append('三')
    # 列表删除元素
    del List[0]
    # 列表修改元素
    List[0] = '京师大学堂'
    # 列表查询元素
    print(List.index('三'))
    print(List[List.index('三')])
    print()
    # 列表遍历
    for i, value in enumerate(List):
        print(i, value)
    print()
    for i in List:
        print(i)

def tupleMethod():
    print()
    # 元组
    Tuple = ('北1', '清1', 2021, 2023)
    print(Tuple)
    # 元组索引
    print("Tuple[0]: ", Tuple[0])
    # 元组切片
    print("Tuple[0:2]: ", Tuple[0:2])
    print()
    # 元组遍历
    for i, value in enumerate(Tuple):
        print(i, value)
    print()
    for i in Tuple:
        print(i)

def transformation():
    print()
    # 元组
    Tuple = ('北1', '清1', 2021, 2023)
    print(Tuple)
    # 列表
    List = ['北1', '清1', 2021, 2023]
    print(List)
    print()
    # 元组转换成列表
    print(list(Tuple))
    # 列表转换成元组
    print(tuple(List))

2、设计一个Member类,类中含有属性name和age,其中,name为公有属性,age为私有属性,并在构造方法中完成name和age的初始化工作。Member类中含有get_name和get_age两个方法,分别返回姓名和年龄信息。设计Student类,继承自Member类,其中Student类拥有的属性score,另有方法get_info,用来返回学生的姓名、年龄、分数。

class Member:
    def __init__(self, name, age):
        # 初始化
        self.name = name
        self.__age = age

    # 返回姓名
    def get_name(self):
        return self.name

    # 返回年龄
    def get_age(self):
        return self.__age


class Student(Member):
    def __init__(self, name, age, score):
        self.name = name
        self.__age = age
        self.score = score

    def get_info(self):
        print('姓名:{0} , 年龄:{1} 岁 , 成绩:{2} 分'.format(self.name, self.__age, self.score))

bob = Student('品多多', 99, 89)
print(bob.get_info())

chapter3

鸢尾花数据集

    # 导入数据集
    from sklearn.datasets import load_iris
    from sklearn.model_selection import train_test_split
    from sklearn.tree import DecisionTreeClassifier
    # 载入数据集
    iris = load_iris()
    X_train,X_test,Y_train,Y_test = train_test_split(iris.data, iris.target, test_size=50)
    
    dtm = DecisionTreeClassifier(criterion="entropy",max_depth=3)
    dtm.fit(X_train,Y_train)
    
    score = dtm.score(X_test, Y_test)
    print(score)```
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
1、判断题: Python语言是一种高级语言。【对】 2、多选题: Jupyter notebook中运行单元格的方法有哪几种?( ) 选项: A:Enter B:Shift+Enter C:Ctrl+Enter D:F5 答案: 【Shift+Enter;Ctrl+Enter】 3、单选题: Jupyter notebook的记事本文件扩展名为:( ) 选项: A:m B:py C:pyc D:ipynb 答案: 【ipynb】 4、判断题: Jupyter notebook 中的助手需要额外安装。答案: 【对】 5、单选题: Python安装扩展库常用的是( )工具 选项: A:setup B:update C:pip D:run 答案: 【pip】 6、单选题: 关于Python语言的注释,以下选项中描述错误的是:( ) 选项: A:python语言有两种注释方式:单行注释和多行注释 B:python语言的单行注释以#开头 C:python语言的单行注释以单引号开头 D:Python语言的多行注释以’’’(三个单引号)开头和结尾 答案: 【Python语言的单行注释以单引号开头】 7、单选题: 以下选项中,不是pip工具进行第三方库安装的作用的是:( ) 选项: A:安装一个库 B:卸载一个已经安装的第三方库 C:列出当前系统已经安装的第三方库 D:脚本程序转变为可执行程序 答案: 【脚本程序转变为可执行程序】 8、单选题: 安装一个库的命令格式是:( ) 选项: A:pip uninstall  B:pip -h C:pip install  D: ip download  答案: 【pip install 】 9、判断题: 标准的缩进格式是Python的语法之一。 选项: A:对 B:错 答案: 【对】 10、多选题: 下列导入第三库的操作中正确的是:( ) 选项: A:import numpy B:import numpy as np C:from matplotlib import pyplot D:from urllib.request import urlopen 案: 【import numpy;import numpy as np;from matplotlib import pyplot;from urllib.request import urlopen】
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网小队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值