First Python program

suggest change

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.

Python 3 is the current version of the language. Python 2 reached end of life in 2020 and should not be used for new code.

Verify if Python is installed

To confirm that Python was installed correctly, run the following command in your terminal (on Windows, add Python to the PATH 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

Hello, World in Python using IDLE

IDLE is a simple editor for Python that comes bundled with Python.

How to create Hello, World program in IDLE

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

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:

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’).

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

In command line, run:

$ python -i hello.py
Hello, World
>>>

There are multiple ways to close the Python shell:

>>> exit()

or

>>> quit()

Alternatively, Ctrl+D closes the shell on Unix and returns you to the terminal. On Windows, use Ctrl+Z and then Enter.

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:

Examples:

Disclaimer: documentation author(s) are not affiliated with any resources listed below.

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.

Shells and Beyond

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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents