The syntax of the Python programming language is the set of rules which defines how a Python program will be written. Python Line Structure:
A Python program is divided into a number of logical lines and every logical line is terminated by the token NEWLINE.
A logical line is created from one or more physical lines.
A line contains only spaces, tabs, formfeeds possibly a comment, is known as a blank line, and Python interpreter ignores it.
A physical line is a sequence of characters terminated by an end-of-line sequence (in windows it is called CR LF or return followed by a linefeed and in Unix, it is called LF or linefeed).
See the following example.
1.|x = 4
2.|if (x > 0):
3.|
4.|
5.| print ("hello world" )
6.| # hello world
print ("hello world." )
if ( condition ):
## code here
print ("Anything." )
if ( condition ):
## code exicuted when if condition is true
print ("Anything." )
else :
## code exicuted when if condition is false
print ("Anything." )
if ( condition ):
## code exicuted when if condition is true
print ("Anything." )
elif ( condition ):
## code exicuted when elif condition is true
print ("anything" )
else :
## code exicuted when if and elif conditions are false
print ("Anything." )
if ( condition ):
## code exicuted when if condition is true
print ("Anything." )
if ( condition ):
## code exicuted when both the if conditions are true
print ("anything" )
else :
## code exicuted when the first (if) condition is true and second (if) condition is false
print ("anything" )
else :
## code exicuted when first (if) condition is false
print ("Anything." )
for i in range (10 ):
print (i)
## here (i) is a varible starts from 0 to range.
while ( condition ):
print ("anything" )
## code exicuted when while condition is true
( code ) ## write an increment or decrement statement to break while loop
for i in range (10 ):
print (i)
## here (i) is a varible starts from 0 upto range.
else :
## code exicuted when ( for loop ) ends
print ("anything after for loop" )
while ( condition ):
print ("anything" )
## code exicuted when while condition is true
( code ) ## write an increment or decrement statement to break while loop
else :
## code exicuted when while condition is false
print ("anything after while loop" )
for i in range (123 ):
if ( condition ):
print ("exicuted when if condition is true" )
else :
print ("exicuted when if condition is false" )
while ( condition ):
if ( condition ):
print ("exicuted when if condition is true" )
elif ( condition ):
print ("exicuted when elif condition is true" )
else :
print ("exicuted when both conditions are false" )
( code ) ## code to break the while loop
for i in range (123 ):
for j in range (5 ):
print (i,j)
while ( condition ):
while ( condition ):
print ("exicuted when ( while ) condition is true" )
( code ) ## code to break the while loop
else :
print ("exicuted when ( while ) condition is false" )
( code ) ## code to break the while loop
else :
print ("code exicuted when while condition is false" )