Python Syntax

Python program is written in a text file editor and save it with .py extension. Invoking the Python interpreter without passing a program file as a parameter brings up the following prompt −

$ python
Python 2.4.3 (#1, Nov 11 2010, 13:34:43)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Command print the text in python prompt

>>> print ("Hello, Python!")
#Output
Hello, Python!

Another Example

>>> x = 5 >>> print("The Value of x is :", x)
#Output 
The Value of x is : 5

Python Script

Let’s write a Python program in the script. Create the text file in your current working directory and write the following line and save the file with .py extension like test.py. Type the following source code in a test.py file.

print("Hello, Python!")
print("Hello, World!")

To execute the python script, open the command prompt and type python or python3, followed by the name of the script file such as follows:

$ python test.py
#Output
Hello, Python!
Hello, World!

Note:

If you have python version 2.x, use python test.py for execution

If you have python version 3.x, use python3 test.py for execution

Indentation

Unlike the other programming language, Python has no { } to indicate the block of code for functions and class. Python use indentation to indicate a block of code. Indentation refers to the spaces at the beginning of a code line. If indentation is not used it is certain that we get error.

Example:

if 5>3:
    print("5 is greater than 3")
If you forgot to give indentation, it throws the error.
if 5>3:
print("5 is greater than 3")

#Output 

File "<ipython-input-7-dddc46867ca5>", line 2
    print("5 is greater than 3")
        ^
IndentationError: expected an indented block

.     .     .

Leave a Reply

Your email address will not be published. Required fields are marked *

Python Tutorials

Python – List Comprehension

Python Set

Python : Assert Keyword

Python Dictionary

Python Tuple

Python List

Python: String

Python: String Formatting using f-string

Python: String Formatting – 2

Python: String Formatting – 1

Python Input/Output

Python Type Conversion and Type Casting

Python Comments

Python – Regular Expression (RegEx)

Python Iterators

Python File Handling

Python Exceptions

Python Function

Python Loops

Python If…Else

Python Collections

Python Operators

Variables in Python

Introduction to Python

Python OOPs Concepts