19. unpack
# Unpack unpacking user1 = ["张三", 21, "1999.1.1"] # Tuple type user2 = ("李四", 21, "1999.10.1") # user_name = user[0] # user_age = user[1] # user_bir = user[2] # Easier way to list elements attached to the variable user_name1, user_age1, user_bir1 = user1 # Age and name for a location closer look at the results! ! ! user_age2, user_name2, user_bir2 = user2 print("name1:"+user_name1) print("age1:" + str(user_age1)) print("bir1:"+user_bir1) Print ( " ----------- ----------------- handsome parting line " ) print("name2:"+str(user_name2)) print("age2:"+user_age2) print("bir2:"+user_bir2) Print ( " John Doe's name and age position is reversed in order to explain this assignment ." )
run results:
20. Exception Handling
Error summary version:
# Exception exception run time errors detected as abnormal # Error 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 / exception handling .py " , Line 19 # print(str_hi) # ^ # IndentationError: unexpected indent str_hi = "Hello SongKe2" print(str_hi) Please enter # num: Songke # Traceback (most recent call last): File # " / Home / yangbin / Study / IdeaProjects / pythonWork / Day3 / exception handling .py " , Line 26 is , in <Module1> # NUM = int (INPUT ( " Please enter NUM: " )) A ValueError #: invalid literal for int () with Base 10 : ' Songke ' num1 = int (INPUT ( " Please enter num1: " )) print(num1) # Wrap the exception codes may appear try: num2 = int (the INPUT ( " Please enter 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 # Denominator can not be 0 # A = 1 / 0 # 0 when the input string is not know when the input 0 transition causes abnormal or need to handle different situations caused # Please enter num3: 0 # division by zero # ------------------------------------------------- # Please enter num3: Songke # invalid literal for int() with base 10: '松柯' try: num3 = int (the INPUT ( " Please enter num3: " )) result = 1 / num3 print(result) # ZeroDivisionError denominator is 0 iso except ZeroDivisionError as error: print(error) # ValueError input error except ValueError as error: print(error) def test_fails(num4): return 1 / num4 try: Num4 = int (the INPUT ( " Please enter Num4: " )) result1 = test_fails(num4) print(result1) # ZeroDivisionError denominator is zero exception except ZeroDivisionError as error: print(error) # ValueError input error except ValueError as error: print(error)
Normal version:
# Exception exception run time errors detected as abnormal # Error 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 / exception handling .py " , Line 19 # print(str_hi) # ^ # IndentationError: unexpected indent str_hi = "Hello SongKe2" print(str_hi) Please enter # num: Songke # Traceback (most recent call last): File # " / Home / yangbin / Study / IdeaProjects / pythonWork / Day3 / exception handling .py " , Line 26 is , in <Module1> # NUM = int (INPUT ( " Please enter NUM: " )) A ValueError #: invalid literal for int () with Base 10 : ' Songke ' num1 = int (INPUT ( " Please enter num1: " )) print(num1) # Wrap the exception codes may appear try: num2 = int (the INPUT ( " Please enter 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 # Denominator can not be 0 # A = 1 / 0 # 0 when the input string is not know when the input 0 transition causes abnormal or need to handle different situations caused # Please enter num3: 0 # division by zero # ------------------------------------------------- # Please enter num3: Songke # invalid literal for int() with base 10: '松柯' try: num3 = int (the INPUT ( " Please enter num3: " )) result = 1 / num3 print(result) # ZeroDivisionError denominator is 0 iso except ZeroDivisionError as error: print(error) # ValueError input error except ValueError as error: print(error) def test_fails(num4): return 1 / num4 try: Num4 = int (the INPUT ( " Please enter Num4: " )) result1 = test_fails(num4) print(result1) # ZeroDivisionError denominator is zero exception except ZeroDivisionError as error: print(error) # ValueError input error except ValueError as error: print(error)
run results: