Python 3 - Tutorialspoint

7m ago
2.85 MB
512 Pages

Python 3About the TutorialPython is a general-purpose interpreted, interactive, object-oriented, and high-levelprogramming language. It was created by Guido van Rossum during 1985 – 1990. LikePerl, Python source code is also available under the GNU General Public License (GPL).Python is named after a TV Show called ‘Monty Python’s Flying Circus’ and not afterPython-the snake.Python 3.0 was released in 2008. Although this version is supposed to be backwardincompatibles, later on many of its important features have been backported to becompatible with the version 2.7. This tutorial gives enough understanding on Python 3version programming language. Please refer to this link for our Python 2 tutorial.AudienceThis tutorial is designed for software programmers who want to upgrade their Python skillsto Python 3. This tutorial can also be used to learn Python programming language fromscratch.PrerequisitesYou should have a basic understanding of Computer Programming terminologies. A basicunderstanding of any of the programming languages is a plus.Execute Python ProgramsFor most of the examples given in this tutorial you will find Try it option, so just make useof it and enjoy your learning.Try the following example using Try it option available at the top right corner of the belowsample code box #!/usr/bin/python3print ("Hello, Python!")Copyright & Disclaimer Copyright 2016 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I)Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republishany contents or a part of contents of this e-book in any manner without written consentof the publisher.We strive to update the contents of our website and tutorials as timely and as precisely aspossible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of ourwebsite or its contents including this tutorial. If you discover any errors on our website orin this tutorial, please notify us at [email protected]

Python 3Table of ContentsAbout the Tutorial . iAudience . iPrerequisites . iExecute Python Programs . iCopyright & Disclaimer . iTable of Contents . iiPYTHON 3 – BASIC TUTORIAL . 11.Python 3 – What is New? . 2The future module . 2The print Function . 2Reading Input from Keyboard . 2Integer Division . 3Unicode Representation . 3xrange() Function Removed . 4raise exceprion . 4Arguments in Exceptions . 4next() Function and .next() Method . 42to3 Utility . 42.Python 3 – Overview . 6History of Python . 6Python Features . 73.Python 3 – Environment Setup. 8Local Environment Setup . 8Getting Python . 8Setting up PATH . 9Setting Path at Unix/Linux . 10Setting Path at Windows . 10Python Environment Variables . 10Running Python . 114.Python 3 – Basic Syntax . 13First Python Program . 13Python Identifiers . 14Reserved Words . 15Lines and Indentation . 15Multi-Line Statements . 17Quotation in Python . 17Comments in Python . 17Using Blank Lines . 18Waiting for the User . 18Multiple Statements on a Single Line . 18Multiple Statement Groups as Suites . 19Command Line Arguments . 19ii

Python 3Parsing Command-Line Arguments . 205.Python 3 – Variable Types . 23Assigning Values to Variables . 23Multiple Assignment . 23Standard Data Types. 24Python Numbers . 24Python Strings. 25Python Lists . 26Python Tuples . 27Python Dictionary . 27Data Type Conversion . 286.Python 3 – Basic Operators . 30Types of Operator . 30Python Arithmetic Operators . 30Python Comparison Operators . 32Python Assignment Operators . 33Python Bitwise Operators. 35Python Logical Operators . 37Python Membership Operators . 38Python Identity Operators . 39Python Operators Precedence . 407.Python 3 – Decision Making . 43IF Statement . 44IF.ELIF.ELSE Statements . 45Nested IF Statements . 48Single Statement Suites . 498.Python 3 – Loops. 51while Loop Statements . 52for Loop Statements . 56Nested loops . 59Loop Control Statements . 60break statement . 61continue Statement . 63pass Statement .