Reserved Keywords In Python- Part 2

As we said in the previous part of this series, reserved keywords are unique keywords used to make a specific task. These reserved keywords are built-in inside the python language, and you shouldn’t use them as a variable name, for example. Enough talking, and let’s jump inside this tutorial

1. Def Keyword

If you are an intermediate level in the python language, you probably know what this keyword is used for. It is used to create a function that will make you use the same block of code by calling it instead of making the same block of code every time you need it. Let us see an example:

# Create a Function
def hello():
    # Print a Message
    print("Hello Guys")

# Calling The Function
hello()

The first thing is to use the def keyword and specify the function name, which is hello in this example. Later you specify the code you want to execute when you call the function, and I want it to print a small phrase to understand how it works. Finally, call the function when you need it, and it will automatically execute the code inside it.

2. Del Keyword

This del keyword is the abbreviation of the delete word. It is used to delete an object in python. These objects can be variables, lists, classes, and anything in python is an object. Let’s see an example:

x = 55

# Print The x Value
print(x)

# Delete The x Value
del x

# Print The x Value
print(x)

The first print will print the x value, but the second will raise the NameError error because the x variable has been deleted using the del keyword. Let’s try to delete an element inside a list:

# Create a List
x = [1, 2, 3, 4]

# Delete The Second Element
del x[1]

# Print The List
print(x)

When you print the list in the last line of this code, the second value is missing. We’ve accessed the second element using indexing and applied the del keyword to it. You can delete anything with this del keyword.

3. Import Keyword

All python developers have used this keyword at least in one of their codes. This import keyword is used to import modules (libraries) inside their code. Let’s see an example:

# Import The Module
import math

# Print The Pi
math.pi

We’ve used the import keyword to import the math module, and we use it later to print the pi number (value) using the math.pi function.

4. From Keyword

The from keyword is used to import a specific section, attribute, or function from a module. Let’s see a simple example:

from math import sqrt

In this example, we’ve used the from keyword to import the sqrt function from the math module. Sometimes you need only an attribute from the whole module, so you will use the from keyword to import it.

5. As Keyword

Most of you had used this keyword to make an alias of a module when you imported it. If the module name is long, we use it to make it shorter so it will be easy to use it inside the code. Let’s see an example:

# Import The Module
import math as m

# Print The Pi
m.pi

We’ve imported the built-in python module (library) called math as an alias of the character m. So whenever you want to use the math module, you need to use the character m. Finally, print the pi number using the m.pi function.

6. Try/Except keyword

Sometimes when you run the code, and something happens in the program, like the code didn’t find a specific variable, or you are going to delete a file, and the file does not exist or anything related to this, it will raise an error. This error will stop the whole program, and you don’t want this. There is a solution for this problem: using the try/except keywords. Let’s see an example:

import os

# File Path
file_path = "/home/abdelhakim/Example/File.txt"

try:
    # Remove File
    os.remove(file_path)
    
    # Print a successful message
    print("File has been deleted successfully")

except:
    # Print an Error Message
    print("The file does not exist")

The code is trying to remove a file from a path. First, it will try the code inside the try keyword. If the command doesn’t execute because the file does not exist, Python will raise an error, stopping the whole program. We’ve used the except keyword that will run the code inside it instead and print that message.

7. In Keyword

This in keyword is used for two things. The first thing is to check whether an element is inside a list, range of numbers, string, etc. Let’s see an example of this:

# Create a List
List = ["Germany", "USA", "Canada"]

# Check The Value Availability
if "Germany" in List:
    # Print a Message
    print("Germany inside the List")

It will print the message inside the if keyword since the word “Germany” is inside the List. Second, it is used to iterate over the list. See this example:

# Create a List
List = ["Germany", "USA", "Canada"]

# Iterate Over a List
for i in List:
    # Print The Values
    print(i)

This time, we’ve looped over the list using the for loop with the help of the in keyword, and it will print the values inside the List on the screen.

Conclusion

Thanks for reading! This article has shown some of the most used reserved keywords in python language that you will need to use in almost all of your python code. In the following part of this tutorial series, we will complete learning more about the reserved keywords in python.

Scroll to Top