Tokens in Python programming
In Python, tokens are the basic pieces that form a program. The interpreter reads your code by breaking it into these pieces and using them to understand what the program should do. Without tokens, Python would not be able to process or run any code.
Types of Python Tokens
There are 6 types of tokens in Python:
1. Keywords
Keywords are reserved words with predefined meaning in Python. They cannot be used as variable names.
Examples:
if, else, while, for, def, return, True, False, None, class, import
2. Identifiers
Identifiers are names given to variables, functions, classes, and objects.
Rules:
- Must start with a letter or underscore
- Can contain letters, digits, and underscore
- Cannot be a keyword
- No spaces allowed
Examples:
total, _count, sum1, studentName
3. Constants
Constants are fixed values that do not change during program execution.
Types of constants:
- Integer constant:
10,-25 - Floating constant:
3.14,2.5 - String constant:
"Hello","Python" - Boolean constant:
True,False
- Integer constant:
4. Operators
Operators are symbols used to perform operations on variables and values.
Types:
- Arithmetic:
+ - * / % - Relational:
> < >= <= == != - Logical:
and or not - Assignment:
= += -= - Comparison:
== !=
- Arithmetic:
5. Delimiters (Special Symbols)
Delimiters help define the structure and syntax of Python programs.
Examples:
( ) [ ] { } : , . =
6. Strings
A string is a sequence of characters enclosed in single or double quotes.
Example:
"Python Programming"
'Hello World'
Example Showing Python Tokens
total = 10 + 20Tokens used:
| Token | Type |
| total | Identifier |
| = | Operator |
| 10, 20 | Constants |
| + | Operator |
Important Points
- Tokens are the basic building blocks of a Python program
- The Python interpreter reads programs token by token
- Understanding tokens makes it easier to learn Python syntax and avoid errors
