Conditional Execution with if not in Python
rajneesh
4 min read
- python
are you looking for why we use not logical operator with if statement as well as how to use if not in python. then you are at right post i will explain everything about if not statement with examples in very easy manner. for this concept i gain Knowledge from Fluent Python book i will explain every point in very easy way by which you can easily understand this topic.
introduction
Conditional execution is a fundamental concept in programming it allow us to control the flow of our code based on the certain condition. in python or any other language we mostly use if statement as compare to if not conditional execution.
in this post we can see the power of if not statement.
In this post, we’ll explore how to use if not effectively, its syntax, and practical examples which help you in easy learning. Let’s dive in!
What is if not
?
The if not
statement is used to check whether a condition is not true. if condition is not true then the "if not" block is executed. it most use to invert the condition for example over condition is false it invert the value false become the true and true become false that we when condition is false code block is executed.
Syntax
The syntax of if not statement is:
if not condition:
# Code execute when condition is false
# ...
heir, condition
represents if expression that evaluates and gives True
or False
. If condition
is False
, then the code block is executed.
Examples
let see some practical examples of if not expression for better understanding of if not expression.
Example 1: if not with Boolean
suppose then you are working on a website and check if is Authenticated then print "you are authenticated" else print "you are not authenticated"
auth = False
if not auth:
print("you are not authenticated")
else:
print("you are authenticated")
# Output: you are not authenticated
In this example, the code inside the
if
block execution because the value of auth is False.Example 2: if not with string
You can use the
if not
statement to check whether the string is empty or not for Example:
string = ""
if not string:
print("your provided String is empty")
else:
print("your provided String is not empty")
# Output: your provided String is empty
Here, conditional string is empty it means it False otherwise its True and we know that by if not statement true become false and false become true that why if not block executed.
Example 3: Checking for an Empty List
Suppose we want to print a message if a list is not empty:
my_list = [10,11,99,63,45]
if not my_list:
print("The list is empty.")
else:
print(f"The list contains {len(my_list)} elements.")
In this example, if list is empty the if not block executed otherwise else block executed because empty list means is False.
Example 4: Validating User Input
suppose we ask user for the name we want to ensure then they provide a non-empty string:
user_input = input("Enter your name: ")
if not user_input:
print("Please provide a valid name.")
else:
print(f"Hello, {user_input}!")
Here, if your input is null or empty they we print please provide a valid name because if input is empty then user_input is empty and empty string equal to False that's why print this.
Example 5: Checking for File Existence
Suppose we want to check whether a file exists or not before performing action on file:
import os
file_path = "my_file.txt"
if not os.path.exists(file_path):
print(f"File '{file_path}' does not exist.")
else:
print(f"File '{file_path}' exists.")
In this case, os.path.exists(file_path)
method use to check whether the file exist or not if file not exist its print file does not exist.
Conclusion
By using it effectively, you can write cleaner and more expressive code. and it also help you to clearly understand the logical operation that`s help you allot in programming life. i have cavered most of the examples related to this topic if you getting any problem you can comment l will also add that example and topic to make to post useful for who is fresher in programming.