Reserved Keywords In Python- Part 1

Reserved keywords are unique words in python used to perform specific actions such as verifying a condition or creating a function and much more. This article will help you learn these reserved keywords in python and how you can use them with some examples in every reserved keyword.

1. False Keyword

Who doesn’t know about this keyword in python language or any other programming language? This False word results from the wrong comparison of operations. Let’s see an example:

# False Examples
print(1 > 44)
print(22 < 1)
print(11 == 22)
print(34 in [22, 44, 21])
print(34 not in [22, 34, 21])
print("Thanks" == "Welcome")
print("Thanks" is "Welcome")
print("Thanks" is not "Thanks")

You will get the False result for all of these commands since they are logically wrong. You can use this keyword with many other keywords, such as the if keyword. It is not just a logical return for the incorrect operation.

2. True Keyword

This True keyword is the opposite of the previous one, False, and you will get this result if the operation is true or correct. Let’s make some changes to the previous example and rerun the code:

# True Examples
print(1 < 44)
print(22 > 1)
print(11 != 22)
print(34 not in [22, 44, 21])
print(34 in [22, 34, 21])
print("Thanks" != "Welcome")
print("Thanks" is not "Welcome")
print("Thanks" is "Thanks")

Rerun this code, and you will get the result True for all of these commands because they are correct now after making these changes. You can also use this keyword in your python code, like making an infinite loop with the while keyword.

3. While Keyword

I think all python developers know what this reserved keyword is using. This while keyword will keep looping whenever the condition is True, but it will stop if it is False or you’ve used the break keyword:

# While Loop
while True:
    # Print a Message
    print("Thank you")

The while loop is given the True boolean evaluation, which means it won’t stop forever until you stop your python code in your IDE or use the CTRL + C in your terminal or CMD.

4. Break Keyword

This keyword, known as the break, is used a lot with the while loop to stop it if a condition happens, but it is used to stop also the for loop. Let’s see an example of this break keyword:

# For Loop
for i in range(10):
    # Verify Condition
    if i > 6:
        break
    # Print a Message
    print("Thank you")

It will print the word “Thank you” 7 times before the condition is met in the if keyword, and it will execute the break statement and stop the program. Let’s use it also for the while loop:

i = 0
# While Loop
while i < 10:
    # Verify Condition
    if i > 6:
        break
    # Print a Message
    print("Thank you")
    # Increase The i Value
    i += 1

It will keep looping until the condition “i < 10” is False, but before that, it will verify the condition inside the if keyword, and if it is 7, meaning bigger than 6, it will break the while loop.

5. If Keyword

This if keyword is used to verify if a condition is met so it will execute the code inside it, or it will ignore it if the condition is False. Let’s see an example:

x = 5
# Verify Condition
if x > 4:
    # Print a Message
    print("x is bigger than 4")

Since the condition “x > 4” in the if keyword is True, it will execute the whole block code inside it. If this condition is False, it will ignore that block of code and complete the rest of your python program.

6. Else Keyword

This else keyword is always used with the if keyword. When the evaluation side the if is False, it will execute the block of code inside the else keyword. Let us see an example:

x = 5
# Verify Condition
if x > 6:
    # Print a Message
    print("x is bigger than 6")
else:
    # Print a Message
    print("x is smaller than 6")

The condition now in the if keyword is False, which means it will ignore the code inside this if and execute the code inside the else keyword.

7. Elif Keyword

This elif keyword is always used with the if & else keywords when there are many conditions to evaluate and execute a specific code when a condition is met. Let us see an example:

x = 6
# Verify Condition
if x > 6:
    # Print a Message
    print("x is bigger than 6")
elif x == 6:
    # Print a Message
    print("x is equal to 6")
else:
    # Print a Message
    print("x is smaller than 6")

The condition is met in the elif keyword so that it will execute that block of code. You can use elif as many times as you need in your code. This elif keyword is a combination of the else & if keywords.

8. None Keyword

The None keyword is anything that doesn’t have a value or the absence of a value or a null value. It is not considered a 0 or an empty string or the False value, but None is None. Let’s see an example:

print(None)

In many cases, when you are running a python code, you will get the word None when there is no value to print or return.

Conclusion

Thanks for reading! These keywords are used a lot among python developers since most python codes need them. But we will explore many of the reserved keywords that are not used a lot, and they can be helpful in many situations.

Scroll to Top