K.C.S.
KASI NADAR COLLEGE OF ARTS & SCIENCE
(Belongs to S.V.H.N.A.Dharma Fund)
LITERALS IN PYTHON PROGRAMMING
By,
Mrs.B.VIJAYALAKSHMI,
Assistant Professor,
Department Of Computer Science.
Literals in Python
• A literal is a sequence of one or more characters that stands for itself.
• Literals often referred to as constant values that have a fixed value.
Python allows several kinds of literals:
• Numeric Literals
• String Literals
Numeric Literals
• A numeric literal is a literal containing only the digits 0–9, a sign character (1
or 2) and a possible decimal point.
• Commas are never used in numeric literals.
There are 3 Numerical types
• integers : positive or negative whole numbers with no decimal points
( Eg : 2, -6, 3, 5, 7 )
• Floating point : Consists of Fractional part. ( Eg. 1.4, 5.6 )
• Complex numbers
Limits of Range in floating point representation
• No limit to the size of an integer that can be represented in Python.
• Floating-point values, Python providing a range of 10 -308
to 10 +308
with 16 to 17 digits of precision.
Limitations of floating point representation
• Arithmetic overflow problem :
If you are trying to multiply two long floating point integers,
we get the result as inf ( infinity )
• Arithmetic underflow problem :
This problem occurs in division.
If denominator is larger than numerator, then it will result in
zero.
Eg: 1/10000=0.00001
Limits of precision in floating point representation
• Loss of precision problem :
This problem occurs in division.
If numerator divided by denominator, then if the result is
never ending.
Eg : 10/3 = 3.33333
String literals in Python
• Single Quote ‘ ‘
• Double Quote “ “
• Triple Quote ‘’’ ‘’’
Represent the string either in ‘ PYTHON ’ or “ PYTHON “
Triple Quote is used to represent “ multi line string “
Eg :
‘’’ WELCOME TO
PYTHON PROGRAMMING ‘’’
Number Formatting
• The built-in format function can be used to produce a value containing a
specific number of decimal places.
Syntax :
format(value, format_specifier)
where value is the value to be displayed
format_specifier can contain specific number of
decimal places.
Eg: format(12/5, '.2f') -> '2.40'
format(5/7, '.2f') -> '0.71‘
format specifier '.2f' rounds the result to two decimal places.
String Formatting
• A built-in-function used to control how the strings are displayed.
Syntax :
format(value, format_specifier)
where value is the value to be displayed
format_specifier can contain a combination of
formatting options.
• Formatting options can be left ( ‘<‘ ), right ( ‘ > ‘ ), center ( ‘ ^ ‘)
• Default formatting is LEFT justified
String Formatting Example
• To produce the string 'Hello' left-justified in a field width of 20 characters
would be done as follows :
format('Hello', ' < 20') ➝ 'Hello '
• To right-justify the string, the following would be used
format('Hello', ' > 20') ➝ ' Hello‘
• To center the string the '^' character is used
format('Hello', '^20') ➝ ' Hello ‘
ESCAPE SEQUENCE IN PYTHON
• Control characters are nonprinting characters used to control the display of output.
• Represented by a combination of characters called an escape sequence .
• An escape sequence is a string of one or more characters used to denote control characters.
• To print the special characters in the screen, we use the escape sequence.
• The backslash (\) serves as the escape character in Python.
EXAMPLE :
>>>print('Hello\nJennifer Smith')
O/P:
Hello
Jennifer Smith
>>> print ('what\'s your name?')
O/P : what's your name?
>>> print ('what's your name?')
THANK YOU!!!