How to run a Python Script?
Python is the most popular programming language right now. Guido van Rossum created it in 1991. Python has become a very popular programming language for general-purpose use in recent years. Python has a lot of applications in Data Science, AI/ML, web development, etc. There are different ways to run a python script in this article we will discuss all of them.
To check your code you have to run your python script that’s why compiling your code is essential. There are mainly four ways to run your python scripts on different operating systems such as mac, Linux, and windows. In this article, we are going to learn all the ways to run a python script with examples. And at the end, we will talk about a new special feature. So what are you waiting for let’s get started?
Installing Python
These are the different ways to compile your python code
- Interactive mode
- Python command
- Text editor
- IDE
We will discuss more of them in the upcoming sections. Python is an interpreted programming language means it will run your code line by line. To run that you need to install python. Now let us download python to your system. Python is an open-source programming language, You can download any version of it on their website.
Windows
If you are using windows you need to install python manually. First, go to the website mentioned above and go to the download sections, and download the latest version available for your windows. Once the installation is done run the .exe
file and allow all the permissions required. Make sure you add python to your path otherwise it will take a lot of effort.
Make sure you tick the add python to your path.
Linux
Python is pre-installed in most Linux distributions. To verify it Enter the following command
$ python --version
Code language: Bash (bash)
(or)
$ python3 --version
Code language: Bash (bash)
If it is not installed you can install it from their official website. If you are using ubuntu enter the following commands in your terminal
$ sudo apt-get update
$ sudo apt-get install python3
Code language: Bash (bash)
macOS
Python comes pre-installed on mac but it is an older version. we need to use the latest version to install it go to the official python website and navigate to download for macOS. Now download the latest version
Interactive mode
To get started in interactive mode you need to download the compiler as mentioned above. Verify it by the python --version
or python3 --version
command. Now once your compiler is installed You will have to open your terminal. If you don’t know what terminal is on windows press your press the windows button and search for the terminal. In mac or Linux just search for the terminal and open it. Once you open the terminal enter python or python3 and hit enter
C:\Users> python
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Code language: Python (python)
>>> This indentation confirms you are in. Now you can write your python code.
>>> print("Welcome to codedamn!")
Welcome to codedamn!
>>> 4/2
2.0
Code language: Python (python)
To exit interactive mode use ctr + z on windows and ctrl + D on mac and Linux systems. Or you can use inbuilt functions like exit() or quit().
The only drawback of this is you can’t save all the code you executed once you close the terminal everything will be erased
Python Command
To run a python file in the terminal using the python command first create one file with the .py
extension and write your python code in the file. For example, I am going to create a hello.py file and write the code to print hello world. Now open your terminal and navigate to the directory where your python file is present and enter the command python (or) python3 followed by name of your python file
C:\Users> python hello.py
Hello World
Code language: Python (python)
This is the most frequent method used by many developers worldwide.
Text Editor
When your coding for big complex applications it is recommended that you use Advanced Text Editor or an IDE. In the next section, we will discuss IDEs, for now, let us discuss advanced text editors. As of now, the two popular text editors are VScode and sublime text.
To run the python script in vscode first download it from their official website. Once it is downloaded open it and create a new file with the .py
extension. To run the code you need to install some extensions and click ctrl+shift+X
on windows and Linux on the mac cmd+shift+X
. Now search for python and code runner and install them. To run your code right click and click on run and the code will compile you can see the output in the bottom terminal.
IDE
An IDE stands for Integrated Development Environment. There are a lot of IDEs out there Pycharm, Visual Studio, etc. All IDEs are moreover similar to each other. To run your python scripts just follow the same process above mentioned for the text editor. The only advantage is you do not need to download any sort of extensions as mentioned above.
Just create a new file with .py
an extension and start writing your code in it and execute the code. To run your code Right click and select Run File in the Python console option it will open a terminal at the bottom and print your output. The biggest advantage of an IDE is it provides all the tools required for a developer in a single application whereas in code editors you need to install a lot before you get started.
Codedamn online playground
Often when compiling your code you get a lot of errors like python not being installed properly or if you didn’t add python to your path the code won’t compile. Or if you are using low-end systems the code editors and IDEs take down a lot of space which in turn slowdowns your system.
To overcome these problems codedamn has come up with a new online python playground where you can write, edit and run your code. The big advantage is you can write code within your browser and no need to download any software all you need is a device with a good internet connection. To use the playground go to your browser and search for codedamn-playground open the first link. Signup to codedamn if you are new to codedamn. Now click on the python playground and create a new project. Now you can write your code and run it.
The best feature about this playground you can use this feature on any device it can be a mobile, tablet, Desktop, Laptop, etc. You can use it on any device all you need is a stable internet connection, more importantly, on mobile and tablet make sure you enable desktop mode in your browser in the menu bar for a good interface of the playground. you can share your playground and collaborate with other developers\friends. You can use the terminal at the bottom to enter into interactive mode and run your code line by line. There are other playgrounds as well check out them once.
Conclusion
Python is a popular interpreted programming language that we use for several purposes. In this article, we learned all about compiling your python scripts. we learned about interpreters and compilers. Command mode and interactive mode are good for small-scale applications. If you are coding for big applications go for a text editor or IDE as they come with some extra features. You can run your python code from your mobile using codedamn python playground you do not need to install anything just need a stable internet connection. That’s it from this article. If you want to learn more about python do check out other articles as well. Thank you!
Sharing is caring
Did you like what Thrishank wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses:
303 students learning
Haris
Python Crash Course for Beginners
Surendra varma Pericherla
Learn Data Structures Using Python