A mistake that Python beginners make easy

Source: Internet
Author: User

1. Colon: Always remember to enter ":" At the end of the first line of the compound statement (if, while, for, and so on)

2. Blank lines are important at the interactive mode prompt. Blank lines within a compound statement in a module file are ignored, but when you enter code at the interactive mode prompt, the blank line is the closing sentence. “...”

3, indentation to be consistent. Avoid mixing tabs and spaces in block indentation unless you know how the text editor handles tabs. Otherwise, if the editor is counting tabs as spaces, what you see in the editor is not necessarily what Python sees.

4. Do not write C code in Python. When you write the IF and while first line, you do not have to test both sides of the input brackets (for example, if (x==1);), do not use semicolons to grow all the statements. Do not embed an assignment statement in a while loop test, and do not use {} around the block, instead using a consistent indentation of the nested program code block.

5. Use a simple for loop, not a while or range. Because Python handles index operations internally for a simple for loop, it is sometimes twice times faster than the equivalent while.

6. Do not expect the function that modifies the object in its original place to return the result. For example, the List.append and List.sort methods have no return value (except none) for this kind of modification operation. So don't write a statement like Mylist=mylist.append (x)

7. Be sure to call the function with parentheses. If closing the file is not file.close but File.close ()

8. Do not use extensions or paths in import and overloading. such as import mod instead of import mod.py

A mistake that Python beginners make easy

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.