Lecture

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.

Assignment Statement Example
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.

Conditional Statement Example
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

Loop Statement Example
for i in range(3): # Repeats from 0 to 2 print(i + 5) # Statement to print i + 5
  • for defines a loop, and i 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 i and prints the result for each loop iteration.

  • i is the variable used in the loop, taking values 0, 1, and 2 in sequence. Each iteration of the code block increments i by 1.

  • The range function 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.

Code Example with Multiple 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.

Expression Example
# 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.

Difference Between Statements and Expressions
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.

Mission
0 / 1

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

Run
Generate

Execution Result