Categories
Python Programming

Statements and Expressions

Computers as machines deal work with data. Data is processed by the computer under the control of a program.

The program takes the data as input, processes it and produces an output.

The code that powers the programs does so using statements and expressions.

Statements

A statement is an instruction that tells the computer to do something. They carry out an action in the code for a computer program.

On Day 1, you used the assignment statement. The assignment statement is represented by “=”. When you created a variable, the assignment statement is used to set its value.

An assignment statement associates a value with a variable.

Another assignment statement we have seen so far is the print statement. The print statement displays a value placed within it by the user.

Expressions

An expression is a piece of code that returns data. Expressions are a sequence of values, joined by operators that can be simplified to other values.

For example, 2 + 2 is an expression representing the value 4.

Expressions are evaluated by the Python interpreter. If you type in an expression into the Python interpreter, it evaluates it and displays the result.

Statements and expressions in the Python programming language

The first line is an expression that has a value of 5. When 1 + 1 is added, we get 2 so that is also an expression.

x = 5 is the assignment statement in action. x = x + 1 will take the current value in x on the right-hand side and add 1 to it. Then assign that value back to x.

The print statement displays the value in the x variable which is 6.