Python – Basic Syntax

Running Python programs is very similar like java, C, programs. Python can be run in different modes

Interpreter or Shell mode

For Starting python shell in command line type bellow command on Windows:

C:\python
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Inte
l)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

On Linux or Ubuntu:

$ python3
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Inte
l)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Python File mode

You can create file with extension .py and run it in the terminal:

$ python3 file.py
Hello, World!

Print Hello World in  Python

Once you start python shell in windows or ubuntu type bellow program in cmd.

print("Hello World!!!")

Rules for Initialising Variables or Identifiers in Python

A python identifiers starts with A to Z or a to z or an underscore( _ ) followed by zero or more letters, underscores and digits (0 to 9).

Allowed Identifiers: A, a, _a, _A123, _123, etc.

Not Allowed Identifiers: 123, $ads, etc.

Comments in Python

For single line comment in Python # symbol and “”” for multiple line is used:

# single line comment
"""
Multiple Line Comment
"""

Line and Indentation

In python every Line is consider as new block of code and python does not uses braces instead Indentation which means Indentation is very important

if 5>2:
print('Five greater than 2')
Filed under: Python

No comment yet, add your voice below!


Leave a Reply