Ch2. Tokens
Ch2. Tokens
The basic fundamental unit of program that cannot be further breakdown is known as
Tokens.
Tokens are divided into following categories
1) Identifiers
2) Keywords
3) Operators
4) Literals
5) Punctuators
We can also use the Python string isidentifier() method to check whether a string is
a valid identifier or not.
You can also list out all keywords of any version of python. For that you just have to
import keyword library and call kwlist function of such library in following manner.
import keyword
print(keyword.kwlist)
Output
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del',
'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal',
'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Unary Operators: are those operators that requires exactly one operand
to perform its working.
E.g. Logical NOT, Unary +, Unary -, Bitwise Not ~ etc.
Binary Operators: are those operators that requires exactly two operands
to perform its working. It is further classified into following categories.
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Identity operators
Membership operators
Bitwise operators
Python Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
Operator Precedence
Operator precedence describes the order in which operations are performed.
The precedence order is described in the table below, starting with the highest precedence at the
top:
Operator Description
() Parentheses
** Exponentiation
+x -x ~x Unary plus, unary
minus, and bitwise
NOT
* / // % Multiplication,
division, floor
division, and
modulus
+ - Addition and
subtraction
<< >> Bitwise left and
right shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
== != > >= < <= is is Comparisons,
not in not in identity, and
membership
operators
not Logical NOT
and AND
or OR
4) Literals in Python
A literal in Python is a syntax that is used to completely express a fixed value of a
specific data type. Literals are constants that are self-explanatory and don’t need to be
computed or evaluated. They are used to provide variable values or to directly
utilize them in expressions. Generally, literals are a notation for representing a fixed
value in source code.
They can also be defined as raw values or data given in variables or constants. In this
article, we will explore the different types of literals in Python along with examples to
demonstrate their usage.