Tokens In Python

Tokens In Python

A token is the smallest individual unit in a python program. Python have following tokens


Tokens In Python
  • 1. Keywords - These are reserved words which have special meaning for the interpreter.
  • 2. Identifiers - These are the names given to the different parts of the program.
  • 3. Literals - These are tokens which have fixed value.
  • 4. Operators - These are symbols which when put between two operands performs some operation.
  • 5. Punctuators - These are symbols used to organize statements, or structures in a program.

Keywords


In Python 3.0.7, there are 33 keywords.


Falseawaitelseimportpass
Nonebreakexceptinraise
Trueclassfinallyisreturn
andcontinueforlambdatry
asdeffromnonlocalwhile
assertdelglobalnotwith
asyncelififoryield

Identifiers


Some rules you need to keep in mind while creating Identifiers.


  • The first character should be letter, or +underscore. No numbers should be the first letter.
  • It should not be a keyword.
  • Identifers are case-sensitve.
  • Except _underscore no symbol is allowed.

Some valid Identifers are:


Hello _Crackexams99

Some invalid Identifers are:


8Hello True

Literals


There are different types of literals:


  • String literals - Letters enclosed in quotes ''/"" are called string literals.
  • Numeric literals - There are generally three types which are int, float, and complex.
  • Boolean literals - It is True or False.
  • None - It indicates nothing.

Operators


There are different types of operators:


  • Unary Operators - Requires only one operand.
  • Binary Operators - Requires two operand.

  • -Arithmetic Operators

    OperatorFunctionSyntax
    +Add two operands or unary plusx + y
    -Subtract right operand from the left or unary minusx - y
    *Multiply two operandsx * y
    /Divide left operand by the right one (always results into float)x / y
    %Modulus - remainder of the division of left operand by the rightx % y
    //Floor division - division that results into whole number adjusted to the left in the number linex // y
    **Exponent - left operand raised to the power of rightx**y

    -Bitwise Operators

    OperatorMeaningSyntax
    &Bitwise ANDx & y
    |Bitwise ORx | y
    ~Bitwise NOT~x
    ^Bitwise XORx ^ y
    >>Bitwise right shiftx >> 2
    <<Bitwise left shiftx << 2

    -Membership Operators

    OperatorFunctionSyntax
    inTrue if value/variable is found in the sequence5 in x
    not inTrue if value/variable is not found in the sequence5 not in x

    -Identity Operators

    OperatorFunctionSyntax
    isTrue if the operands are identicalx is True
    is notTrue if the operands are not identicalx is not True

    -Logical Operators

    OperatorFunctionSyntax
    andTrue if both the operands are truex and y
    orTrue if either of the operands is truex or y
    notTrue if operand is falsenot x

    -Relational Operators

    OperatorFunctionSyntax
    >Greater thanx > y
    <Less thanx < y
    ==Equal tox == y
    !=Not equal tox != y
    >=Greater than or equal tox >= y
    <=Less than or equal tox <= y

    -Assignment Operators

    OperatorUseSyntax
    =x = 1x = 1
    +=x += 1x = x + 1
    -=x -= 1x = x - 1
    *=x *= 1x = x * 1
    /=x /= 1x = x / 1
    %=x %= 1x = x % 1
    //=x //= 1x = x // 1
    **=x **= 1x = x ** 1
    &=x &= 1x = x & 1
    |=x |= 1x = x | 1
    ^=x ^= 1x = x ^ 1
    >>=x >>= 1x = x >> 1
    <<=x <<= 1x = x << 1


Punctuators


These are symbols used to organize statements, or structures in a program. Examples are ' " \ # @ , : = ( ) [ ] { }


Variables In Python

Load comments