Python Syntax Tutorial – POFTUT

Python Syntax Tutorial


PYthon is practical and readable programming language. Python have different syntax from popular programming languages like C, C++, C#, Java etc. Python syntax makes it easy to learn. In this tutorial we will look some aspect of Python programming language syntax.

Identifier

Identifier is the one of the main topics of all programming languages.  Identifiers are used by programmer to specify some programming language structs like variable, class, function etc. Identifiers can start with lowercase and uppercase letters and underscore.

a to z

A to Z

_

Numbers can be used in identifiers except first character

Valid Identifiers

a

a9

_a

myname

my9name

MYNAME

Invalid Identifiers

9

@name

$myname

.

=

Reserved Words

Python have already uses some words. These words provides programming language functionality to the programmer. We call this words reserved words. Reserved words can not be used as identifiers. Reserved words also called Python keywords. All reserved words are lowercase letters only. Here list of Python reserved words.

and exec not
assert finally or
break for pass
class from print
continue global raise
def if return
del import try
elif in while
else is with
except lambda yield

Comments

While writing python applications we may want to take notes on the code or we simply need to explain what the code, function, class, variable do. This notes are called Comments and can be written with # sign. Comment lines are not interpreted or used by python they are just text not a instruction.

#Print the name of the user
print(name)

Comments can be also start after an instruction line like below.

print(name)  #Print the name of the user

Lines and Indentation

One of the pythons most interesting feature is indentation. While developing application we need to group instructions and create blocks. In python we use indentation in order to group or create block. The number of spaces in indentation is variable but must be the same in all of the file. If it is not the same we will get error and code will no run.

LEARN MORE  C Syntax and Basics

Following is an example where we used 3 spaces as indentation and will work perfectly.

def myfunc():
   print("Hi")

if True:
   print("True")

Blank Line

There is no meaning blank lines in Python programming language.

Multi Line

Normally each line is used for new instruction. So we can not use multiple lines for the same instruction this is the nature of the Python. But \ can be use to provide multi line instructions which makes given lines like a single line.

text="this" + \
   "single" + \
   "line"

But one more exception is [],{},() can be used in multiple lines without \ .

name=['pof' 
,'tut' 
,'com'] 
name 
#['pof', 'tut', 'com']

Leave a Comment