0% found this document useful (0 votes)
3 views16 pages

Python Unit 1(Lecture 2)

The document provides an overview of the basic tokens in Python programming, including keywords, identifiers, literals, operators, and punctuators. It explains the characteristics and rules associated with each token type, such as the predefined keywords, naming conventions for identifiers, and types of literals like strings and numbers. Additionally, it covers the various operators used in Python and the role of punctuators in structuring code.

Uploaded by

upasanabishtiitm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Python Unit 1(Lecture 2)

The document provides an overview of the basic tokens in Python programming, including keywords, identifiers, literals, operators, and punctuators. It explains the characteristics and rules associated with each token type, such as the predefined keywords, naming conventions for identifiers, and types of literals like strings and numbers. Additionally, it covers the various operators used in Python and the role of punctuators in structuring code.

Uploaded by

upasanabishtiitm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Basics of Python

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 has following Tokens:


Keywords
Identifiers
Literals
Operators
Punctuators
Keywords
• Keywords are predefined words with special meaning to the language
compiler or interpreter. These are reserved for special purpose and must
not be used as normal identifier names.
• Python programming language contains the following keywords:
False assert del for in or
while None break elif from is
pass with True class else global
lambda raise yield and continue except
if nonlocal return as def finally
import not try async await
Identifiers
• Identifiers are the names given to different parts of the program viz.
variables, objects, classes, functions, lists, dictionaries and so forth.

• The naming rules for Python identifiers can be summarized as follows:

Variables names must only be a non-keyword word with no spaces in


between.
Variable names must be made up of only letters, numbers, and
underscore.
Variable names cannot begin with a number, although they can contain
numbers.
Literals
• Literals are data items that have a fixed/constant value.

• 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

>>>print(“Hello\ multi line string


World”)

>>>print(’’’Hello No backslash needed


World’’’)
Escape Sequences
• In Strings, you can include non-graphic characters through escape
sequences.

\\ 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.

The integer literals can be written in:


Decimal Form: an integer beginning with digits 1-9 e.g. 1234, 4100 etc.
Octal Form: an integer beginning with 0o(Zero followed by letter o) e.g.
0o35, 0o77 etc. Here do remember that for octal 8 and 9 are invalid digits.
Hexadecimal Form: an integer beginning with 0x(Zero followed by letter x)
e.g. 0x73, 0xAF etc. Valid digits for hexadecimal numbers are 0-9 and A-F.
 Floating Point Literals: Floating point literals or real literals floats
represent real numbers and are written with a decimal point dividing the
integer and fractional parts. These can be written in fractional form e.g. -
13.0, .75 etc. or in Exponent form e.g. 0.17E5, 3.E2, .6E4 etc.

 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:
• ’”#\()[]{}@,:.`=

You might also like