In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the built-in exceptions in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what built-in anomalies are there in Python"?
1. SyntaxError
SyntaxError is mainly caused by errors in Python syntax, such as a few colons, multiple quotes, and so on. A slight carelessness in programming will lead to errors, which should be the most common exception.
In [1]: While True print ('1') File "", line 1 While True print ('1') ^ SyntaxError: invalid syntax
2. TypeError
TypeError is a type error, that is, when an operation or function is applied to an object of an inappropriate type, such as addition and subtraction between integer and character types, subtraction between two lists, and so on.
In [8]: a = [1J 2] B = [2mer3] In [9]: a murb-TypeError Traceback (most recent call last) in-> 1 a Murb TypeError: unsupported operand type (s) for -: 'list' and' list'
3. IndexError
IndexError refers to an error in the index, such as the most common subscript index that exceeds the sequence boundary, such as when a sequence m has only three elements and attempts to access m [4].
In [16]: M = [1] 2 3] In [17]: M [4]-IndexError Traceback (most recent call last) in-> 1 m [4] IndexError: list index out of range
4. KeyError
KeyError is a keyword error, and this exception occurs mainly in the dictionary, such as when a user tries to access a key that does not exist in the dictionary.
In [18]: dict_ = {'1mm In [19]: dict_ [3']-KeyError Traceback (most recent call last) In-> 1 dict_ ['3'] KeyError:'3'
5. ValueError
ValueError is a value error, which is raised when the user passes in a value that the caller does not expect, even if the value is of the correct type, such as trying to get an index of a value that does not exist in a list.
In [22]: n = [1] 2 3] In [23]: n.index (4)-ValueError Traceback (most recent call last) in-> 1 n.index (4) ValueError: 4 is not in list
6. AttributeError
AttributeError is a property error that is thrown when a user tries to access a property that does not exist in an object, such as the list has an index method but the dictionary does not, so calling this method on a dictionary object throws the exception.
In [25]: dict_ = {'1mm In [26]: dict_.index ('1')-AttributeError Traceback (most recent call last) In-> 1 dict_.index ('1') AttributeError: 'dict' object has no attribute' index'
7. NameError
NameError refers to an error in the name of a variable, such as when a user tries to call a variable that has not been assigned or initialized.
In [27]: print (list_)-NameError Traceback (most recent call last) in-> 1 print (list_) NameError: name 'list_' is not defined
8. FileNotFoundError
FileNotFoundError is an open file error that is raised when a user attempts to open a file that does not exist in read mode.
In [29]: fb = open ('. / list' 'r')-FileNotFoundError Traceback (most recent call last) in-> 1 fb = open ('. / list' 'r') FileNotFoundError: [Errno 2] No such file or directory:'. / list'
9. StopIteration
StopIteration is an iterator error, and this exception is thrown when access continues when the last value of the iterator is accessed, reminding the user that there are no values in the iterator to access.
In [30]: list1 = [1Power2] In [31]: list2 = iter (list1) In [33]: next (list2) Out [33]: 1 In [34]: next (list2) Out [34]: 2 In [35]: next (list2)-- -StopIteration Traceback (most recent call last) in-> 1 next (list2)
10. AssertionError
AssertionError is an assertion error, which is thrown when a user detects an exception using an assertion statement if the expression detected by the assertion statement is false.
In [45]: list3 = [1Magne2] In [46]: assert len (list3) > 2-AssertionError Traceback (most recent call last) in -> 1 assert len (list3) > 2 AssertionError: thank you for reading. The above is the content of "what built-in exceptions are there in Python". After the study of this article, I believe you have a deeper understanding of what built-in exceptions are in Python, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.