Home > Mobile >  Is there Empty function in Python, as in PHP?
Is there Empty function in Python, as in PHP?

Time:05-07

How to check whether the variable is equivalent to zero or does not exist at all in one step?

CodePudding user response:

You can check it in if statement

if var is not None or (var == 0)

Shorter:

if var or var==0

CodePudding user response:

So when you use function and variable interchangeably in Python it can get confusing since we have both variables and we also have user defined functions or methods.

The first example is a function, and for a variable it's at the bottom.

So to answer the first question --

Is there an empty function in python?

Yes! We use the pass statement in python, which makes it do absolutely nothing.

Here is some syntax:

def emptyfunc():
    pass

Checking if it's equal to nothing can be done in several ways,

-If Statement You can use an if statement to check if something is True or False.

With an if statement there is also an elif, and else statement.

Elif Statement means that if it was not True the first time, it runs the second statement to see if it's true.

Else is the failsafe, if after all of it, it still turns out to be false it will run the else statement.

Syntax:

emptyvar = None
if emptyvar == None:
    print('This variable has no value, is equal to None')
elif emptyvar == 0:
    print('This is equal to 0, and has a value')
else:
    print('just an example of else')

Python also has the 'raise' keyword, which in fact lets you raise errors to stop something from happening or to let you know.

However, I'm not a novel writer and someone else answered it while I was typing it so here is some information for ya!

CodePudding user response:

There is no function in the python, similar to the function Empty in PHP.

  • Related