Getting Started with Python
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:
- Python 3.x is the current version and is under active development.
- Python 2.x is the legacy version and will receive only security updates until 2020. No new features will be implemented. Note that many projects still use Python 2, although migrating to Python 3 is getting easier.
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 setups for common use cases, such as math, data analysis or scientific use. See the list at the official site.
Installation of Python 2.7.x and 3.x[edit | edit source]
Note: Following instructions are written for Python 2.7 (unless specified): instructions for Python 3.x are similar.
First, download the latest version of Python 2.7 from the official Website (https://www.python.org/downloads/). Version is provided as an MSI package. To install it manually, just double-click the file.
By default, Python installs to a directory:
Warning: installation does not automatically modify the PATH environment variable. Assuming that your Python installation is in C:\Python27, add this to your PATH:
Now to check if Python installation is valid write in cmd:
Python 2.x and 3.x Side-By-Side
To install and use both Python 2.x and 3.x side-by-side on a Windows machine:
- Install Python 2.x using the MSI installer.
- Ensure Python is installed for all users.
- Optional: add Python to
PATHto make Python 2.x callable from the command-line using
- Install Python 3.x using its respective installer.
- Again, ensure Python is installed for all users.
- Optional: add Python to
PATHto make Python 3.x callable from the command-line using
python. This may override Python 2.x
PATHsettings, so double-check your
PATHand ensure it's configured to your preferences.
- Make sure to install the
py launcherfor all users.
Python 3 will install the Python launcher which can be used to launch Python 2.x and Python 3.x interchangeably from the command line:
P:\>py -3 Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> C:\>py -2 Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
To use the corresponding version of
pip for a specific Python version, use:
C:\>py -3 -m pip -V pip 9.0.1 from C:\Python36\lib\site-packages (python 3.6) C:\>py -2 -m pip -V pip 9.0.1 from C:\Python27\lib\site-packages (python 2.7)
The latest versions of CentOS, Fedora, Redhat Enterprise (RHEL) and Ubuntu come with Python 2.7.
To install Python 2.7 on linux manually, just do the following in terminal:
wget --no-check-certificate https://www.python.org/ftp/python/2.7.X/Python-2.7.X.tgz tar -xzf Python-2.7.X.tgz cd Python-2.7.X ./configure make sudo make install
Also add the path of new python in PATH environment variable. If new python is in
/root/python-2.7.X then run
export PATH = $PATH:/root/python-2.7.X
Now to check if Python installation is valid write in terminal:
Ubuntu (From Source)
If you need Python 3.6 you can install it from source as shown below (Ubuntu 16.10 and 17.04 have 3.6 version in the universal repository). Below steps have to be followed for Ubuntu 16.04 and lower versions:
sudo apt install build-essential checkinstall sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz tar xvf Python-3.6.1.tar.xz cd Python-3.6.1/ ./configure --enable-optimizations sudo make altinstall
As we speak, macOS comes installed with Python 2.7.10, but this version is outdated and slightly modified from the regular Python.
The version of Python that ships with OS X is great for learning but it’s not good for development. The version shipped with OS X may be out of date from the official current Python release, which is considered the stable production version. (source)
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install Python 2.7:
brew install python
For Python 3.x, use the command
brew install python3 instead.
Verify if Python is installed[edit | edit source]
To confirm that Python was installed correctly, you can verify that by running the following command in your favourite terminal (If you are using Windows OS, you need to add the path of python to the environment variable before using it in the 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.
Hello, World in Python using IDLE[edit | edit source]
IDLE is a simple editor for Python, that comes bundled with Python.
How to create Hello, World program in IDLE
- Open IDLE on your system of choice.
- In older versions of Windows, it can be found at
All Programsunder the Windows menu.
- In Windows 8+, search for
IDLEor find it in the apps that are present in your system.
- On Unix-based (including Mac) systems you can open it from the shell by typing
$ idle python_file.py.
- In older versions of Windows, it can be found at
- It will open a shell with options along the top.
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
Hello World Python file[edit | edit source]
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
Launch an interactive Python shell[edit | edit source]
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.
Other Online Shells[edit | edit source]
Various websites provide online access to Python shells.
Online shells may be useful for the following purposes:
- Run a small code snippet from a machine that lacks python installation(smartphones, tablets etc).
- Learn or teach basic Python.
- Solve online judge problems.
Disclaimer: documentation author(s) are not affiliated with any resources listed below.
- https://www.python.org/shell/ - The online Python shell hosted by the official Python website.
- https://ideone.com/ - Widely used on the Net to illustrate code snippet behaviour.
- https://repl.it/languages/python3 - Powerful and simple online compiler, IDE and interpreter. Code, compile and run code in Python.
- http://rextester.com/l/python3_online_compiler - Simple and easy to use IDE which shows the execution time
Run commands as a string[edit | edit source]
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.
Shells and Beyond[edit | edit source]
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 in 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.
IDLE - Python GUI[edit | edit source]
IDLE is Python’s Integrated Development and Learning Environment and is an alternative to the command line. As the name may imply, IDLE is very useful for developing new code or learning python. On Windows, this comes with the Python interpreter, but in other operating systems you may need to install it through your package manager.
The main purposes of IDLE are:
- Multi-window text editor with syntax highlighting, autocompletion, and smart indent
- Python shell with syntax highlighting
- Integrated debugger with stepping, persistent breakpoints, and call stack visibility
- Automatic indentation (useful for beginners learning about Python's indentation)
- Saving the Python program as .py files and run them and edit them later at any them using IDLE.
In IDLE, hit
run Python Shell to launch an interpreter. Using IDLE can be a better learning experience for new users because code is interpreted as the user writes.
Troubleshooting[edit | edit source]
If you're on Windows, the default command is
python. If you receive a
"'python' is not recognized" error, the most likely cause is that Python's location is not in your system's
PATH environment variable. This can be accessed by right-clicking on 'My Computer' and selecting 'Properties' or by navigating to 'System' through 'Control Panel'. Click on 'Advanced system settings' and then 'Environment Variables...'. Edit the
PATH variable to include the directory of your Python installation, as well as the Script folder (usually
C:\Python27;C:\Python27\Scripts). This requires administrative privileges and may require a restart.
When using multiple versions of Python on the same machine, a possible solution is to rename one of the
python.exe files. For example, naming one version
python27.exe would cause
python27 to become the Python command for that version.
You can also use the Python Launcher for Windows, which is available through the installer and comes by default. It allows you to select the version of Python to run by using
py -[x.y] instead of
python[x.y]. You can use the latest version of Python 2 by running scripts with
py -2 and the latest version of Python 3 by running scripts with
This section assumes that the location of the
python executable has been added to the
PATH environment variable.
If you're on Debian/Ubuntu/MacOS, open the terminal and type
python for Python 2.x or
python3 for Python 3.x.
which python to see which Python interpreter will be used.
- Arch Linux
The default Python on Arch Linux (and descendants) is Python 3, so use
python3 for Python 3.x and
python2 for Python 2.x.
- Other systems
Python 3 is sometimes bound to
python instead of
python3. To use Python 2 on these systems where it is installed, you can use