Python Statement and Expression

Table of contents

Python Statements

Python statements are the instructions that python interpreter executes. For example, count = 1 is a statement which assigns ‘1’ to variable ‘count’. These type of statements are called assignment statements.

Python Assignment Statements

Assignment statements are used to assign or change variable values or create a new variable.

For example:

var1 = "Yonep"
var1 = "Learn Programming with Yonep"

In the above example, the first statement creates variable var1 and assigns the string “Yonep” to the variable. The second line changes the value of the same variable var1 to “Learn Programming with Yonep”.

The assignment statements can also have arithmetic operators.

For example:

a = 1
b = 2
c = a + b

In the above example, the first and second statements create variables a and b, and assign integers 1 and 2 to them respectively. The third statement, however, creates a variable c and assigns a value of a + b to it. We will discuss python arithmetic operators later but in this example, + sign basically adds values of a and b.

There are other types of statements as well such as while, for, if, else, import, etc. We cover these statements in other tutorials.

Python Multi-line Statements

The normal python statements are terminated by the end of the line. However, Python also allows us to extend a statement over multiple lines. To do so, we can use the line continuation character (\).

For example:

var1 = "Yonep, \
Learn Programming."

The above example may seem like it has two statements. However, with the use of character \ at the end of the first line, the statement is extended to two lines. Thus, it is a single multi-line statement.

The statements containing parentheses ( ), brackets [ ] or braces { } do not need to have the line continuation character to extend them to multiple lines.

For example:

list1 = [1, 2, 3,
4, 5, 6]
a = (1 + 2 + 3 +
4 + 5 + 6)

Multiple Statements in a Single Line

Python also allows having multiple statements in a single line with the use of semicolons. However, keep in mind that none of the statements in a single line start a new code block.

For example:

x = 0; y = 5; z = 10

Python Expressions

Python expression is a combination of variables, operators, function calls, and values (numbers, strings, etc.) The value and a variable themselves are valid expressions. The expressions can be used to perform arithmetic operations, call in-built or user-defined functions, and so on.

For example:

# arithmetic expression
a = (10*2) + (9/3) - 7

# function call expression
b = len("Yonep")

In the first example, a result of the arithmetic expression is assigned to variable a and the value of a is printed. In the second example, len() function is called with an argument “Yonep” and stored in a variable b. Then, the value of b is displayed on a console. We will discuss more the functions and in-built functions later in the tutorial. len() is an in-built Python function that returns the length of the passed arguments.