How to Code Python

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 –

python --version

If Python is installed in your computer, then it will show the version of Python.

command to check Python version

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–

py command to check Python

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.

You can download it for free from the official website of python: https://www.python.org/.

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 –

python --version

If Python is not installed or you have an older version of Python in your Mac or Linux, then install the latest version of python from the official website of python: https://www.python.org/.

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:

C:\Users\user>python

You can try writing “py” instead of “python” if the “python” command did not work.

C:\Users\user>py

Now, let’s try to run a simple Hello World program-

print("Hello, World! Welcome to TutorialBrain")

The output is –
Hello World! Welcome to TutorialBrain

Hello World using command line

In the same way, you can code Python on Mac or Linux.

To Quit the command-prompt, you just need to type –

exit()

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.

My first program file in 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 –

print("Hello, World! Welcome To TutorialBrain")

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 –

  1. Type G: to change the drive to ‘G:’ as shown below

C:\Users\user>G:

Now, you are in G drive as shown below –

G:\>
  1. Navigate to the particular folder where myfirstpgm.py resides by typing the folder name as below –

G:\>cd python

cd stands for ‘change directory’ so this command will change the path to G:\python as below –

G:\python>
  1. To execute the file, simply type – python myfirstpgm.py as shown below –

G:\python>python myfirstpgm.py

If this does not work, then type ‘py myfirstpgm.py’ as shown here-

Hello World using a file

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.

Bonus Tips
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.

Tutorials for all brains!