How to check if multiple variables are not None in Python

How to check if multiple variables are not None in Python

In this post we will give you information about How to check if multiple variables are not None in Python. Hear we will give you detail about How to check if multiple variables are not None in PythonAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to check if a multiple variables are not none in Python with the help of examples.

Note: Not None means there is some data stored inside the variables.

Consider, that we have the following variables in our code:

x = 1y = 2z = 3

Now, we need to check the above variables are not None.

Checking if multiple variables are not None

To check if multiple variables are not None, we can use the built-in all() function in Python.

The all() function returns True if all items in the iterable are true. Otherwise, it returns false.

Here is an example:

x = 1y = 2z = 3if all(item is not None for item in [x, y, z]):    print('Multiple variables are not None')else:    print('Some of the variables are None')

Output:

'Multiple variables are not None'

In the above code,

  1. We have first added the variables into the list.

  2. Then iterated over the list using in operator.

  3. On each iteration, we have checked if the item is not None.

Finally, the all() function returns True if the all items in the list returns true, otherwise false is returned.

If it returns True then it should prints the Multiple variables are not None, if at least one of the variables is None then it returns False and prints Some of the variables are None.

Example 2: Some of the variables are none

x = 1y = 2z = Noneif all(item is not None for item in [x, y, z]):    print('Multiple variables are not None')else:    print('Some of the variables are None')

Output:

'Some of the variables are None'

In the above code, it prints “Some of the variables are None” because the variable z is assigned to None.

Hope this code and post will helped you for implement How to check if multiple variables are not None in Python. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US