Python Keywords with examples

By: On:

In this python tutorial, you will learn about what are Python Keywords with a few examples. There are various python reserved keywords that can be used in Python. Here we will check:

  • What are Keywords in Python
  • class keyword in python
  • del keyword in python
  • def keyword in python
  • False keyword in python
  • True keyword in python
  • break keyword in python
  • continue keyword in python
  • if keyword in python
  • else keyword in python
  • return keyword in python
  • import keyword in python
  • lambda keyword in python
  • None keyword in python
  • Try keyword in python
  • Pass keyword in python
  • in keyword in python

What are Keywords in Python

In python, keywords are reserved words that cannot be used as a variable name, identifier, or any function name.


List of keywords in python

classdeldeffalse
truebreakcontinueif
elsereturnimportlambda
nonetrypassin
List of keywords

class keyword in python

In python, class keyword is used to create a class and a class is like a user defined objects.

Example:

class Student:
name = "Jack"
roll = 21
print(Student.name)

After writing the above code (class keyword in python), Ones you will print “ Student.name ” then the output will appear as a “ Jack “. Here, my class name is ” Student “.


You can refer to the below screenshot for python class keyword

Class keyword in python
Class keyword in python

del keyword in python

In python, del keyword is used to delete the particular item from the list or variable etc.

Example:

value = ["A", "B"]
del value[0]
print(value)

After writing the above code (del keyword in python), Ones you will print “ value ” then the output will appear as a “ [‘B’] “. Here, the index [0] item is deleted from the list and it return the rest items from the list.

You can refer to the below screenshot for python del keyword

del keyword in python
del keyword in python

def keyword in python

In python, the def keyword is used to create a new user defined function.

Example:

def function1():
print("Welcome to function in python")
function1()

After writing the above code (def keyword in python), Ones you will print then the output will appear as a “ Welcome to function in python “. Here, we define the function and then the function is called.

You can refer to the below screenshot for python def keyword

def keyword in python
def keyword in python

False keyword in python

In python, the false keyword is the boolean value and false keyword is also represented as zero which means nothing.

Example:

print(10<3)

After writing the above code (false keyword in python), Ones you will print then the output will appear as a “ false “. Here, we used the comparison operator to check whether 3 is larger than 10, so it returns false.

You can refer to the below screenshot for false keyword in python

False keyword in python
False keyword in python

True keyword in python

In python, the true keyword is the boolean value and true keyword is represented as one.

Example:

print(10>3)

After writing the above code (true keyword in python), Ones you will print then the output will appear as a “ true “. Here, we used the comparison operator to check whether 10 is larger than 3, so it returns true.

You can refer to the below screenshot for true keyword in python

True keyword in python
True keyword in python

break keyword in python

In python, the break keyword is used to terminate the current iteration of the loop.

Example:

for value in range(5):
if value > 2:
break
print(value)

After writing the above code (break keyword in python), Ones you will print ” value ” then the output will appear as a “ 0 1 2 “. Here, we use the break keyword to break out of the loop.

You can refer to the below screenshot for break keyword in python.

break keyword in python
break keyword in python

continue keyword in python

In python, the continue keyword is used to end the current iteration of for loop and continue on with the next iteration.

Example:

for value in range(6):
if value == 4:
continue
print(value)

After writing the above code (continue keyword in python), Ones you will print ” value ” then the output will appear as a “ 0 1 2 3 5 “. Here, we use the continue keyword loop does not terminate but continue with the next iteration.

You can refer to the below screenshot for continue keyword in python.

continue keyword in python
continue keyword in python

if keyword in python

In python, if keyword is used to define the conditional statement and it executes the block of code if the condition is true.

Example:

value = 4
if value > 2:
print("Yes value is greater")

After writing the above code (if keyword in python), Ones you will print ” value ” then the output will appear as a “ Yes value is greater “. Here, we use the if keyword to check if a certain condition is satisfied.

You can refer to the below screenshot for if keyword in python.

if keyword in python
if keyword in python

else keyword in python

In python, else keyword is used as a conditional statements it decide what to do if the condition is false.

Example:

value = 2
if value > 4:
print("Yes value is greater")
else:
print("No value is not greater")

After writing the above code (else keyword in python), Ones you will print then the output will appear as a “ No value is not greater “. Here, we use the else keyword if the condition gets false.

You can refer to the below screenshot for else keyword in python.

else keyword in python
else keyword in python

return keyword in python

In python, return keyword is used to return the value from the calling function.

Example:

def my_fun():
return 5+7
print(my_fun())

After writing the above code (return keyword in python), Ones you will print ” my_fun() “ then the output will appear as a “ 12 “. Here, the return keyword will exit the function and return the value.

You can refer to the below screenshot for return keyword in python.

return keyword in python
return keyword in python

import keyword in python

In python, the import keyword is used to import the datetime module, and then it will display the current date and time.

Example:

import datetime
a = datetime.datetime.now()
print(a)

After writing the above code (import keyword in python), Ones you will print ” a “ then the output will appear as a “ 2020-08-28 17:30:26.590678 “. Here, the import keyword is used to import a module in python.

You can refer to the below screenshot for import keyword in python.

import keyword in python
import keyword in python

lambda keyword in python

In python, lambda keyword can take any number of arguments but only one expression and it creates a small anonymous function.

Example:

s = lambda x : x + 20
print(s(5))

After writing the above code (lambda keyword in python), Ones you will print ” s(5) “ then the output will appear as a “ 25 “. Here, the lambda keyword will evaluate the expression and it will return the result.

You can refer to the below screenshot for lambda keyword in python.

lambda keyword in python
lambda keyword in python

None keyword in python

In python, none keyword is the object of its own datatype and it is used to define a null value which means no value.

a = None 
print(a)

After writing the above code (none keyword in python), Ones you will print ” a “ then the output will appear as a “ None “. Here, the none keyword is used to define the absence of a value and it can be assigned to variables.

You can refer to the below screenshot for none keyword in python.

None keyword in python
None keyword in python

Try keyword in python

In python, the try keyword is used for error and exception handling and the try block executes if nothing went wrong.

try:
x > 10
except:
print("Something is wrong")

After writing the above code (Try keyword in python), Ones you will print then the output will appear as a “ Something is wrong “. Here, the try block will not execute as an exception occurred so it raised an error.

You can refer to the below screenshot for try keyword in python.

Try keyword in python
Try keyword in python

Pass keyword in python

In python, the pass keyword is a dummy placeholder, when the pass keyword is executed nothing happens and it avoids an error when the empty code is not allowed.

for a in [10, 11, 12]:
pass

After writing the above code (pass keyword in python), Ones you will run the code then nothing is displayed in the output. Here, the pass keyword is used as a dummy placeholder and it can be seen as a null statement.

You can refer to the below screenshot for pass keyword in python.

Pass keyword in python
Pass keyword in python

in keyword in python

In python, in keyword is used to iterate through a for loop and is also used to check if the value is present in the list, string, etc.

flower = ["rose", "sunflowers", "tulips"]
if "tulips" in flower:
print("yes")

After writing the above code (in keyword in python), Ones you will print then the output will appear as a “ yes “. Here, the in keyword is used to check that the value is present or not.

You can refer to the below screenshot for in keyword in python.

in keyword in python
in keyword in python

You may like the following Python tutorials:

In this Python tutorial, we learned about various python keywords present in python like:

  • class keyword in python
  • del keyword in python
  • def keyword in python
  • False keyword in python
  • True keyword in python
  • break keyword in python
  • continue keyword in python
  • if keyword in python
  • else keyword in python
  • return keyword in python
  • import keyword in python
  • lambda keyword in python
  • None keyword in python
  • Try keyword in python
  • Pass keyword in python
  • in keyword in python

Leave a Comment