< previous | Table of Contents | next > |
2.4 STATEMENTS AND EXPRESSIONS
Statements are instructions that conduct an action, such as assigning a value to a variable but do not evaluate to and output a result on their own. Statements can make up a single, for example, an assignment statement (simple statements) or multiple lines, such as a for-loop (compound statements). The difference is that expressions will produce a value that can be returned or printed for the user. Other types of statements besides assignment statements include while-statements, for-statements, if-statements, etc.
ASSIGNMENT STATEMENTS
These are statements that are used to assign values to variables [33]. If you were diligent in reading through the text above you should already be familiar with assignment statements. The following are examples of single line assignment statements.
a = 56 user_active = True greeting = "Hello!"
EXPRESSION STATEMENTS
On the other hand, expressions consist of values, variables, operators, and function calls that are evaluated by the interpreter to produce a new value. For example, 5 * 10
and print(“Hey there!”)
are expressions. The key difference is that statements are used to conduct operations, such as assigning a value to a variable, and they do not produce a return value, whereas expressions do.
Here’s an example of evaluating expressions in the shell in Thonny.
CONTROL STATEMENTS
Conditional and looping statements are characterized as control statements. Control flow statements manage execution of a program according to defined conditions. The main control flow statements in Pyhton are if
, for
, while
, break
, continue
, return
, and pass
. We will cover all of these in more detail later in the course.
Here's an example of a control statement using the if
statement.
bank_balance = 250.64 if bank_balance > 125.99: print(“You can buy the new release of shoes you like!” else: print(“You need to save a bit more money.”)
IMPORT STATEMENTS
Import statements allow you to add pre-built functionality into your program that extends the capabilities of what you can do within your program without having to code this functionality yourself. Import statements are made up of import
and from
keywords. This again is something we will cover in a bit more detail later on. However, here's a quick example.
# importing the entire random module import random # Generating and printing a random number random_number = random.randint(0, 100) print(random_number) # Importing only a specific function from the random module from random import randint # Generating and printing a random number, notice how we can now write just randint() # However, this way we can’t access any other functions random_number = randint(0, 100) print(random_number)
KEY THINGS TO REMEMBER
Statements
are the basic execution units in Python, each performing specific tasks such as assigning values or controlling the flow of a program.Assignment
statements are used to assign values to variables.Expression
statements consist of values, variables, operators, and functional calls which are evaluated to produce a new value.- We can use the
import
statement to access and add more functionality to our programs without coding the logic ourselves.
SELF-CONTROL EXERCISES
< previous | Table of Contents | next > |