Python Unit 1(Lecture 2)
Python Unit 1(Lecture 2)
Programming
(UNIT-1)
Lecture-2
Tokens in Python
• The smallest individual unit in a program is known as a Token or a lexical
unit.
• Python allows several kinds of literals, which are being given below:
String Literal
Numeric Literals
Boolean Literals
Special Literal None
String Literal
• A string literal is a sequence of characters surrounded by quotes(single or
double or triple quotes).
• String literals can either be single line strings or multi-line strings.
• Single line strings must terminate in one line i.e. the closing quotes
should be on the same line as that of the opening quotes.
• Multiline Strings are stings spread across multiple lines. With single and
double quotes, each line other than the concluding line has an end
character as \ (backslash) but with triple quotes, no backslash is needed
at the end of intermediate lines.
>>>print(“Hello World”) single line string
\\ Backslash(\)
\’ Single Quote(’)
\” Double Quote(”)
\a ASCII Bell(BEL)
\b ASCII Backspace(BS)
\f ASCII Formfeed(FF)
\n New Line Character
\N{name} Character named name in the Unicode database
\r Carriage Return
\t Horizontal Tab
\uxxxx Character with 16-bit hex value xxxx
\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx
\v ASCII Vertical Tab
\ooo Character with octal value ooo
\xhh Character with hex value hh
Numerical Literals
• Numeric literals are numeric values and these can be one of the following
types:
int(signed integer) often called just integers or ints are positive or
negative whole numbers with no decimal point.
Complex Number Literals: Complex number literals are of the form a+bJ,
where a and b are floats and J(or j) represents , which is an imaginary
number). A is the real part of the number, and b is the imaginary part.
Boolean Literals
• A Boolean literal in Python is used to represent one of the two Boolean
values i.e., True or False. A Boolean literal can either have value as True or
as False.
Special Literal None
• Python has one special literal, which is none. The None literal is used to
indicate absence of value.
• Python can also store literal collections, in the form of tuples and lists
etc.
Operators
• Operators are tokens that trigger some computation/action when applied
to variables and other objects in an expression.
• The operators can be arithmetic operators(+,-,*,/,%,**,//), bitwise
operators(&,^,|),shift operators(<<,>>), identity operators(is, is not),
relational operators(>,<,>=,<=,==,!=), logical operators(and, or),
assignment operator(=), membership operators(in, not in), and arithmetic
assignment operators(/=,+=,-=,*/,%=,**=,//=).
Punctuators
• Punctuators are symbols that are used in programming languages to
organize sentence structures, and indicate the rhythm and emphasis of
expressions, statements, and program structure.
• Most common punctuators of Python programming are:
• ’”#\()[]{}@,:.`=