Tokens In Python
A token is the smallest individual unit in a python program. Python have following tokens
- 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.
False | await | else | import | pass |
None | break | except | in | raise |
True | class | finally | is | return |
and | continue | for | lambda | try |
as | def | from | nonlocal | while |
assert | del | global | not | with |
async | elif | if | or | yield |
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
Operator | Function | Syntax |
---|---|---|
+ | Add two operands or unary plus | x + y |
- | Subtract right operand from the left or unary minus | x - y |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus - remainder of the division of left operand by the right | x % y |
// | Floor division - division that results into whole number adjusted to the left in the number line | x // y |
** | Exponent - left operand raised to the power of right | x**y |
-Bitwise Operators
Operator | Meaning | Syntax |
---|---|---|
& | Bitwise AND | x & y |
| | Bitwise OR | x | y |
~ | Bitwise NOT | ~x |
^ | Bitwise XOR | x ^ y |
>> | Bitwise right shift | x >> 2 |
<< | Bitwise left shift | x << 2 |
-Membership Operators
Operator | Function | Syntax |
---|---|---|
in | True if value/variable is found in the sequence | 5 in x |
not in | True if value/variable is not found in the sequence | 5 not in x |
-Identity Operators
Operator | Function | Syntax |
---|---|---|
is | True if the operands are identical | x is True |
is not | True if the operands are not identical | x is not True |
-Logical Operators
Operator | Function | Syntax |
---|---|---|
and | True if both the operands are true | x and y |
or | True if either of the operands is true | x or y |
not | True if operand is false | not x |
-Relational Operators
Operator | Function | Syntax |
---|---|---|
> | Greater than | x > y |
< | Less than | x < y |
== | Equal to | x == y |
!= | Not equal to | x != y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
-Assignment Operators
Operator | Use | Syntax |
---|---|---|
= | x = 1 | x = 1 |
+= | x += 1 | x = x + 1 |
-= | x -= 1 | x = x - 1 |
*= | x *= 1 | x = x * 1 |
/= | x /= 1 | x = x / 1 |
%= | x %= 1 | x = x % 1 |
//= | x //= 1 | x = x // 1 |
**= | x **= 1 | x = x ** 1 |
&= | x &= 1 | x = x & 1 |
|= | x |= 1 | x = x | 1 |
^= | x ^= 1 | x = x ^ 1 |
>>= | x >>= 1 | x = x >> 1 |
<<= | x <<= 1 | x = x << 1 |
Punctuators
These are symbols used to organize statements, or structures in a program. Examples are ' " \ # @ , : = ( ) [ ] { }