Python is an easy to learn, powerful programming language. It has efficienthigh-level data structures and a simple but effective approach toobject-oriented programming. Python’s elegant syntax and dynamic typing,together with its interpreted nature, make it an ideal language for scriptingand rapid application development in many areas on most platforms.
The Python interpreter and the extensive standard library are freely availablein source or binary form for all major platforms from the Python web site,https://www.python.org/, and may be freely distributed. The same site alsocontains distributions of and pointers to many free third party Python modules,programs and tools, and additional documentation.
The Python interpreter is easily extended with new functions and data typesimplemented in C or C++ (or other languages callable from C). Python is alsosuitable as an extension language for customizable applications.
This tutorial introduces the reader informally to the basic concepts andfeatures of the Python language and system. It helps to have a Pythoninterpreter handy for hands-on experience, but all examples are self-contained,so the tutorial can be read off-line as well.
For a description of standard objects and modules, see The Python Standard Library.The Python Language Reference gives a more formal definition of the language. To writeextensions in C or C++, read Extending and Embedding the Python Interpreter andPython/C API Reference Manual. There are also several books covering Python in depth.
This tutorial does not attempt to be comprehensive and cover every singlefeature, or even every commonly used feature. Instead, it introduces many ofPython’s most noteworthy features, and will give you a good idea of thelanguage’s flavor and style. After reading it, you will be able to read andwrite Python modules and programs, and you will be ready to learn more about thevarious Python library modules described in The Python Standard Library.
The Glossary is also worth going through.
1. Whetting Your Appetite2. Using the Python Interpreter2.1. Invoking the Interpreter2.1.1. Argument Passing2.1.2. Interactive Mode2.2. The Interpreter and Its Environment2.2.1. Source Code Encoding3. An Informal Introduction to Python3.1. Using Python as a Calculator3.1.1. Numbers3.1.2. Text3.1.3. Lists3.2. First Steps Towards Programming4. More Control Flow Tools4.1. if Statements4.2. for Statements4.3. The range() Function4.4. break and continue Statements4.5. else Clauses on Loops4.6. pass Statements4.7. match Statements4.8. Defining Functions4.9. More on Defining Functions4.9.1. Default Argument Values4.9.2. Keyword Arguments4.9.3. Special parameters4.9.3.1. Positional-or-Keyword Arguments4.9.3.2. Positional-Only Parameters4.9.3.3. Keyword-Only Arguments4.9.3.4. Function Examples4.9.3.5. Recap4.9.4. Arbitrary Argument Lists4.9.5. Unpacking Argument Lists4.9.6. Lambda Expressions4.9.7. Documentation Strings4.9.8. Function Annotations4.10. Intermezzo: Coding Style5. Data Structures5.1. More on Lists5.1.1. Using Lists as Stacks5.1.2. Using Lists as Queues5.1.3. List Comprehensions5.1.4. Nested List Comprehensions5.2. The del statement5.3. Tuples and Sequences5.4. Sets5.5. Dictionaries5.6. Looping Techniques5.7. More on Conditions5.8. Comparing Sequences and Other Types6. Modules6.1. More on Modules6.1.1. Executing modules as scripts6.1.2. The Module Search Path6.1.3. “Compiled” Python files6.2. Standard Modules6.3. The dir() Function6.4. Packages6.4.1. Importing * From a Package6.4.2. Intra-package References6.4.3. Packages in Multiple Directories7. Input and Output7.1. Fancier Output Formatting7.1.1. Formatted String Literals7.1.2. The String format() Method7.1.3. Manual String Formatting7.1.4. Old string formatting7.2. Reading and Writing Files7.2.1. Methods of File Objects7.2.2. Saving structured data with json8. Errors and Exceptions8.1. Syntax Errors8.2. Exceptions8.3. Handling Exceptions8.4. Raising Exceptions8.5. Exception Chaining8.6. User-defined Exceptions8.7. Defining Clean-up Actions8.8. Predefined Clean-up Actions8.9. Raising and Handling Multiple Unrelated Exceptions8.10. Enriching Exceptions with Notes9. Classes9.1. A Word About Names and Objects9.2. Python Scopes and Namespaces9.2.1. Scopes and Namespaces Example9.3. A First Look at Classes9.3.1. Class Definition Syntax9.3.2. Class Objects9.3.3. Instance Objects9.3.4. Method Objects9.3.5. Class and Instance Variables9.4. Random Remarks9.5. Inheritance9.5.1. Multiple Inheritance9.6. Private Variables9.7. Odds and Ends9.8. Iterators9.9. Generators9.10. Generator Expressions10. Brief Tour of the Standard Library10.1. Operating System Interface10.2. File Wildcards10.3. Command Line Arguments10.4. Error Output Redirection and Program Termination10.5. String Pattern Matching10.6. Mathematics10.7. Internet Access10.8. Dates and Times10.9. Data Compression10.10. Performance Measurement10.11. Quality Control10.12. Batteries Included11. Brief Tour of the Standard Library — Part II11.1. Output Formatting11.2. Templating11.3. Working with Binary Data Record Layouts11.4. Multi-threading11.5. Logging11.6. Weak References11.7. Tools for Working with Lists11.8. Decimal Floating-Point Arithmetic12. Virtual Environments and Packages12.1. Introduction12.2. Creating Virtual Environments12.3. Managing Packages with pip13. What Now?14. Interactive Input Editing and History Substitution14.1. Tab Completion and History Editing14.2. Alternatives to the Interactive Interpreter15. Floating-Point Arithmetic: Issues and Limitations15.1. Representation Error16. Appendix16.1. Interactive Mode16.1.1. Error Handling16.1.2. Executable Python Scripts16.1.3. The Interactive Startup File16.1.4. The Customization Modules