Python First Program

Python syntax has some similarities with other languages like C++, Java but Python is much simpler and easier. The simplicity of the python syntax makes the language popular among the beginners and developers.

This article will tell you about:

There are two ways to create a program in python.

  1. You can code directly in the python shell and run the code.
  2. You can use the command prompt to run a python code. First, you need to create the code in a file with .py extension. This file can be written in any text editor like notepad.
    To run this file on the command prompt, a command is used:
    C:\Users\TS7>python filename.py​

Basic Syntax

Let us create a basic program to see the syntax of python using python shell. We will create the traditional "Hello World" Program to start with python programming.

>>>print("Hello World")
Hello World

Let us see different components in the above program.

  • >>>: Whenever you enter the python shell, you will see >>> prompt.
  • print("Hello world"): In this above code, a function print() is used to display data on the output screen. We know that print() is a function because it is followed by a pair of parentheses.
    Anything you want to print is always written inside parentheses which acts as an argument to the print() function.
    Here, a string is displayed on the output screen. Strings are always written within double quotes. Quotations represent the beginning and end of a string.
  • Hello World: This is the output of the program. As you press enter after 'print("Hello World")', the line of code is terminated and executed by the interpreter and string is printed on the command line itself.

No use of semicolon or terminator

In python, semicolon or any terminator is not used to end a line of code. As the enter key is pressed, the line of code ends and the interpreter executes the code.

Since Python uses an interpreter, it executes the code as soon as it is created. The instructions are executed one by one.

Comments in Python (#)

Python also provides comments to create a self-explanatory code. Comments in Python start with the # symbol. Any instruction starting with # symbol is ignored by the interpreter.

Syntax:
#This is a comment.

Docstring

You can also comment multiple lines in python using triple quotes(" " "-----" " "). This is known as Docstring.

Syntax:
"""This is a Docstring."""

Indentation in Python

Indentation plays the most important role in a python code. Indentation refers to whitespaces and blanks. Indentation is merely used to increase readability in other languages but with python, indentation is much more than readability.

Python does not use curly braces to create blocks for functions or loops, instead, it uses indentation. If you will not provide proper indentation to the code, you may face errors.

Let us see the program.

>>> if(10>1):
...    print("True")
...

Output:

True

Explanation:

  • In the above code, if statement is used. It tests a condition i.e (10>1).
  • Here, the print statement comes under the block of if statement. Thus, a tab is given before the print statement as indentation.
  • To come out of an indented block, you may need to hit the enter button twice.

Let us try removing the indentaion from the above code. 

>>> if(10>1):
...print("True")

Output:

File "<stdin>", line 2
print("True")
^
IndentationError: expected an indented block

As you can see in the output, an Indentation error has occurred.