Summary of exceptions in Python basics, and summary of python Basics

Source: Internet
Author: User

Summary of exceptions in Python basics, and summary of python Basics
Today, we will explore most of the exception reports in python.

First of all, what is an exception? exception vernacular interpretation is abnormal. In a program, it generally means that the format entered by the programmer is not standard, or the required parameter types are not matched, and so on.

For example, many companies send Apple laptops at the end of the year. If you think it is a (MAC) laptop, you will receive an apple + notebook... The type is incorrect first, and the quantity is also incorrect.

First, let's look at several common errors:

NameErrorThe reason for naming error is: name 'A' is not defined name a is not defined. Simply put, the program does not know who is in Table a. If a = 1, the Program understands that a represents 1.

In the future, this type of code can be solved only by finding the undefined error a and assigning the corresponding value to it.

Now let's continue to look at the following:

IndexErrorIndex error (also known as boundary error) cause: the index of the list index out of range list has exceeded its range.

List a has only two parameters. The subscript is 0, 1 (do not understand the basis of the list), and I want to print the value at the position of subscript 2, which is beyond its standard and out of the range.

 

For example, if you take a bus for less than 1 meter and less than 2 meters, you will not be charged if you come up with a bus for less than 1 meter, Because you exceed the specified height.

To continue:

KeyErrorThe key-value error occurs generally in the dictionary because no value of height exists in Dictionary a, and the name has been printed because a already exists.

The dictionary is composed of a one-to-one correspondence between dict = {key: value}. For example, dict is like a company marked by employee key.

The value is equal to the employee's property (Position, salary, corresponding personal information, etc.). This error is equivalent to finding a certain employee in a company. As a result, the company says no such person is the same.

Continue to continue:

ImportErrorCause of import error: No module named nothing does not have the method named nothing.

Generally, this error occurs in spelling or case. The module method is not installed and the reference location is not correct. (The method name or new folder may be changed when the module version is updated)

At this time, I recommend you go to the \ Lib \ site-packages file in the python2.x or python3.x directory to find it (third-party library modules will be stored in these types)

Continue to continue:

IOErrorCause of input/output error: No such file or directory: 'test' is not found in the directory named test

This is easy to understand and out of nothing. The program said: Who are you heading?

Continue to continue:

AttributeErrorCause of member attribute error: Everything is an object. However, no built-in function in the dictionary a object is called sort sorting. As we all know, the dictionary is unordered and the list has this method.

When you define a variable and do not know what built-in functions and methods can be used, you can use dir to view the following methods that can be used currently.

 

 

SyntaxErrorSyntax error: Invalid Syntax a is not defined, so it cannot be converted to an integer. This is also a common error.

Next:

TypeErrorType Error: Unknown list of hash types (which can be understood later). However, it is difficult to put tuples into the list. The dictionary key is generally a single element or a unique identifier,

The sequence that happens to be immutable in tuples is also a unique identifier, so tuples can be placed in keys. For example, if you are the only key in the world and do not exist exactly like you.

The above are common problems and errors. Once learned, you can think about the solution yourself. This will also help you improve your level.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.