In Python, a token is the smallest unit of a program that has meaning to the interpreter. When a Python program is written, it is broken down into tokens for execution.
Python tokens are mainly classified into the following categories:
Keywords are reserved words in Python that have a predefined meaning. These words cannot be used as identifiers (variable names, function names, etc.).
False, True, None, and, or, not,
if, else, elif,
for, while, break, continue,
def, return, class,
try, except, finally,
import, from, as,
with, lambda, pass, yield
if True:
print("This is a keyword example")
Identifiers are the names used to identify variables, functions, classes, or objects in a program.
_name = "Rahul"
_age = 20
total_marks = 95
1name # starts with digit
class # keyword
my-name # contains hyphen
Literals are constant values assigned to variables.
a = 10 # Integer
b = 3.14 # Float
c = 2+3j # Complex
name = "Python"
msg = 'Hello'
x = True
y = False
value = None
list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
set1 = {1, 2, 3}
dict1 = {"a": 1, "b": 2}
Operators are symbols used to perform operations on variables and values.
+ # Addition
- # Subtraction
* # Multiplication
/ # Division
// # Floor Division
% # Modulus
** # Exponent
Example:
print(10 + 5) # 15
== # Equal to
!= # Not equal
> # Greater than
< # Less than
>= # Greater or equal
<= # Less or equal
and
or
not
= # Assign
+= # Add and assign
-= # Subtract and assign
*= # Multiply and assign
/= # Divide and assign
in
not in
Example:
print("a" in "apple") # True
is
is not