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. Here is all you need to know about getting started with Python.

Getting Started With Python Main Versions

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 setup for common use cases, such as math, data analysis or scientific use. See the list at the official site.

Getting Started With Python – Verify if Python is installed

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

GoalKicker.com – Python® Notes for Professionals 2

Python 3.x Version ≥ 3.0

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

Python 2.x Version ≤ 2.7

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

IDLE is a simple editor for Python, that comes bundled with Python. Here is what you need to do for getting started with Python Hello, World in IDLE:

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 Programs under the Windows menu.
  • In Windows 8+, search for IDLE or 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.
  • 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")

Hit Enter .

print("Hello, World") Hello, World

Hello World Python file

Create a new file hello.py that contains the following line:

Python 3.x Version ≥ 3.0

print(‘Hello, World’)

Python 2.x Version ≥ 2.6

You can use the Python 3 print function in Python 2 with the following import statement:

GoalKicker.com – Python® Notes for Professionals
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.

Python 2.x Version ≤ 2.7

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 hello.py.

Type 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 python /home/user/hello.py.

Launch an interactive Python shell

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.

$ 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 .

In command line, run:

python -i hello.py "Hello World"

Getting Started with Python – How to Close Python Shell

There are multiple ways to close the Python shell:

GoalKicker.com – Python® Notes for Professionals
4

exit()

or

 quit()

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 .

Try an interactive Python shell online.

Other Online Shells

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 which lacks python installation(smartphones, tablets etc).

Learn or teach basic Python.

Solve online judge problems.

Examples:

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 behavior.

https://repl.it/languages/python3 – Powerful and simple online compiler, IDE and interpreter. Code, compile, and run code in Python.

https://www.tutorialspoint.com/execute_python_online.php – Full-featured UNIX shell, and a user-friendly project explorer.

http://rextester.com/l/python3_online_compiler – Simple and easy to use IDE which shows execution time

Getting Started With Python Run commands as a string

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.

Python Shells and Beyond

Package Management – The PyPA recommended tool for installing Python packages is PIP. To install, on your command line execute pip install . 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 )

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

GoalKicker.com – Python® Notes for Professionals 5

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here