This video introduces keywords and how Python reserves them. Watch it, then read on for the detail and practice.
What keywords are and why they are reserved
Keywords are reserved words in Python with fixed meanings that the language needs to understand your code. They are the guardrails that shape how your program runs. You cannot use them as variable names, function names, or class names, no matter how much you want to call a variable for or class.
When Python reads your code, it scans for these keywords first. As soon as it sees one, it knows what you intend to do: create a loop, define a function, make a decision, or import a module. This is why keywords must be protected. If you could name a variable if, Python would not know whether you meant the decision keyword or a label for your data. That ambiguity would break the language.
Keywords are different from built in functions like print(), len(), or type(). Built in functions are part of Python's standard library and you can (in theory) override them, though you should never do so. Keywords, by contrast, are syntax. They are part of the language grammar itself. You cannot override them at all.
The complete list: how to see every keyword
Python keeps its keyword list small and fixed. There are about 35 keywords in current versions of Python (3.10 and later). Rather than memorise them, you should know how to inspect them.
Open a Python interpreter or a cell in your notebook and run this:
import keyword
print(keyword.kwlist)
You will see output like this:
['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', 'with', 'while', 'yield']
You can also check whether a word is a keyword:
import keyword
print(keyword.iskeyword('for')) # True
print(keyword.iskeyword('my_var')) # False
print(keyword.iskeyword('list')) # False (it's a built in, not a keyword)
This is a useful habit to build early. When you are reading code and you see an unfamiliar word, check it. Is it a keyword? Is it a built in function? Is it something the author defined? That distinction will help you understand what the code does.
Use the keyword module instead of memory. Keywords change slowly, but the keyword module keeps track of them for you.
Keywords by category: control flow, definition, and logic
Keywords are easier to understand if you group them by purpose. The main categories are control flow, definition, and logic.
Control flow keywords
These keywords control where execution goes in your program: branches and loops.
if, elif, else: Make decisions. Run different code based on conditions.
for: Loop over a sequence (a list, a string, a range).
while: Loop while a condition is true.
break: Exit a loop early.
continue: Skip to the next iteration of a loop.
return: Exit a function and send a value back to the caller.
Definition keywords
These keywords introduce new structures in your code.
def: Define a function.
class: Define a class (for object oriented programming).
import, from: Import modules or specific names from modules.
as: Rename something you import, or create an alias in a context manager.
with: Set up a context (used with files, database connections, and other resources that need cleanup).
try, except, finally, raise: Handle errors and exceptions.
assert: Test a condition and raise an error if it fails (useful in development and testing).
Logical keywords
These keywords express logic: combinations of conditions, type checks, and membership tests.
and, or, not: Boolean logic. Combine or invert conditions.
is: Check if two names refer to the same object in memory.
in: Check if a value is in a sequence or a member of a collection.
Special keywords
These have unique roles.
True, False, None: These are constants. True and False are boolean values. None represents the absence of a value.
pass: A placeholder that does nothing. Useful when you need a statement but have nothing to write yet.
lambda: Define a small anonymous function inline.
async, await: For asynchronous programming (rarely needed in early finance projects).
global, nonlocal: Declare scope. Tell Python to use a variable from an outer scope rather than create a new local one.
yield: Turn a function into a generator (covered later in your learning).
del: Delete a variable or an item from a list.
Common keywords in practice
if, elif, else: making decisions in your code
The most common control flow keywords are if, elif, and else. They branch your code based on conditions.
A weekly note on treasury, liquidity and practical Python. No spam, unsubscribe any time.
balance = 50000
interest_rate = 0.05
if balance > 100000:
interest = balance * interest_rate
elif balance > 50000:
interest = balance * (interest_rate * 0.8)
else:
interest = balance * (interest_rate * 0.5)
print(f"Interest accrued: {interest}")
Notice the colon after each if, elif, and else. That colon tells Python that a block of indented code follows. The block is the code that runs if the condition is true.
You do not have to use elif or else. You can write just if. But if you have multiple branches, elif and else make your code clearer and faster (Python stops checking as soon as it finds a match).
for and while: repeating actions
Loops let you repeat code without writing it over and over. The for keyword iterates over a sequence. The while keyword repeats while a condition is true.
A for loop over a list of transaction amounts:
transactions = [1000, 2500, 500, 3200]
total = 0
for amount in transactions:
total = total + amount
print(f"Total: {total}")
Here, for takes each item in transactions and assigns it to the variable amount. The indented block runs once for each item. This is clearer and less error prone than using a counter and indexing by hand.
A while loop that asks for input until the user gives a valid amount:
amount = 0
while amount <= 0:
try:
amount = float(input("Enter an amount: "))
if amount <= 0:
print("Amount must be positive.")
except ValueError:
print("That's not a number. Try again.")
print(f"You entered: {amount}")
Notice the break keyword is not used here, but it can be. break exits the loop immediately, skipping any remaining code in the loop. continue skips to the next iteration without running the rest of the block. Both are useful, but avoid them if you can. They make code harder to follow.
def: writing reusable code
The def keyword defines a function: a reusable block of code with a name and (usually) parameters.
def calculate_interest(principal, rate, years):
return principal * (1 + rate) ** years
amount = calculate_interest(10000, 0.04, 5)
print(amount)
The keyword def says "I am defining a function". The name follows, then a pair of parentheses with parameters inside. The colon introduces the indented block. The return keyword sends a value back to whoever called the function.
Without functions, you would write the same calculation every time you needed it. With functions, you write it once and call it by name.
import and from: bringing in what you need
The import and from keywords let you use code from other modules (libraries, packages, or files you write).
Import an entire module:
import datetime
today = datetime.date.today()
print(today)
Import a specific function from a module:
from datetime import date
today = date.today()
print(today)
Import and rename using as:
import pandas as pd
data = pd.read_csv('transactions.csv')
The as keyword creates an alias. This is important, not just convenience. In finance work, many libraries have long names. pandas becomes pd. numpy becomes np. These are conventions. Follow them so your code matches what others write.
Logical keywords: and, or, not, is, in
Boolean logic is everywhere in finance code. You check conditions with and, or, and not. You test identity with is and membership with in.
Using and and or:
balance = 5000
account_type = 'savings'
if balance > 1000 and account_type == 'savings':
interest_rate = 0.03
elif balance > 1000 or account_type == 'premium':
interest_rate = 0.02
else:
interest_rate = 0.01
print(interest_rate)
and means both conditions must be true. or means at least one must be true.
Using not:
is_closed = False
if not is_closed:
print("Account is active.")
Using is to check identity (whether two names point to the same object):
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True, b is assigned from a
print(a is c) # False, c is a different list with the same content
print(a == c) # True, the content is the same
This matters. is checks identity. == checks equality. In most code, you want ==. Use is when you specifically need to know if two names refer to the same object.
Using in:
transactions = [100, 200, 300, 400]
if 200 in transactions:
print("Found the transaction.")
for amount in transactions:
print(amount)
in checks membership. It also works with strings, dictionaries, and ranges.
Mistakes to avoid and how to spot keywords when reading code
Common mistakes
Do not try to use a keyword as a variable name. If you write this:
for = 10 # SyntaxError!
Python will reject it immediately with SyntaxError: invalid syntax. This is a sign you have used a keyword.
Do not confuse keywords with built in names. You can (but should not) shadow built in functions:
list = [] # This runs, but 'list' is no longer the list type
dict = {} # This runs, but 'dict' is no longer the dict type
This is legal Python, but it is a bad idea. You have broken the built in names. Later code that tries to use list() or dict() will fail or behave unexpectedly. If you need to store a list, name the variable something descriptive: transactions, balances, accounts. The language will thank you.
Reading code with confidence
When you read code written by others, look for keywords. They are the signposts. When you see for, you know a loop is starting. When you see def, you know a function is being defined. When you see if, you know a decision is coming.
Make a habit of pausing at keywords and reading the block that follows. The indented code after a keyword is what the keyword controls. Understand that block, and you understand that part of the code.
Next steps: keywords as your foundation
Keywords are the foundation of Python. You will use them every day. You do not need to memorise them all (the keyword module has your back), but you do need to recognise them and understand what they do.
Once you are comfortable with keywords, you are ready to write loops, conditionals, and functions. Those are the structures that let you write real programs. Start with the links below if you are building your learning path from the start.
For your next step, pick a keyword that interests you and write a small program that uses it. Define a function with def. Write a conditional with if and else. Loop over a list with for. The practice will stick them in your memory far better than any list.

The Complete Python Course
Welcome to the most practical and beginner friendly Python Bootcamp Course on YouTube.
Take the courseGet the next one in your inbox
A weekly note across Finance & Treasury, Innovation & Automation and Career Development. No spam, unsubscribe any time.
Notes across finance and treasury, innovation and automation, and career development, written by practitioners who do the work.
