Trusted answers to developer questions

What are keywords in Python?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

Keywords are reserved words in Python that are used to trigger specific tasks. We cannot use a keyword as a variable name, function name, or any other identifier since these keywords each hold a special meaning. All of the keywords in Python are lowercase.

The table below shows the list of keywords and their meanings:

Keyword Meaning
print Prints to console.
while Controls the flow of the code by starting a loop.
for Iterates over items of a collection in the order that they appear.
continue Interrupts the current cycle. The remaining part of the current iteration will be skipped and the program will move on to the next iteration.
break Exits the (loop) cycle.
if Determines which statements are going to be executed.
elif Stands for “else if”. If the first test evaluates to False, then this keyword checks for the next one.
else Is optional. The statement after the else keyword is executed when the if condition is False.
is Tests for object identity.
not Negates a Boolean value.
and All conditions in a Boolean expression must be met.
or At least one condition must be met.
import Imports other modules into a Python script.
as Gives a module a different alias.
from Imports a specific variable, class, or function from a module.
def Creates a new user-defined function.
return Exits the function and returns a value if needed.
lambda Creates a new anonymous function.
global Accesses variables defined outside functions.
try Specifies exception handlers.
except Catches the exception and executes codes.
raise Creates a user-defined exception.
del Deletes an object.
pass Tells the program to do nothing. Used in if-else statements to make the loop do nothing upon encountering certain conditions.
assert Used for debugging purposes.
class Creates a new user-defined object.
exec Executes Python code dynamically.
yield Used with generators.

RELATED TAGS

python
keyword
python3
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?