The in Keyword

Description

To test if a specific value is present in a Sage list, we can use the in keyword:

Sage Cell

Note that true is returned for 2 in A, as 2 is present in the list, while False is returned for 2 in B because 2 is not present in list B.

Code

A = [ 1, 2, 3 ]
B = [ 4, 5 ]
print(2 in A)
print(2 in B)

Options

Using the in keyword in if statements

Because using in returns a Boolean value, we can use the in keyword in an if statement:

Code

A = [ 1, 2, 3 ]
if (2 in A):
    print("2 is present in list A!")

Tags

Primary Tags—Programming: Python

Secondary Tags—Python: Lists

Related Cells

Any related cells go here. Provide a link to the page containing the information about the cell.

Attribute

Permalink:

Author:

Date: 22 Mar 2019 14:52

Submitted by: Zane Corbiere

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License