top of page

Python Syntax

Here we have used simple python syntax, you can work as per given below statements.

Now we write a simple print statement using python 

you can run python script directly  from python shell or from command line.

Open python interactive shell and try this print statement:

>>> print(“Welcome to Codersarts.com”)

Welcome to Codersarts

There is no need to terminate statement by semicolon (;) like other programming language c,c++ or Java etc.

Syntax to run python file from command line(cmd) in windows or terminal in linux or mac:

creating a file  and save it  with extension .py, and write can write python code in that file

C:\Users\Your Name>python myfile.py

In the above statement python is command and myfile.py is file 

Python Indentation

indentation is very important in python which is used to write  block of code like other programming languages  curly braces '{ }' is used to write code of block but in python space is used.

Indentation refers to the spaces at the beginning of a code line.

if  4 % 2 == 0:
  print("Four  is Even number!")

Colon ":" is used to indicate block of code or start of block

Python Statement

Instructions that a Python interpreter can execute are called statements.

For example:

 x = 1 is an assignment statement. 

if statement, for statement, while statement etc. 

we can write simple statement in one line but what if a statement or expression is extended in multiple line and can't efficient to write in one line for code readability. so solve this issue we use continuation character (\)

Example

a = 1 + 2 + 3 + \

      4 + 5 + 6 + \

      7 + 8 + 9

print("Instructions that a Python interpreter can execute are called statements."+\
      "we can write simple statement in one line but what if "+\
      "end here")

Syntax for Python Variables

In python there is no concept like declaration the variable first and later assign value.

Here variable  is  created and  value is assigned  to it along with:

x = 10
word = "Hello, World!"

Syntax for Python Comments

Python has commenting capability for the purpose of in-code documentation.

Comments start with a #, and Python will render the rest of the line as a comment:

#This is a comment.
print("Hello, World!")

In the next tutorial, we will learn about “python comments

bottom of page