Learn about Python Conditions

You are here: Home » Blog » Computer Programming Languages » Python » Learn about Python Conditions

Learn about Python Conditions

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:

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

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:

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

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

By |2019-03-28T18:52:31-04:00April 7th, 2018|Categories: Computer Programming Languages, Python|Tags: , |

About the Author:

I am a loving father, & husband. I am a computer enthusiast. I have used and enjoyed computers since I was young and I enjoy teaching young minds how to code, because it teaches them how to think. Today with YouTube, and social media garbage our youth are losing the ability to think on their own and solve problems. I believe this is a serious epidemic as kids today dont understand that technology is a tool. This tool is being abused, and its underlying effects are taking its toll on kids behaviour, and learning.