One, iterator
Python iterator
Summary: An object that can be called by the next () function and continuously returns the next value becomes an iterator
ITER (): an iterative object = = Iterator
Next (): Iterate object in Call iterator
> All data types that can directly act on a for loop are iterative objects
> All functions that use the next () method are iterator types
The > collection (set) data type is an iterator object but not an iterator, but can be passed via ITER ()
method to obtain an iterator object.
Code:
1.
A = ['a','b','C'= ITER (a)print(next (a_iter))print(Next (A_iter))
Operation Result:
e:\python_vs_code\directory[directory]>d://py3.6//python.exe e:/python_vs_code/directory[directory]/demo0801/ Demo01.pyab
The result is visible: theiter () method converts the list to an iterator, and the next () method invokes the iterator object.
2.
> iterators
> Cycle Control Process
> Exception Handling
Code:
""
City Information Display (Provincial and municipal cascade display)" "dict_city= {'Shaanxi':['Xian','Xianyang','Yulin','Tongchuan'], 'Henan':['Zhengzhou','Kaifeng','Anyang','Shangqiu'], 'Hubei':['Wuhan','Huanggang','Zhoukou','Yuzhou']}dict_iter=iter (dict_city) Dict_val=iter (Dict_city.values ()) whileTrue:Try: Pro_name=Next (Dict_iter)Print('--%s--'%Pro_name) Val=Next (dict_val) Val_name=Iter (Val) whileTrue:Try: Print('|--%s'%Next (val_name))exceptstopiteration:Print('--'*20) Break exceptstopiteration:Print('End') Break
Operation Result:
e:\python_vs_code\directory[directory]>d://py3.6//python.exe e:/python_vs_code/directory[directory]/demo0801/py_ flie.py--Shaanxi--|-- XI ' an |-- xianyang |-- Yulin |-- Tongchuan ------------------------------------ ------Henan--|-- Zhengzhou |-- Kaifeng |-- anyang |-- Shangqiu ------------------------------------------ Hubei--|-- Wuhan |-- Huanggang |-- zhoukou |-- Yuzhou ---------------------------------------- End
Results visible: iterating through dictionary information using loops
Second, exception handling
1.
Try
# Error Location
Except error type:
# catch errors, handle errors
Else
# The error didn't happen, everything was OK
Finally
# regardless of whether or not an error occurs, execute
Exception writing:
Try: Except try...except...finally ... try.....finally try: Except. Else Note: When an exception occurs in a try block, the code that handles the exception block is executed directly, and the exception that occurs after the code in the current try block is no longer executed. 2. Multiple exception handling exception capture Try...except....except .... Code:
#get two valuesNum1=input ("Please enter the first number") num2=input ("Please enter a second number")#print (num1/num2)#two-number conversion of a divideTry: A=Int (NUM1) b=Int (num2) a/b str1="a"Info=str1[3]exceptValueError:Print("Please enter a number")exceptZerodivisionerror:Print("the divisor cannot be zero")except:#capture of any exception Print("exception occurs")finally: Print("End of program execution")#catch exception in try when any exception can be handled except is in the first except subsequent is not allowed to appear exceptTry: A=Int (NUM1) b=Int (num2) a/b str1="a"Info=str1[3]exceptValueError:Print("Please enter a number")exceptZerodivisionerror:Print("the divisor cannot be zero")except:#capture of any exception Print("exception occurs")finally: Print("End of program execution")
Operation Result:
e:\python_vs_code\directory[directory]>d://py3.6//python.exe e:/python_vs_code/directory[directory]/demo0801/py_except (multi). PY Enter the first Number 10 Please enter a second number 0 divisor cannot be zero program execution end divisor cannot be zero program execution end e:\python_vs_code\directory[Directory]>d://py3.6// Python.exe e:/python_vs_code/directory[directory]/demo0801/py_except (Multi). PY Please enter the first number a Please enter the second number 2 Please enter the number program execution end Please enter the number program execution end
Results: two different error types with different results, multiple error exception handling.
3. Custom Exceptions
Raise ... An exception occurred
Custom exception Classes
Inherit the exception class code:
classageexcept (Exception):Pass Age=1000#Age Value#when the age is greater than 150 exceptionsTry: ifage>150: RaiseAgeexcept ('age .... ')#consider an exception to occur manually #int (' a ')exceptageexcept as ex:Print(ex)Print("exception occurs")#just temporary customizations, not permanent. int'a')
Operation Result:
e:\python_vs_code\directory[directory]>d://py3.6//python.exe e:/python_vs_code/directory[directory]/demo0801/py_except (custom). PY age .... Exception occurs Traceback (most recent call last): "e:/python_vs_code/directory[directory]/demo0801/py_except ( Custom). py" in <module> int ('a' ) for'a'
The result is visible: The exception name can be customized, but only temporarily modified
Python Learning-iterators and exception handling