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.
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:
>>> 2 + 2 4
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.
>>> 17 17 >>> x 2
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
- Values, Variables and Keywords
- Operators and Operands in Python
- Operator Precedence
- Taking Input and Displaying Output
- Putting Comments
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.
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:
It is the most common form.
student = Nerds print(student)
# equivalent to: (x, y) = (20, 500) x, y = 20, 500 print('x = ', x) print('y = ',
x = 20 y = 500
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.
This functions in the same way as the tuple assignment above.
[x, y] = [2, 4] print('x = ', x) print('y = ', y)
x = 2 y = 4
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)
a = E b = N c = D
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.
p = H q = ['e', 'l', 'l', 'o']
x = y = 75 print(x, y)
Python assigns a reference to the same object (the object which is rightmost) to all the target on the left in this form.
p = H
q = ['e', 'l', 'l', 'o']
p = H q = ['e', 'l', 'l', 'o']
It is a shorthand assignment which combines an expression and an assignment. x = 2 # equivalent to: x = x + 1 x += 1 print(x)
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.