Introduction

Welcome to Python! This introduction will cover some of the groundwork for setting up Python, choosing the version and editing tools that are right for you, then dig into the basics of Python. This introduction will cover the following:


What is Python?

Python is an object-oriented scripting (non-compiled) coding language. Its applications are extensive, and it is used around the world to create web applications, perform data analysis, provide quantitative modeling, and many other uses. The true power of Python is its intuitive syntax, relative speed in program creation, multitudinous supplementary packages, and level of community support.

Python is a non-compiled language, meaning that the code processes each line at a time during execution. This has some benefits and drawbacks. Python takes longer to perform calculations than a compiled language like C++. This is generally accepted as the speed of creating programs and importing supplementary modules usually offsets the relative slowness in calculations.

You can learn Python in half the time

I see people struggling with Python every day and I want to help. That's why I developed this systematic approach to learning Python - FAST. This powerful training program exposes you to the Python programming language in a natural way so learning is easy.

Of course I want to get free Python tips

How Do I Get Python?

The most basic form of Python can be freely downloaded from the Python Software Foundation. Instructions for downloading and installing Python depend largely on operating system, which the Python download page discusses in detail.


Python Version 2 or 3?

One of the major questions when approaching an initial installation of Python is the determination of which version to use. As of the writting of this tutorial, there are two versions commonly used: Version 2.* and Version 3.*. The final release of Version 2 was in 2010, and is considered the legacy version of Python. Version 3 continues to have releases and patches, and is currently at version 3.7 as of the writing of this tutorial. Version 3 was created to fix parts of the core code and to eliminate various quirks in processing input that exist within Version 2.

There are many sources that go into detail on the subtle differences between the two, but since most of the differences are in the underlying structures of the code these enumerations can be confusing and tedious. In general, there are two major considerations you have to make before choosing a version:

  1. Does my code have to be integrated with previous versions of Python?
  2. Do I need to use modules that were developed with previous versions?

If the answer to either of these questions is yes, then you should use Python Ver. 2. Otherwise, choose Version 3. All tutorials presented here will assume the use of Python Ver. 3. A detailed analysis of changes are found in Python’s Documentation if you wish to learn more.

You can learn Python in half the time

I see people struggling with Python every day and I want to help. That's why I developed this systematic approach to learning Python - FAST. This powerful training program exposes you to the Python programming language in a natural way so learning is easy.

Of course I want to get free Python tips

Backwards Compatibility

In general, Python Version 3 is not backwards compatible to Version 2. If you are involved with projects that use Version 2 code, then Version 2 will have to be used. An example of a difference is Version 3’s handling of the print operation as a function rather than a statement in Version 2:

print "Hello"     # Version 2 
print("Hello")    # Version 3

Other changes involve functions that will be discussed in later tutorials. The main takeaway is that projects involving legacy versions of Python should use Python Ver. 2, and new projects which do not depend on Version 2 code should use Version 3.


Using Python

Once you’ve downloaded Python, you’ll want to figure out how to use it. There are several ways to use Python, from the vanilla terminal interface and word processor based scripts, to the incredibly useful IDEs available for free online.

Standalone Python

Python in its vanilla form can be used from a command line using the python command. Executing python with no additional arguments will launch a Python terminal, like the following with a Window’s Command Prompt:

Python Command Line Execution

Each line entered after >>> will be interpreted as Python code. Because Python is a scripting language, each line will execute immediately after the line is entered. Multiple lines can be entered for Python structures such as Python for loops and if statements.

Python script files are called “modules” and are associated with the .py suffix. Python modules can be executed directly from the command line with

python [path]\file.py

Modules can be created with any basic word processor by saving the plain text file with the .py suffix. However, creating a full Python script using only a plain text editor is tedious and not recommended. Enriched code editing software such as Notepad++ or Microsoft Wordpad with Python language formatting will greatly ease the process of coding. Further simplifications to coding can be found in various Integrated Development Environments (IDEs).


Integrated Development Environments (IDE)

IDEs are an excellent supplement to any coding project. IDEs are a suite of code development products which typically include an interactive terminal, visual programming enhancements, and debugging tools. There are several IDEs available for Python, with IDLE and Anaconda being popular options.

Python IDLE is included in the download of Python from Python Software Foundation. IDLE includes a GUI terminal, a text editor, and a limited set of debugging and code checking tools. IDLE has limited functionality compared to other IDEs, however it is prevalent due to its inclusion in native Python.

Anaconda includes an installation of Python, an interactive shell for Python called IPython, and numerous supplementary modules such as NumPy and SciPy. One package included in Anaconda is Jupyter Notebook. Jupyter Notebook provides tools for generating HTML, PDF, and slideshow files using markdown and Python scripting directly. These tools are extremely using for collaboration on technical projects and the generation of detailed and dynamic reports.


Overview of Basic Commands and Operations

Once you’ve downloaded and installed whichever Python version you need, you can begin playing around with Python code. In lieu of using a picture of a command prompt with each series of commands, from now on I’ll use the following format for representing a Python input lines:

print("This prints an input")  # Characters to the right of "#" are comments disregarded by Python
> "This prints an input"       # The ">" character will represent an output

From the above, we can see that the print function printed to output the string “This prints an input.” We have a nice tutorial where you can read more about Python strings and the print function.


Python as a Calculator

Python can act as a basic calculator with +, -, / (divide), * (multiply), ** (exponentiate), and % (modulo).

1 + 2
> 2
2*4
> 8
4 / 2
> 2.0
2**2
> 4
10 % 8
> 2

Additional math operations are included in the math module, which we will explore in a future tutorial on modules.


Exceptions: An Error by Any Other Name

When Python encounters an error, it will raise an “exception” with some informative text indicating what when wrong and where. Exceptions are Python’s method for indicating interpretation and runtime errors within code. For example, dividing by zero will raise an exception:

300 / 0  
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> ZeroDivisionError: division by zero

The Traceback lines will give some indication as to where the exception was raised, and the final lines give the exception type and some explanatory text. I’ll omit the Traceback portion of the exception in future examples because it is dependent on the script executed, which may be different between executions.

Python’s use of exceptions are distinct from other languages in that they can be handled without the program completely stopping. The Python philosophy to exception handling is “better to ask for forgiveness than for permission,” which can lead to unusual and useful algorithms. Exceptions will be detailed in a later tutorial.


Interrupting Python Processing

If for some reason your code freezes, enters an infinite loop, or you simply don’t want it to execute anymore, then “Ctrl + c” will cause Python to raise a KeyboardInterrupt exception and exit the execution. For example:

while True:  # This is an infinite loop. We will cover loops in a future tutorial
	x = 0
# Ctrl + c
> KeyboardInterrupt

Finally, if we want to exit the Python terminal and return to the command line prompt, we can execute the exit() function.


Next Step

By far the best way to become comfortable with Python is to simply mess around with commands, use the help() function, and see what outcomes arise from what inputs. Every exception raised is a learning experience!

The following series of tutorials will cover basic Python data types, structures, control flows, and functions using terminal input for execution. As we progress further into the understanding of Python, we will introduce the use of modules, class objects, and intermediate topics to further explore the uses of Python. Click the “Next Tutorial” button at the bottom of this page to move on to the next lesson. Have fun!

You can learn Python in half the time

I see people struggling with Python every day and I want to help. That's why I developed this systematic approach to learning Python - FAST. This powerful training program exposes you to the Python programming language in a natural way so learning is easy.

Of course I want to get free Python tips

Did you find this free tutorial helpful? Share this article with your friends, classmates, and coworkers on Facebook and Twitter! When you spread the word on social media, you’re helping us grow so we can continue to provide free tutorials like this one for years to come.