Essential Python 3
()
About this ebook
This book is written to provide all the essential information to get you programming in Python 3. The book starts by running through the basics of the language before showing you how to use some of the powerful libraries included in Python 3.
Related to Essential Python 3
Related ebooks
Python Programming For Beginners: Learn The Basics Of Python Programming (Python Crash Course, Programming for Dummies) Rating: 5 out of 5 stars5/5The 1 Page Python Book Rating: 2 out of 5 stars2/5Python for Beginners: An Introduction to Learn Python Programming with Tutorials and Hands-On Examples Rating: 4 out of 5 stars4/5The Ultimate Python Programming Guide For Beginner To Intermediate Rating: 5 out of 5 stars5/5Fundamentals of Programming: Using Python Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python Programming: Your Advanced Guide To Learn Python in 7 Days Rating: 0 out of 5 stars0 ratingsLearning Python Rating: 5 out of 5 stars5/5Learn Python in One Hour: Programming by Example Rating: 3 out of 5 stars3/5PYTHON PROGRAMMING Rating: 4 out of 5 stars4/5Easy-To-Follow Tutorial To Learn Python Programming In Less Than One Week Rating: 3 out of 5 stars3/5Python 3 Programming: A Beginner Crash Course Guide to Learn Python 3 in 1 Week Rating: 3 out of 5 stars3/5Python: Programming For Intermediates: Learn The Basics Of Python In 7 Days! Rating: 0 out of 5 stars0 ratingsPython: Beginner's Guide to Programming Code with Python Rating: 0 out of 5 stars0 ratingsPython for Beginners: Learn It as Easy as Pie Rating: 0 out of 5 stars0 ratingsPython Handbook For Beginners. A Hands-On Crash Course For Kids, Newbies and Everybody Else Rating: 0 out of 5 stars0 ratingsPython: Programming for Advanced: Learn the Fundamentals of Python in 7 Days Rating: 2 out of 5 stars2/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python Made Simple: Learn Python programming in easy steps with examples Rating: 5 out of 5 stars5/5Python Programming: Your Beginner Guide To Learn Python in 7 Days Rating: 4 out of 5 stars4/5Python from the Very Beginning Rating: 0 out of 5 stars0 ratingsLearn Python in 10 Minutes Rating: 4 out of 5 stars4/5
Programming For You
HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsLearn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5The Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5Hacking: Ultimate Beginner's Guide for Computer Hacking in 2018 and Beyond: Hacking in 2018, #1 Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsSQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5The Little SAS Book: A Primer, Sixth Edition Rating: 5 out of 5 stars5/5Teach Yourself C++ Rating: 4 out of 5 stars4/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5Web Designer's Idea Book, Volume 4: Inspiration from the Best Web Design Trends, Themes and Styles Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5
Reviews for Essential Python 3
0 ratings0 reviews
Book preview
Essential Python 3 - Kevin Vans-Colina
Essential Python 3
Copyright 2021 Kevin Vans-Colina
Thank you for downloading this ebook. This book remains the copyrighted property of the author, and may not be redistributed to others for commercial or non-commercial purposes. If you enjoyed this book, please encourage your friends to download their own copy from their favorite authorized retailer. Thank you for your support.
The author assumes no responsibility for errors, emissions or damages that result from using the information and programs in this book.
Published by Sorefoot Software 29/11/21
Distributed by Smashwords.
Contact: essentialpython@gmail.com
Table of Contents
1. Introduction
2. What is Python
3. Getting Python
Install Python
Setting Up the Path
Running Python
4. Python 2
5. Python Basics
Using the Interpreter Interactively
Using the Interpreter with a Script
6. Python Help
7. Python Syntax
Identifiers in Python
Program Layout
Comments
8. Variables
Immutability
Numeric Variables
int (signed integers)
float (floating point real values)
Complex (complex numbers)
Strings
String Methods
String Formatting
Variable Scope and Namespaces
9. Data Types
Lists
Tuple
Dictionaries
10. Data Type Conversion
11. Operators
Arithmetic Operators
Comparison Operators
Assignment Operators
Bitwise Operators
Logical Operators
Membership Operators
Identity Operators
Operator Precedence
12. Program Flow Control
While Loop
For Loop
Offsets and Items
Continue Statement
Break Statement
Loop Statements and Else
If Statement
if / else Ternary Expression
13. Functions
Function Definition
Default Function Arguments
Variable Length Arguments
Anonymous Functions
Map Function
Filter Function
Reduce Function
14. Modules
Module Reloads
Locating Module Files
Module Data Hiding
__name__ and __main__
15. Classes and Objects
Class Definition
Static methods
Inheritance
Function Overloading
Overloading Operators
Class attributes
Garbage Collection
16. Input and Output
Screen Output
Keyboard Input
Opening Files for Reading and Writing
Writing to Files
Reading from Files
Closing Files
Binary Files
Moving around Files
File Attributes
Managing Files and Directories
17. Exceptions
try - except
try – finally
Exception Arguments
Raising Exceptions
18. Date and Time
Time Module
Formatted Time
DateTime Module
Calendar Module
19. Example Project – Software Bug Tracker
Issue Class
Project Class
20. Command Line Arguments
21. Configuration Files
22. Databases
SQL
23. XML
Simple API for XML (SAX)
Document Object Module for XML (DOM)
24. HTML Parsing
25. Tkinter- GUI
Overview
Widget Layout
Pack Manager
Place Manager
Grid Manager
Simple Form
Practical Form
Message Boxes
Dialogs
File Dialog
Menus
Resizing the form
Scroll Bars
26. Where Next?
27. Appendix 1-Interpreter Command Line Options
28. Appendix 2 – Reserved Words
29. Appendix 3– Bug Tracker Code
Issue.py
project.py
bugTracker.py
30. Appendix 4 – Simple Notepad 1
31. Appendix 5 – Simple Notepad 2
1. Introduction
This book is designed to give all the essential information to get you programming in Python. The book starts by running through the basics of the language before showing you how to use some of the powerful libraries included in Python, finally it will show you how to produce full applications with form based user interfaces.
2. What is Python
Python is an easy to learn programming language which particularly popular with the hobbyist it is also used by professional programs to write utilities. Python is an interpreted language which makes it a good choice for rapid application development.
Python has a clear defined syntax, a small set of keywords and a simple program structure, this allows the programmer to quickly learn how to program in Python. The simple syntax means that it is also easy to read which has the advantage that source code is easy to maintain.
Python has a large standard library this means that scripts can be used across multiple platforms without having compatibility issues. There are also a vast number of freely available libraries which allow you to use Python for virtually anything. Low level modules can easily be added to the Python interpreter by writing these modules in an compiled language such as C these modules run much faster. Finally GUIs can be generated in Python which can then be ported to many other operating systems.
Python also has support for objected orientated, structured and functional programming. It can be complied as byte code for larger programs or run as an interpreted language. It has high level dynamic data types and supports dynamic type checking. Memory management is taken care of automatically by Python.
Python is available on a wide variety of platforms including
Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX)
Win 9x/ NT /2000 / XP / Vista /Windows 7
Macintosh
OS/2
DOS
PalmOS
Windows CE
Acorn/RISB OS
BeOS
Amiga
Java virtual machine
.NET virtual machine
3. Getting Python
The best place to download a copy of Python is from the official web site which is http://www.Python.org. This is also a good source of documentation, news, current source code etc. This is probably the best place to get further information on keywords and libraries.
Install Python
If the binary for your platform is available then you can download it, if there is not a version for your platform then you can download the source code and compile it manually.
If you have downloaded the binary then it can simply be installed using the installer.
Setting Up the Path
To make life easier when working with Python it is worth setting up the path variable for your platform, the path variables tells the computer where to look for executable files in this case the Python interpreter.
Note the path in the following examples may vary so you might have to check where your copy of Python was installed.
Unix / Linux Path
Csh shell
setenv PATH $PATH:/usr/local/bin/Python
Bash shell (Linux)
Export PATH= $PATH:/usr/local/bin/Python
sh or ksh shell
PATH=$PATH:/usr/local/bin/Python
Windows Path
In the command prompt type
path %path%;C:\Python
Running Python
Python can be run interactively from the interpreter, from the command line using a script or from an IDE.
To run the interpreter from the start menu open a command window then type
python
The Python interpreter should run and something similar to the following should be displayed on the screen, if it doesn't then check your path variable is correctly pointing to where Python was installed.
Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32
Typehelp
, copyright
, credits
or licence for more information
To run a Python script directly type python followed by the program name
python helloworld.py
The command input above will cause a Python script called helloworld.py to be run.
There are numerous Integrated Development Environments available for Python (IDLE, PythonWin, PyCharm) these allow program creation and allow you to run the Python programs directly as you develop them, although any text editor can also be used to create programs using an IDE will speed up developing scripts.
4. Python 2
This book is written for Python 3 which is the latest version of Python. Python 3.x is not fully compatible with the previous versions of Python. Python 3.x breaks some of the features of Python 2.x to clean up and improve the constancy of the language. The major changes are described below but the Python website will give you full details if you want to make your code backward compatible.
The print statement has been changed into a function print() and now uses keyword arguments to replace the syntax used by the old print statement.
The input function raw_input() has been changed to input().
Integers now only have a single type.
Divisions now by default return floating point values, the whole number can be returned by using a double backslash ( x = y // z).
Strings are all Unicode rather than 8 bit character based.
Some of the APIs return views rather than lists for example the dictionary dict.keys().
Not equal can only be expressed by != , <> has been removed.
String formatting no longer uses the % operator.
To future proof modules it is better to update libraries to Python 3.x rather than write your code in Python 2.x.
5. Python Basics
Using the Interpreter Interactively
Start up the interpreter and type
print ('Hello World')
followed by 'enter', the interpreter should display a new line
Hello World
The interpretor has interpreted the print command and has printed 'Hello World' to the screen.
To exit the interpretor use type exit() or quit();
Using the Interpreter with a Script
While the interpreter can be used interactively this is not very useful if every time you want to run a program you need to type all the commands in one line at a time. For this reason writing Python code in script files makes a lot more sense.
To write a simple script use