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.
Course Video
Course Video English:
YouTube Reference :
Use the keyword module in Python. For example, import keyword; print(keyword.kwlist) displays a list of all Python keywords.
No, type is not a keyword; it is a built-in function used to check the type of an object or create new types dynamically.
No, list is not a keyword. It is a built-in type used to create lists, such as my_list = [1, 2, 3].
Use the in keyword to check if a key exists in a dictionary, e.g., ‘key’ in my_dict.
Use len(keyword.kwlist) to get the total number of keywords in Python.
Python has 35 keywords (in Python 3.x), including if, else, def, and class.
Python is widely used by millions of developers worldwide due to its simplicity and versatility.
Java is generally faster than Python because it is a compiled language, whereas Python is interpreted.
Reserved words are Python keywords like if, for, class, and try, which have special meanings in the language syntax.
print is not a keyword but a built-in function used to display output in Python.