Unlike human languages, Python consists of a relatively small set of words. These words are called “reserved words”, and each of them has a specific meaning in the language.
However, when you write your own programs, you are free to create your own words as well. These are called variables, and you can choose their names freely—as long as they are not reserved words.
To understand this better, imagine dealing with a pet. You give it commands like “sit,” “stay,” or “fetch.” But if you use words it hasn’t been trained to understand, it will just stare at you in confusion.
Similarly, Python only understands its reserved words. For example, if you say something like:
“I wish more people walked to stay healthy,”
the only word Python might recognize is “walk”—because it is meaningful in its own language.
Here are some examples of Python reserved words:
and del global not with
as elif if or yield
assert else import pass
break except in raise
class finally is return
continue for lambda try
def from nonlocal while
Python already knows these words. For instance, when you use the word try, Python attempts to execute certain instructions. Don’t worry about the meanings of these words yet—you will learn them later. Let us analyze each of these keywords in detail. In order to make these keywords easy to understand, we have divided them into different catagories.
These control logical expressions.
and → True if both operands are trueor → True if at least one operand is truenot → Negates a conditionx = 5
print(x > 3 and x < 10) # True
print(not(x > 10)) # True
Used for decision-making.
if → Primary condition checkelif → Additional conditionelse → Default blockif x > 10:
print("Big")
elif x > 5:
print("Medium")
else:
print("Small")
Control repetitive execution.
for → Iterate over sequencewhile → Loop while condition is truebreak → Exit loop immediatelycontinue → Skip current iterationpass → Placeholder (does nothing)for i in range(3):
if i == 1:
continue
print(i)
Define functions and manage variable scope.
def → Define a functionreturn → Exit function with valuelambda → Anonymous functionglobal → Use global variablenonlocal → Use variable from enclosing scopedef add(a, b):
return a + bsquare = lambda x: x * x
Manage runtime errors.
try → Block to test codeexcept → Handle exceptionelse → Runs if no exceptionfinally → Always executesraise → Trigger exceptiontry:
x = int("abc")
except ValueError:
print("Error")
finally:
print("Done")
Used in class definitions.
class → Define a classclass Car:
pass
Bring external code into your program.
import → Import modulefrom → Import specific partas → Aliasimport math as m
from math import sqrt
in → Check membershipis → Check identity (same object)print(3 in [1,2,3]) # True
with → Automatically manage resources (files, locks)with open("file.txt") as f:
data = f.read()
yield → Produces values lazily (generator)def count():
yield 1
yield 2
assert → Checks condition, raises error if falseassert x > 0, "x must be positive"
del → Delete variable/objectx = 10
del x
pass → No operation (placeholder)break / continue → Already covered in loops
No Comments