Statements and Expressions
Statements and Expressions are fundamental units that compose a program.
Statements
A statement is a code block that directs the program to perform a specific task.
Statements can take various forms, such as the following:
Assignment Statement
It stores a value in a variable (a container for data) for reuse later.
To assign a value to a variable, use the = symbol.
number = 10 # A statement assigning 10 to the variable number
Conditional Statement
A conditional statement allows the program to execute a specific part of the code based on whether a condition is true or false.
It uses the keywords if, elif (short for "else if"), and else to define different conditions or actions.
Each conditional line must end with a colon (:) to indicate the start of the associated block of code.
if number > 5: # If number is greater than 5 print("The number is greater than 5") # Statement to print 'The number is greater than 5'
Loop Statement
Executes a part of the code multiple times as long as a certain condition is met.
Use the keywords for, while, followed by a colon (:) to define the start of the loop block
for i in range(3): # Repeats from 0 to 2 print(i + 5) # Statement to print i + 5
-
fordefines a loop, andi in range(3)denotes the loop range. -
The indented code block under the colon (:) defines what to execute on each loop iteration. The above example adds 5 to
iand prints the result for each loop iteration. -
iis the variable used in the loop, taking values 0, 1, and 2 in sequence. Each iteration of the code block incrementsiby 1. -
The
rangefunction generates a series of numbers, and range(3) generates numbers from 0 to 2. -
This loop outputs 5, 6, and 7 because it adds 5 to the loop variable
i, which takes values 0, 1, and 2.
Programs process data, execute code based on conditions, and perform repetitive tasks using various statements.
a = 1 # Variable assignment statement for i in range(3): # Loop statement if i == 1: # Conditional statement print(i) # Print statement
Expressions
An expression is a unit of code that yields a value.
Expressions are parts of code that evaluate to a single value and are often used within statements.
# An expression that returns 7 3 + 4 # An expression that generates the string "Hello World" "Hello" + " " + "World"
Difference Between Statements and Expressions
Statements are executed code, while expressions are code that produce a value.
In simple terms, statements tell Python what to do, whereas expressions tell it what to calculate or evaluate.
For example, in the code below, 5 + 3 is an expression, while a = 5 + 3 is a statement that assigns the value 8 to the variable a.
a = 5 + 3
Coding Practice
Complete the conditional statement by typing if number > 5:.
Be careful not to forget the trailing colon(:)! Omitting it results in a syntax error.
Which of the following is an expression in Python?
for i in range(3):
if number > 5:
3 + 4
number = 10
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result