Language

Tokens of Python

Introduction to python

In Python, tokens are the smallest lexical unit of a program that hold meaning. They are the basic building blocks of python code. The Python interpreter breaks down the source code into these tokens to understand and execute it correctly. Each tokens should be separated from other by a space, tab, carriage return which are correctly termed as white space. Every word, symbols, or number you use in a Python program is classified as a token. Python has five main types of tokens – Keyword, Identifiers, Literals, Operators and some symbols.

How do tokens important in programming terms ?

Tokens of python are different from concepts like expressions or statements. While expressions combine tokens to form meaningful code, tokens themselves are the individual components (like keywords, operators, or variables). In Python, compiler uses tokens to translate the source code into bytecode whereas interpreter uses token to execute bytecode directly.

Types of Python Tokens

Python has Five main types of token:

A. Keywords

Keywords are reserved words in Python that have special meanings. They should not be used as a normal variable names. There are 33 keywords in python. Example- if, else, while, for, def, return, import, etc. Keywords contains only alphabet symbols. int, float, etc., data types are not required in Python to declare a variable that’s why not available in Python keyword list.

B. Identifiers

Identifiers are the names given to variables, functions, classes, and objects which can be used for identification. They must follow specific rules: – identifiers must start with a letter (A-Z or a-z) or an underscore (_). – identifiers cannot be a keyword. – identifiers should not begin with numbers or digits.

D. Operators

Operators are symbols that perform operations on variables and values. Examples include:
Arithmetic operators: +, -, *, /, %
Comparison operators: ==, !=, >, <, >=, <=
Logical operators: and, or, not
Assignment operators: =, +=, -=, *=, /=

C. Literals

Literals are fixed values used in a program. Literals can be defined as a data given that is given in a variable or constant. Some literals are given below: String literals – sequence of characters in the quotes. Numeric literals – number stores numeric values. Boolean literals – can either have – True, False. Special literal – special literal None.

E. Special Symbols

Special symbols in Python include characters used in syntax and structure, such as:
Parentheses (), curly braces {}, square brackets []
Colon :, semicolon ;, at symbol @
Hash # (used for comments)


CONCLUSION

In conclusion, Python tokens are the fundamental building blocks of Python code, serving as the smallest meaningful units the interpreter recognizes. Understanding how the tokenizer breaks down your code into these tokens is crucial for comprehending Python’s syntax and how the language interprets your instructions. From identifiers and keywords to operators and literals, each token plays a specific role in defining the structure and meaning of your program. By gaining a solid grasp of tokenization, you’ll be better equipped to write correct, efficient, and readable Python code, and you’ll have a deeper understanding of how Python programs are processed.

About the author

Pooja Rastogi

Leave a Comment