Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Essential Python 3
Essential Python 3
Essential Python 3
Ebook210 pages1 hour

Essential Python 3

Rating: 0 out of 5 stars

()

Read preview

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.

LanguageEnglish
Release dateJul 3, 2021
ISBN9780463901809
Essential Python 3

Related to Essential Python 3

Related ebooks

Programming For You

View More

Related articles

Reviews for Essential Python 3

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    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

    Enjoying the preview?
    Page 1 of 1