Python Fundamental

Python Tokens

Python Tokens are the smallest unit of a Python program.
The Python program is constructing expressions and statements using Tokens.
Python program is a group of different types of tokens, comments, and white spaces.
There are various tokens used in Python:
  1. Keywords
  2. Identifiers
  3. Literals
  4. Operators

1. Keywords

Python keywords are those predefine words which has a special meaning to an Interpreter to perform a task.
In Python there are around 35 keywords.
In Python you can get the list of keywords using below code:
help("keywords")
You can get more details about any keywords using below code:
help("break")

2. Identifiers

Identifiers are used to identify the things.
Identifiers are used with class, function, variable etc.
There are following rules to define an Identifier:

Valid Identifiers


  • Identifier should be start with characters.
  • Identifier can be start with underscore (_).
  • Identifiers are the case-senstive like class and Class are different things.
  • Identifier name length should be minimum 3 or 4 and maximum 15.

  • Invalid Identifiers


  • Identifiers never start from number.
  • Identifiers never have special symbols.
  • Identifier never have hyphen (-).
  • Identifier never have a space.
  • Identifier never be a keyword.
  • Identifier never have Operators like (+,-,*,/) etc.

  • 3. Literals

    Any constant value which is assigned to a variable that is called literal.
    There are following types of literals:

  • Numeric Literals
  • String Literals
  • Boolean Literals
  • Special Literals
  • Collection Literals

  • i. Numeric Literals

    Numeric Literals are immutable (unmodifiable).
    There are three types of Numeric Literals:
    1. Integer
    2. Float
    3. Complex
    For Example:

    ii. String Literals

    A string literal is a sequence of characters.
    String always represent by single, double or triple quotes.
    For Example:

    iii. Boolean Literals

    Boolean Literals can have only one value either True or False.
    In Python True represent the value as 1 and False represent the value as 0.
    You can use True and False in a Numeric expressions.
    For Example:

    iv. Special Literals

    None is a Special Literals in Python.
    You can use None with that field which is not created.
    It is also used for end of lists in Python.
    For Example:

    v. Collection Literals

    There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.
    For Example:

    4. Operators

    Operators are special symbols in Python that carry out arithmetic or logical computation.
    The value that the operator operates on is called the operand.
    There are following types of Operators in Python:
    1. Arithmetic operators
    2. Comparison operators
    3. Logical operators
    4. Bitwise operators
    5. Assignment operators
    6. Special or identity operators
    7. Membership operators

    No comments: