Python is a widely used high-level programming language for general-purpose programming, created by Guido van Rossum and first released in 1991. Python features a dynamic type system and automatic memory management and supports multiple programming paradigms, including object-oriented, imperative, functional programming, and procedural styles. It has a large and comprehensive standard library.
Two major versions of Python are currently in active use:
You can download and install either version of Python here. See Python 3 vs. Python 2 for a comparison between them. In addition, some third-parties offer re-packaged versions of Python that add commonly used libraries and other features to ease setup for common use cases, such as math, data analysis or scientific use. See the list at the official site.
To confirm that Python was installed correctly, you can verify that by running the following command in your favorite terminal (If you are using Windows OS, you need to add path of python to the environment variable before using it in command prompt):
$ python --version
If you have Python 3 installed, and it is your default version (see Troubleshooting for more details) you should see something like this:
$ python --version Python 3.6.0
If you have Python 2 installed, and it is your default version (see Troubleshooting for more details) you should see something like this:
$ python --version Python 2.7.13
If you have installed Python 3, but
$ python --version outputs a Python 2 version, you also have Python 2 installed. This is often the case on MacOS, and many Linux distributions. Use
$ python3 instead to explicitly use the Python 3 interpreter.
IDLE is a simple editor for Python, that comes bundled with Python.
How to create Hello, World program in IDLE
All Programsunder the Windows menu.
IDLEor find it in the apps that are present in your system.
$ idle python_file.py.
In the shell, there is a prompt of three right angle brackets:
Now write the following code in the prompt:
>>> print("Hello, World")
>>> print("Hello, World") Hello, World
Create a new file
hello.py that contains the following line:
You can use the Python 3
from __future__ import print_function
Python 2 has a number of functionalities that can be optionally imported from Python 3 using the
__future__ module, as discussed here.
If using Python 2, you may also type the line below. Note that this is not valid in Python 3 and thus not recommended because it reduces cross-version code compatibility.
print 'Hello, World'
In your terminal, navigate to the directory containing the file
python hello.py, then hit the Enter key.
$ python hello.py Hello, World
You should see
Hello, World printed to the console.
You can also substitute
hello.py with the path to your file. For example, if you have the file in your home directory and your user is "user" on Linux, you can type
By executing (running) the
python command in your terminal, you are presented with an interactive Python shell. This is also known as the Python Interpreter or a REPL (for 'Read Evaluate Print Loop').
$ python Python 2.7.12 (default, Jun 28 2016, 08:46:01) [GCC 6.1.1 20160602] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print 'Hello, World' Hello, World >>>
If you want to run Python 3 from your terminal, execute the command
$ python3 Python 3.6.0 (default, Jan 13 2017, 00:00:00) [GCC 6.1.1 20160602] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print('Hello, World') Hello, World >>>
Alternatively, start the interactive prompt and load file with
python -i <file.py>.
In command line, run:
$ python -i hello.py "Hello World" >>>
There are multiple ways to close the Python shell:
Alternatively, CTRL + D will close the shell and put you back on your terminal's command line.
If you want to cancel a command you're in the middle of typing and get back to a clean command prompt, while staying inside the Interpreter shell, use CTRL + C.
Various websites provide online access to Python shells.
Online shells may be useful for the following purposes:
Disclaimer: documentation author(s) are not affiliated with any resources listed below.
Python can be passed arbitrary code as a string in the shell:
$ python -c 'print("Hello, World")' Hello, World
This can be useful when concatenating the results of scripts together in the shell.
Package Management - The PyPA recommended tool for installing Python packages is PIP. To install, on your command line execute
pip install <the package name>. For instance,
pip install numpy. (Note: On windows you must add pip to your PATH environment variables. To avoid this, use
python -m pip install <the package name>)
Shells - So far, we have discussed different ways to run code using Python's native interactive shell. Shells use Python's interpretive power for experimenting with code real-time. Alternative shells include IDLE - a pre-bundled GUI, IPython - known for extending the interactive experience, etc.
Programs - For long-term storage you can save content to .py files and edit/execute them as scripts or programs with external tools e.g. shell, IDEs (such as PyCharm), Jupyter notebooks, etc. Intermediate users may use these tools; however, the methods discussed here are sufficient for getting started.
Python tutor allows you to step through Python code so you can visualize how the program will flow, and helps you to understand where your program went wrong.
PEP8 defines guidelines for formatting Python code. Formatting code well is important so you can quickly read what the code does.