Write your first Python Program

1
342
how to write your first python progam by manish sharma

Now that we have installed and configured the Python on our machine next we have to learn how to write and execute a Python Program. That is exactly what you are going to learn in this tutorial. In this tutorial, you will be learning-

  • How to write your first python program, and
  • How to work with the command prompt for writing the python code?

All the tips, tricks, and code shown in this tutorial will work on all the operating systems be it the Windows, Linux as well as Mac. So don’t worry and follow along.

Since we just installed the python on the machine thus the only editor which is available at our disposal is the Windows’s command prompt or Linux’s terminal, except the one which comes within the bundle. We will talk about that in the next tutorial, but for now, let’s concentrate on writing Python code using Command Prompt. Shall we?

How To Launch The Python Shell?

In order to start writing our code we first need to start the Python Language’s interpreter. The interpreter is a special program which converts the code into a machine understandable language. It also takes care of the wellbeing of the code and makes sure that the programmer has followed all the rules and regulations of the underlying programming language.

In order to launch Python shell in windows, you first need to launch the command prompt. You can do so by writing “cmd” and pressing “OK” button in the Run Command Box (press win key + R).

Hello World first python program by manish sharma

Once you have your command prompt ready in front of you, then you are ready to start your Python. We have two commands to launch the Python interpreter.

  1. python, and
  2. py

Depending on your choice you simply have to write either of these command and press enter. Doing so will start the Python interpreter and take you into the python shell.

Hello World first python program by manish sharma 02

How to write your first python program?

Unlike various other high-level languages, Python has the easiest programming structure. That makes it the first programming choice of millions of beginners around the world. It’s the simplest yet powerful programming language in the market which you can learn. Having said that let’s write our first Python Program.

print (“What’s Up Internet?”)

This single executable statement makes the entire program in the Python language. In this statement, we are calling the “print” function of the python language and asking it to print the supplied argument on the output screen of the user. That supplied argument in our case is our favorite string “what’s up internet!”

Python is not a compiler based language rather it is an interpreter based programming language. The compiler converts the entire program into the machine language in one go whereas Interpreter converts the program by taking a single line at a time. Which means as soon as you hit Enter Key the python engine will straightaway show you the result. It won’t wait for you to enter any other command.

Python is not a compiler based language rather it is an interpreter based programming language. Learn how to write your first Python Program Click To Tweet

 

Is Python a case sensitive programming language?

Yes, Python is strongly a case sensitive programming language which is why all the three statements mentioned below have different meaning and execute differently.

#Function name in all “small cap”.
print (“What’s Up Internet?”)


#Function name in “Sentence case”
Print (“What’s Up Internet?”)


#Function name in “ALL CAPS”
PRINT (“What’s Up Internet?”)


The first “print” function call where the name of the Print function is written in all small letters is the only legitimate function call which will be recognized and executed by the python interpreter without any error. While the rest will raise an error. You can watch the video here for live demonstration.

Info: In python almost all the names whether it’s a keyword or name of any function or module is in small letters thus try to keep them in that way, unless you want to fight with an error.

Is Python an indentation-sensitive language?

Python is not only a case sensitive language but also an indentation-sensitive language. Which means with Python Programming language you need to be very careful with your space bar or tab key. Single white space or a mistakenly pressed tab key can cause an error.

How to clear the command prompt in Python Programming Language?

Using Command prompt for writing code will raise the need to clear the screen quite often. I will say that unlike other high-level languages, clearing the screen in Python programming language is not an easy task.

In order to clear your command prompt in Python, you need to call the underlying operating system’s shell command which is “cls” in case of Windows and “Clear” in case of Mac and Linux. To execute the operating system’s shell command in Python we need to use the “system” function of the “os” module. Let’s see how it is done. Also, refer to the picture given below.

Step 1: Import the “os” module 

import os

Step 2: Call “system” function

For windows

os.system(‘cls’)

For Mac or Linux

os.system(‘clear’)

How to clear Python Prompt by Manish Sharma

The function “system” takes an argument and that argument could be any underlying operating system’s shell command.

How to exit from Python Prompt in Windows?

Let’s say you are done working with Python programming and now you want to come out from it. Closing the windows could be the fastest but it’s not the safest way. If this is not the safest way then what is it?

There are two ways of coming out from your python shell. These are-

  1. Using exit( ) function
  2. Using keyboard shortcut

Using Exit ( ) function

To come out from your Python you can simply call the “exit( )” function. This function will bring you out from Python shell.

exit()

How to clearn python prompt using os system by Manish Sharma

Using Keyboard Shortcut

Another way of coming out from python is using control + z (^z) keyboard shortcut. That is a comparatively faster way of coming out from Python.

In this tutorial we have covered various concepts which may look small but could save you from time consuming errors and bugs. Hope you enjoyed reading. Do make sure to share this blog on your social media.

Thanks for reading. Good Luck and Godspeed.

1 COMMENT