当前位置:
首页 > temp > 简明python教程 >
-
python基础学习(九)
19.解包
# 解包 unpacking user1 = ["张三", 21, "1999.1.1"] # tuple 类型 user2 = ("李四", 21, "1999.10.1") # user_name = user[0] # user_age = user[1] # user_bir = user[2] # 更简单的方式把列表元素 附给变量 user_name1, user_age1, user_bir1 = user1 # age和name换了位置 仔细看结果!!! user_age2, user_name2, user_bir2 = user2 print("name1:"+user_name1) print("age1:" + str(user_age1)) print("bir1:"+user_bir1) print("-----------帅气分割线-----------------") print("name2:"+str(user_name2)) print("age2:"+user_age2) print("bir2:"+user_bir2) print("李四的名字和age 位置反了 这说明是按照顺序来赋值的")
run结果:
20.异常处理
错误总结版本:
# 异常 exception 运行时候检测到的错误为 异常 # 错误 error # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 5 # if True # ^ # SyntaxError: invalid syntax if True print("Hello SongKe") # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 13 # print("Hello SongKe1") # ^ # IndentationError: expected an indented block if True: print("Hello SongKe1") # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 19 # print(str_hi) # ^ # IndentationError: unexpected indent str_hi = "Hello SongKe2" print(str_hi) # 请输入num:松柯 # Traceback (most recent call last): # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 26, in <module> # num = int(input("请输入num:")) # ValueError: invalid literal for int() with base 10: '松柯' num1 = int(input("请输入num1:")) print(num1) # 包起来 可能出现异常的代码 try: num2 = int(input("请输入num2:")) print(num2) except: print("error input") # Traceback (most recent call last): # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 40, in <module> # num = 1/0 # ZeroDivisionError: division by zero # 分母不能为0 # num = 1 / 0 # 当输入0 时候 不知道是字符串转换引起的还是 输入0引起的异常 还需要能处理不同情况的 # 请输入num3:0 # division by zero # ------------------------------------------------- # 请输入num3:松柯 # invalid literal for int() with base 10: '松柯' try: num3 = int(input("请输入num3:")) result = 1 / num3 print(result) # ZeroDivisionError 分母为0的异 except ZeroDivisionError as error: print(error) # ValueError 输入异常 except ValueError as error: print(error) def test_fails(num4): return 1 / num4 try: num4 = int(input("请输入num4:")) result1 = test_fails(num4) print(result1) # ZeroDivisionError 分母为0的异常 except ZeroDivisionError as error: print(error) # ValueError 输入异常 except ValueError as error: print(error)
正常版本:
# 异常 exception 运行时候检测到的错误为 异常 # 错误 error # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 5 # if True # ^ # SyntaxError: invalid syntax if True: print("Hello SongKe") # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 13 # print("Hello SongKe1") # ^ # IndentationError: expected an indented block if True: print("Hello SongKe1") # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 19 # print(str_hi) # ^ # IndentationError: unexpected indent str_hi = "Hello SongKe2" print(str_hi) # 请输入num:松柯 # Traceback (most recent call last): # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 26, in <module> # num = int(input("请输入num:")) # ValueError: invalid literal for int() with base 10: '松柯' num1 = int(input("请输入num1:")) print(num1) # 包起来 可能出现异常的代码 try: num2 = int(input("请输入num2:")) print(num2) except: print("error input") # Traceback (most recent call last): # File "/home/yangbin/Study/IdeaProjects/pythonWork/day3/异常处理.py", line 40, in <module> # num = 1/0 # ZeroDivisionError: division by zero # 分母不能为0 # num = 1 / 0 # 当输入0 时候 不知道是字符串转换引起的还是 输入0引起的异常 还需要能处理不同情况的 # 请输入num3:0 # division by zero # ------------------------------------------------- # 请输入num3:松柯 # invalid literal for int() with base 10: '松柯' try: num3 = int(input("请输入num3:")) result = 1 / num3 print(result) # ZeroDivisionError 分母为0的异 except ZeroDivisionError as error: print(error) # ValueError 输入异常 except ValueError as error: print(error) def test_fails(num4): return 1 / num4 try: num4 = int(input("请输入num4:")) result1 = test_fails(num4) print(result1) # ZeroDivisionError 分母为0的异常 except ZeroDivisionError as error: print(error) # ValueError 输入异常 except ValueError as error: print(error)
run结果:
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数