Python Keywords

Python Keywords

These are reserved keywords in Python and have defined meanings and purposes. These keywords define the syntax and structure of a program. They are reserved for a specific meaning and, thus, cannot be used for naming variables, functions, or identifiers. Assigning a particular meaning to Python keywords means you can’t use them for other purposes in our code. You’ll get a message of SyntaxError if you attempt to do the same. If you attempt to assign anything to a built-in method or type, you will not receive a SyntaxError message; however, it is still not a smart idea.

The following are a few common keywords in Python explained very simply:

1. and

  • Logical operator used to combine two conditions.
  • Example:

     x = 5
     y = 10
     if x > 2 and y > 5:
        print("Both conditions are True")

2. or

  • Logical operator that verifies whether any of the conditions are True.
  • Example:

    x = 5
    y = 10
    if x > 6 or y > 5:
       print("At least one condition is True")

3. if, elif, else

  • Used for conditional statements to make decisions.
  • Example:

    x = 10
    if x > 10:
       print("Greater than 10")
    elif x == 10:
       print("Equal to 10")
    else:
       print("Less than 10")

4. for

  • Used to create a loop that iterates over a sequence (like a list, string, etc).
  • Example:

    for i in range(5):
       print(i)

5. not

  • Logical operator that reverses the condition (True becomes False, False becomes True).
  • Example:


    x = True
       print(not x) # Outputs: False

6. while

  • Used to create a loop that continues as long as a condition is True.
  • Example:

    x = 0
    while x < 5:
       print(x)
       x += 1

7. def

  • Used to define a function.
  • Example:

    def greet():
       print("Hello, World!")

    greet()

8. return

  • Used to return a value from a function.
  • Example:

    def add(a, b):
       return a + b

   result = add(3, 5)
    print(result)  # Outputs: 8

9. class

  • Used to specify a class (used in Object-Oriented Programming).
  • Example:

class Person:
    def __init__(self, name):
      self.name = name

    def greet(self):
      print(f"Hello, my name is {self.name}")

p = Person("Alice")
p.greet()

10. try, except

  • Used for exception handling to catch errors in code.
  • Example:

try:
   x = 10 / 0
except ZeroDivisionError:
   print("Cannot divide by zero!")

11. import

  • Imported modules are used for importing reusable code libraries.
  • Example:

import math
print(math.sqrt(16))  # Outputs: 4.0

12. True, False

  • Boolean values for truth or falsehood.
  • Example:


x = True
y = False
print(x and y)   # Outputs: False

Above are the some of the most frequent keywords for beginners. More have been added to Python recently, but you may prioritize some as you learn it best. You can check every keyword in your version of Python with this command.


import keyword
print(keyword.kwlist)

Leave a Reply

Your email address will not be published. Required fields are marked *