Python Files and the Interpreter


Python is an interpreted language, meaning that it is compiled every time that it is run. There are a number of pros and cons when talking about an interpreted language like this.

First, on a positive note, they tend to be easier to debug. They fail immediately when they are run, and tell you what went wrong, which is nice compared to compiled languages like C/C++, which can compile just fine, but fail silently when run.

Interpreted languages are also very portable. All you have to do is install the interpreter on a system, and most code written in that language can run fine, regardless of the operating system. There are some exceptions when dealing with operating system specific code and libraries, but if you’ve planned for portability, you can work around those situations.

There is one major downside, though, and that’s speed. Interpreted languages are slow, and compiling code every time it’s run is very inefficient. There’s really not way around that fact, and that’s the reason why you will never find Python or any other interpreted language running low level operations or large resource intense applications. That’s okay. There are plenty of other places with Python shines.

Because Python is an interpreted language, there are two ways you can run it. First, you can write your code in a file, and launch it from the command line after you’re done. As an alternative, you can open up a real time interpreter and code as you go. Now, the second option is great for learning, but couldn’t possibly work well for real world scenarios. The first option can be tough to get into when you’re just starting, but it allows you to go back and review something tangible. You can start out with either one, and this guide will cover both to begin with.

Running Python

Whichever way you go, you need to be mindful of spaces. In Python, spaces are very important. The dictate they entire structure of your code, and they must be consistent throughout your code. So, be sure that you select something that you know that you’re going to remember. Most Python programmers prefer to use four spaces for indentation. It’s probably a good idea to configure tabbing in your terminal or text editor to match. It will save you a lot of headaches going forward.

Python File

The file extension for Python is .py. There isn’t anything else special there, so you can create one by opening it up in your favorite text editor or using touch.

$ touch
///// OR //////
$ vim
$ chmod +x

Either way, when you open up your Python file, you should add a shebang line to specify your Python interpreter. If your distribution needed a python3 package to install Python 3, be sure to specify that. Otherwise, if your distribution uses Python 3 by default, you can just generally specify Python.

#! /usr/bin/python3

Real Time Interpreter

Getting the real time interpreter running is somewhat easier than getting a Python file set up. To run the Python interpreter, open up a terminal and run the command below.

$ python3

Yes, Python provides its own command for the interpreter. If your distribution was one that used Python 3 by default, just leave the “3” off the end of the command.

When the interpreter opens up, it will print some information in the terminal window and drop you into a different prompt. It should look similar to to the lines below.

Python 3.4.3 (default, Jul 16 2016, 20:19:21) 
[GCC 4.9.3] on linux
Type "help", "copyright", "credits" or "license" for more information.

As long as you’re in the Python interpreter, you will see the >>> prompt.

When you’re done with the interpreter, you can exit by pressing Ctrl+d or typing exit() and pressing Enter.

If you’ve been keeping up, you’re now ready to start familiarizing yourself with Python and experimenting with the language. Know that at some point, you are going to have to use both the interpreter and Python files, so it’s best to familiarize yourself with both.

Table of Contents

  1. Python Introduction and Installation Guide
  2. Python Files and the Interpreter
  3. Experimenting With Numbers and Text In Python
  4. Python Variables
  5. Working With Number Variables In Python
  6. Python String Basics
  7. Advanced Python Strings
  8. Python Comments
  9. Python Lists
  10. Python List Methods
  11. Python Multidimensional Lists
  12. Python Tuples
  13. Python Boolean Operators
  14. Python If Statements
  15. Python While Loops
  16. Python For Loops
  17. Python Dictionaries
  18. Python Advanced Dictionaries
  19. Python Functions

Comments and Discussions
Linux Forum