Python Keywords - List of all Keywords in Python Programming Language
Keywords in Python
The latest version of Python is 3.9. Python 3.9 has a set of keywords that are reserved words that cannot be used as variable names, function names, or identifiers. In other words Python keywords are special reserved words that have specific meanings and purpose.
Python Keywords are used to define the syntax and structure of the Python programming language. Keywords in Python are case sensitive. They must be spelled exactly as it is. All the keywords in Python are lowercase letters except these 3 keywords (True, False and None).
What is a Python Keyword ?
A python keyword is a reserved word which should not use as a variable name, class name, function name.
What are Keywords in Python ?
Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier. Keywords in Python are case sensitive.
Is print a keyword in Python ?
In Python 3.+ it is not a keyword but it is a keyword in Python 2. The print has been removed from Python 2 as keyword and included as built-in function in Python 3.
How Many Keywords are there in Python Programming Language ?
There are 36 keywords in Python 3.9 programming language. These keywords are always available, you need not to import them into your code. Python keywords are different from Python's built-in functions and data types. The built-in functions and data types are also always available.
Following are the list of all Keywords in Python Programming Language
All keywords in python are in lowercase or uppercase ?
All the keywords in Python are lowercase letters except these 3 keywords (True, False and None).
How to Get and Print all Keywords from Python ?
For printing the list of all the keywords from Python, you should use the "keyword.kwlist" statement after importing the "keyword" module. It returns the list of all the keywords available in the installed Python version.
Following Python programme prints the list of all the keywords from Python.
How to Get and Print all Keywords from Python ?
The above programme will print the list of Python keywords as follows.
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
How to get the Python keywords list on your operating system ?
Run the command prompt and type python then press enter. After that type help() and hit enter. Then after type keywords and press enter to get the list of Python keywords for the current Python version running on your operating system.
The Python help shows the Python keywords list as Here is a list of the Python keywords. Enter any keyword to get more help. Following screen shot showing the list of keywords from Python help.
List of Python Keywords and Description with Examples
What is False Keyword in Python ?
The False keyword is a Boolean value, and result of a comparison operation. The False keyword is the same as 0 zero. True and False are truth values in Python. They are the results of comparison operations or logical (Boolean) operations in Python.
The above statement returns False.
What is None Keyword in Python ?
None is a special constant in Python that represents the absence of a value or a null value. The None keyword in Python is used to define a null value or no value at all. Void functions that do not return anything will return a None object automatically. None is also returned by functions in which the program flow does not encounter a return statement.
What is True Keyword in Python ?
The True keyword is a Boolean value, and result of a comparison operation. Other than 0 zero is True. True and False are truth values in Python. They are the results of comparison operations or logical (Boolean) operations in Python.
The above statement returns True.
What is and Keyword in Python ?
The and keyword is a logical operator. and, or, not are the logical operators in Python. Logical operators are used to combine conditional statements. The return value will only be True if both statements return True, otherwise it will return False.
What is as Keyword in Python ?
The as keyword is used to create an alias. as is used to create an alias while importing a module. It means giving a different name to a module while importing it in Python.
What is assert Keyword in Python ?
The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. assert is check if our assumptions are true. assert find bugs more conveniently. assert is followed by a condition.
What is async and await Keyword in Python ?
The async and await keywords are provided by the asyncio library in Python. They are used to write concurrent code in Python. The async keyword is equal to synchronized keyword in Java.
What is break Keyword in Python ?
The break statement is used to break out of a loop. It is used inside for and while loops to alter the normal behaviour. break will end the loop it is in and control flows to the statement immediately below the loop.
What is class Keyword in Python ?
The class keyword is used to define a new user defined class in Python. Class is a collection of related attributes and methods. Classes can be defined anywhere in a program. But it is a good practice to define a single class in a module.
You may have noticed the self parameter in function definition inside the class but we called the method simply as p.printPersonName() without any arguments.
In Python programme calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method's object before the first argument.
The first argument of the function in class must be the object itself. This is conventionally called self. You can named it yourself.
What is continue Keyword in Python ?
The continue keyword is used to end the current iteration in a for loop or a while loop, and continues to the next iteration. The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.
What is def Keyword in Python ?
The def keyword is used to create or define a user defined function in Python. Function or Method is a block of related statements, which together does some specific task.
Which keyword is used to define methods in Python ?
The def keyword is used to define methods in Python. Python has simple rules to define a function in Python. Function blocks begin with the keyword def followed by the function name and parentheses ( ) ends with colon : . The colon : is used to define a block of code in Python.
What is del Keyword in Python ?
The del keyword is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list.
What is elif Keyword in Python ?
The elif keyword is used in conditional statements. It is same as else if.
What is else Keyword in Python ?
The else keyword is used in conditional statements (if statements), and decides what to do if the condition is False. The else keyword can also be use in try...except blocks. The else keyword in a try...except block to define what to do if no errors were raised.
What is except Keyword in Python ?
The except keyword is used in try...except blocks. It defines a block of code to run if the try block raises an error.
What is finally Keyword in Python ?
The finally keyword is used to create a block of code that follows a try block. The finally block of code is always executed whether an exception has occurred or not. The finally block is useful to close objects and clean up resources.
What is for Keyword in Python ?
A for keyword is used to create a for loop. We use for when we know the number of times we want to loop. In Python for keyword is used to iterate through a sequence, list and tuple.
What is from Keyword in Python ?
The from keyword is used to import only a specified section from a module. In other words from is used to import specific attribute or function into the current namespace.
What is global Keyword in Python ?
The global keyword is used to create global variables. global is used to declare that a variable inside the function is global i.e. outside the function scope.
What is if Keyword in Python ?
The if keyword is used to create conditional statements (if statements), and allows you to execute a block of code only if a condition is True.
What is import Keyword in Python ?
The import keyword is used to import modules into the current namespace. Below example import the datetime module and display the system date and time. In other words import keyword is used to import other modules into a Python script. For example we can use the import keyword to import the math module into the namespace. The as keyword is used to give a module a different alias. The import keyword uses Python's module import mechanism to load a module.
What is in Keyword in Python ?
The in keyword is used to check if a value is present in a sequence (list, range, string). Another usage of in keyword is also used to iterate through a sequence in a for loop. The in keyword returns True if the value is present, else it returns False.
What is is Keyword in Python ?
The is keyword is used for testing object identity in Python. It is used to test if the two variables refer to the same object. The is keyword returns True if the objects are identical and False if are not identical.
What is lambda Keyword in Python ?
The lambda keyword is used to create an anonymous function (function with no name). It is an inline function that does not contain a return statement. It consists of an expression that is evaluated and returned.
A lambda function can take any number of arguments, but can only have one expression. The expression is evaluated and the result is returned.
What is nonlocal Keyword in Python ?
The nonlocal keyword indicates that particular variables live in an enclosing scope and should be rebound there. The nonlocal keyword is used to work with variables inside nested functions, where the variable should not belong to the inner function. Use the keyword nonlocal to declare that the variable is not local.
What is not Keyword in Python ?
The not keyword is the logical operator in Python. The not operator is used to invert the truth value. The not keyword will return True if the statement(s) are not True, otherwise it will return False.
What is or Keyword in Python ?
The or keyword is the logical operator in Python. The or keyword used to combine conditional statements. The or keyword will return True if any of the operands is True, otherwise it will return False.
What is pass Keyword in Python ?
The pass is a null statement in Python. Nothing happens when it is executed. It is used as a placeholder. Empty code is not allowed in loops, function definitions, class definitions, or in if statements. If we have a function that is not implemented yet, but we want to implement it in the future, it will throw IndentationError exception. Instead of this, we construct a blank body with the pass statement. We can do the same thing in an empty class as well.
What is raise Keyword in Python ?
The raise keyword is used to raise an exception. The raise keyword is equal to throw keyword in Java. We can raise an exception explicitly with the raise keyword. You can raise exceptions of user defined exceptions as well as Python system defined exceptions using the raise keyword of Python.
What is return Keyword in Python ?
The return keyword is used inside a function to exit it and return a value. If you won't return a value explicitly, None is returned automatically in Python.
What is try Keyword in Python ?
Python try keyword is used in exception handling in Python programming language. It is same as try keyword in Java programming language. The try keyword is used in try...except blocks. It defines a block of code test if it contains any errors. You can define different blocks for different error types.
What is while Keyword in Python ?
The while keyword is used to create a while loop. The statements inside a while loop continue to execute until the condition for the while loop evaluates to False or a break statement is encountered.
What is with Keyword in Python ?
The with keyword is used to wrap the execution of a block of code within methods defined by the context manager. Context managers are a really helpful structure in Python. Each context manager executes specific code before and after the statements you specify.
Using with gives you a way to define code to be executed within the context manager's scope. The most basic example of this is when you are working with file I/O in Python.
After the code is executed, the file pointer closes automatically. Even if your code in the with block raises an exception, the file pointer would still close.
What is yield Keyword in Python ?
The yield keyword is used inside a function like a return statement. But yield returns a generator. When a function has a yield statement, what gets returned is a generator. The generator can then be passed to Python's built-in next() to get the next value returned from the function.
When you call a function with yield statements, Python executes the function until it reaches the first yield keyword and then returns a generator. These are known as generator functions.
In other words yield is a keyword in Python that is used to return from a function without destroying the states of its local variable and when the function is called, the execution starts from the last yield statement. Any function that contains a yield keyword is termed as generator.
What is Generator in Python ?
Generator is an iterator that generates one item at a time in Python. A large list of values will take up a lot of memory. Generators are useful in this situation as it generates only one value at a time instead of storing all the values in memory.
When to use yield keyword instead of return keyword in Python ?
You can use yield keyword in a generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return.