Notes

Python Tokens (Keywords, Identifiers, Literals, Operators) [ English ]

< Prev Next >

1. Introduction

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:

  1. Keywords
  2. Identifiers
  3. Literals
  4. Operators

2. Keywords

2.1 Definition

Keywords are reserved words in Python that have a predefined meaning. These words cannot be used as identifiers (variable names, function names, etc.).

2.2 Examples of Keywords

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

2.3 Example

if True:
    print("This is a keyword example")

2.4 Important Points

3. Identifiers

3.1 Definition

Identifiers are the names used to identify variables, functions, classes, or objects in a program.

3.2 Rules for Naming Identifiers

3.3 Examples

name = "Rahul"
_age = 20
total_marks = 95

3.4 Invalid Identifiers

1name   # starts with digit
class   # keyword
my-name # contains hyphen

4. Literals

4.1 Definition

Literals are constant values assigned to variables.

4.2 Types of Literals

1. Numeric Literals

a = 10      # Integer
b = 3.14    # Float
c = 2+3j    # Complex

2. String Literals

name = "Python"
msg = 'Hello'

3. Boolean Literals

x = True
y = False

4. Special Literal

value = None

5. Collection Literals

list1 = [1, 2, 3]
tuple1 = (1, 2, 3)
set1 = {1, 2, 3}
dict1 = {"a": 1, "b": 2}

5. Operators

5.1 Definition

Operators are symbols used to perform operations on variables and values.

5.2 Types of Operators

1. Arithmetic Operators

+   # Addition
-   # Subtraction
*   # Multiplication
/   # Division
//  # Floor Division
%   # Modulus
**  # Exponent

Example:

print(10 + 5)  # 15

2. Comparison Operators

==  # Equal to
!=  # Not equal
>   # Greater than
<   # Less than
>=  # Greater or equal
<=  # Less or equal

3. Logical Operators

and
or
not

4. Assignment Operators

=   # Assign
+=  # Add and assign
-=  # Subtract and assign
*=  # Multiply and assign
/=  # Divide and assign

5. Membership Operators

in
not in

Example:

print("a" in "apple")  # True

6. Identity Operators

is
is not

6. Summary

< Prev Next >