How to Use ‘In’ in a Python If Statement

Avatar

By squashlabs, Last Updated: November 2, 2023

How to Use ‘In’ in a Python If Statement

The ‘in’ keyword in Python is used to check if a value is present in a sequence or collection. It is commonly used in if statements to perform conditional execution based on the presence or absence of a value. In this answer, we will explore how to use ‘in’ in a Python if statement with different types of sequences and collections.

Using ‘in’ with Lists

To check if a value is present in a list, you can use the ‘in’ keyword followed by the list name. Here’s an example:

fruits = ['apple', 'banana', 'orange']
if 'apple' in fruits:
    print('Apple is present in the list')

In this example, the if statement checks if the value ‘apple’ is present in the list ‘fruits’. If it is, the statement inside the if block is executed, which prints ‘Apple is present in the list’.

You can also use the ‘in’ keyword with the ‘not’ keyword to check if a value is not present in a list. Here’s an example:

fruits = ['apple', 'banana', 'orange']
if 'grape' not in fruits:
    print('Grape is not present in the list')

In this example, the if statement checks if the value ‘grape’ is not present in the list ‘fruits’. If it is not, the statement inside the if block is executed, which prints ‘Grape is not present in the list’.

Related Article: How To Limit Floats To Two Decimal Points In Python

Using ‘in’ with Strings

The ‘in’ keyword can also be used to check if a substring is present in a string. Here’s an example:

message = 'Hello, world!'
if 'world' in message:
    print('The word "world" is present in the message')

In this example, the if statement checks if the substring ‘world’ is present in the string ‘message’. If it is, the statement inside the if block is executed, which prints ‘The word “world” is present in the message’.

You can also use the ‘in’ keyword with the ‘not’ keyword to check if a substring is not present in a string. Here’s an example:

message = 'Hello, world!'
if 'python' not in message:
    print('The word "python" is not present in the message')

In this example, the if statement checks if the substring ‘python’ is not present in the string ‘message’. If it is not, the statement inside the if block is executed, which prints ‘The word “python” is not present in the message’.

Using ‘in’ with Tuples

The ‘in’ keyword can also be used to check if a value is present in a tuple. Here’s an example:

numbers = (1, 2, 3, 4, 5)
if 3 in numbers:
    print('The number 3 is present in the tuple')

In this example, the if statement checks if the value 3 is present in the tuple ‘numbers’. If it is, the statement inside the if block is executed, which prints ‘The number 3 is present in the tuple’.

Using ‘in’ with Sets

The ‘in’ keyword can also be used to check if a value is present in a set. Here’s an example:

numbers = {1, 2, 3, 4, 5}
if 3 in numbers:
    print('The number 3 is present in the set')

In this example, the if statement checks if the value 3 is present in the set ‘numbers’. If it is, the statement inside the if block is executed, which prints ‘The number 3 is present in the set’.

Related Article: How To Rename A File With Python

Using ‘in’ with Dictionaries

The ‘in’ keyword can also be used to check if a key is present in a dictionary. Here’s an example:

person = {'name': 'John', 'age': 30, 'city': 'New York'}
if 'name' in person:
    print('The key "name" is present in the dictionary')

In this example, the if statement checks if the key ‘name’ is present in the dictionary ‘person’. If it is, the statement inside the if block is executed, which prints ‘The key “name” is present in the dictionary’.

You can also use the ‘in’ keyword with the ‘not’ keyword to check if a key is not present in a dictionary. Here’s an example:

person = {'name': 'John', 'age': 30, 'city': 'New York'}
if 'email' not in person:
    print('The key "email" is not present in the dictionary')

In this example, the if statement checks if the key ’email’ is not present in the dictionary ‘person’. If it is not, the statement inside the if block is executed, which prints ‘The key “email” is not present in the dictionary’.

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

How To Check If List Is Empty In Python

Determining if a list is empty in Python can be achieved using simple code examples. Two common methods are using the len() function and the not operator. This article... read more

How To Check If a File Exists In Python

Checking if a file exists in Python is a common task for many developers. This article provides simple code snippets and explanations on how to check file existence... read more

How to Use Inline If Statements for Print in Python

A simple guide to using inline if statements for print in Python. Learn how to use multiple inline if statements, incorporate them with string formatting, and follow... read more

How to Use Stripchar on a String in Python

Learn how to use the stripchar function in Python to manipulate strings. This article covers various methods such as strip(), replace(), and regular expressions. Gain... read more

How To Delete A File Or Folder In Python

Deleting files or folders using Python is a common task in software development. In this article, we will guide you through the process step-by-step, using simple... read more

How To Move A File In Python

Learn how to move a file in Python with this simple guide. Python move file tutorial for beginners. This article discusses why the question of moving files in Python is... read more