Python Tutorial PYTHON TUTORIAL Simply Easy Learning by tutorialspoint.com tutorialspoint.com i ABOUT THE TUTORIAL Python Tutorial Python is a general-purpose, interpreted, interactive, object-oriented and high-level programming language. Python was created by Guido van Rossum in the late eighties and early nineties. Like Perl, Python source code is also now available under the GNU General Public License (GPL). Audience This tutorial has been designed for software programmers with a need to understand the Python programming language starting from scratch. This tutorial will give you enough understanding on Python programming language from where you can take yourself to a higher level of expertise. Prerequisites Before proceeding with this tutorial you should have a basic understanding of Computer Programming terminologies. A basic understanding of any of the programming languages will help you in understanding the Python programming concepts and move fast on the learning track. Copyright & Disclaimer Notice All the content and graphics on this tutorial are the property of tutorialspoint.com. Any content from tutorialspoint.com or this tutorial may not be redistributed or reproduced in any way, shape, or form without the written permission of tutorialspoint.com. Failure to do so is a violation of copyright laws. This tutorial may contain inaccuracies or errors and tutorialspoint provides no guarantee regarding the accuracy of the site or its contents including this tutorial. If you discover that the tutorialspoint.com site or this tutorial content contains some errors, please contact us at [email protected] TUTORIALS POINT Simply Easy Learning Table of Content Python Tutorial .......................................................................... 2 Audience .................................................................................... 2 Prerequisites .............................................................................. 2 Copyright & Disclaimer Notice ................................................... 2 Python Overview ..................................................................... 40 History of Python: ..................................................................................... 40 Python Features: ...................................................................................... 40 Python Environment ................................................................ 42 Getting Python: ........................................................................................ 43 Install Python: ........................................................................................... 43 Unix & Linux Installation: .......................................................................... 43 Windows Installation: ............................................................................... 43 Macintosh Installation: .............................................................................. 44 Setting up PATH: ..................................................................................... 44 Setting path at Unix/Linux: ....................................................................... 44 Setting path at Windows: ......................................................................... 44 Python Environment Variables: ................................................................ 45 Running Python: ....................................................................................... 45 (1) Interactive Interpreter: ......................................................................... 45 (2) Script from the Command-line: ........................................................... 46 (3) Integrated Development Environment ................................................ 46 Python Basic Syntax ................................................................ 47 First Python Program: .............................................................................. 47 INTERACTIVE MODE PROGRAMMING: ................................................ 47 SCRIPT MODE PROGRAMMING: .......................................................... 47 Python Identifiers: .................................................................................... 48 Reserved Words: ..................................................................................... 48 Lines and Indentation: .............................................................................. 49 Multi-Line Statements: ............................................................................. 50 Quotation in Python: ................................................................................. 50 Comments in Python: ............................................................................... 50 Using Blank Lines: ................................................................................... 51 Waiting for the User: ................................................................................ 51 Multiple Statements on a Single Line: ...................................................... 51 Multiple Statement Groups as Suites: ...................................................... 51 Command-Line Arguments: ..................................................................... 51 Accessing Command-Line Arguments: .................................................... 52 TUTORIALS POINT Simply Easy Learning Example: .................................................................................................. 52 Parsing Command-Line Arguments: ....................................... 52 getopt.getopt method: .............................................................................. 53 exception getopt.GetoptError: .................................................................. 53 Example ................................................................................................... 53 Python Variable Types ............................................................ 55 Assigning Values to Variables: ................................................................. 55 Multiple Assignment: ................................................................................ 55 Standard Data Types: .............................................................................. 56 Python Numbers: ..................................................................................... 56 Examples: ................................................................................................ 57 Python Strings: ......................................................................................... 57 Python Lists: ............................................................................................. 58 Python Tuples: ......................................................................................... 58 Python Dictionary: .................................................................................... 59 Data Type Conversion: ............................................................................ 59 Python Basic Operators ........................................................... 62 Python Arithmetic Operators: ................................................................... 62 Example: .................................................................................................. 63 Python Comparison Operators: ................................................................ 64 Example: .................................................................................................. 64 Python Assignment Operators: ................................................................ 66 Example: .................................................................................................. 66 Python Bitwise Operators: ........................................................................ 67 Example: .................................................................................................. 68 Python Logical Operators: ........................................................................ 69 Example: .................................................................................................. 69 Python Membership Operators: ............................................................... 70 Example: .................................................................................................. 70 Python Identity Operators: ....................................................................... 71 Example: .................................................................................................. 71 Python Operators Precedence ................................................................. 72 Example: .................................................................................................. 73 Python Decision Making .......................................................... 75 If statements ............................................................................................. 76 Syntax: ..................................................................................................... 76 Flow Diagram: .......................................................................................... 76 Example: .................................................................................................. 76 if...else statements ................................................................................... 77 TUTORIALS POINT Simply Easy Learning Syntax: ..................................................................................................... 77 Flow Diagram: .......................................................................................... 77 Example: .................................................................................................. 78 The elif Statement .................................................................................... 78 Example: .................................................................................................. 78 nested if statements ................................................................................. 79 Syntax: ..................................................................................................... 79 Example: .................................................................................................. 79 Single Statement Suites: .......................................................................... 80 Python Loops ........................................................................... 81 while loop ................................................................................................. 82 Syntax: ..................................................................................................... 82 Flow Diagram: .......................................................................................... 82 Example: .................................................................................................. 83 The Infinite Loop: ..................................................................................... 83 The else Statement Used with Loops ...................................................... 84 Single Statement Suites: .......................................................................... 84 Syntax: ..................................................................................................... 84 Flow Diagram: .......................................................................................... 85 Example: .................................................................................................. 85 Iterating by Sequence Index: ................................................................... 86 The else Statement Used with Loops ...................................................... 86 Syntax: ..................................................................................................... 87 Example: .................................................................................................. 87 Loop Control Statements: ........................................................................ 88 Syntax: ..................................................................................................... 88 Flow Diagram: .......................................................................................... 89 Example: .................................................................................................. 89 Syntax: ..................................................................................................... 90 Flow Diagram: .......................................................................................... 90 Example: .................................................................................................. 90 Syntax: ..................................................................................................... 91 Example: .................................................................................................. 91 Python Numbers ...................................................................... 92 Examples: ................................................................................................ 92 Number Type Conversion: ....................................................................... 93 Mathematical Functions: .......................................................................... 93 Syntax ...................................................................................................... 96 Parameters ............................................................................................... 96 TUTORIALS POINT Simply Easy Learning Return Value ............................................................................................ 97 Example ................................................................................................... 97 Description ............................................................................................... 97 Syntax ...................................................................................................... 97 Parameters ............................................................................................... 97 Return Value ............................................................................................ 97 Example ................................................................................................... 97 Description ............................................................................................... 98 Syntax ...................................................................................................... 98 Parameters ............................................................................................... 98 Return Value ............................................................................................ 98 Example ................................................................................................... 98 Description ............................................................................................... 99 Syntax ...................................................................................................... 99 Parameters ............................................................................................... 99 Return Value ............................................................................................ 99 Example ................................................................................................... 99 Description ............................................................................................... 99 Syntax .................................................................................................... 100 Parameters ............................................................................................. 100 Return Value .......................................................................................... 100 Example ................................................................................................. 100 Description ............................................................................................. 100 Syntax .................................................................................................... 100 Parameters ............................................................................................. 100 Return Value .......................................................................................... 101 Example ................................................................................................. 101 Description ............................................................................................. 101 Syntax .................................................................................................... 101 Parameters ............................................................................................. 101 Return Value .......................................................................................... 101 Example ................................................................................................. 101 Description ............................................................................................. 102 Syntax .................................................................................................... 102 Parameters ............................................................................................. 102 Return Value .......................................................................................... 102 Example ................................................................................................. 102 Description ............................................................................................. 103 Syntax .................................................................................................... 103 TUTORIALS POINT Simply Easy Learning Parameters ............................................................................................. 103 Return Value .......................................................................................... 103 Example ................................................................................................. 103 Description ............................................................................................. 103 Syntax .................................................................................................... 104 Parameters ............................................................................................. 104 Return Value .......................................................................................... 104 Example ................................................................................................. 104 Description ............................................................................................. 104 Syntax .................................................................................................... 104 Parameters ............................................................................................. 104 Return Value .......................................................................................... 104 Example ................................................................................................. 105 Random Number Functions: .................................................................. 105 Description ............................................................................................. 105 Syntax .................................................................................................... 105 Parameters ............................................................................................. 106 Return Value .......................................................................................... 106 Example ................................................................................................. 106 Description ............................................................................................. 106 Syntax .................................................................................................... 106 Parameters ............................................................................................. 106 Return Value .......................................................................................... 106 Example ................................................................................................. 106 Description ............................................................................................. 107 Syntax .................................................................................................... 107 Parameters ............................................................................................. 107 Return Value .......................................................................................... 107 Example ................................................................................................. 107 Description ............................................................................................. 108 Syntax .................................................................................................... 108 Parameters ............................................................................................. 108 Return Value .......................................................................................... 108 Example ................................................................................................. 108 Description ............................................................................................. 109 Syntax .................................................................................................... 109 Parameters ............................................................................................. 109 Return Value .......................................................................................... 109 Example ................................................................................................. 109 TUTORIALS POINT Simply Easy Learning Description ............................................................................................. 110 Syntax .................................................................................................... 110 Parameters ............................................................................................. 110 Return Value .......................................................................................... 110 Example ................................................................................................. 110 Trigonometric Functions: ........................................................................ 110 Description ............................................................................................. 111 Syntax .................................................................................................... 111 Parameters ............................................................................................. 111 Return Value .......................................................................................... 111 Example ................................................................................................. 111 Description ............................................................................................. 111 Syntax .................................................................................................... 112 Parameters ............................................................................................. 112 Return Value .......................................................................................... 112 Example ................................................................................................. 112 Description ............................................................................................. 112 Syntax .................................................................................................... 112 Parameters ............................................................................................. 112 Return Value .......................................................................................... 113 Example ................................................................................................. 113 Description ............................................................................................. 113 Syntax .................................................................................................... 113 Parameters ............................................................................................. 113 Return Value .......................................................................................... 113 Example ................................................................................................. 113 Description ............................................................................................. 114 Syntax .................................................................................................... 114 Parameters ............................................................................................. 114 Return Value .......................................................................................... 114 Example ................................................................................................. 114 Description ............................................................................................. 115 Syntax .................................................................................................... 115 Parameters ............................................................................................. 115 Return Value .......................................................................................... 115 Example ................................................................................................. 115 Description ............................................................................................. 115 Syntax .................................................................................................... 115 Parameters ............................................................................................. 116 TUTORIALS POINT Simply Easy Learning Return Value .......................................................................................... 116 Example ................................................................................................. 116 Description ............................................................................................. 116 Syntax .................................................................................................... 116 Parameters ............................................................................................. 116 Return Value .......................................................................................... 116 Example ................................................................................................. 117 Description ............................................................................................. 117 Syntax .................................................................................................... 117 Parameters ............................................................................................. 117 Return Value .......................................................................................... 117 Example ................................................................................................. 117 Description ............................................................................................. 118 Syntax .................................................................................................... 118 Parameters ............................................................................................. 118 Return Value .......................................................................................... 118 Example ................................................................................................. 118 Mathematical Constants: ........................................................................ 119 Python Strings ....................................................................... 120 Accessing Values in Strings: .................................................................. 120 Updating Strings: .................................................................................... 120 Escape Characters: ................................................................................ 121 String Special Operators: ....................................................................... 121 String Formatting Operator: ................................................................... 122 Triple Quotes: ......................................................................................... 123 Raw String: ............................................................................................. 124 Unicode String: ....................................................................................... 124 Built-in String Methods: .......................................................................... 125 Description ............................................................................................. 127 Syntax .................................................................................................... 127 Parameters ............................................................................................. 127 Return Value .......................................................................................... 127 Example ................................................................................................. 127 Description ............................................................................................. 128 Syntax .................................................................................................... 128 Parameters ............................................................................................. 128 Return Value .......................................................................................... 128 Example ................................................................................................. 128 Description ............................................................................................. 128 TUTORIALS POINT Simply Easy Learning