Batteries included. With Python versions 2.7, 3.5, 3.6, 3.7 and 3.8, and all the goodies you normally find in a Python installation, PythonAnywhere is also preconfigured with loads of useful libraries, like NumPy, SciPy, Mechanize, BeautifulSoup, pycrypto, and many others. Python Programming Language is very well suited for Beginners, also for experienced programmers with other programming languages like C and Java. This specially designed Python tutorial will help you learn Python Programming Language in most efficient way, with the topics from basics to advanced (like Web-scraping, Django, Deep-Learning, etc.
- Programiz Python Compiler
- Python Programs Examples
- Python Editor
- Python Programming Runners
- Python Fiddle
Learn how to run Python code?
You can execute Python code from the terminal or from a Python IDE. An IDE is a graphical environment that assitsts in software development.
If you are new to Python, I recommend this course:
Complete Python Programming Course & Exercises
Run Python Interactively
One of the ways to run Python code is by using the interactive shell (repl).
To start an interactive shell, open the terminal app or command line and type python
enter.
By default it shows >>>
, where you can type Python code. You can type any expression or statement.
It shows the output of your code immediately.
The downside of the interactive shell is, once you close the shell, your code is gone.
To quit interactive mode:
- presss
Ctrl
+d
(Unix) orCtrl
+z
(Windows) - type
quit()
and press enter.
How to Run Python
You can run Python in the command line. Create the following code in a text editor:
Then save the file as hello.py. You must use the .py extension.
All Python programs are written in code, text files with lots of instructions. These are saved with the extension .py. A program made in Python can be one or more .py files.
Then, if Python is installed, you can run the script with this command:
You should see Hello World
on the screen. If you see it, you just ran your first Python code!
Related course:Complete Python Programming Course & Exercises
Run in Terminal
Run Python on Linux
To run a Python program, you need to have Python installed. Start the terminal.
In the terminal, type the command below to test if Python is installed
Python installed?
Open a terminal and enter the directory of your program. The directory may contain more than one python (.py) file.
In the terminal type:
Programiz Python Compiler
where file.py is the name of your program. If you have more than one file, the main program is often the name of the program itself;
Related course:Complete Python Programming Course & Exercises
Run Python on Windows
Python Programs Examples
To run Python programs on windows, there are a few steps you need to do.
Save your code with the
.py
extension. Lets call it hello.pyOpen a command prompt, by pressing start, run and type “cmd” (in the text field) and press OK.
Go to the directory of your Python file with the cd command.
- Run the script with Python
- If you get an error, like the one shown below:
then python is not in the path (environment variables). Make sure it’s included in your Windows environment variables.
The easiest way to do that, is selecting a box in the installation.
If you have already installed Python, you can set the environment variable in the control panel.
Related course:Complete Python Programming Course & Exercises
Run Python on Mac
You need Python installed on your Mac OS X system. On Mac the older Python 2 is often installed by default.
If you don’t have Python installed, open the Terminal app. Then install Xcode tools:
Install homebrew with this command:
Finally install Python, type the command:
Run Python code
Then you can run the command on your code:
Python Editor
You must be in the same directory as your file, or add the path.
or move into the directory:
Run from IDE
Sometimes an easier way to execute Python programs is using a Python IDE.
What is an IDE?
An IDE is a tool that assists in software development. Its a software program that has many features like:
- code coloring (syntax highlighting)
- file navigation
- quick search
- and much more.
In most IDEs, there’s a play button that instantly runs the Python program. In other words, in a Python IDE, you simply press the ‘play’ button.
PyCharm
The PyCharm IDE lets you run Python code with a single click. After creating the project, press the green play button in the right corner.
The image above shows the header of the program PyCharm, a Python IDE. The green play button can be used to start a program.
Python Programming Runners
In PyCharm, you can also press “Run”, “Run ‘HelloWorld’”.
Python Fiddle
Related course:Complete Python Programming Course & Exercises