Ways of executing code in Python
There are 3 standard ways of using Python, namely:
- Using Interactive Mode
- Using Command-line
- Using an Integrated Development Environment (IDE)
Let’s explore them in detail.
Using Interactive mode:
In this mode, you basically call the Python interpreter and throw a bunch of commands to it to execute. To enter into the interactive mode of Python use the below commands:
$python # Unix/Linux or python% # Unix/Linux or C:> python # Windows/DOS
Example:
Here we will enter the interactive mode and ask Python to solve a simple computation. Look at the image below:
Using command-line:
In this method of using python, you need to call the python interpreter first and then ask it to run a python file.
Example:
Let’s make a python file that simply computes the sum of 5 and 10 and returns the result and save it as gfg.py file. This would look somewhat like the below:
Now execute the file by using the below command:
python gfg.py
This will result in the following:
Using an IDE
There are plenty of IDEs available on the internet like VScode, Sublime editor, and pycharm, etc. Here we will demonstrate the use of python using Jupyter Notebook, a python IDE from Anaconda.
Example:
Here we will write a simple python code and ask the IDE to execute it in Python.
Now if you hit the Run Button on the IDE it will call for the interpreter automatically and execute the program. This would look like below:
Environment Variables in Python
In Python, its behavior is highly influenced by the setup of the environment variables. There is a fixed number of environment variables that Python recognizes and these generally are processed before the command line switches. Whenever a conflict arises between the environmental variable and the command line switches, the environment variable gets overridden.