首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
  • 您找到你想要的搜索结果了吗?
    是的
    没有找到

    python基础6

    *******************             *  异常处理与调式         *             ******************* ***常见错误:*** 1) 名字没有定义,NameError In [1]: print a --------------------------------------------------------------------------- NameError                                 Traceback (most recent call last) <ipython-input-1-9d7b17ad5387> in <module>() ----> 1 print a NameError: name 'a' is not defined 2) 分母为零,ZeroDivisionError In [2]: 10/0 --------------------------------------------------------------------------- ZeroDivisionError                         Traceback (most recent call last) <ipython-input-2-242277fd9e32> in <module>() ----> 1 10/0 ZeroDivisionError: integer division or modulo by zero 3) 文件不存在,IOError In [3]: open("westos") --------------------------------------------------------------------------- IOError                                   Traceback (most recent call last) <ipython-input-3-2778d2991600> in <module>() ----> 1 open("westos") IOError: [Errno 2] No such file or directory: 'westos' 4) 语法错误,SyntaxError In [4]: for i in [1,2,3]   File "<ipython-input-4-ae71676907af>", line 1     for i in [1,2,3]                     ^ SyntaxError: invalid syntax 5) 索引超出范围,IndexError In [5]: a = [1,2,3] In [6]: a[3] --------------------------------------------------------------------------- IndexError                                Traceback (most recent call last) <ipython-input-6-94e7916e7615> in <module>() ----> 1 a[3] IndexError: list index out of range In [7]: t =(1,2,3) In [8]: t[3] --------------------------------------------------------------------------- IndexError                                Traceback (most recent call last) <ipython-input-8-7d5cf04057c5> in <module>() ----> 1 t[3] IndexError: tuple index out of range In [9]: t[1:9]            ###切片的时候,若超出范围,则默认为全部,不报错 Out[9]: (2, 3) ####python异常处理机制:try......except......finally###### 例: #!/usr/bin/env python #coding:utf-8 try:                ###将可能发生错误的部分放在try下###     print "staring......"     li = [1,2,3]     print a     pri

    02

    python基础3

    交换: a,b=b,a 相当于定义了一个元组t=(b,a) 然后将t[0]的值给了a,t[1]的值给了b ####字典#### 定义用花括号 集合定义若为空的话,会默认为字典,所以集合不能为空 子典只能通过关键字来查找值,因为字典是key-value(关键字-值),因此不能通过值来查找关键字 In [1]: dic = {"user1":"123","user2":"234","user3":"789"} In [3]: dic["234"] --------------------------------------------------------------------------- KeyError                                  Traceback (most recent call last) <ipython-input-3-2845b64d96b1> in <module>() ----> 1 dic["234"] KeyError: '234' 字典是一个无序的数据类型,因此也不能进行索引和切片等操作。 In [1]: dic = {"user1":"123","user2":"234","user3":"789"} In [2]: dic["user1"] Out[2]: '123' In [5]: dic["user2"] Out[5]: '234' In [7]: user = ['user1','user2','user3'] In [8]: passwd = ['123','234','456'] In [9]: zip(user,passwd) Out[9]: [('user1', '123'), ('user2', '234'), ('user3', '456')] In [10]: 当你有一个用户名单和密码,若使用列表的类型,判断用户是否和密码一致时,就比较麻烦,而使用字典时,只需通过关键子就可以返回相对应的值,(如上例子:当定义一个子典当你搜索user1时,字典类型就会返回该关键字对应的密码,此时只需判断该密码是否匹配即可) ####字典的基本操作### In [17]: dic. dic.clear       dic.items       dic.pop         dic.viewitems dic.copy        dic.iteritems   dic.popitem     dic.viewkeys dic.fromkeys    dic.iterkeys    dic.setdefault  dic.viewvalues dic.get         dic.itervalues  dic.update       dic.has_key     dic.keys        dic.values 字典添加 In [12]: dic Out[12]: {'user1': '123', 'user2': '234', 'user3': '789'} In [13]: dic["westos"]='linux' In [14]: dic Out[14]: {'user1': '123', 'user2': '234', 'user3': '789', 'westos': 'linux'} In [15]: dic["hello"]='world' In [16]: dic            ####由此可以看出字典是无序的,在添加时,并不会按照顺序往后添加#### Out[16]: {'hello': 'world',  'user1': '123',  'user2': '234',  'user3': '789',  'westos': 'linux'} In [17]: 字典更新 In [22]: dic Out[22]: {'hello': 'world', 'user1': '123', 'user2': '234', 'user3': '789'} In [23]: dic["user1"]="redhat"        ###可直接通过赋值对关键字进行更新### In [24]: dic Out[24]: {'hello': 'world', 'user1': 'redhat', 'user2': '234', 'user3': '789'} ###或者通过dic.update更新### In [25]: dic Out[25]: {'hello': 'world', 'user1': 'redhat', 'user2': '234', 'user3': '789'} In [26]: help(di

    01

    怎样从优秀教师变成超级主播?本文全都告诉你

    为了控制疫情,全国各大中小学都推迟了开学时间。教育部门希望各个学校根据自身情况,开展停课不停学的工作。可是,许多在讲台上经验丰富的老师,变成主播之后瞬间各种翻车,许多同事都想让我给大家讲讲如何方便的给学生上网课。 从我个人的经验看,直播网课比录播网课的效果更好。首先,能够与学生直接交流的直播网课更接近传统教学,老师更容易发挥出水平,学生更能集中注意力。其次,直播教学能比较好控制的时间,节约老师的精力。要知道,如果老师录制一段视频,还要进行剪辑、上传等工作,出了错可能还要重新录制,花费的时间远远超过

    03
    领券