In this section, we will discuss keywords in Python—special identifiers reserved for specific syntactic purposes. Understanding keywords is essential as they form the foundation of Python's syntax and structure.
- Keywords are reserved words in Python that have special meanings and cannot be used as regular identifiers (like variable names or function names).
- Python's keywords consist only of lowercase letters.
- Python v2 has 31 keywords.
- Python v3 has 35 keywords.
# Keywords that cannot be used as identifiers
if = 10 # ❌ Invalid: 'if' is a keyword
def = 20 # ❌ Invalid: 'def' is a keyword
- Some keywords begin simple statements or clauses within compound statements.
- Other keywords serve as operators within expressions.
In Python v2, the keywords include:
and continue except global lambda raise yield
as def exec if not return
assert del finally import or try
break elif for in pass while
class else from is print with
- In Python v3:
exec
andprint
are no longer keywords. They were statements in Python v2 but are now functions in Python v3.- To use the
print
function in Python v2, you can start your source file with:from __future__ import print_function
- New keywords in Python v3 include
False
,None
,True
, andnonlocal
.False
,None
, andTrue
were built-in constants in Python v2, but they were not technically keywords.
- Additional Keywords in Python v3 include:
async
andawait
: Used for asynchronous programming.
In Python v3, the updated list of keywords includes:
and as assert async await break
class continue def del elif else
except False finally for from global
if import in is lambda None
nonlocal not or pass raise return
True try while with yield
Keyword | Description | Code Example |
---|---|---|
False , True |
Data values from the data type Boolean | False == (1 > 2) True == (2 > 1) |
and , or , not |
Logical operators: - (x and y) → both x and y must be True- (x or y) → either x or y must be True- (not x) → x must be false |
x, y = True, False (x or y) == True # True (x and y) == False # True (not y) == True # True |
break |
Ends loop prematurely | while(True): break # no infinite loop print("hello world") |
continue |
Finishes current loop iteration | while(True): continue print("43") # dead code |
class , def |
class defines a new class.def defines a new function or method. |
class Beer: def __init__(self): self.content = 1.0 def drink(self): self.content = 0.0 becks = Beer() becks.drink() # empty beer bottle |
if , elif , else |
Conditional program execution: program starts with if , tries elif , and ends with else . |
x = int(input("your val: ")) if x > 3: print("Big") elif x == 3: print("Medium") else: print("Small") |
for , while |
Looping constructs: for loop iterates over a sequence, while continues as long as the condition is true. |
for i in [0, 1, 2]: print(i) j = 0 while j < 3: print(j) j += 1 |
in |
Checks whether an element is in a sequence | 42 in [2, 39, 42] # True |
is |
Checks whether two references point to the same object | y = x = 3 x is y # True [3] is [3] # False |
None |
Represents a null value or absence of value | def f(): x = 2 f() is None # True |
lambda |
Defines a small anonymous function | (lambda x: x + 3)(3) # returns 6 |
return |
Ends the execution of a function and optionally returns a value | def incrementor(x): return x + 1 incrementor(4) # returns 5 |
This section provided an overview of keywords in Python and their specific uses. Keywords are fundamental building blocks of Python syntax, and knowing them is key to mastering the language. 🚀