The “in” operator

The “in” operator could be used to check if a specified object exists within an iterable object container, such as a list:

[python] name = "John"
if name in ["John", "Rick"]:
print("Your name is either John or Rick.")[/python]

Trinket.io on-line Python compiler

Python uses indentation to define code blocks, instead of brackets. The standard Python indentation is 4 spaces, although tabs and any other space size will work, as long as it is consistent. Notice that code blocks do not need any termination.

Here is an example of using Python’s “if” statement using code blocks:

if :
    
    ....
    ....
elif : # else if
    
    ....
    ....
else:
    
    ....
    ....

Trinket.io on-line Python compiler

For example:

[python] x = 2
if x == 2:
print("x equals two!")
else:
print("x does not equal to two.")[/python]

Trinket.io on-line Python compiler

A statement is evaluated as true if one of the following is correct: 1. The “True” Boolean variable is given, or calculated using an expression, such as an arithmetic comparison. 2. An object which is not considered “empty” is passed.

Here are some examples for objects which are considered as empty:

  1. An empty string: “”
  2. An empty list: [ ]
  3. The number zero: 0
  4. The false Boolean variable: False

Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.