korsygfhrtzangaiide
Elepffwdsff
/
usr
/
share
/
doc
/
python-docs-2.7.5
/
html
/
tutorial
/
Upload FileeE
HOME
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>The Python Tutorial — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="next" title="1. Whetting Your Appetite" href="appetite.html" /> <link rel="prev" title="What’s New in Python 2.0" href="../whatsnew/2.0.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="appetite.html" title="1. Whetting Your Appetite" accesskey="N">next</a> |</li> <li class="right" > <a href="../whatsnew/2.0.html" title="What’s New in Python 2.0" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="the-python-tutorial"> <span id="tutorial-index"></span><h1>The Python Tutorial<a class="headerlink" href="#the-python-tutorial" title="Permalink to this headline">¶</a></h1> <p>Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.</p> <p>The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, <a class="reference external" href="http://www.python.org/">http://www.python.org/</a>, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.</p> <p>The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension language for customizable applications.</p> <p>This tutorial introduces the reader informally to the basic concepts and features of the Python language and system. It helps to have a Python interpreter handy for hands-on experience, but all examples are self-contained, so the tutorial can be read off-line as well.</p> <p>For a description of standard objects and modules, see <a class="reference internal" href="../library/index.html#library-index"><em>The Python Standard Library</em></a>. <a class="reference internal" href="../reference/index.html#reference-index"><em>The Python Language Reference</em></a> gives a more formal definition of the language. To write extensions in C or C++, read <a class="reference internal" href="../extending/index.html#extending-index"><em>Extending and Embedding the Python Interpreter</em></a> and <a class="reference internal" href="../c-api/index.html#c-api-index"><em>Python/C API Reference Manual</em></a>. There are also several books covering Python in depth.</p> <p>This tutorial does not attempt to be comprehensive and cover every single feature, or even every commonly used feature. Instead, it introduces many of Python’s most noteworthy features, and will give you a good idea of the language’s flavor and style. After reading it, you will be able to read and write Python modules and programs, and you will be ready to learn more about the various Python library modules described in <a class="reference internal" href="../library/index.html#library-index"><em>The Python Standard Library</em></a>.</p> <p>The <a class="reference internal" href="../glossary.html#glossary"><em>Glossary</em></a> is also worth going through.</p> <div class="toctree-wrapper compound"> <ul> <li class="toctree-l1"><a class="reference internal" href="appetite.html">1. Whetting Your Appetite</a></li> <li class="toctree-l1"><a class="reference internal" href="interpreter.html">2. Using the Python Interpreter</a><ul> <li class="toctree-l2"><a class="reference internal" href="interpreter.html#invoking-the-interpreter">2.1. Invoking the Interpreter</a><ul> <li class="toctree-l3"><a class="reference internal" href="interpreter.html#argument-passing">2.1.1. Argument Passing</a></li> <li class="toctree-l3"><a class="reference internal" href="interpreter.html#interactive-mode">2.1.2. Interactive Mode</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="interpreter.html#the-interpreter-and-its-environment">2.2. The Interpreter and Its Environment</a><ul> <li class="toctree-l3"><a class="reference internal" href="interpreter.html#error-handling">2.2.1. Error Handling</a></li> <li class="toctree-l3"><a class="reference internal" href="interpreter.html#executable-python-scripts">2.2.2. Executable Python Scripts</a></li> <li class="toctree-l3"><a class="reference internal" href="interpreter.html#source-code-encoding">2.2.3. Source Code Encoding</a></li> <li class="toctree-l3"><a class="reference internal" href="interpreter.html#the-interactive-startup-file">2.2.4. The Interactive Startup File</a></li> <li class="toctree-l3"><a class="reference internal" href="interpreter.html#the-customization-modules">2.2.5. The Customization Modules</a></li> </ul> </li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="introduction.html">3. An Informal Introduction to Python</a><ul> <li class="toctree-l2"><a class="reference internal" href="introduction.html#using-python-as-a-calculator">3.1. Using Python as a Calculator</a><ul> <li class="toctree-l3"><a class="reference internal" href="introduction.html#numbers">3.1.1. Numbers</a></li> <li class="toctree-l3"><a class="reference internal" href="introduction.html#strings">3.1.2. Strings</a></li> <li class="toctree-l3"><a class="reference internal" href="introduction.html#unicode-strings">3.1.3. Unicode Strings</a></li> <li class="toctree-l3"><a class="reference internal" href="introduction.html#lists">3.1.4. Lists</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="introduction.html#first-steps-towards-programming">3.2. First Steps Towards Programming</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="controlflow.html">4. More Control Flow Tools</a><ul> <li class="toctree-l2"><a class="reference internal" href="controlflow.html#if-statements">4.1. <tt class="docutils literal"><span class="pre">if</span></tt> Statements</a></li> <li class="toctree-l2"><a class="reference internal" href="controlflow.html#for-statements">4.2. <tt class="docutils literal"><span class="pre">for</span></tt> Statements</a></li> <li class="toctree-l2"><a class="reference internal" href="controlflow.html#the-range-function">4.3. The <tt class="docutils literal"><span class="pre">range()</span></tt> Function</a></li> <li class="toctree-l2"><a class="reference internal" href="controlflow.html#break-and-continue-statements-and-else-clauses-on-loops">4.4. <tt class="docutils literal"><span class="pre">break</span></tt> and <tt class="docutils literal"><span class="pre">continue</span></tt> Statements, and <tt class="docutils literal"><span class="pre">else</span></tt> Clauses on Loops</a></li> <li class="toctree-l2"><a class="reference internal" href="controlflow.html#pass-statements">4.5. <tt class="docutils literal"><span class="pre">pass</span></tt> Statements</a></li> <li class="toctree-l2"><a class="reference internal" href="controlflow.html#defining-functions">4.6. Defining Functions</a></li> <li class="toctree-l2"><a class="reference internal" href="controlflow.html#more-on-defining-functions">4.7. More on Defining Functions</a><ul> <li class="toctree-l3"><a class="reference internal" href="controlflow.html#default-argument-values">4.7.1. Default Argument Values</a></li> <li class="toctree-l3"><a class="reference internal" href="controlflow.html#keyword-arguments">4.7.2. Keyword Arguments</a></li> <li class="toctree-l3"><a class="reference internal" href="controlflow.html#arbitrary-argument-lists">4.7.3. Arbitrary Argument Lists</a></li> <li class="toctree-l3"><a class="reference internal" href="controlflow.html#unpacking-argument-lists">4.7.4. Unpacking Argument Lists</a></li> <li class="toctree-l3"><a class="reference internal" href="controlflow.html#lambda-forms">4.7.5. Lambda Forms</a></li> <li class="toctree-l3"><a class="reference internal" href="controlflow.html#documentation-strings">4.7.6. Documentation Strings</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="controlflow.html#intermezzo-coding-style">4.8. Intermezzo: Coding Style</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="datastructures.html">5. Data Structures</a><ul> <li class="toctree-l2"><a class="reference internal" href="datastructures.html#more-on-lists">5.1. More on Lists</a><ul> <li class="toctree-l3"><a class="reference internal" href="datastructures.html#using-lists-as-stacks">5.1.1. Using Lists as Stacks</a></li> <li class="toctree-l3"><a class="reference internal" href="datastructures.html#using-lists-as-queues">5.1.2. Using Lists as Queues</a></li> <li class="toctree-l3"><a class="reference internal" href="datastructures.html#functional-programming-tools">5.1.3. Functional Programming Tools</a></li> <li class="toctree-l3"><a class="reference internal" href="datastructures.html#list-comprehensions">5.1.4. List Comprehensions</a><ul> <li class="toctree-l4"><a class="reference internal" href="datastructures.html#nested-list-comprehensions">5.1.4.1. Nested List Comprehensions</a></li> </ul> </li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="datastructures.html#the-del-statement">5.2. The <tt class="docutils literal"><span class="pre">del</span></tt> statement</a></li> <li class="toctree-l2"><a class="reference internal" href="datastructures.html#tuples-and-sequences">5.3. Tuples and Sequences</a></li> <li class="toctree-l2"><a class="reference internal" href="datastructures.html#sets">5.4. Sets</a></li> <li class="toctree-l2"><a class="reference internal" href="datastructures.html#dictionaries">5.5. Dictionaries</a></li> <li class="toctree-l2"><a class="reference internal" href="datastructures.html#looping-techniques">5.6. Looping Techniques</a></li> <li class="toctree-l2"><a class="reference internal" href="datastructures.html#more-on-conditions">5.7. More on Conditions</a></li> <li class="toctree-l2"><a class="reference internal" href="datastructures.html#comparing-sequences-and-other-types">5.8. Comparing Sequences and Other Types</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="modules.html">6. Modules</a><ul> <li class="toctree-l2"><a class="reference internal" href="modules.html#more-on-modules">6.1. More on Modules</a><ul> <li class="toctree-l3"><a class="reference internal" href="modules.html#executing-modules-as-scripts">6.1.1. Executing modules as scripts</a></li> <li class="toctree-l3"><a class="reference internal" href="modules.html#the-module-search-path">6.1.2. The Module Search Path</a></li> <li class="toctree-l3"><a class="reference internal" href="modules.html#compiled-python-files">6.1.3. “Compiled” Python files</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="modules.html#standard-modules">6.2. Standard Modules</a></li> <li class="toctree-l2"><a class="reference internal" href="modules.html#the-dir-function">6.3. The <tt class="docutils literal"><span class="pre">dir()</span></tt> Function</a></li> <li class="toctree-l2"><a class="reference internal" href="modules.html#packages">6.4. Packages</a><ul> <li class="toctree-l3"><a class="reference internal" href="modules.html#importing-from-a-package">6.4.1. Importing * From a Package</a></li> <li class="toctree-l3"><a class="reference internal" href="modules.html#intra-package-references">6.4.2. Intra-package References</a></li> <li class="toctree-l3"><a class="reference internal" href="modules.html#packages-in-multiple-directories">6.4.3. Packages in Multiple Directories</a></li> </ul> </li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="inputoutput.html">7. Input and Output</a><ul> <li class="toctree-l2"><a class="reference internal" href="inputoutput.html#fancier-output-formatting">7.1. Fancier Output Formatting</a><ul> <li class="toctree-l3"><a class="reference internal" href="inputoutput.html#old-string-formatting">7.1.1. Old string formatting</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="inputoutput.html#reading-and-writing-files">7.2. Reading and Writing Files</a><ul> <li class="toctree-l3"><a class="reference internal" href="inputoutput.html#methods-of-file-objects">7.2.1. Methods of File Objects</a></li> <li class="toctree-l3"><a class="reference internal" href="inputoutput.html#the-pickle-module">7.2.2. The <tt class="docutils literal"><span class="pre">pickle</span></tt> Module</a></li> </ul> </li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="errors.html">8. Errors and Exceptions</a><ul> <li class="toctree-l2"><a class="reference internal" href="errors.html#syntax-errors">8.1. Syntax Errors</a></li> <li class="toctree-l2"><a class="reference internal" href="errors.html#exceptions">8.2. Exceptions</a></li> <li class="toctree-l2"><a class="reference internal" href="errors.html#handling-exceptions">8.3. Handling Exceptions</a></li> <li class="toctree-l2"><a class="reference internal" href="errors.html#raising-exceptions">8.4. Raising Exceptions</a></li> <li class="toctree-l2"><a class="reference internal" href="errors.html#user-defined-exceptions">8.5. User-defined Exceptions</a></li> <li class="toctree-l2"><a class="reference internal" href="errors.html#defining-clean-up-actions">8.6. Defining Clean-up Actions</a></li> <li class="toctree-l2"><a class="reference internal" href="errors.html#predefined-clean-up-actions">8.7. Predefined Clean-up Actions</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="classes.html">9. Classes</a><ul> <li class="toctree-l2"><a class="reference internal" href="classes.html#a-word-about-names-and-objects">9.1. A Word About Names and Objects</a></li> <li class="toctree-l2"><a class="reference internal" href="classes.html#python-scopes-and-namespaces">9.2. Python Scopes and Namespaces</a></li> <li class="toctree-l2"><a class="reference internal" href="classes.html#a-first-look-at-classes">9.3. A First Look at Classes</a><ul> <li class="toctree-l3"><a class="reference internal" href="classes.html#class-definition-syntax">9.3.1. Class Definition Syntax</a></li> <li class="toctree-l3"><a class="reference internal" href="classes.html#class-objects">9.3.2. Class Objects</a></li> <li class="toctree-l3"><a class="reference internal" href="classes.html#instance-objects">9.3.3. Instance Objects</a></li> <li class="toctree-l3"><a class="reference internal" href="classes.html#method-objects">9.3.4. Method Objects</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="classes.html#random-remarks">9.4. Random Remarks</a></li> <li class="toctree-l2"><a class="reference internal" href="classes.html#inheritance">9.5. Inheritance</a><ul> <li class="toctree-l3"><a class="reference internal" href="classes.html#multiple-inheritance">9.5.1. Multiple Inheritance</a></li> </ul> </li> <li class="toctree-l2"><a class="reference internal" href="classes.html#private-variables-and-class-local-references">9.6. Private Variables and Class-local References</a></li> <li class="toctree-l2"><a class="reference internal" href="classes.html#odds-and-ends">9.7. Odds and Ends</a></li> <li class="toctree-l2"><a class="reference internal" href="classes.html#exceptions-are-classes-too">9.8. Exceptions Are Classes Too</a></li> <li class="toctree-l2"><a class="reference internal" href="classes.html#iterators">9.9. Iterators</a></li> <li class="toctree-l2"><a class="reference internal" href="classes.html#generators">9.10. Generators</a></li> <li class="toctree-l2"><a class="reference internal" href="classes.html#generator-expressions">9.11. Generator Expressions</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="stdlib.html">10. Brief Tour of the Standard Library</a><ul> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#operating-system-interface">10.1. Operating System Interface</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#file-wildcards">10.2. File Wildcards</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#command-line-arguments">10.3. Command Line Arguments</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#error-output-redirection-and-program-termination">10.4. Error Output Redirection and Program Termination</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#string-pattern-matching">10.5. String Pattern Matching</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#mathematics">10.6. Mathematics</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#internet-access">10.7. Internet Access</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#dates-and-times">10.8. Dates and Times</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#data-compression">10.9. Data Compression</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#performance-measurement">10.10. Performance Measurement</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#quality-control">10.11. Quality Control</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib.html#batteries-included">10.12. Batteries Included</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="stdlib2.html">11. Brief Tour of the Standard Library – Part II</a><ul> <li class="toctree-l2"><a class="reference internal" href="stdlib2.html#output-formatting">11.1. Output Formatting</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib2.html#templating">11.2. Templating</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib2.html#working-with-binary-data-record-layouts">11.3. Working with Binary Data Record Layouts</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib2.html#multi-threading">11.4. Multi-threading</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib2.html#logging">11.5. Logging</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib2.html#weak-references">11.6. Weak References</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib2.html#tools-for-working-with-lists">11.7. Tools for Working with Lists</a></li> <li class="toctree-l2"><a class="reference internal" href="stdlib2.html#decimal-floating-point-arithmetic">11.8. Decimal Floating Point Arithmetic</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="whatnow.html">12. What Now?</a></li> <li class="toctree-l1"><a class="reference internal" href="interactive.html">13. Interactive Input Editing and History Substitution</a><ul> <li class="toctree-l2"><a class="reference internal" href="interactive.html#line-editing">13.1. Line Editing</a></li> <li class="toctree-l2"><a class="reference internal" href="interactive.html#history-substitution">13.2. History Substitution</a></li> <li class="toctree-l2"><a class="reference internal" href="interactive.html#key-bindings">13.3. Key Bindings</a></li> <li class="toctree-l2"><a class="reference internal" href="interactive.html#alternatives-to-the-interactive-interpreter">13.4. Alternatives to the Interactive Interpreter</a></li> </ul> </li> <li class="toctree-l1"><a class="reference internal" href="floatingpoint.html">14. Floating Point Arithmetic: Issues and Limitations</a><ul> <li class="toctree-l2"><a class="reference internal" href="floatingpoint.html#representation-error">14.1. Representation Error</a></li> </ul> </li> </ul> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="../whatsnew/2.0.html" title="previous chapter">What’s New in Python 2.0</a></p> <h4>Next topic</h4> <p class="topless"><a href="appetite.html" title="next chapter">1. Whetting Your Appetite</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/tutorial/index.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="appetite.html" title="1. Whetting Your Appetite" >next</a> |</li> <li class="right" > <a href="../whatsnew/2.0.html" title="What’s New in Python 2.0" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>