How to Code Python
If you are an absolute beginner, you can start running Python in Command Prompt.
As you gradually move to Intermediate or advanced level, you can use an advanced Code Editor or an IDE.
Let’s learn how to code with python –
Check Python Installation in Windows
Before learning how to code in python, you have to first install python in your computer.
Sometimes, Python is pre-installed on your Windows, Linux or macOS. To check if python is pre-installed on Windows, open the command prompt (cmd.exe) and type –
If Python is installed in your computer, then it will show the version of Python.
If your command prompt is not able to recognize the word “python”, then simply type the command “py” in the command prompt without the quotation mark as below–
If it displays a version of python, then python is preinstalled in your machine. In this case, the version is Python 3.8.0.
If you try both the commands one by one but still the command prompt does not recognize the word ‘python’ or ‘py’, then you should install Python in your machine.
How to Install Python on Windows
To install Python on your Windows PC you have to follow the given steps:
- Download the current version of Python.
- Run the installer file
- Click on add python to environment variables so that you can run Python from any part of your computer
- Now, choose the path where Python is installed.
- Click on install and install Python on your computer.
Congratulations! You installed Python on your Windows.
Check Python Installation in Mac or Linux
To check if Python is pre-installed on your Linux or Mac, open your command prompt on Mac or open the Terminal in case you have Linux and type –
How to test a Python Code using Command Line
To test basic Python code, simply open the command prompt. The Command Prompt can easily interpret the Python code.
Type the following on the Windows, Mac or Linux command line:
You can try writing “py” instead of “python” if the “python” command did not work.
Now, let’s try to run a simple Hello World program-
The output is –
Hello World! Welcome to TutorialBrain
In the same way, you can code Python on Mac or Linux.
To Quit the command-prompt, you just need to type –
Basic Way to run a python file in terminal or command Line
As Python is an interpreted programming language, you need an Interpreter to run it. If you are a beginner, you can write the python code in a text editor like Notepad++. After writing the code, you must save the python file with .py extension. Suppose, you save the file name as myfirstpgm.py in the location G:\python.
Now, to execute myfirstpgm.py file, you have to go to the directory where the file resides. In this case, it is G:\python. Suppose, the file contains a simple print statement as shown below –
Steps to execute the python file
Suppose you are at c:\users\user and you want to run myfirstpgm.py from G:\python, then you have to follow these steps –
- Type G: to change the drive to ‘G:’ as shown below
Now, you are in G drive as shown below –
- Navigate to the particular folder where myfirstpgm.py resides by typing the folder name as below –
cd stands for ‘change directory’ so this command will change the path to G:\python as below –
- To execute the file, simply type – python myfirstpgm.py as shown below –
If this does not work, then type ‘py myfirstpgm.py’ as shown here-
Congratulations, you have successfully executed your first Python program using a file.
Best Way to execute a Python File - Alternative approach
If you wish to use a Code Editor to run the Python file, then you can use an advanced Code Editor like Visual Studio Code to run Python. It is the most used Code editor for Website Development.
Alternatively, the best tool to master Python is to use an IDE such as Jupyter, PyCharm, etc. You can think of IDE as an advanced version of any code Editor.
To know about the most popular Python Code Editors and IDEs, read the next article or Click Here.
The reason for the success of Python in data science is the availability of data science libraries and these libraries are continuously upgraded.
Some of the most important libraries available to perform data analysis are –
- NumPy – In Python, it good to execute scientific computing using NumPy as it contains lots of mathematical functions.
- SciPy can interact with with NumPy arrays for easy numerical integration and upgrades.
- Pandas – It is designed using NumPy as a base. This helps in data structures and operations involving mathematical tables.