As in every programming language the smallest unit used to build up hey program it’s called a token. In other words, every individual word or symbol that we write in our programs it’s called a token.
Python facilitates the following file tokens:
Keywords are specific words that have specific meaning for the compiler or the interpreter. These are reserved words and are used in the program exactly as they are. In Python 3.8, there are 35 different keywords that we can use. These are as follows:
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 |
variable is a very important part of every programming language. Since the very early programming languages, variables have been used extensively. In python variables are treated a little different than the other languages. Here objects, classes, functions, lists, dictionaries etc. all are treated as variables.
The naming rules for variables:
Literals are nothing but the fixed or constant values that are often stored with the names of variables. The kinds of literals allowed by python are as under:
String Literals: Strings are sequence of characters covered by quotes. Single quotes and double quotes often work the same but there is also a use of triple quotes. Unlike of other languages, in python, strings can either be single line or multi-line.
Single Line Strings: These strings start and end in the same line. Technically the ending quote is considered by python as an EOL or (End of Line) hence it must be in the same line for single line strings.Ex.:
Multiline Strings: These strings can be spread over multiple lines. As we know, ending quotes are considered as EOL, there must be another EOL so that the string can be spread into another line. That another EOL is a backslash (\). However, if we use triple quotes (single * 3), this backslash is not needed.
Or
NOTE: The reason behind the liberty to not use backslash in triple quote strings, is that an escape sequence (\n) is added as EOL(end of line) by python itself where the line ends.
Escape Sequences: Escape sequences are used in strings to allow many other non-graphic characters that are not in ASCII. Following is a list of major escape sequences:
Numeric Literals: Numeric literals are the numeric values that are used in calculations. In python following types of literals are allowed:
Int (Integers): These are numeric values as whole numbers. Unlike of C language, here is no any concept of unsigned, hence python integers are signed integers. Integers are also of three different types:
Decimal int: Integer values beginning with digits 1 to 9. These numeric literals are considered by python as decimal numbers. Ex.: 248, 8109 etc.
Octal int: Integer values beginning with digits 0o (a zero followed by an alphabet o). These numeric literals are considered by python as octal numbers. Ex.: 0o241, 0o87 etc.
Hexadecimal int: Integer values beginning with digits 0x (a zero followed by an alphabet x). These numeric literals are considered by python as Hexadecimal numbers. Ex.: 0xd1, 0xaf etc.
Float (Floating Point or Real numbers): These are numeric values with a decimal point in between. The value left to it is an integer and the value right to it is a fraction. Python allows following two types of float values:
float in fractional form: A float in its basic form. Ex.: 3.14, 57.427 etc.
float in exponential form: A float in its exponential form. Ex.: 0.314E1, 0.057427E3.
here, E is a symbol of exponent hence the part after E is an exponent to 10.
0.314E1 refers to 0.314 * 101 that again refers to 3.14
0.057427E3 refers to 0.057427 * 103 that again refers to 57.427
Boolean Literals: A Boolean literal is either True or False. It is something that represents Boolean values True (1) or False (0).
None Literal: None is itself a literal in python. It is a special literal that indicates absence of any value in a variable.
Operators are symbols that operate some operation on operands. Usually in an expression, there are many operands written along with operators. Ex.: 43 + 46, 34 > 89 etc.
In python, operators are of many different types like-
arithmetic (+, -, *, /, %, **, //),
bitwise (&, ^, |),
shift (<<, >>),
identity (is, is not),
relational (<, >, <=, >=, ==, !=),
logical (and, or),
assignment (=),
membership (in, not in) and
arithmetic assignment (+=, -=, *=, /=, %=, **=, //=).
Punctuators are nothing but the symbols that represent start or end of a statement, block or a section. The common punctuators are: ‘, “, #, \, (), {}, [], ,, :, ., `, = etc.