Python Tutorial - Python Syntax


Python syntax is the set of rules which defines how a Python program will be written, and that rules make python highly readable language.

Lines in Python

Codes of python is divided into logical lines which consist of one or more physical lines.
Any line contains only spaces, tabs or comment  is known as a blank line and Python totally ignores it.

Comments in Python

A comment in python begins with a hash character(#) which is not a part of string literal and ends at the end of the physical line, so that comment apply for one line only, Python has no multi-lines or block comments facility.

print ("Line 1")
#print ("Line 2")
print ("Line 3")
#Only the first and third lines will executed.

Joining two lines

when you write a long code in one line in python you can simply divide it into a small physical lines each one end with the backslash character (\) as a sign to join with the next line.

print "Line 1 " \
"line 2 " \
"line 3 "
Statements contained within the [], {}, or () brackets do not need to use backslash character (\).

colors = ['Red', 'Blue'
  , 'Orange',
    'Black', 'Yellow']

In other hand if we want to write two or more statements in one physical line, we should end each statement with semicolon (;)

print "Line 1 "; print "line 2 "; print "line 3 "

Indentation

One of the rules that makes python is highly readable language is using spaces and tabs to define the program blocks like using braces ({...}) in other languages such as C++, PHP

if 1==1:
    print "line 1"
    print "line 2"
else:
    print "line 3"
    print "line 4"
print "finish"

Python Reserve words

And, Assert, Break, Class, Continue, def, del, elif, else, except, exec, finally, for, from, global
if, import, in, is, lambda, Not, or, pass, print, raise, return, try, while, with , yield








If this post was good and helpful for you, Please give it Like.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment