Treading on Python Series
Illustrated Guide to Python 3
A Complete Walkthrough of Beginning Python with Unique Illustrations Showing how Python Really Works
Matt Harrison
Technical Editors: Roger A. Davidson, Andrew McLaughlin
Version: Oct 2, 2017
Copyright 2017
While every precaution has been taken in the preparation of this book, the publisher and author assumes no responsibility for errors oromissions, or for damages resulting from the use of theinformation contained herein.
Introduction
Are you ready to jumpstart your Python programming career? This book willarm you with years of knowledge and experience that are condensed into aneasy to follow format. Rather than taking months reading blogs,websites, and searching mailing lists and groups, this book will allowa programmer to quickly become knowledgeable and comfortable with Python.
Programming is fun and Python makes it delightful. Basic Python is notonly easy, but approachable for all ages. I have taught elementarystudents, teenagers, industry professionals and golden years folksthe Python programming language. If you are willing to read and type,you are about to begin an exciting path. Where it ultimatelytakes you depends on how hard you are willing to work.
There are different levels of Python. Basic Python syntax is smalland easy to learn. Once you master basic Python, doors will open toyou. You should be able to read a lot of Python and understandit. From there, you can learn more advanced topics, specifictoolkits, start contributing to open source projects written inPython, or use that knowledge to learn other programming languages.
A recommended approach for learning the language is to read a chapterand then sit down at a computer and type out some of the examplesfound in the chapter. Python makes it easy to get started, eliminatingmuch of the hassle found in running programs in otherlanguages. The temptation will likely be to merely read the book. Byjumping in and actually typing the examples you will learn a lot morethan by just reading.
Why Python?
Python is booming! It is the top language being taught inuniversities. Python developers are among the highest paid.With the boom in data science, Python is quickly becomingone of the most desired skills for analytics. Operations arealso adopting Python to manage their backend systems. Theyare discovering what web developers using Python have knownfor a long time; namely, that Python will make you productive.
Python has crossed the tipping point. No longer are only small, agile startupsrelying on it. Looking to take advantage of its power and efficiency,enterprises have also converged on Python. Over the past year, I've taughtPython to hundreds of seasoned developers with years of experience at largecompanies, who are moving to Python.
Python enables increased productivity. I came to Python as a Perl programmer. Atwork, I was assigned to a team with a co-worker who was well versed in Tcl.Neither of us wanted to jump ship though both of us were interested inlearning Python. In 3 days our prototype was completed, much fasterthan we expected, and we both promptly forgot our previous gotolanguage. What appealed to me about Python was that it fit my brain. Ifirmly believe if you have some experience in programming, you canlearn the basics of Python in a few days.
Python is easy to learn. For beginning programmers, Python is a greatstepping stone. Learning to write simple programs is pretty easy,yet Python also scales up to complex enterprise systems. Python alsoscales with ageI have personally seen people from 7-80+ learn basicprogramming skills using Python.
Which Version of Python?
This book will focus on Python 3. Python 2 has served us well overthe years. The Python Software Foundation, which manages releases of thelanguage, has stated that Python 2 is coming to an end. As such,after 2020 they will no longer support the language.
Python 3, has been out for a bit nowand is somewhat backward incompatible with the 2 series. For green fielddevelopment, you should move forward with Python 3. If you have legacysystems on Python 2, do not fret. In fact, most of the content inthis book is perfectly applicable to Python 2. If you want to focuson Python 2, check out the prior version of this book.
Python installation
Python 3 is not installed on most platforms. Some Linux distributions shipwith it, but Windows and Mac users will need to install it.
For Windows folks, go to the download area of the Python website and find a link that says Python 3.6 Windows Installer. This will link toa .msi file that will install Python on your Windows machine. Downloadthe file, open it by double-clicking it, and follow theinstructions to finish the installation.
Note
On the Windows installer there is an option labeled "Add Python to PATH".Please make sure it is checked. That way, when you run python from thecommand prompt, it will know where to find the Python executable. Otherwise,you can go to System properties (click WIN+Pause, or run environ from the start menu), Advanced system settings, and click on theEnvironment Variables button. There you can update the PATH variable by addingthe following:
C:\Program Files\Python 3.6;C:\Program Files\Python 3.6\Scripts
If you have UAC (User Account Control) enabled on Windows, then path is:
C:\Users\\AppData\Local\Programs\Python\Python36
Likewise, Mac users should download the Mac installer from the Python website.
Note
Another option for installing Python is to use the Anaconda distribution. This runs on Windows, Mac, and Linux, and also providesmany pre-built binaries for doing scientific calculations. Traditionally,these libraries have been annoying to install as they wrap librarieswritten in C and Fortran, that require some setup for compiling.
Mac users might also want to look into the Homebrew version . Ifyou are already familiar with Homebrew, it is a simple brew install python3 away.
Which editor?
In addition to installing Python, you will need a text editor. Aneditor is a tool for writing code. A skilled craftsman will invest thetime to learn to use their tool appropriately and it will paydividends. Learning to use the features of an editor can make churningout code easier. Many modern editors today have some semblance ofsupport for Python.
If you are just beginning with Python and have not had much experiencewith real text editors, most Python installations include IDLE, whichhas decent Python editing features. The IDLE development environmentalso runs on Windows, Mac and Linux.
A feature to look for in editors is integration with the Python REPL .Later you will see an example with IDLE. Hopefully your editor of choice willhave similar features.
Popular editors with decent Python support include Emacs, Vim, Atom,Visual Studio Code, and Sublime Text. If you are interested in more fancy editors thathave support for refactoring tools and intelligent completion, PyCharm andWing IDE are also popular.
Summary
Python 3 is the current version of Python. Unless you are working on legacycode, you should favor using this version. You can find the latest versionon the Python website.
Most modern editors contain some support for Python. There are various levelsof features that editors and IDEs provide. If you are getting startedprogramming, give the IDLE editor a try. It is a great place to start out.
Exercises
Install Python 3 on your computer. Make sure you can start Python.
- If you are used to a particular editor, do some investigation into its support for Python. For example, does it:
Next page