四时宝库

程序员的知识宝库

Python 100题(二)input 、list、tuple和class应用基础

大家好,我是小白君。今天,我们就开始python 100题的第二篇笔记,共2小题。上一篇文章的直达链接在这:Python 100题(一)万事开头难,3小题过把瘾,3小题过把瘾。希望我们都可以坚持住,坚持学习,坚持交流,坚持分享。

阅读全文大约需要3 minutes,建议关注+收藏,边撸代码边学习,效率更高哦!

4. 题目

Write a program which accepts a sequence of comma-separated(逗号分隔) numbers from console and generate a list and a tuple which contains every number. Suppose the following input is supplied to the program:34,67,55,33,12,98,

Then, the output should be:

['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98')

中文对照: 编写一个程序,接受一系列逗号分隔(逗号分隔)数字从控制台和生成一个列表和元组包含每个数字。假设向程序提供了以下输入:34,67,55,33,12,98,则输出为:

['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98')

关键分析:

  • 控制台接受逗号分隔数字
  • 生成列表
  • 生成元组

关键对策:

  • input()在python100 1中学习过
  • 逗号分隔split()
  • list(), tuple()

method 1:

value=input('Please input a sequence of comma-separated numbers :')
l = value.split(',')
t=tuple(l)
print(l)
print(t)

output:

Please input a sequence of comma-separated numbers :23,56,65,3,1,96
['23', '56', '65', '3', '1', '96']
('23', '56', '65', '3', '1', '96')

关键注释 1:

一个tuple对象创建后,再也无法原地改变自身特有的元素! tuple具有如下的特点:

1、 不支持添加元素【增】

2、不支持删除元素【删】

3、不支持修改元素(修改操作的步骤是:先删除、再添加)【改】

4、支持2种查找元素【查】

a、根据下标查找元素,常称为【访问】元素,时间复杂度为O(1)

b、根据元素获取下标,常称为【查找】元素,时间复杂度为O(n)

关键注释 2:

  • 我们之前把数字转换成字符串是这样写: S = str(a) ,其中:a是数字,S是转换成的结果
  • 我们之前把字符串换成整数是这样写: S = int(a) ,其中:a是字符串,S是转换成的结果
  • 把字符串换成列表就是 : S = list(a) ,其中:a是字符串,S是转换成的结果
  • 把列表转换成字符串 : S = str(a) ,其中:a是列表,S是转换成的结果
  • 将字符串转换为元组: S = tuple(a) ,其中:a是字符串,S是转换成的结果

5.题目

Define a class which has at least two methods:

  • getString: to get a string from console input
  • printString: to print the string in upper case.

Also please include simple test function to test the class methods.

中文对照: 编写一个类,至少包括以下两种方法:

  • getString: 从控制台获得输入
  • printString: 以大写字母形式打印出来

关键分析:

  • 一个输入输出类
  • 从控制台获得输入
  • 大写字母形式打印出来

关键对策:

  • 定义类:def class:
  • input控制台获得输入
  • print打印输出

method 1:

class IOstring():
    def get_string(self):
        self.s = input('Please input a string:')

    def print_string(self):
        print(self.s.upper())

iostring= IOstring()
iostring.get_string()
iostring.print_string()

output:

Please input a string:hello531
HELLO531

如果不是题目要求的least two methods,那一个print_string就够了,input直接在init里面就执行了。如下:

class IOstring():
    def __init__(self):
        self.s = input('Please input a string:')

    def print_string(self):
        print(self.s.upper())

iostring = IOstring()
iostring.print_string()

关键注释:类的内置函数

  • init(self,...) 初始化对象,在创建新对象时调用
  • del(self) 释放对象,在对象被删除之前调用
  • new(cls,*args,**kwd) 实例的生成操作
  • str(self) 在使用print语句时被调用
  • getitem(self,key) 获取序列的索引key对应的值,等价于seq[key]
  • len(self) 在调用内联函数len()时被调用
  • cmp(stc,dst) 比较两个对象src和dst
  • getattr(s,name) 获取属性的值
  • setattr(s,name,value) 设置属性的值
  • delattr(s,name) 删除name属性
  • getattribute() getattribute()功能与__getattr__()类似
  • gt(self,other) 判断self对象是否大于other对象
  • lt(slef,other) 判断self对象是否小于other对象
  • ge(slef,other) 判断self对象是否大于或者等于other对象
  • le(slef,other) 判断self对象是否小于或者等于other对象
  • eq(slef,other) 判断self对象是否等于other对象
  • call(self,*args) 把实例对象作为函数调用

上面分析就这么些,后面我们专门开一期,好好学习一下python class问题,以及对上述类的内置函数,掰开的一一详述,期待吧。

今天就学习两题,如果你是新手,那这篇文章就再适合不过了。如果你是老手,也希望能对你有所启发。 慢即是快(slow is fast),每日一题,跟着更新,就会变强。欢迎各位留言、私信。去公众号CV初学者,留言讨论,这样也方便我们的交流。让我们一起在学习这条道儿上,携手前行。PS:CV初学者 招合作编辑,喜欢写作总结的小伙伴,一起来分享,后台联系加小白君微信。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接