An expression is any piece of program code, which returns a value
(including None
) after being executed
Expressions can be a single value or can consist of literals (strings, numbers, lists, sets, tuples), variables, operators, and functions
The operators and functions may be built-in or user defined
4
3 + 3
[32, "a string literal inside a list"]
str(len("this string is an expression in itself while using len() to get its length can be also considered as an expression") -1) + " + 1"
Each of the lines in above example is an expression
Statements are instructions that a Python interpreter can execute and are often composed of expressions (and/or other statements)
In interactive mode, the value of a statement containing only expressions is converted to string and displayed, but not while executing a Python file
Examples
x = 4
print(3 + 3)
someList = [x, 32, "a string literal inside a list"]
A simple statement is comprised within a single logical line
Several simple statements may occur on a single line separated by semicolons
Simple statements can be an expression statement, assignment statement, break
, del
, return
statement etc
Compound statements contain (groups of) other statements and affect or control the execution of those other statements in some way
In general, compound statements span multiple lines, although in some cases a compound statement may be contained in one line
if
, while
, for
, try
etc are compound statements
A compound statement consists of one or more clauses
A clause consists of a header
and a suite
(eg if
clause, else
clause )
Each clause header begins with a uniquely identifying keyword and ends with a colon
A suite is a group of statements controlled by a clause, which can be one or more indented statements on subsequent lines or can be one or more semicolon-separated simple statements on the same line as the header
The clause headers of a particular compound statement are all at the same indentation level
if x % 2 == 0:
print("even")
else:
print("odd")
In above example, the line if x % 2 == 0:
is the clause header
for the if
clause
which belongs to the if-else
compound statement
The statement print("even")
is the suite
for the if clause
Indentation in Python refers to the space characters (spaces and tabs) that are used at the beginning of a statement
Programming languages like C, Java, Javascript use braces { } to define a block of code
Python uses indentation to recognize blocks of code or statements which belong to a compound statement (or clauses of a compound statement)
Statements with the same indentation belong to the same suite if there are no statements in between with lesser amount of indentation
Consider following Python code which defines a function to display (or print) some words to the screen, when executed
def display_function():
print("string from a function")
display_function()
Here, the line print("string from a function")
is indented with 4 spaces (i.e., the actual statement is typed after keeping 4 spaces at the beginning of the line)
This is done so that when Python executes this program(or code), it recognizes that line to be part of the function display_function()
Such indented lines are called a suite(or code block) which belong to the recent clause of a compound statement
In case, indentation is of 0 spaces, then these statements are considered global statements, which get executed when the file is run or imported
The function definition does not execute the function body; this gets executed only when the function is called
For example, when python parses or executes the code provided above, it stores the display_function()
in memory as a function object with the print() statement as its suite, without executing the print statement
It then moves on to parse (execute) the next dedented line (i.e. the 3rd line above), and as a result executes the print() statement
Indentation is required for statements belonging to a clause or compound statement (like if
, while
)
Consider following Python code containing a function which takes a number and checks if the number is a whole number, negative number etc
def number_check(n):
if n == int(n):
if n >= 0:
print(n, "is whole number")
else :
print(n, "is a negative number")
else:
print(n, "is not an integer")
number_check(20)
number_check(20.0001)
number_check(-20)
Output
20 is whole number
20.0001 is not an integer
-20 is negative number
In the above example the 2nd line if n == int(n):
and 7th line else:
are in the same suite since they have the same indentation
Similarly, the 3rd and 5th lines represent the suite for the 2nd line, and furthermore have their own blocks
For any input, the execution flow first executes the if condition if n == int(n):
If the number is an integer then it moves to the corresponding block and executes the condition if n>= 0
If it evaluates to true then its corresponding print statement print(n, "is whole number")
is executed and corresponding else statement is skipped
Similarly the outer else is also skipped (for cases where its corresponding if
statement evaluates to True)
Following example shows some indentation errors
def perm(l): # error: first line indented
for i in range(len(l)): # error: not indented
s = l[:i] + l[i+1:]
p = perm(l[:i] + l[i+1:]) # error: unexpected indent
for x in p:
r.append(l[i:i+1] + x)
return r # error: inconsistent dedent