We will start with the basic Python “Hello World” program.
>>> print("Hello World!") Hello World!
That’s it! You can see the simplicity of Python by its syntax; no header files are included as in C\C++, no need to define any main function and so on. You also don’t need statement terminators, and you just write print and the content
Hello World enclosed in double quotation marks inside parenthesis.
Python Basic syntax:
Consider the code below which multiplies two numbers:
# Multiplying two numbers x = 4 y = 2 result = x * y print(result)
Explanation of above program:
- The first line is a comment starting with
#. Comments are the lines that are not executed but ignored. So they do not disturb the normal flow of a program.
- The next line
x = 4is the definition of a variable. The name of the variable is
xwhich stores 4.
- Similarly, you can see
yis a variable which stores 2.
- The line
result = x * yis a Python statement which evaluates the multiplication expression
x*yand stores the result in the variable
result. To multiple two number
*operator is used.
print()statement is used to print strings or values of variables on the screen.
You can note here there are no curly braces (delimiters); in Python to represent any block indentation is used. For example if you are creating a class, you need to use indentation as follows:
class1: class2: statements class2: statements