Python Keywords with Examples
🐍 Here’s a complete list of all Python keywords with short, understandable examples for each usage – ideal for students and beginners.
🧠 Python Keywords with Examples #
🔑 Keyword | 🧩 Meaning | 🧪 Example |
---|---|---|
False |
Boolean value for “false” | is_cold = False |
True |
Boolean value for “true” | is_warm = True |
None |
No value | answer = None |
and |
Logical AND | if x > 0 and x < 10: |
or |
Logical OR | if name == "Tom" or name == "Lena": |
not |
Logical NOT | if not finished: |
if |
Condition | if age > 18: |
elif |
Additional condition | elif age > 10: |
else |
Alternative | else: print("Too young") |
while |
Loop with condition | while x < 5: x += 1 |
for |
Loop over sequence | for i in range(3): print(i) |
break |
Break loop | if input == "stop": break |
continue |
Skip loop iteration | if i % 2 == 0: continue |
pass |
Placeholder | if condition: pass |
def |
Define function | def say_hello(): print("Hello") |
return |
Return value | return name |
yield |
Return generator value | yield i |
lambda |
Anonymous function | square = lambda x: x * x |
class |
Define class | class Student: |
try |
Start error handling | try: x = int(input) |
except |
Catch error | except ValueError: print("Error") |
finally |
Always execute | finally: print("Done") |
raise |
Raise error | raise ValueError("Invalid") |
assert |
Check condition | assert age > 0 |
import |
Load module | import math |
from |
Load part of module | from math import sqrt |
as |
Assign alias | import math as m |
global |
Use global variable | global points |
nonlocal |
Use outer local variable | nonlocal counter |
del |
Delete object | del list[0] |
with |
Context manager | with open("file.txt") as f: |
await |
Wait for async function | answer = await question() |
async |
Define asynchronous function | async def question(): |
match |
Start pattern matching | match color: |
case |
Define pattern | case "red": print("Fire") |
is |
Check identity | if x is None: |
in |
Check membership | if "a" in word: |