Python Keywords
Keywords are the reserved words in python programming dedicated for a certain type of task or action. A Keyword cannot be used as a variable or a constant.
Python Keywords:
Keyword | Description | Example |
---|---|---|
FALSE | Represents the boolean value False. | is_valid = False |
None | Represents a null or no value. | result = None |
TRUE | Represents the boolean value True. | is_valid = True |
and | Logical AND operator. Used to combine conditional statements. It checks two condition and if both conditions are true then it returns true | if x > 5 and x < 10: |
as | Used to create an alias or short form for a module or exception to avoid repeatedly using the big name of a module. | import math as m |
break | Exits the immediate enclosing loop. | while True: break |
class | Keyword used to define a new class. | class Dog: |
continue | Skips the ongoing iteration and starts the next iteration. | for i in range(10): continue |
def | Keyword used to define a new function. | def my_function(): |
elif | Equivalent keyword for else if in C#. used in if else ladder | if x > 5: elif x < 3: |
else | Defines the alternative branch in a conditional statement. | if x > 5: else: |
except | Catches exceptions in a try block. | try: except ValueError: |
finally | Executes code after a try block, regardless of exceptions. | try: finally: |
for | Keyword used to define a for loop. | for i in range(5): |
from | Used to import specific parts from a module. | from math import sqrt |
global | Declares a global variable inside a function. | global count |
if | Keyword used to define a (if) conditional statement. | if x == 5: |
import | Imports a module. | import os |
in | Checks if a value exists in a sequence. | if x in list: |
lambda | Defines an anonymous function. | lambda x: x + 2 |
not | Logical NOT operator. Inverts a boolean value. | if not x: |
or | Logical OR operator. Used to combine conditional statements. | if x > 5 or x < 2: |
pass | A null statement; does nothing. | def func(): pass |
raise | Raises an exception. Used in exception handling | raise ValueError(“Error!”) |
return | Exits a function and optionally returns a value. | return x |
try | Defines a block of code to be tested for exceptions. | try: |
while | Keyword to define a while loop. | while x < 5: |
You cannot use any of the above keywords as a name of variable or a constant. This will raise an error if you do so.