Basics

Statement

Python

 

Statement

Python 2.7
print('statement 1')
print('statement 2')
# ...

Statements are similar to sentences in natural languages.

Statement is a complete unit of execution.

 

Semicolon

Python 2.7
print('statement 1');
print('statement 2'); print('statement 3');

Each line in Python forms a statement.

You can have multiple statements in one line by separating them using semicolons (;).

Semicolons (;) are optional and Not recommended in Python.

 

Whitespaces

Python 2.7
print (  'statement 1' )

print
  ('statement 2')  # Syntax Error

print(
  'statement 3'
)

Unlike languages C and Java, indentations in Python are syntactically meaningful and form blocks.

Any other use of Whitespaces, as long as they don't make ambiguity, are ignored in Python.

 

Case Sensitivity

Python 2.7
age = 12

print(age)  # 12
print(Age)  # error
PrinT(age)  # error

Python is a Case Sensitive language.

 

Blocks

Python 2.7
def printStatements():
  print('statement 1')
  print('statement 2')

Block is used to group one or more statements.

Blocks in Python are called Suites.

Suite is defined by using a group of statements with the same level of indentation.

Python doesn't support anonymous Blocks of code like C and Java.

Here, we are defining a function printStatements with two statements.

 

Empty Statement

Python 2.7
def emptyFunction():
  pass

pass keyword is used to provide no statement, although Python syntax would expect one.

For example, if you want to run a for loop without a block.

Here, we are defining a function emptyFunction with an empty block.