In Python, different logical operators such as “And”, “Or”, “Not“, etc. are used for various purposes. The “Not” operator is normally used in all types of programs to control the flow of a program. In Python, the “Not” operator is used along with the “if” statement. The “if-not” statement is used in the program to show the effect of something. The only difference between the normal “if” statement and the “if not” statement is that the “If Not” statement executes when the condition is not satisfied. This blog post will present a detailed Python guide for using the If Not statement in Python. In this article, the below-listed aspect is explained:
- What is Python if Not Statement?
- Example 1: Using Python If Not to Print String and List
- Example 2: Using Python If Not To Check the Correct Number
- Example 3: Using Python If Not For Checking Elements of List
So, let’s get started!
What is Python if Not Statement?
The “if Not” statement executes the statement of codes when the condition is false. The “if not” statement executes any data type structure of Python such as list, strings, set, dictionary, etc. The syntax of the “if not” statement is shown below:
if not value:
statement(s)
The statement inside the “if not” block executes only when the “not” condition is “false”.
Example 1: Using Python If Not For Printing String and List
“If Not”is executed on any value, including list, tuple, dictionary, string, etc. This statement is used to check the empty value of the string and list in the code given below.
Code
string_value = '15'
list_value = []
if not string_value:
print('String is empty.')
elif not list_value:
print('List is empty.')
else:
print(list_value)
print(string_value)
In the above code:
- The string variable named “string_value” and list variable named “list_value” is initialized.
- The “if not” is used to check the variable value of the string, and if the value is false or not equal to the condition, then the “not” operator value will be “True”.
- “Elif” with “not” checks the value of the list variable named “list_value”, and if the list value is empty, then the print statement runs.
- If both of the “not operator” conditions are not satisfied, then the else statement executes and prints the original value of both variables.
Output
In the above output, the “elif not” statement executes and prints the result “List is empty”.
Example 2: Using Python If Not to Check the Correct Number
The “if Not” statement is also used to check the correct number or compare the numbers. Let’s check the number using “if Not” statement by the example of code given below:
Code
number_value = 92
if not number_value > 39:
print('number is greater than 39')
else:
print('number is not greater than 39')
In the above example:
- The variable named “number_value” is initialized.
- The “If expression along with Not operator” checks the number value of a variable. if the number is greater than “39” then the “not” operator condition fails, and it executes the “else” block.
- If the number value is smaller than “39”, then the “not” operator condition becomes “True”, and it prints the statement that lies within the if block.
Output
As the input value was greater than 39. Therefore, a statement inside the “else” block is printed.
Example 3: Using Python If Not For Checking Elements of List
The “if Not” statement is also used to check the element’s value of the List. In the example given below, we are going to check the number of values present in the elements of the collection.
Code
list_value = [1992,1932,1332,1562,1672]
if not 1947 in list_value:
print("Every Number is Present except: 1947")
else:
print("Number 1947 is also Present ")
In the above code:
- The list is initialized in a variable named “list_value”.
- The “if not” statement checks whether the number “1947” is present in the collection of lists or not. If the number “1947” is not present in the list, then the “if” statement body code executes.
- If the number “1947” is present in the list element, then the else block statement will execute.
Output
The output shows that the number value “1947” is not present in the list.
That’s all from this Python guide!
Conclusion
In Python, the “If Not” statement is used to execute the statements of a program when the condition is “True”. The “If Not” statement is used to check any empty iterable of different data structures. The “If Not” statement inverts the normal logic. So while programming, we need to be careful while defining the condition of “If Not”. This post has briefly explained the working and practical usage of the Python If Not Statement with appropriate examples.
TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY