Reserved Keywords In Python- Part 4

Reserved keywords have been discussed in this series of tutorials, and said that is a built-in keyword used to extend the python language functionality. You shouldn’t use these reserved keywords in your python code as variable names. We will complete discussing these keywords more in this tutorial.

1. Lambda Keyword

This lambda keyword is also called the lambda function, used to create an anonymous function. The lambda function has no name like the other function you create using the dek keyword and without the return statement:

# Create a Lambda Function
x = lambda a, b: a + b

# Print The Result
print(x(2, 3))

You will first assign the lambda function to a variable x and use the lambda keyword to create the function and specify its inputs, a and b.

In the above example, you will also specify the function’s operation on these inputs, a + b. Finally, call the function using the variable assigned to it, x, and specify the inputs values inside the parenthesis.

2. Return Keyword

The return keyword is used when you create a function in your python code, and you want this function to return a value. Sometimes, you don’t need your function to return a value, such as only used for printing a statement, so you don’t need to use it. Let’s see an example:

# Create a Function
def mul(a, b):
    # Return The Value
    return a * b

# Calling The Function
mul(2, 5)

When you execute the code above, you will get the value 10 because we tell the python function to return the multiplication of 2 times 5. Let’s see an example where you don’t need to return any value:

# Create a Function
def gretting(name):
    # Print a Message
    print("Hello,", name)

# Calling The Function
gretting("John")

You will only need to pass the name inside the parenthesis when you call the function, and it will print the statement.

3. Finally Keyword

This finally keyword is used with the try/except keywords, and it will always be executed wherever the try raised an error or not. It doesn’t also matter if it executed the code block inside the try or the except keywords. Let’s see an example:

a = 5
try:
    # Logical Operation
    a > 3
    # Print a Message
    print("The a value is bigger than 3")
except:
    # Print a Message
    print("The above answer is wrong")
finally:
    # Print a Message
    print("The try/except execution has been finished")

It will execute the code inside the try keyword since it is the right one, and then it will run the code inside the finally keyword.

4. Global Keyword

The global keyword allows you to change the value of a variable outside the current scope. When you create a variable, it is global by default, meaning you can access it everywhere inside your code. When you add a variable inside a function, it is local by default. The functions can access any variable outside it, but it can’t modify it. Let’s see this example:

# Define a Variable
a = 20

# Define a Function
def mul():
    a = a * 2
    print(a)

# Calling The Function
mul()

When you execute this previous code, you will get an UnboundLocalError error because you can’t change a variable you’ve declared outside a function inside the function. Let’s fix this issue by using the global keyword:

# Define a Variable
a = 20

# Define a Function
def mul():
    global a
    a = a * 2
    print(a)

# Calling The Function
mul()

When we’ve used the keyword global and specified the variable a to it inside the function, it means we are using the a variable we’ve declared before, and we want to be able to change it inside the function.

5. Nonlocal Keyword

Sometimes you need to use nested functions, which is a function inside a function in your code. Both functions have their variables, and if you want to create a variable inside the inner function but does not belong to it, you should use the nonlocal keyword. Let’s see an example:

# Outside Function
def out_func():
    a = 4
    # Inside Function
    def in_func():
        nonlocal a
        a = 20
        print("Inside function: ",a)
    in_func()
    print("Outside function: ",a)

# Calling The Function
out_func()

The variable a is created first at the out_func() function (Not global), but if you want to modify it inside the in_func() function, you need to use the nonlocal keyword. When you execute the code, you will see that the value of the a has been changed to 20 after it was 4 the first time it was created.

6. Raise Keyword

This raise keyword is used to raise an exception or an error and stops the control flow of your program. You can also provide a message to tell the user about this error. Let’s see a simple example to understand:

# Define a Variable
a = 20

# Verify a Condition
if a < 30:
    # Raise an Exception
    raise Exception("The value a is smaller than 30")

# Print The a Variable
print(a)

We make use of the if statement to check whether the value of a is smaller than 30 or not. Since the value of a is smaller than 30, it will raise the exception and print that message for us. It didn’t run the last line of the code, which is printing the value, because the program stopped when the error was raised.

Conclusion

Thanks for reading! We’ve discussed a lot of the reserved keywords in python in this series of tutorials. But there is more than we’ve seen. You can discover the other ones when you are using this python language when you code complex programs, but we will stop in this part.

Scroll to Top