PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Python Statements

Python Statements

Updated on: September 1, 2021 | 22 Comments

In this tutorial, you will learn Python statements. Also, you will learn simple statements and compound statements.

Table of contents

  • Multi-Line Statements
  • Python Compound Statements
  • Simple Statements
    • Expression statements
    • The pass statement
    • The del statement
    • The return statement
    • The import statement
    • The continue and break statement

What is a statement in Python?

A statement is an instruction that a Python interpreter can execute. So, in simple words, we can say anything written in Python is a statement.

Python statement ends with the token NEWLINE character. It means each line in a Python script is a statement.

For example, a = 10 is an assignment statement. where a is a variable name and 10 is its value. There are other kinds of statements such as if statement, for statement, while statement, etc., we will learn them in the following lessons.

There are mainly four types of statements in Python, print statements, Assignment statements, Conditional statements, Looping statements.

The print and assignment statements are commonly used. The result of a print statement is a value. Assignment statements don’t produce a result it just assigns a value to the operand on its left side.

A Python script usually contains a sequence of statements. If there is more than one statement, the result appears only one time when all statements execute.

Example

# statement 1
print('Hello')

# statement 2
x = 20

# statement 3
print(x)
Code language: Python (python)

Output

Hello
20

As you can see, we have used three statements in our program. Also, we have added the comments in our code. In Python, we use the hash (#) symbol to start writing a comment. In Python, comments describe what code is doing so other people can understand it.

We can add multiple statements on a single line separated by semicolons, as follows:

# two statements in a single
l = 10; b = 5

# statement 3
print('Area of rectangle:', l * b)

# Output Area of rectangle: 50Code language: Python (python)

Multi-Line Statements

Python statement ends with the token NEWLINE character. But we can extend the statement over multiple lines using line continuation character (\). This is known as an explicit continuation.

Example

addition = 10 + 20 + \
           30 + 40 + \
           50 + 60 + 70
print(addition)
# Output: 280
Code language: Python (python)

Implicit continuation:

We can use parentheses () to write a multi-line statement. We can add a line continuation statement inside it. Whatever we add inside a parentheses () will treat as a single statement even it is placed on multiple lines.

Example:

addition = (10 + 20 +
            30 + 40 +
            50 + 60 + 70)
print(addition)
# Output: 280Code language: Python (python)

As you see, we have removed the the line continuation character (\) if we are using the parentheses ().

We can use square brackets [] to create a list. Then, if required, we can place each list item on a single line for better readability.

Same as square brackets, we can use the curly { } to create a dictionary with every key-value pair on a new line for better readability.

Example:

# list of strings
names = ['Emma',
         'Kelly',
         'Jessa']
print(names)

# dictionary name as a key and mark as a value
# string:int
students = {'Emma': 70,
            'Kelly': 65,
            'Jessa': 75}
print(students)Code language: Python (python)

Output:

['Emma', 'Kelly', 'Jessa']
{'Emma': 70, 'Kelly': 65, 'Jessa': 75}

Python Compound Statements

Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way.

The compound statement includes the conditional and loop statement.

  • if statement: It is a control flow statement that will execute statements under it if the condition is true. Also kown as a conditional statement.
  • while statements: The while loop statement repeatedly executes a code block while a particular condition is true. Also known as a looping statement.
  • for statement: Using for loop statement, we can iterate any sequence or iterable variable. The sequence can be string, list, dictionary, set, or tuple. Also known as a looping statement.
  • try statement: specifies exception handlers.
  • with statement: Used to cleanup code for a group of statements, while the with statement allows the execution of initialization and finalization code around a block of code.

Simple Statements

Apart from the declaration and calculation statements, Python has various simple statements for a specific purpose. Let’s see them one by one.

If you are an absolute beginner, you can move to the other beginner tutorials and then come back to this section.

Expression statements

Expression statements are used to compute and write a value. An expression statement evaluates the expression list and calculates the value.

To understand this, you need to understand an expression is in Python.

An expression is a combination of values, variables, and operators. A single value all by itself is considered an expression. Following are all legal expressions (assuming that the variable x has been assigned a value):

5
x
x + 20Code language: Python (python)

If your type the expression in an interactive python shell, you will get the result.

So here x + 20 is the expression statement which computes the final value if we assume variable x has been assigned a value (10). So final value of the expression will become 30.

But in a script, an expression all by itself doesn’t do anything! So we mostly assign an expression to a variable, which becomes a statement for an interpreter to execute.

Example:

x = 5
# right hand side of = is a expression statement

# x = x + 10 is a complete statement
x = x + 10Code language: Python (python)

The pass statement

pass is a null operation. Nothing happens when it executes. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.

For example, you created a function for future releases, so you don’t want to write a code now. In such cases, we can use a pass statement.

Example:

# create a function
def fun1(arg):
    pass  # a function that does nothing (yet)
Code language: Python (python)

The del statement

The Python del statement is used to delete objects/variables.

Syntax:

del target_listCode language: Python (python)

The target_list contains the variable to delete separated by a comma. Once the variable is deleted, we can’t access it.

Example:

x = 10
y = 30
print(x, y)

# delete x and y
del x, y

# try to access it
print(x, y)Code language: Python (python)

Output:

10 30
NameError: name 'x' is not defined

The return statement

We create a function in Python to perform a specific task. The function can return a value that is nothing but an output of function execution.

Using a return statement, we can return a value from a function when called.

Example:

# Define a function
# function acceptts two numbers and return their sum
def addition(num1, num2):
    return num1 + num2  # return the sum of two numbers

# result is the return value
result = addition(10, 20)
print(result)
Code language: Python (python)

Output:

30

The import statement

The import statement is used to import modules. We can also import individual classes from a module.

Python has a huge list of built-in modules which we can use in our code. For example, we can use the built-in module DateTime to work with date and time.

Example: Import datetime module

import datetime

# get current datetime
now = datetime.datetime.now()
print(now)Code language: Python (python)

Output:

2021-08-30 18:30:33.103945

The continue and break statement

  • break Statement: The break statement is used inside the loop to exit out of the loop.
  • continue Statement: The continue statement skip the current iteration and move to the next iteration.

We use break, continue statements to alter the loop’s execution in a certain manner.

Read More: Break and Continue in Python

Filed Under: Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Loading comments... Please wait.

In: Python Python Basics
TweetF  sharein  shareP  Pin

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com