Tokens in
Python
► The smallest unit/element in the
python program/script is known as a
Token or a Lexical unit.
► Python has following Tokens:
► Keywords
► Identifiers
► Literals
► Operators
► Punctuators
Keywor
ds
Keywords are the reserve words/pre-defined
words/special words of python which have a special
meaning
False for the
Trueinterpreter.
None def if
lambda class yield continue else
assert or while break elif
del from is not pass
for global finally import as
in nonlocal return With And
int except Raise print
Identifiers(user defined
names)
►Identifiers are the name given to the
different programming elements like
variables, objects,
classes, functions, lists, dictionaries etc.
Non- • Must be • Cannot
keyword made up of begin
word only letters, with a
with no numbers Number,
Rule 2
Rule 3
Rule
space in and although
between Underscore( may
1
_) contain
numbers
\
Identifie
rs
Some Valid
Identifiers:
Identifie
rs
Some Invalid
Identifiers:
VID-
REC
• Contains special character -
(hyphen)
2020RN
O
• Starting with a
Digit
continu
e• Reserved
keyword
Emp.Co
de
• Contains special
character . (dot)
Litera
►lsLiterals are the data items that have a
fixed or
constant value
String Literals
►A string literal is a sequence of characters
surrounded by Quotes (Single, Double or
Triple Quotes).
Single Line
Strings
• string that
terminate
in one line
eg- ”I am a student”, ”hi
friends”
MultiLine Strings
the strings that are spread across multiple lines .These strings are created in
foloowing two ways:
1.By adding a backslash at the end of physical line
eg-
txt_1=“hello\
world!’
2.By enclosing the multiple lines of string in triple quotes
eg-
text_2=“”hello world!
welcome!!
to the world of python.””
String Literals
Single line String
>>>Txt1 = “Hello World”
Multiline string
>>>Txt2 =
“Hello\ World”
>>>Txt3
=‘’’Hello
World”’
Escape sequences – Python allows
Numeric Literals
► Numeric Literals are numeric values
like integer
floating point number or a complex
number
float long
• long integers or
longs are
integers of
unlimited size
Numeric Literals
► integer
literals
Decimal Octal Hexadecim
Form Form al
form
• Digits 0- • Begin with • Begin with Ox
9 Oo • Base 16(0-
• Base 10 • Base 8(0-7) 9,a-f)
• e.g. • e.g. 0o32 • e.g.OxAF
1234
Numeric Literals
► Floating Point Literals/ Real Literals &
Complex Numbers n
Floating point Complex Number
• Decimal point • a+bj form
divides the • a & b are real
integer and • j = v-1, imaginary
fraction • e.g. 2+3j
part
• e.g. -13.0, 3.E2,
0.17E5
Boolean Literals
► A Boolean literal in Python is used to
represents
one of the two Boolean Values i.e. True or
False
Boolea Boolea
n n
True False
Special Literal – None(empty legal value)
In python, None literal is used to indicate
something that has not yet been
created /absence of value
Operators in
Python
►Operators are the symbols or words that
perform some kind of operation on given values
(operands) in an expression and returns the result.
arithmetic • +,-,/,*,%,**,//
bitwise • &, ^, |
identity • is, is not
relational •>,<,>=,<=,==,!=
logical • and, or,not
shift • <<, >>
Assignment •=
Membership • in, not in
arithmetic-assignment
+=,-=,//=,**=,*=,/=
Punctuators in Python
►Punctuators are the symbols that are
used in programming language to organize
sentence structure, indicate the rhythm and
emphasis of expressions, statements and
Program Structure.
► Common Punctuators are:
‘ ‘’ “” # \ (){}[] @,
, : .;
Tokens in
Python
Keywords are the reserve words of python which have a special
meaning for
the interpreter.
Identifiers are the name given to the different programming
elements like variables, objects, classes, functions, lists,
dictionaries etc.
Literals are the data items that have a fixed /constant value.
Operators are the symbols or words that perform some kind
of operation on
given values (operands) in an expression and returns the
result.