0% found this document useful (0 votes)
8 views7 pages

Classwork 94226

The document provides an overview of literals in Python programming, categorizing them into string, numeric, boolean, and special literals. It explains string literals, including single-line and multiline strings, as well as numeric literals like integer, floating-point, and complex literals. Additionally, it covers the concept of boolean literals and the special literal 'None', along with examples and questions for practice.

Uploaded by

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

Classwork 94226

The document provides an overview of literals in Python programming, categorizing them into string, numeric, boolean, and special literals. It explains string literals, including single-line and multiline strings, as well as numeric literals like integer, floating-point, and complex literals. Additionally, it covers the concept of boolean literals and the special literal 'None', along with examples and questions for practice.

Uploaded by

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

Python programming

language

PART-II

1|Page
III. Literals:- Literals or constants are data items that are fixed values. Several kinds of literals are

1. String Literals:- A text enclosed in ′ ′ or ″ ″ form a string literal.

Eg: ′ a ′ , ″ a ″ , ′ xy ′ , ″ 123 ″

Python allows certain non graphic characters in string values. Non-graphic characters are those
characters that cannot be typed directly from the keyboard . They are known as escape sequence. These
characters are represented by a backslash(\) followed by one or more characters

Escape Sequence Explanation

\n New line character

\b Backspace

\t Horizontal tab

\v Vertical tab

\a Alert bell

There are two type of strings

❑ Single Line Strings

❑ Multiline Strings

• Single Line Strings:- The strings that are created by enclosing in single quotes or double quotes
(′ ′ or ″ ″ ) that are terminated in a single line are Single Line Strings. Python by default
creates single line strings. So if at the end of a string if there is no closing quotation for an open
quotation it reports an error.

Eg: ′ This is python Programming ′

″ this is my book ″

″ This is class xi ′ → Reports an error

′ python programming→ Error

• Multiline Strings:- The strings that are created by enclosing in single or double quotes
which can be in multiple lines are Multiline Strings. Multiline strings can be created in 2
ways
2|Page
❖ By adding a backslash at the end of normal single/double quote strings. (Add a backslash
before pressing the enter key).Do not intent when continuing in the next line after

pressing \.

Eg: Text= ″ Programming\

Language ″

Text= ″ Programming\

Language” reports an error as the text is indented after pressing the

enter key

❖ By typing the text in triple single or double quotation marks.

Eg:Text = ′ ′ ′ Welcome

to

python ′ ′ ′

OR

Text= ″ ″ ″ Welcome

to

Python

Programming ″ ″ ″

Size of Strings

The size of the string is determined as the count of the number of characters in the string.

1. ″ Hello ″-→ 5

2. ″ Python Programming ″ →18

3. ″ \ab → 2 (\a is considered as a single character.Escape sequence is considered as a single

character )

4. Seema\’s Pen- → 11

For Multiline Strings ,the EOL(End Of Line) character at the end of the line is counted.

3|Page
1. Text= ″ Python

Programming ″→ 18

2. Text= ″ a

c″→5

For Multiline strings created with backslash , the backslash is not counted

Text= ″ Python\

Programming ″ → 17

Text= ″ a\

b\

c″→3

The size of the string can be determined using the len()

Text= ′ Welcome′

len(Text)

Output-7

A= ″ Computer Science with Python ″

len(A)

Output-28

2. Numeric Literal:- The numeric literal is classified into

a. Integer Literal

b. Floating point Literal

c. Complex Literal

a. Integer literal:- They are reffered as integers or int .They represent +ve or –ve

numbers without any decimal point. A number with no sign is considered as an +ve

number. No commas are allowed in between numbers ie 34,987 is not allowed .

Instead we can write as 34987.


4|Page
Integer literals are classified into 3

i. Decimal Integer Literal:- An integer literal consisting of numbers 0 to 9 which does not

start with a 0 is considered as an decimal integer literal

Eg: 1234,789,304

ii. Octal Integer Literal:- It is an integer literal consisting of numbers 0 to 7 which starts with

0o (Digit 0 followed by alphabet o) Eg:0o156,0o709

iii. Hexadecimal Integer Literal: It is an integer literal consisting of numbers 0 to 15 where 0 to 9

are represented by 0 to 9 itself and 10 to 15 are presented by A to F. It is preceded with 0x or

0X

Eg: 0x179,0XAF3,0x79C

b. Floating point literal:- They are also called as real literal. They are numbers having a fractional part.

They can be written in 2 forms .

Fractional Form:- A real literal in fractional form consist of signed or unsigned digits including a

decimal point between the numbers . It should have at least one digit either before or after the

decimal point

Eg2.57, .7, 3. , -23.67,-0.006

Exponent Form:- A real literal in exponent form consist of 2 parts.

Mantissa part and Exponent part

Exponent Part

2.5 E 5

Mantissa Part

5|Page
The Mantissa part can be either a real or integer constant. It is followed by the letter E or e .

The exponent must be an integer.

Eg: 2.5 X 109 → 2.5 E 9

0.00625 X 10 -4 →0.00625 e -4

1.2567 → 125.67 e -2 or 0.0012567 e 3

45.67 → 4.567 E 1 or 456.7 E-1

c. Complex Literal :- They are numbers in the form of a + ib where a and b are floating point

values and i represents √-1 which is an imaginary number. a is the real part and b is the imaginary part

3. Boolean Literal:- A Boolean literal is used to represent one of the two values True or False.

4.Special Literal None:-The None literal is used to represent the absence of a value. The value None

means “There is Nothing”. It does not display anything when asked to display the value of a variable

containing None

Qn: Identify the type of literal

i. 23.789

ii. 23789

iii. True

iv. ‘True’

v. O124

vi. OxA4F6

vii. None

Qn: What will be the size of the following constant

i. ‘\a’

ii. “\a”

iii. “It’s”

6|Page
iv. xy\

yz”

v. “ “ “ xy

yz” ” ”

Qn: Write the following in Exponent form

1. 23.56 X106

2. 0.00567

3. 12.678

4.1356

5. 1.67 X 10 7

7|Page

You might also like