How to get Python up and running?
Through this guide, you will learn how to install and run Python on your machine. You’ll also learn how to write your very first “Hello World!” program in Python. Are you excited? Because we absolutely are!
A little about Python
Python is a High Level Language (HLL) which means it is easy to read and write by humans. It is also cross-platform, meaning, it runs easily on various Operating Systems given the Python interpreter is installed on the machine. From the likes of Windows, Linux and Mac OS to more platforms. Various different versions of Python are used in different industries and environments as per the necessities. So, it is safe to say that Python is popular for availability of various different flavours too. For example:
- CPython is a Python variation whose compiler was written in C programming language.
- Jython is a variation of Python whose compiler was written in Java.
- PyPy is a Python interpreter which was written in Python itself.
Note: Whenever “python compiler” word is used, it must be understood that it is “Python interpreter”
Various ways of running Python programs
You can run Python programs through various different ways. These are some of the methods you can follow:
Run Python programs online
You can write and execute your Python programs online without the need of downloading anything at all. There are various websites which provide online compilers to compile and run your Python code. Some of the sites are listed below:
TutorialsPoint https://www.tutorialspoint.com/execute_python_online.php
You can directly visit this URL and start coding in Python. It’ll use the default Python compiler present on their server to execute your Python programs and give accurate results.
CodeChef IDE https://www.codechef.com/ide
At CodeChef IDE, you have to select any of the available Python interpreters (like CPython or Python) and you’re good to go.
IDE One https://ideone.com/
IDE One is one of the oldest online IDE hub where you can run your Python programs. You just have to choose a Python interpreter (like Python 2.7 or Python 3.5) and you can start coding there.
Run Python programs directly on your computer
Running Python programs directly on your machine will require two things; Python Interpreter and a Python IDE (where you can write the actual code). Here are the steps you need to follow to run Python natively:
- Download a stable version of Python https://www.python.org/downloads/
- Run the downloaded setup and follow the steps to install Python
- While installing Python through the wizard, make sure to check “Add Python to environment variable”. Doing this will add a path to your system, so as to execute a python file from any directory or drive of your computer.
- After you’ve finished the installation, you’re ready to write Python code!
After you’ve installed Python, you can either write Python programs and then execute it, or you can use the Python Interpreter to execute Python code on the go, without creating any new files.
Run Python Programs in Python Interpreter
To start using the Python Interpreter, you need to open your command line window. To do that, simply press the Windows key + R, and then type in cmd
and hit the Enter key.
Next, type “python” and it’ll take you to Python Interpreter environment. This is the place where you can write your Python code directly and get instant output. If you type 1 + 2 and hit the Enter key, it’ll print the sum of two numbers, i.e., 3. If you type “Python” + “Programmer” and hit the Enter key, it’ll return “PythonProgrammer” (concatenation of two strings, more on that later!).
Run Python programs in IDE (Integrated Development Environment)
You can also run Python programs in IDE’s. Most of the Python developers in fact, use IDE’s. This is because an IDE provides a versatile and feature-rich environment to write, debug and execute big Python projects as well as small unit programs.
What an IDE does is basically, provides you a good environment to write you code, save your files and execute the programs. It also provides multiple features like code highlighting, warning indications, auto-complete features, etc. These python files are always saved with a .py extension, where py stands for Python. No brainer.
There are multiple IDE’s present for Python Development. PyCharm is a really good and powerful IDE. However, for beginners, light-weight editors are suggested.
You’ll be surprised to know that when you installed Python on your computer, it also installed a Python IDE. This IDE is known as IDLE. Although light-weight, it is good enough for beginners. To start using this IDE, just go to Start and search for “IDLE” in your Windows machine.
Once you open IDLE, a Python Shell window will appear. Here, you can create a new file, name it whatever you want, and save it with a .py extension. For instance, helloworld.py
.
Here you can write some Python code, for example:
print(“Hello World!”)
Then you can press the F5 key to execute it or click on Run > Run Module. This will print “Hello World!” on the console window.
The Hello World Program
Finally you have Python installed on your computer and you’re ready to write your very first program in Python!
You’ll write a simple program that’ll just print “Hello World!” on your computer screen. It’s been a tradition in all the programming languages to write this program as your first program.
All you have to do is, type the following code in one of the text editors or IDE’s and save the file. You may save the file as helloworld.py
print(“Hello World!”)
After saving the file, run the file and you’ll get the following output on the screen:
Hello World!
Bravo! You have successfully written your first program in Python. This was probably the simplest Hello World! program you’d ever see because no other programming language has as small syntax declaration as Python.
PythonWarriors are here to help you in this journey.