Variables, Expressions and Statements

Expressions and Statements

A statement refers to an instruction that the Python interpreter can execute. So far, we have seen only the assignment statement. However, there are some other types of statements too which we will see shortly. Thus, they include while statements, if statement, import statements and other kinds. After that, an expression refers to a combination of values, variables, operators and calls to functions. In other words, expressions are required to be evaluated. Similarly, if we ask Python to print an expression, the interpreter evaluates the expression and displays the result.

expressions

Evaluating Expressions

An expression is basically a combination of values, values and operators. In other words, if we type an expression on the command line, the interpreter will evaluate it and display the result. For instance, see below:

Further, you will notice that the evaluation of an expression will generate a value. Therefore, it is why expressions are displayed on the right-hand side of assignment statements. Moreover, a value all by itself is a simple expression. The same is the case with a variable.

Further, it is also important to remember that evaluating an expression is not quite the same thing as printing a value.

                    

>>> message = "Excuse me, Hello?"

>>> message

" Excuse me, Hello?"

>>> print message

Excuse me, Hello?

Thus, you see that when the Python shell will make the value of an expression appear, it will make use of the same format as one would use for entering a value. For instance, when we look at the case of strings, it means that it includes the quotation marks.

However, the statement prints the value of the expression. Further, in this case, it is the contents of the string. Similarly, please remember that in a script, an expression all by itself is a legal statement but it does not do anything.

You will notice how the script below does not produce any output at all. So, how will you change the script to display the values of these four expressions?

                    

17

3.2

"Hello, World!"

2 + 2

Browse more Topics Under Variables, Expressions and Statements

Expression Statements

We use expression statements mostly interactively for computing and writing a value or usually for calling a procedure. It is basically a function that returns no meaningful result. In Python, procedures return the value None.

Further, the other uses of expression statements are allowed and occasionally useful. Thus, the syntax for an expression statement will be:

expression_stmt ::=  starred_expression

An expression statement evaluates the expression list which may be a single expression. Moreover, in interactive mode, if the value is not None, it converts to a string through the use of the built-in repr() function.

Thus, the resulting string is written to standard output on a line by itself. But, except if the result is None so that the procedure calls do not result in any output.

Assignment Statements

In Python, we make use of assignment statements for assigning objects to names. Also, the target of an assignment statement is written on the left side of the equal sign (=). Moreover, the object on the right can be an arbitrary expression that computes an object.

There are some essential properties of assignment which are as follows:

  • Assignment produces object references as an alternative to copying the objects.
  • Python generates a variable name the first time when they are assigned a value.
  • Names must be assigned before being referenced.
  • Some operations are there which perform assignments implicitly.

Assignment Statement Forms

We will take a look at the assignment statement forms below:

Basic Form

It is the most common form.

                    

student = Nerds

print(student)

Output

Tuple Assignment

                    

# equivalent to: (x, y) = (20, 500)

x, y = 20, 500

print('x = ', x)

print('y = ',

Output

You see that when you code a tuple on the left side of the Python will pair objects on the right side with the targets on the left by position and will also assign them from left to right. Thus, the values of x and y are 20 and 500 correspondingly.

 List Assignment

This functions in the same way as the tuple assignment above.

                    

[x, y] = [2, 4]

print('x = ', x)

print('y = ', y)

Output

Sequence Assignment

The recent version of Python has generalized the tuple and list assignment into instances of sequence assignment. It is basically any sequence of names that can be assigned to any sequence of values and Python assigns the items one at a time by position.

                    

a, b, c = 'END'

print('a = ', a)

print('b = ', b)

print('c = ', c)

Output

Extended Sequence Unpacking

This allows more flexibility in selecting portions of a sequence to assign.

                    

p, *q = 'Hello'

print('p = ', p)

print('q = ', q)

p is matched here with the first character in the string on the right and q with the rest. Further, starred name *q is assigned a list that collects all items in the sequence not assigned to other names.

Output

                    

p = H

q = ['e', 'l', 'l', 'o']

Multiple-Target Assignment

Python assigns a reference to the same object (the object which is rightmost) to all the target on the left in this form.

Output

 

p = H

q = ['e', 'l', 'l', 'o']

Augmented Assignment

                    

It is a shorthand assignment which combines an expression and an assignment.

x = 2

# equivalent to: x = x + 1

x += 1

print(x)

Output

                    

3

There are several other augmented assignment forms:

-=, **=, &=, etc.

FAQ on Expressions and Statements

Question 1: How do you write an assignment statement in Python?

Answer 1: We make use of Python assignment statements for assigning objects to names. Further, the target of an assignment statement is written on the left side of the equal sign (=). Moreover, the object on the right may be an arbitrary expression that computes an object.

Question 2: What are the three types of statements in python?

Answer 2: Python has different kinds of a statement in its programming language. Thus, some of them are assignment statement, conditional statement, looping statement and more. Consequently, all these help the user in getting the needed output.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

One response to “Values, Variables and Keywords”

  1. Brij Bhushan says:

    Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I also love your website because all type of information is available in your blogs. You made my day. Thanks you for everything. I have bookmarked more article from this website. Such a nice blog you are providing.

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.