Home Programming Learn Programming in Python with Cody Jackson

Learn Programming in Python with Cody Jackson

By Cody Jackson
books-svg-icon Book
eBook $29.99 $20.98
Print $38.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $29.99 $20.98
Print $38.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
  1. Free Chapter
    The Fundamentals of Python
About this book
Python is a cross-platform language used by organizations such as Google and NASA. It lets you work quickly and efficiently, allowing you to concentrate on your work rather than the language. Based on his personal experiences when learning to program, Learn Programming in Python with Cody Jackson provides a hands-on introduction to computer programming utilizing one of the most readable programming languages–Python. It aims to educate readers regarding software development as well as help experienced developers become familiar with the Python language, utilizing real-world lessons to help readers understand programming concepts quickly and easily. The book starts with the basics of programming, and describes Python syntax while developing the skills to make complete programs. In the first part of the book, readers will be going through all the concepts with short and easy-to-understand code samples that will prepare them for the comprehensive application built in parts 2 and 3. The second part of the book will explore topics such as application requirements, building the application, testing, and documentation. It is here that you will get a solid understanding of building an end-to-end application in Python. The next part will show you how to complete your applications by converting text-based simulation into an interactive, graphical user interface, using a desktop GUI framework. After reading the book, you will be confident in developing a complete application in Python, from program design to documentation to deployment.
Publication date:
November 2018
Publisher
Packt
Pages
304
ISBN
9781789531947

 

The Fundamentals of Python

Python is a programming language that, as of the time of writing, is ranked number four on the the TIOBE programming language popularity index. It's one of the most popular languages taught in college, as well as heavily used in industry. Companies such as Google, Rackspace, Industrial Lights and Magic, D-Link, NASA, and others, as well as the Department of Defense and a large number of hobby projects, rely on Python to get work done.

This chapter will cover the following items:

  • What is Python?
  • Working with Python
  • Commenting Python code
  • Launching Python programs
  • Using the IPython shell
 

What is Python?

Python is a programming language widely used in a number of applications, such as machine learning, computer graphics, video games, and shell scripts. Nearly any computer application can be implemented in Python, though there are some areas where Python may not be the best solution, such as low-level programs that have to access hardware. In general, though, Python is a good tool for initial application prototyping. Once the initial design has been clarified with Python, it can be re-implemented in a more appropriate language, or the Python code itself can be revised for better optimization.

Python versions

Two main version lines exist for Python: Python 2 and Python 3. Python 2 is the legacy line (version 2.7.15, at the time of writing); while it is still used for some new projects nowadays, it is predominately seen in old software that either can't or won't be upgraded to the Python 3 line. Python 2.7 is the last major release number for this line; incremental upgrades will be provided to back-port Python 3 features or for security patching, but no major features are written for it.

Python 3 (version 3.7.0, as of this writing) is the main development line, and all new features are added here first. Many features in Python 3 are not available in Python 2, or are renamed, so significant effort must be made to convert one version to another.

The Python tools 2to3 and 3to2 are provided with every Python download to help with this conversion process, but they can only handle simple things, such as changing print statements or automatically renaming built-in functions. Anything beyond that requires a programmer to look at the code and make the changes. As this is a non-trivial process (each line of code must be assessed), it may be easier to simply rewrite the code.

Python, as normally used, is technically called CPython, as it is actually written in C code. Python has bindings for use in non-native Python environments, such as Java (Jython), the .NET framework (IronPython), or microcontrollers (MicroPython). This means that you can write regular Python code and it will be interpreted into the correct byte-code for a particular environment. This way, for example, you can interact with a Java program without having to actually write Java code; the Jython interpreter translates Python into equivalent Java code.

Interpreted versus compiled

Python is classified as a scripting language, because it doesn't require a compiler to generate machine code. It actually uses an interpreter to create byte-code, which is cross-platform, and, therefore, any system that has Python installed should be able to run the code. (There are caveats to this, which will be addressed later in the book.)

Byte-code is common among higher-level languages, such as Java, because it makes it easy to write software that runs in many different environments. Languages that use byte-code have a language-specific virtual machine; that is, the virtual machine's sole purpose is to translate the byte-code into something the host computer's operating system can understand. Any OS that has a language-specific virtual machine can process and use the byte-code, thus making an interpreted programming language system agnostic. The programmer doesn't have to do anything special prior to releasing the software.

Machine code is basically the opposite. It is compiled from the raw source code for a particular computer system; this is more common for low-level languages like C++ and Go. The code is portable between systems, but has to be recompiled for each system; it cannot be run immediately like it can with byte-code. Thus, a programmer must either generate the compiled code for each target OS, or has to provide the source code so an end user can perform that compilation step.

Compiled languages tend to operate faster than interpreted languages because the code has already been optimized for the environment. The compiler also finds many errors before the code is actually executed (the "runtime"). However, compilers can take minutes or even hours to compile the source code, depending on various factors. When errors occur, the programmer has to fix them and rerun the compiler; this compile-fix-compile process continues until the compiler returns no errors.

Compilers can't identify all errors, so the final product must be tested. If problems are found, the code must be fixed, leading to another round of compile-fix-compile, as the fixes to the runtime errors may introduce new errors during compilation.

Working with interpreted languages can be quicker, as there is no compilation step. The code can be run as often as necessary while fixing errors, so the development process is much faster. For many products, developer time is more important than computer time, so having a programmer who can quickly write a program is more desirable than a program that is quicker to run.

In addition, utilizing interpreted languages also allows software developers to provide a scripting interface to the end user; the user can manipulate the program without having to dive into the source code itself. Referring to the previous Jython example, a program written in Java could allow the user to manipulate the data or the actions performed by writing a simple Jython script, essentially adjusting on the fly how the results are generated. This type of customization is commonly found in video games, such as modding communities.

Dynamic versus static

Python is a dynamic typed language. Many other languages are static typed, such as C/C++ and Java. A static typed language requires the programmer to explicitly tell the computer what type of "thing" each data construct is.

For example, if you were writing a C inventory program and one of the variables was cost, you would have to declare cost as a float type, which tells the C compiler that the only data that can be used for that variable must be a floating point number, that is, a number with a decimal point, such as 3.14. If any other data type was assigned to that variable, like an integer or a text string, the compiler would give an error when trying to compile the program. (A programming variable is similar to a math variable; it's just a placeholder for a particular value.)

Python, however, doesn't require this. You simply give your variables names and assign values to them. The interpreter takes care of keeping track of the kinds of objects your program is using. This also means that you can change the size of the values as you develop the program. (For the curious, this is handled by adding metadata to a C construct. Every time the item is used, the metadata is looked at to determine how Python should interact with it, as well as potentially modifying the metadata.)

Say you have another decimal number you need in your program. With a static typed language, you have to decide the memory size the variable can take when you first initialize that variable. A double is a floating point value that can handle a much larger number than a normal float (the actual amount of memory used depends on the operating environment, but a float is typically 32 bits long while a double is 64 bits). If you declare a variable to be a float but later on assign a value that is too big to it, your program can develop errors or be slower than expected; changing it to a double will correct these problems.

With Python, it doesn't matter what type of data a construct is. You simply give it whatever number you want, and Python will take care of manipulating it as needed. It even works for derived values. For example, say you are dividing two numbers. One is a floating point number and one is an integer. Python realizes that it's more accurate to keep track of decimals so it automatically calculates the result as a floating point number. The following code example shows what it would look like in the Python interpreter—floating point and integer division:

>>> 6.0 / 2 
3.0
>>> 6 / 2.0
3.0

As you can see, it doesn't matter which value is the numerator or denominator; Python "sees" that a float is being used and gives the output as a decimal value.

This would be a good time to note one of the differences between Python 2 and Python 3. Python 2 truncates division operations, whereas Python 3 automatically converts to decimal values. The following section offers examples of the two versions.

Python 2 versus Python 3 division

While most Python 2.7 code is compatible with 3.x code, you can see that certain things don't carry over well. For example, Python 2 truncates the output of division calculations:

# Python 2
>>> 7/2
3

Python 3, as the following shows, provides the remainder when dividing. This is important to remember, as the code you're writing will break if it uses features or side effects of a particular version but is run on a different version:

# Python 3
>>> 7/2
3.5
 

Working with Python

Python can be programmed through an interactive command line (the interpreter), but anything you code won't be saved. Once you close the session it all goes away. To save your program, it's easiest to just type it in a text file and save it (be sure to use the .py extension, that is, foo.py).

To use the interpreter, simply type python at the command prompt (*nix and Mac) or click the Python application icon (Windows and Mac). If you're using Windows and installed the Python .msi file, you should be able to also type python at the command prompt, or find the launch icon in the Start menu.

Though they may look the same, the main difference between the Python interpreter and the system command prompt is that the command prompt is part of the operating system while the interpreter is part of Python. The command prompt can be used for other tasks besides messing with Python; the interpreter can only be used for Python.

Note that *nix is used throughout this book to denote any UNIX-like operating system, such as Linux, BSD, and others, as these OSes tend to have similar functionality.

Installation

Depending on your operating system, Python may already be installed. Python is very prevalent in the *nix world, though different operating systems use different versions. It is almost guaranteed that Python 2 is installed, and an increasing number of systems have some version of Python 3 installed as well.

It is recommended to go to https://www.python.org and download the latest version of Python. While this book will focus on version 3.6 and later, the majority of the information will apply to older versions of Python 3 as well. Various installers are available for the major operating systems, as well as some specialized and older platforms; installation instructions are provided with the download.

Launching the Python interpreter

If you're using Linux, BSD, or another *nix operating system, I'll assume you already know about the Terminal; you probably even know how to get Python up and running already. For those who aren't familiar with opening Terminal or the command prompt (same thing, different name on different operating systems), the following sections explain how to do it.

The interactive Python interpreter is sometimes referred to as the Python shell, Python prompt, Python terminal, or Python command line. They all mean the same thing—a special, text-based interface that allows for Read-Evaluate-Print Loop (REPL) interaction with Python.

Windows (Win8 and above)

The following steps will help you to install Python in Windows:

  1. Press the Windows key.
  2. Type cmd and press Enter.
  3. You should now have a black window with white text. This is the command prompt.
  4. If you type python at the prompt, you should be dropped into the Python interpreter prompt. If not, Python isn't installed correctly.

Mac

The following steps will help you to install Python in Mac:

  1. Open Applications.
  2. Open Utilities.
  3. Scroll down and open Terminal.
  1. You should now have a black window with white text. This is the command prompt.
  2. Type python at the prompt and you will be in the Python interpreter.

Using the Python command prompt

Your Terminal should look similar to the screenshot labeled Python 3 prompt. Notice that the command to launch the interpreter is actually python3. If both Python 2 and Python 3 are installed on the system, you need to expressly indicate which version to use; otherwise, the system default will be used, which may be different from what is desired. The screenshot labeled Default Python prompt shows what the default prompt on the author's system looks like.

Anaconda is a customized Python distribution (https://www.anaconda.com) that includes a large number of data science and machine learning tools by default, making it easier for users to manage their environment. This simply demonstrates that, in addition to multiple Python versions, different Python distributions can be installed on the same system, each one customized to a particular use. Below is an example of the interactive Python shell for a vanilla Python installation:

Python 3 prompt

In addition, the astute reader will see there is a difference in the Python environment between the different versions. The following screenshot states that the Python 3 environment is standard Python, whereas the preceding screenshot shows that Python 2 is part of the Anaconda distribution:

Default Python prompt

For the most part, you won't even notice which version is in use; for example, version 3.4 versus 3.7, unless you are using a library, function, or method for a specific version. Then, you can simply add a special code to identify what version the user has and provide notification to upgrade, or you can modify your code so it is backwards-compatible.

The >>> characters in the preceding screenshot is the Python command prompt; your code is typed here and the result is printed on the following line, without a prompt. For example, the following screenshot shows how the user can interact with the Python interpreter just like using a normal operating system command prompt.

Python prompt example

If you write a statement that doesn't require any processing by Python, it will simply return you to the prompt, awaiting your next order. In the previous example, the print() function simply takes the text that is placed in the parentheses and prints it to the screen. Python doesn't have to do anything with this, in terms of performing calculations or anything, so it prints the statement and then waits for a new command.

(By the way, Python was named after Monty Python, not the snake. Hence, some of the code you'll find on the internet, and tutorial, and books will have references to Monty Python sketches.)

The standard Python interpreter can be used to test ideas before you put them in your code. This is a good way to test the logic required to make a particular function work correctly or see how a conditional loop will work. You can also use the interpreter as a simple calculator; if you import various mathematical libraries, you can perform complex calculations as well.

The following screenshot shows the Python interpreter being used for simple arithmetic; it also shows that the math library is imported so more complex calculations can be performed (importing libraries will be covered in more detail in the Importing modules section):

Python calculator
 

Commenting Python code

Another thing to discuss is that comments in Python are marked with the # symbol. Comments are used to annotate notes or other information without having Python try to perform an operation on them. For example, the following screenshot demonstrates the use of comments when writing code. It should be noted that, normally, comments in the interactive Python prompt are not used, since it is more of a scratchpad for testing bits of code:

Python comments

You will see later on that, even though Python is a very readable language, it still helps to put comments in your code. Sometimes, it's to explicitly state what the code is doing, to explain a neat shortcut you used, or to simply remind yourself of something while you're coding, like a "to do" list.

 

Launching Python programs

If you want to run a Python program, simply type python foo.py at the shell command prompt (make sure it's not Python's interactive prompt).

The foo.py code is a stand-in term for a generic program; don't try to actually run it because it won't work.

The following screenshot demonstrates how to call a Python program from the command line. This particular program simulates rolling a number of dice; the actual program will be discussed later in this book:

Launching a program

Files saved with the .py extension are called modules and can be called individually at the command line or imported into a program, similar to header files in other languages; we saw an example of this in the screenshot labeled Python calculator. If your program is going to import other modules, it is easiest to ensure they are all saved in the same directory on the computer, or you have to do some extra work to point to a different directory. More information on working with modules can be found in Chapter 2, Data Types and Modules, in the Importing modules section, or in the Python documentation.

Depending on the program, certain arguments can be added to the command line when launching the program. This is similar to adding switches to a Windows command prompt. The arguments tell the program what exactly it should do.

For example, perhaps you have a Python program that can output its processed data to a file rather than to the screen. To invoke this function in the program you simply launch the program like the following example—launching a Python program with arguments:

$ python foo.py -f /home/User/Documents

The -f argument is received by the program and calls a function that saves the data to the designated location (/home/User/Documents) within the computer's filesystem instead of printing it to the screen.

 

Using the IPython shell

The default Python shell is fine, but there are alternatives. The most popular option is to install the IPython shell from https://ipython.org. This is also included with the Anaconda distribution, as well as a number of supporting tools that enhance the development experience.

IPython provides a number of enhancements to the regular interactive Python experience, such as:

  • Syntax-highlighted interactive shells
  • Web-based notebooks that support multimedia output
  • Interactive visualizations
  • Interactive parallel application development
  • Tab completion
  • Object exploration
  • Magic functions
  • Command history
  • Direct implementation of shell commands

The following screenshot demonstrates how the IPython shell differs from the default Python shell. The first thing that is most noticeable is that there is now color within the Python commands. Keywords, errors, and so on are all shown with different colors, easily highlighting different parts of the code.

In addition, each line has its own line number associated with it, rather than the >>> symbol. Lines one and five show that, when necessary, IPython will provide an associated output result if the input command requires it.

Lines six and seven show how IPython can call Bash shell commands directly, in this case pinging a website and printing the current directory, respectively. Because of this ability, some programmers treat IPython as an alternative to the default command shell on *nix systems:

IPython example

However, there is an alternative to using IPython in this manner: the Xonsh shell, found at https://xon.sh. Depending on your preference, it is pronounced using the Greek letter Chi ("Χ"), to sound like "conch," or with a "Z," to sound like "zonsh."

Xonsh is built on Python 3.4 and includes Bash shell functions; it is designed to improve on perceived problems with Bash, as well as making the lives of Python programmers easier. This is because Xonsh essentially replaces the Bash shell with Python, allowing the use of Python code directly at the command line without having to invoke a Python interactive prompt. It also means that Python code in Xonsh has direct access to the underlying OS processing and filesystems, allowing the user to never have to drop back to Bash to interact with the OS.

If you look back through the previous screenshots, you'll note that at the top of each window, the name "xonsh" was listed. Xonsh functions just like the default Bash shell in *nix; it's only when you start using commands that are associated with Python that you will notice differences.

The following screenshot shows the errors that occur when trying to run Python commands directly with a Bash shell:

Bash command errors

The following screenshot shows the same commands successfully functioning within the Xonsh shell:

Xonsh commands

For the purposes of this book, normal Bash commands will be used when demonstrating OS shell commands, to limit confusion. However, interactive Python sessions will be demonstrated through IPython, rather than the default Python shell.

 

Summary

In this chapter, we discussed what the Python programming language is and how it differs from other languages. We learned how to use it on different operating systems and how to interact with the interactive Python shell. We saw how to comment Python code to provide a better explanation of what the code is doing. Finally, we discussed two alternative Python environments: IPython and the Xonsh shell.

In the next chapter, we will take a look at the Python data types (such as lists, dictionaries, and sets), how these types are used when programming, and how Python modules are imported and utilized to enhance coding projects.

About the Author
  • Cody Jackson

    Cody Jackson is a disabled military veteran, the founder of Socius Consulting, an IT and business management consulting company in San Antonio, Texas, and a co-founder of Top Men Technologies with Scott Thompson. He is currently employed at CACI International as the lead ICS/SCADA modeling and simulations engineer. He has been involved in the tech industry since 1994, when he left Gateway Computers to join the Navy as a nuclear chemist and radcon technician. Prior to joining CACI, he worked at ECPI University as a computer information systems adjunct professor. He is a self-taught Python programmer, and is the author of Learning to Program Using Python and Secret Recipes of the Python Ninja. He holds an Associate in Science degree in electromechanical technology, a Bachelor of Science degree in computer engineering technology, and a Master of Science degree in IT management, as well as numerous IT certifications.

    Browse publications by this author
Latest Reviews (2 reviews total)
*********************************************************************************************************
seems very comprehensive. about a third of the way through
Learn Programming in Python with Cody Jackson
Unlock this book and the full library FREE for 7 days
Start now