- What are Python tokens and why they matter in your code
- Keywords — Python's reserved words and what they do
- Identifiers — How to name variables, functions, and classes
- Literals — Constants and fixed values in Python
- Operators — Symbols that perform operations on data
- Delimiters — Punctuation that structures your code
- Write and run Python code with tokens in the browser
- Test your knowledge with interactive quizzes
So, What Exactly Are Python Tokens?
Think of tokens as the building blocks of Python code — the smallest units that the Python interpreter understands. When you write a program, Python breaks it down into these tokens to figure out what you want it to do.
Here's a simple way to think about it: just like how English sentences are made up of words and punctuation, Python programs are made up of tokens. Each token has a specific meaning to the interpreter.
Python has five main types of tokens:
- Keywords: Reserved words with special meanings
- Identifiers: Names for variables, functions, and classes
- Literals: Fixed values like numbers and strings
- Operators: Symbols that perform operations
- Delimiters: Punctuation that structures your code
💡 Fun fact: When the Python interpreter reads your code, it first breaks it down into tokens — this process is called "lexical analysis." Once it has all the tokens, it figures out what to do with them!
1. Keywords — Python's Reserved Words
Keywords are reserved words that have a special meaning in Python. You cannot use them as variable names, function names, or identifiers.
Think of keywords as Python's built-in vocabulary — they're the words that Python understands without any explanation. Here are some common keywords:
Keywords in Python (36 in total): False class finally is return None continue for lambda try True def from nonlocal while and del global not with as elif if or yield assert else import pass break except in raise
Here's how you can list all the keywords yourself:
# Python script to list all keywords import keyword print(keyword.kwlist)
📝 Important: There are 36 keywords in Python 3.11. They're all in lowercase except True, False, and None.
2. Identifiers — Naming Your Code
Identifiers are names you give to variables, functions, classes, and other objects. They help you refer to things in your code.
Here are the rules for naming identifiers in Python:
- Start with a letter or underscore: Must begin with a letter (a-z, A-Z) or underscore (_)
- Can contain numbers: After the first character, you can use numbers (0-9)
- Case-sensitive:
myVarandmyvarare different - Can't use keywords: You can't use
if,while,class, etc. - No special characters: Only letters, numbers, and underscores are allowed
✅ Valid Identifiers
my_var_privatename123userName
❌ Invalid Identifiers
123name (starts with number)my-var (has hyphen)if (is a keyword)
💡 Pro tip: Use descriptive names like user_age instead of u or age_of_user. It makes your code much easier to read!
3. Literals — Fixed Values
Literals are fixed values that you write directly in your code. They represent constant values that don't change.
Python has several types of literals:
# Examples of different literals name = "Python" # String literal age = 30 # Integer literal price = 19.99 # Float literal is_popular = True # Boolean literal nothing = None # None literal
4. Operators — Performing Operations
Operators are symbols that tell Python to perform specific operations on values and variables.
Python has several types of operators:
Common Operators in Python: Arithmetic: + - * / // % ** Comparison: == != > < >= <= Logical: and or not Assignment: = += -= *= /= Membership: in not in Identity: is is not
# Examples of operators a = 10 b = 3 print(a + b) # 13 (addition) print(a - b) # 7 (subtraction) print(a * b) # 30 (multiplication) print(a / b) # 3.33 (division) print(a % b) # 1 (modulus - remainder) print(a ** b) # 1000 (power) # Comparison print(a > b) # True print(a == b) # False
5. Delimiters — Structuring Your Code
Delimiters are punctuation marks that help organize your code. They define where blocks start and end, and they separate elements.
Common Delimiters in Python:
( ) Parentheses - Used for function calls and grouping
[ ] Square brackets - Used for lists and indexing
{ } Curly braces - Used for dictionaries and sets
; Semicolon - Separates statements (rarely used)
: Colon - Indicates start of a block
, Comma - Separates items
. Period - Access object attributes
# Examples of delimiters
my_list = [1, 2, 3] # [ ] and ,
my_dict = {"name": "Python"} # { } and :
print(my_dict["name"]) # ( ) and [ ]
Try It Yourself!
Now it's your turn! Write and run Python code directly in your browser:
['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', 'while', 'with', 'yield']
My name is Python Learner
I am 25 years old
Name: Sankalan
Score: 95
Pi: 3.14159
Awesome? True
10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3.3333333333333335
10 % 3 = 1
List: [1, 2, 3, 4, 5]
Dictionary: {'name': 'Python', 'age': 30}
🎯 Challenge Yourself!
• Add a new variable with your name and print it
• Try using the multiplication operator with two numbers
• Create a list of your favorite colors
🎉 You've Mastered Python Tokens!
You now understand the building blocks of Python — keywords, identifiers, literals, operators, and delimiters. These are the foundation of every Python program you'll ever write!
Quick Quiz - Test Your Knowledge
Let's see what you've learned about Python tokens:
Frequently Asked Questions
🤔 What is the difference between a keyword and an identifier? ▼
if, while, and class. You cannot use them as names for variables or functions. Identifiers are names you create for your own variables, functions, and classes. They can be anything as long as they follow Python's naming rules.
💡 Why do we need delimiters in Python? ▼
() are used for function calls and grouping expressions, while colons : indicate the start of a block like an if statement or loop.
🎯 Can I use a keyword as a variable name? ▼
if or while as a variable name, Python will give you a syntax error. This is why you should always check if a word is a keyword before using it as a name.
📚 Why are tokens important to understand? ▼
📚 Where to Go From Here
Now that you understand Python tokens, here are some related topics to explore:
🔤 Character Set
Learn about Python's character set and encoding
📊 Data Types
Explore Python's built-in data types
🔧 Operators
Deep dive into Python operators