Expressions and Statements in Python

Expressions and Statements in Python

In this article, you’ll learn about expressions and statements in python, along with that you will also learn about the difference between them with examples.

So, Let’s get started!

What are Expressions in Python?

An Expression is a sequence or combination of values, variables, operators and function calls that always produces or returns a result value.

Example: x = 5y = 3z = x + y

In the above example xy and z are variables, 5 and 3 are values, = and + are operators.

So, the first combination x = 5 is an expression, the second combination y = 3 is an another expression and at last, z = x + y is also an expression.

An Expression always evaluates (calculate) to itself.

Evaluation of an expression: It simply means that, to calculate or to solve the value of something. Such as in the above example: z = x + y, which is equivalent to z = 5 + 3. So, here we are calculating the value of z. and, hence, z = 8.

Note:

  • The evaluation of an expression does not changes state and produces at least one value as a result.
  • And, The evaluated result either returns to a function call or displays at the Python prompt.

A value or a variable all by itself is also considered an expression, because these always evaluates to itself.

So,

The following are all legal expressions:

Example-1:

>>> 16
16
>>> a = 4
a 
4
>>> a + 16   #using Arithmetic expressions
20

Example-2:

#using function in an expression

type(5)    
<class 'int'>    

The above code snippet (i.e., example 2nd) is an example of a function call:

The name of the function is type. The expression inside parentheses is called the argument of the function. The result for this function is the type of the argument.

And, The result is also called the return value.

So,

If you type an expression at the IDLE, Python shell or on the command prompt the interpreter evaluates it, which means that it finds the value of the expression and displays the result.

In the 1st example, a has the value 4 and a + 16 has the value 20.


What is a Statement in Python?

Any Instruction that a python interpreter can execute (carry out) is called a Statement.

And,

If you don’t know – What does Instruction mean?

Then,

Here is the answer – An Instruction is an order/command given to a computer processor by a computer program to perform some mathematical or logical manipulations (calculations).

And,

Each and every line or a sentence in any programming language is called an instruction.

If you didn’t understand the above definition of statement clearly,

Then,

Here is a simple way to define Statement,

So,

In simple words – A Statement is the smallest executable unit of code that has an effect, like creating a variable or displaying a value.

Each and every line of code that we write in any programming language is called a statement.

Because, all the lines are executable by the interpreter or the compiler of that programming language.

Example:

>>> x = 3
print(x)

Output:

>>> 3

The first line is an assignment statement that gives a value to x. The second line is a print statement that displays the value of x.

When you type a statement then the interpreter executes it, which means that it does whatever the statement says.

Some other kinds of statements in Python are if statement, else statement, while statement, for statement, import statement, etc. which will be discussed on later article.

Points to be noted:

  • The execution of a statement changes state
  • Execution of a statement may or may not produces or displays a result value, it only does whatever the statement says.
    • As we can see in the above example: print statement displays a result value (i.e., 3 in the above example) at the prompt, while the assignment statement don’t produces any result

Difference between Expressions and Statements in Python:

ExpressionStatement
An expression evaluates to a valueA statement executes something
The evaluation of a statement does not changes stateThe execution of a statement changes state
Evaluation of an expression always Produces or returns a result value.Execution of a statement may or may not produces or displays a result value, it only does whatever the statement says.
Every expression can’t be a statement.Every statement can be an expression.
Example: >>> a + 16
>>> 20
Example: >>> x = 3
>>> print(x)

Output: 3

Conclusion:

So,

In short – An Expression always evaluates to a value.

And,

A statement does something, like creating a variable or displaying a value, it only does whatever the statement says.

I hope this article helped you a lot.

If still you have any question, feel free to ask in the comment section below right now.

And,

Don’t forget to share this article to your lovely friends.

Keep learning! 🤗


Share this Post

2 thoughts on “Expressions and Statements in Python”

Leave a Reply

Your email address will not be published. Required fields are marked *