Lessons
Courses

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

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

  • Keywords are case-sensitive
  • They have fixed meaning
  • Cannot be used as variable names

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

  • Must start with a letter (a–z, A–Z) or underscore _
  • Cannot start with a digit
  • Can contain letters, digits, and underscores
  • Cannot be a keyword
  • Case-sensitive

3.3 Examples

name = "Rahul"_age = 20total_marks = 95

3.4 Invalid Identifiers

1name # starts with digitclass # keywordmy-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 # Integerb = 3.14 # Floatc = 2+3j # Complex

2. String Literals

name = "Python"msg = 'Hello'

3. Boolean Literals

x = Truey = 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

andornot

4. Assignment Operators

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

5. Membership Operators

innot in

Example:

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

6. Identity Operators

isis not

6. Summary

  • Tokens are the smallest units of a Python program.
  • Keywords are reserved words with predefined meaning.
  • Identifiers are names given to variables and functions.
  • Literals are constant values used in a program.
  • Operators are symbols used to perform operations.