More Related Content Similar to Python tutorial Similar to Python tutorial (20) Python tutorial3. ABOUT THE TUTORIAL
TUTORIALS POINT
Simply
Easy
Learning
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 webmaster@tutorialspoint.com
4. 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
5. 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
6. 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
7. 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
8. 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
9. 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
10. 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
11. Syntax .................................................................................................... 128
Parameters ............................................................................................. 129
Return Value .......................................................................................... 129
Example ................................................................................................. 129
Description ............................................................................................. 129
Syntax .................................................................................................... 129
Parameters ............................................................................................. 129
Return Value .......................................................................................... 130
Example ................................................................................................. 130
Description ............................................................................................. 130
Syntax .................................................................................................... 130
Parameters ............................................................................................. 130
Return Value .......................................................................................... 130
Example ................................................................................................. 130
Description ............................................................................................. 131
Syntax .................................................................................................... 131
Parameters ............................................................................................. 131
Return Value .......................................................................................... 131
Example ................................................................................................. 131
Description ............................................................................................. 132
Syntax .................................................................................................... 132
Parameters ............................................................................................. 132
Return Value .......................................................................................... 132
Example ................................................................................................. 132
Description ............................................................................................. 132
Syntax .................................................................................................... 132
Parameters ............................................................................................. 132
Return Value .......................................................................................... 132
Example ................................................................................................. 133
Description ............................................................................................. 133
Syntax .................................................................................................... 133
Parameters ............................................................................................. 133
Return Value .......................................................................................... 133
Example ................................................................................................. 133
Description ............................................................................................. 134
Syntax .................................................................................................... 134
Parameters ............................................................................................. 134
Return Value .......................................................................................... 134
Example ................................................................................................. 134
TUTORIALS POINT
Simply
Easy
Learning
12. Description ............................................................................................. 134
Syntax .................................................................................................... 134
Parameters ............................................................................................. 135
Return Value .......................................................................................... 135
Example ................................................................................................. 135
Description ............................................................................................. 135
Syntax .................................................................................................... 135
Parameters ............................................................................................. 135
Return Value .......................................................................................... 135
Example ................................................................................................. 135
Description ............................................................................................. 136
Syntax .................................................................................................... 136
Parameters ............................................................................................. 136
Return Value .......................................................................................... 136
Example ................................................................................................. 136
Description ............................................................................................. 136
Syntax .................................................................................................... 136
Parameters ............................................................................................. 137
Return Value .......................................................................................... 137
Example ................................................................................................. 137
Description ............................................................................................. 137
Syntax .................................................................................................... 137
Parameters ............................................................................................. 137
Return Value .......................................................................................... 137
Example ................................................................................................. 137
Description ............................................................................................. 138
Syntax .................................................................................................... 138
Parameters ............................................................................................. 138
Return Value .......................................................................................... 138
Example ................................................................................................. 138
Description ............................................................................................. 138
Syntax .................................................................................................... 139
Parameters ............................................................................................. 139
Return Value .......................................................................................... 139
Example ................................................................................................. 139
Description ............................................................................................. 139
Syntax .................................................................................................... 139
Parameters ............................................................................................. 139
Return Value .......................................................................................... 139
TUTORIALS POINT
Simply
Easy
Learning
13. Example ................................................................................................. 139
Description ............................................................................................. 140
Syntax .................................................................................................... 140
Parameters ............................................................................................. 140
Return Value .......................................................................................... 140
Example ................................................................................................. 140
Description ............................................................................................. 140
Syntax .................................................................................................... 140
Parameters ............................................................................................. 141
Return Value .......................................................................................... 141
Example ................................................................................................. 141
Description ............................................................................................. 141
Syntax .................................................................................................... 141
Parameters ............................................................................................. 141
Return Value .......................................................................................... 141
Example ................................................................................................. 141
Description ............................................................................................. 142
Syntax .................................................................................................... 142
Parameters ............................................................................................. 142
Return Value .......................................................................................... 142
Example ................................................................................................. 142
Description ............................................................................................. 142
Syntax .................................................................................................... 142
Parameters ............................................................................................. 142
Return Value .......................................................................................... 143
Example ................................................................................................. 143
Description ............................................................................................. 143
Syntax .................................................................................................... 143
Parameters ............................................................................................. 143
Return Value .......................................................................................... 143
Example ................................................................................................. 143
Description ............................................................................................. 144
Syntax .................................................................................................... 144
Parameters ............................................................................................. 144
Return Value .......................................................................................... 144
Example ................................................................................................. 144
Description ............................................................................................. 144
Syntax .................................................................................................... 144
Parameters ............................................................................................. 145
TUTORIALS POINT
Simply
Easy
Learning
14. Return Value .......................................................................................... 145
Example ................................................................................................. 145
Description ............................................................................................. 145
Syntax .................................................................................................... 145
Parameters ............................................................................................. 145
Return Value .......................................................................................... 145
Example ................................................................................................. 145
Description ............................................................................................. 146
Syntax .................................................................................................... 146
Parameters ............................................................................................. 146
Return Value .......................................................................................... 146
Example ................................................................................................. 146
Description ............................................................................................. 147
Syntax .................................................................................................... 147
Parameters ............................................................................................. 147
Return Value .......................................................................................... 147
Example ................................................................................................. 147
Description ............................................................................................. 147
Syntax .................................................................................................... 147
Parameters ............................................................................................. 148
Return Value .......................................................................................... 148
Example ................................................................................................. 148
Description ............................................................................................. 148
Syntax .................................................................................................... 148
Parameters ............................................................................................. 148
Return Value .......................................................................................... 148
Example ................................................................................................. 148
Description ............................................................................................. 149
Syntax .................................................................................................... 149
Parameters ............................................................................................. 149
Return Value .......................................................................................... 149
Example ................................................................................................. 149
Description ............................................................................................. 149
Syntax .................................................................................................... 149
Parameters ............................................................................................. 150
Return Value .......................................................................................... 150
Example ................................................................................................. 150
Description ............................................................................................. 150
Syntax .................................................................................................... 150
TUTORIALS POINT
Simply
Easy
Learning
15. Parameters ............................................................................................. 150
Return Value .......................................................................................... 150
Example ................................................................................................. 150
Description ............................................................................................. 151
Syntax .................................................................................................... 151
Parameters ............................................................................................. 151
Return Value .......................................................................................... 151
Example ................................................................................................. 151
Description ............................................................................................. 151
Syntax .................................................................................................... 151
Parameters ............................................................................................. 152
Return Value .......................................................................................... 152
Example ................................................................................................. 152
Description ............................................................................................. 152
Syntax .................................................................................................... 152
Parameters ............................................................................................. 152
Return Value .......................................................................................... 152
Example ................................................................................................. 152
Description ............................................................................................. 153
Syntax .................................................................................................... 153
Parameters ............................................................................................. 153
Return Value .......................................................................................... 153
Example ................................................................................................. 154
Description ............................................................................................. 154
Syntax .................................................................................................... 154
Parameters ............................................................................................. 154
Return Value .......................................................................................... 154
Example ................................................................................................. 154
Syntax .................................................................................................... 155
Parameters ............................................................................................. 155
Return Value .......................................................................................... 155
Example ................................................................................................. 155
Python Lists ........................................................................... 156
Python Lists: ........................................................................................... 156
Accessing Values in Lists: ...................................................................... 156
Updating Lists: ....................................................................................... 157
Delete List Elements: ............................................................................. 157
Basic List Operations: ............................................................................ 157
Indexing, Slicing, and Matrixes: ............................................................. 158
TUTORIALS POINT
Simply
Easy
Learning
16. Built-in List Functions & Methods: .......................................................... 158
Description ............................................................................................. 159
Syntax .................................................................................................... 159
Parameters ............................................................................................. 159
Return Value .......................................................................................... 159
Example ................................................................................................. 159
Description ............................................................................................. 159
Syntax .................................................................................................... 160
Parameters ............................................................................................. 160
Return Value .......................................................................................... 160
Example ................................................................................................. 160
Description ............................................................................................. 160
Syntax .................................................................................................... 160
Parameters ............................................................................................. 160
Return Value .......................................................................................... 160
Example ................................................................................................. 160
Description ............................................................................................. 161
Syntax .................................................................................................... 161
Parameters ............................................................................................. 161
Return Value .......................................................................................... 161
Example ................................................................................................. 161
Description ............................................................................................. 161
Syntax .................................................................................................... 161
Parameters ............................................................................................. 162
Return Value .......................................................................................... 162
Example ................................................................................................. 162
Description ............................................................................................. 163
Syntax .................................................................................................... 163
Parameters ............................................................................................. 163
Return Value .......................................................................................... 163
Example ................................................................................................. 163
Description ............................................................................................. 163
Syntax .................................................................................................... 163
Parameters ............................................................................................. 163
Return Value .......................................................................................... 164
Example ................................................................................................. 164
Description ............................................................................................. 164
Syntax .................................................................................................... 164
Parameters ............................................................................................. 164
TUTORIALS POINT
Simply
Easy
Learning
17. Return Value .......................................................................................... 164
Example ................................................................................................. 164
Description ............................................................................................. 165
Syntax .................................................................................................... 165
Parameters ............................................................................................. 165
Return Value .......................................................................................... 165
Example ................................................................................................. 165
Description ............................................................................................. 165
Syntax .................................................................................................... 165
Parameters ............................................................................................. 166
Return Value .......................................................................................... 166
Example ................................................................................................. 166
Description ............................................................................................. 166
Syntax .................................................................................................... 166
Parameters ............................................................................................. 166
Return Value .......................................................................................... 166
Example ................................................................................................. 166
Description ............................................................................................. 167
Syntax .................................................................................................... 167
Parameters ............................................................................................. 167
Return Value .......................................................................................... 167
Example ................................................................................................. 167
Description ............................................................................................. 167
Syntax .................................................................................................... 168
Parameters ............................................................................................. 168
Return Value .......................................................................................... 168
Example ................................................................................................. 168
Description ............................................................................................. 168
Syntax .................................................................................................... 168
Parameters ............................................................................................. 168
Return Value .......................................................................................... 168
Example ................................................................................................. 168
Python Tuples ........................................................................ 170
Accessing Values in Tuples: .................................................................. 170
Updating Tuples: .................................................................................... 171
Delete Tuple Elements: .......................................................................... 171
Basic Tuples Operations: ....................................................................... 171
Indexing, Slicing, and Matrixes: ............................................................. 172
No Enclosing Delimiters: ........................................................................ 172
TUTORIALS POINT
Simply
Easy
Learning
18. Built-in Tuple Functions: ......................................................................... 172
Description ............................................................................................. 173
Syntax .................................................................................................... 173
Parameters ............................................................................................. 173
Return Value .......................................................................................... 173
Example ................................................................................................. 173
Description ............................................................................................. 174
Syntax .................................................................................................... 174
Parameters ............................................................................................. 174
Return Value .......................................................................................... 174
Example ................................................................................................. 174
Description ............................................................................................. 174
Syntax .................................................................................................... 174
Parameters ............................................................................................. 174
Return Value .......................................................................................... 175
Example ................................................................................................. 175
Description ............................................................................................. 175
Syntax .................................................................................................... 175
Parameters ............................................................................................. 175
Return Value .......................................................................................... 175
Example ................................................................................................. 175
Description ............................................................................................. 176
Syntax .................................................................................................... 176
Parameters ............................................................................................. 176
Return Value .......................................................................................... 176
Example ................................................................................................. 176
Python Dictionary .................................................................. 177
Accessing Values in Dictionary: ............................................................. 177
Updating Dictionary: ............................................................................... 178
Delete Dictionary Elements: ................................................................... 178
Properties of Dictionary Keys: ................................................................ 179
Built-in Dictionary Functions & Methods: ............................................... 179
Description ............................................................................................. 180
Syntax .................................................................................................... 180
Parameters ............................................................................................. 180
Return Value .......................................................................................... 180
Example ................................................................................................. 180
Description ............................................................................................. 180
Syntax .................................................................................................... 181
TUTORIALS POINT
Simply
Easy
Learning
19. Parameters ............................................................................................. 181
Return Value .......................................................................................... 181
Example ................................................................................................. 181
Description ............................................................................................. 181
Syntax .................................................................................................... 181
Parameters ............................................................................................. 181
Return Value .......................................................................................... 181
Example ................................................................................................. 181
Description ............................................................................................. 182
Syntax .................................................................................................... 182
Parameters ............................................................................................. 182
Return Value .......................................................................................... 182
Example ................................................................................................. 182
Description ............................................................................................. 183
Syntax .................................................................................................... 183
Parameters ............................................................................................. 183
Return Value .......................................................................................... 183
Example ................................................................................................. 183
Description ............................................................................................. 183
Syntax .................................................................................................... 184
Parameters ............................................................................................. 184
Return Value .......................................................................................... 184
Example ................................................................................................. 184
Description ............................................................................................. 184
Syntax .................................................................................................... 184
Parameters ............................................................................................. 184
Return Value .......................................................................................... 184
Example ................................................................................................. 184
Description ............................................................................................. 185
Syntax .................................................................................................... 185
Parameters ............................................................................................. 185
Return Value .......................................................................................... 185
Example ................................................................................................. 185
Description ............................................................................................. 185
Syntax .................................................................................................... 186
Parameters ............................................................................................. 186
Return Value .......................................................................................... 186
Example ................................................................................................. 186
Description ............................................................................................. 186
TUTORIALS POINT
Simply
Easy
Learning
20. Syntax .................................................................................................... 186
Parameters ............................................................................................. 186
Return Value .......................................................................................... 186
Example ................................................................................................. 186
Description ............................................................................................. 187
Syntax .................................................................................................... 187
Parameters ............................................................................................. 187
Return Value .......................................................................................... 187
Example ................................................................................................. 187
Description ............................................................................................. 187
Syntax .................................................................................................... 187
Parameters ............................................................................................. 187
Return Value .......................................................................................... 188
Example ................................................................................................. 188
Description ............................................................................................. 188
Syntax .................................................................................................... 188
Parameters ............................................................................................. 188
Return Value .......................................................................................... 188
Example ................................................................................................. 188
Description ............................................................................................. 189
Syntax .................................................................................................... 189
Parameters ............................................................................................. 189
Return Value .......................................................................................... 189
Example ................................................................................................. 189
Python Date & Time .............................................................. 190
What is Tick? .......................................................................................... 190
Example: ................................................................................................ 190
What is TimeTuple? ............................................................................... 190
Getting current time -: ............................................................................ 191
Getting formatted time -: ........................................................................ 191
Getting calendar for a month -: .............................................................. 192
The time Module: ................................................................................... 192
Description ............................................................................................. 193
Syntax .................................................................................................... 193
Parameters ............................................................................................. 193
Return Value .......................................................................................... 193
Example ................................................................................................. 193
Description ............................................................................................. 194
Syntax .................................................................................................... 194
TUTORIALS POINT
Simply
Easy
Learning
21. Parameters ............................................................................................. 194
Return Value .......................................................................................... 194
Example ................................................................................................. 194
Description ............................................................................................. 194
Syntax .................................................................................................... 195
Parameters ............................................................................................. 195
Return Value .......................................................................................... 195
Example ................................................................................................. 195
Description ............................................................................................. 195
Syntax .................................................................................................... 195
Parameters ............................................................................................. 196
Return Value .......................................................................................... 196
Example ................................................................................................. 196
Description ............................................................................................. 196
Syntax .................................................................................................... 196
Parameters ............................................................................................. 196
Return Value .......................................................................................... 196
Example ................................................................................................. 196
Description ............................................................................................. 197
Syntax .................................................................................................... 197
Parameters ............................................................................................. 197
Return Value .......................................................................................... 197
Example ................................................................................................. 197
Description ............................................................................................. 197
Syntax .................................................................................................... 197
Parameters ............................................................................................. 197
Return Value .......................................................................................... 197
Example ................................................................................................. 198
Description ............................................................................................. 198
Syntax .................................................................................................... 198
Parameters ............................................................................................. 198
Return Value .......................................................................................... 198
Example ................................................................................................. 198
Description ............................................................................................. 199
Syntax .................................................................................................... 199
Parameters ............................................................................................. 199
Directive ................................................................................................. 199
Return Value .......................................................................................... 200
Example ................................................................................................. 200
TUTORIALS POINT
Simply
Easy
Learning
22. Description ............................................................................................. 200
Syntax .................................................................................................... 200
Parameters ............................................................................................. 200
Directive ................................................................................................. 200
Return Value .......................................................................................... 201
Example ................................................................................................. 201
Description ............................................................................................. 202
Syntax .................................................................................................... 202
Parameters ............................................................................................. 202
Return Value .......................................................................................... 202
Example ................................................................................................. 202
Description ............................................................................................. 202
Syntax .................................................................................................... 203
Parameters ............................................................................................. 203
Return Value .......................................................................................... 203
Example ................................................................................................. 203
The calendar Module ............................................................................. 204
Other Modules & Functions: ................................................................... 205
Python Function ..................................................................... 206
Defining a Function ................................................................................ 206
Syntax: ................................................................................................... 206
Example: ................................................................................................ 206
Calling a Function .................................................................................. 207
Pass by reference vs value .................................................................... 207
Function Arguments: .............................................................................. 208
Required arguments: .............................................................................. 208
Keyword arguments: .............................................................................. 209
Default arguments: ................................................................................. 209
Variable-length arguments: .................................................................... 210
The Anonymous Functions: ................................................................... 210
Syntax: ................................................................................................... 211
The return Statement: ............................................................................ 211
Scope of Variables: ................................................................................ 212
Global vs. Local variables: ..................................................................... 212
Python Modules ..................................................................... 213
Example: ................................................................................................ 213
The import Statement: ............................................................................ 213
The from...import Statement .................................................................. 214
The from...import * Statement: ............................................................... 214
TUTORIALS POINT
Simply
Easy
Learning
23. Locating Modules: .................................................................................. 214
The PYTHONPATH Variable: ................................................................ 214
Namespaces and Scoping: .................................................................... 215
The dir( ) Function: ................................................................................. 215
The globals() and locals() Functions: ..................................................... 216
The reload() Function: ............................................................................ 216
Packages in Python: .............................................................................. 216
Python Files I/O ..................................................................... 218
Printing to the Screen: ............................................................................ 218
Reading Keyboard Input: ....................................................................... 218
The raw_input Function: ......................................................................... 218
The input Function: ................................................................................ 219
Opening and Closing Files: .................................................................... 219
The open Function: ................................................................................ 219
SYNTAX: .............................................................................................. 219
The file object attributes: ........................................................................ 220
EXAMPLE: ............................................................................................. 220
The close() Method: ............................................................................... 221
SYNTAX: ................................................................................................ 221
EXAMPLE: ............................................................................................. 221
Reading and Writing Files: ..................................................................... 221
The write() Method: ................................................................................ 221
SYNTAX: ................................................................................................ 221
EXAMPLE: ............................................................................................. 221
The read() Method: ................................................................................ 222
SYNTAX: ................................................................................................ 222
EXAMPLE: ............................................................................................. 222
File Positions: ......................................................................................... 222
EXAMPLE: ............................................................................................. 222
Renaming and Deleting Files: ................................................................ 223
The rename() Method: ........................................................................... 223
SYNTAX: ................................................................................................ 223
EXAMPLE: ............................................................................................. 223
The remove() Method: ............................................................................ 223
SYNTAX: ................................................................................................ 223
EXAMPLE: ............................................................................................. 224
Directories in Python: ............................................................................. 224
The mkdir() Method: ............................................................................... 224
SYNTAX: .............................................................................................. 224
TUTORIALS POINT
Simply
Easy
Learning
24. EXAMPLE: ........................................................................................... 224
The chdir() Method: ................................................................................ 224
SYNTAX: .............................................................................................. 224
EXAMPLE: ........................................................................................... 224
The getcwd() Method: ............................................................................ 224
SYNTAX: .............................................................................................. 225
EXAMPLE: ........................................................................................... 225
The rmdir() Method: ............................................................................... 225
SYNTAX: .............................................................................................. 225
EXAMPLE: ........................................................................................... 225
File & Directory Related Methods: ......................................................... 225
File Object Methods ............................................................................... 225
Description ............................................................................................. 226
Syntax .................................................................................................... 226
Parameters ............................................................................................. 226
Return Value .......................................................................................... 227
Example ................................................................................................. 227
Description ............................................................................................. 227
Syntax .................................................................................................... 227
Parameters ............................................................................................. 227
Return Value .......................................................................................... 227
Example ................................................................................................. 227
Description ............................................................................................. 228
Syntax .................................................................................................... 228
Parameters ............................................................................................. 228
Return Value .......................................................................................... 228
Example ................................................................................................. 228
Description ............................................................................................. 229
Syntax .................................................................................................... 229
Parameters ............................................................................................. 229
Return Value .......................................................................................... 229
Example ................................................................................................. 229
Description ............................................................................................. 229
Syntax .................................................................................................... 229
Parameters ............................................................................................. 230
Return Value .......................................................................................... 230
Example ................................................................................................. 230
Description ............................................................................................. 230
Syntax .................................................................................................... 230
TUTORIALS POINT
Simply
Easy
Learning
25. Parameters ............................................................................................. 230
Return Value .......................................................................................... 231
Example ................................................................................................. 231
Description ............................................................................................. 231
Syntax .................................................................................................... 231
Parameters ............................................................................................. 231
Return Value .......................................................................................... 231
Example ................................................................................................. 232
Description ............................................................................................. 232
Syntax .................................................................................................... 232
Parameters ............................................................................................. 232
Return Value .......................................................................................... 232
Example ................................................................................................. 232
Description ............................................................................................. 233
Syntax .................................................................................................... 233
Parameters ............................................................................................. 234
Return Value .......................................................................................... 234
Example ................................................................................................. 234
Description ............................................................................................. 234
Syntax .................................................................................................... 234
Parameters ............................................................................................. 235
Return Value .......................................................................................... 235
Example ................................................................................................. 235
Description ............................................................................................. 235
Syntax .................................................................................................... 235
Parameters ............................................................................................. 236
Return Value .......................................................................................... 236
Example ................................................................................................. 236
Description ............................................................................................. 236
Syntax .................................................................................................... 236
Parameters ............................................................................................. 237
Return Value .......................................................................................... 237
Example ................................................................................................. 237
Description ............................................................................................. 238
Syntax .................................................................................................... 238
Parameters ............................................................................................. 238
Return Value .......................................................................................... 238
Example ................................................................................................. 238
Description ............................................................................................. 242
TUTORIALS POINT
Simply
Easy
Learning
26. Syntax .................................................................................................... 242
Parameters ............................................................................................. 242
Return Value .......................................................................................... 243
Example ................................................................................................. 243
Description ............................................................................................. 243
Syntax .................................................................................................... 243
Parameters ............................................................................................. 243
Return Value .......................................................................................... 244
Example ................................................................................................. 244
Description ............................................................................................. 244
Syntax .................................................................................................... 244
Parameters ............................................................................................. 244
Return Value .......................................................................................... 245
Example ................................................................................................. 245
Description ............................................................................................. 245
Syntax .................................................................................................... 246
Parameters ............................................................................................. 246
Return Value .......................................................................................... 246
Example ................................................................................................. 246
Description ............................................................................................. 246
Syntax .................................................................................................... 246
Parameters ............................................................................................. 247
Return Value .......................................................................................... 247
Example ................................................................................................. 247
Description ............................................................................................. 247
Syntax .................................................................................................... 247
Parameters ............................................................................................. 247
Return Value .......................................................................................... 247
Example ................................................................................................. 247
Description ............................................................................................. 248
Syntax .................................................................................................... 248
Parameters ............................................................................................. 248
Return Value .......................................................................................... 248
Example ................................................................................................. 248
Description ............................................................................................. 248
Syntax .................................................................................................... 249
Parameters ............................................................................................. 249
Return Value .......................................................................................... 249
Example ................................................................................................. 249
TUTORIALS POINT
Simply
Easy
Learning