Reserved Keywords In Python- Part 3

Reserved keywords have been discussed in the previous parts of this series of tutorials. They are some built-in keywords that you can use to extend the functionality of your python code, and you shouldn’t use them as a variable name in your python code.

1. And Keyword

The and keyword is used for logical operation and for combining conditional statements. If both statements are True, they will return True. Otherwise, it will return False even if one of them is False. Let’s see an example:

# Create Two Statements
first = (2 > 1 and 55 < 77)
second = (2 < 1 and 55 < 77)

# Print The Result
print(first)
print(second)

First, we’ve created two statements, and the first one will return True since the two conditional statements are True. The second one will return False since one of them is False. Let’s also use the and keyword with the if statement:

# Verify The Condition
if 2 > 1 and 55 < 77:
    # Print a Message
    print("Both of the statements are True")

else:
    # Print a Message
    print("One of these two statements or both of them are False")

It will execute the first block of code inside the if statement since both conditions are True, returning True. Make one of these conditions False, and it will run the second block of code inside the else statement.

2. Or Keyword

This or keyword is also like the and keyword used for logical operation. This or keyword will return True if only one of the statements is True. Otherwise, it will return False if all of the statements are False. Let’s see an example:

# Create Two Statements
first = (2 < 1 or 55 < 77)
second = (2 < 1 or 55 > 77)

# Print The Result
print(first)
print(second)

The first result will be True since only one of the two comparisons is False. The second one will return False since both the conditions are False.

3. Not Keyword

This not keyword is used to reverse the result of a logical comparison. If the result is True of a logical comparison, it will make it False. Also, if it is False, it will be True. Let’s see an example:

# True Value
x = True

# Print The Result
print(x)
print(not x)

We’ve created a True value inside the x variable, and when we printed it out the first time, it gave you the True result. But when you use the not keyword, it will become False.

4. Assert Keyword

This assert keyword is used for debugging purposes. You should provide a condition for this keyword, and if the condition returns True, then nothing will happen. Otherwise, it will raise an AssertionError error message. Let’s see an example:

x = 6

# Verify The Condition
assert x < 5

You will get an AssertionError error message since the condition is False. You can optionally provide a message to be printed on the screen once the condition is False. Let’s see an example:

x = 6

# Verify The Condition
assert x < 5, "x must be greater than 5"

Since the condition is False, it will raise the error and show you the message you have specified after making the assert keyword and condition.

5. Class Keyword

The class keyword is always used to create a class in python, which is like an object instructor. You can consider a class as a code template for creating objects. Let’s see an example of creating a class and call it also:

class Employee:
    Name = "John"
    Salary = 100000
    
E1 = Employee()
print(E1.Salary)

First, you used the class keyword to create a class, followed by the class name. You need to specify the variables the class will have, which are the Name & Salary in this example.

When you want to use it, you need to create an object out of this class which is E1 in this case. You can access these variables inside the class like you see when we accessed the employee salary E1.Salary inside the print function.

6. Continue Keyword

This continue keyword returns the control to the beginning of your loop. Either you are using the for loop or the while loop. This keyword is used to stop the current iteration and continue to the next one. Let’s see this example:

# For Loop
for x in range(5):
    # Verify a Condition
    if x == 3:
        continue
    # Print The Value x
    print(x)

In this example, we will loop over five values starting from 0 to 4, and when the loop is in the value three, it will execute the continue keyword and return to the beginning of the loop instead of completing and printing the value 3.

7. Pass Keyword

This pass keyword is often used when you watch Youtube videos trying to explain something but then write the keyword pass and get back later to implement the code. 

Nothing happens when you execute the pass keyword because it is a null statement. You can use this keyword instead of leaving the code empty, so you get an error. Let’s see an example:

# For Loop
for x in ["Germany", "UK", "USA"]:
    pass

When you run the code, nothing will happen, but the pass keyword avoids making python give you an error for this. Also, you can use it with many other things, like using with functions and classes and anything to avoid giving you errors.

Conclusion

Thanks for reading! We’ve discussed many reserved keywords available in python but still many other reserved keywords built-in we didn’t see even we are in the third part of this article. We will complete our series of reserved keywords in the following tutorial.

Scroll to Top