UNIT1 Literals
UNIT1 Literals
ArtificialIntelligence
Intelligenceusing
usingPython-
Python-24U5CAC06
24U5CAC06
VIVEKANANDHA
College of Arts and Sciences for Women (Autnomous)
PG AND RESEARCH DEPARTMENT OF COMPUTER SCIENCE & APPLICATIONS
Handling by
Dr.N.Jean Effil
1
VICAS
Artificial Intelligence using Python- 24U5CAC06
VIVEKANANDHA
College of Arts and Sciences for Women (Autnomous)
PG AND RESEARCH DEPARTMENT OF COMPUTER SCIENCE & APPLICATIONS
AGENDA
Python Literals
2
VICAS
What is Literals?
• A constant is a variable whose value cannot be
modified.
• Literals are raw values or data that are stored in
a variable or constant.
• literals (123, 4.3, "Hello") are static values
Eg. x = 10
Here 10 is a literal as numeric value
representing 10, which is directly stored in
memory
VICAS 3
Types of Python Literals
VICAS 4
Python - Integer Literal
• Any representation involving only the digit
symbols (0 to 9) creates an object of int type.
• Example: Decimal Literal
Take a look at the following example −
x = 10
y = -25
z=0
Python allows an integer to be represented as an
octal number or a hexadecimal number.
VICAS 5
Integer literal
• Example: Octal Literal
• P. A numeric representation with only eight digit symbols
(0 to 7) but prefixed by 0o or 0O is an octal number in
Python.
• x = 0O34
• Example: Hexadecimal Literal
• Similarly, a series of hexadecimal symbols (0 to 9 and a to
f), prefixed by 0x or 0X represents an integer in
Hexedecimal form in Python.
• x = 0O34
VICAS 6
# Using Octal notation
x = 0O34
print ("0O34 in octal is", x, type(x))
# Using Hexadecimal notation
x = 0X1c
print ("0X1c in Hexadecimal is", x, type(x))
OUTPUT
0O34 in octal is 28 <class 'int'>
0X1c in Hexadecimal is 28 <class 'int'>
VICAS 7
Float Literal
# Using normal floating point notation
x = 1.23
print ("1.23 in normal float literal is", x, type(x))
# Using Scientific notation
x = 1.23E5
print ("1.23E5 in scientific notation is", x, type(x))
x = 1.23E-2
print ("1.23E-2 in scientific notation is", x, type(x))
OUTPUT
1.23 in normal float literal is 1.23 <class 'float'>
1.23E5 in scientific notation is 123000.0 <class 'float''>
1.23E-2 in scientific notation is 0.0123 <class 'float''>
VICAS 8
Python - Complex Literal
OUTPUT
2+3j complex literal is (2+3j) <class 'complex'>
2.5+4.6j complex literal is (2+3j) <class 'complex'>
VICAS 9
String Literal
var1='hello'
print ("'hello' in single quotes is:", var1,
type(var1))
var2="hello"
print ('"hello" in double quotes is:', var1,
type(var1)) var3='''hello'''
print ("''''hello'''' in triple quotes is:", var1,
type(var1)) var4="""hello"""
print ('"""hello""" in triple quotes is:', var1,
type(var1))
OUTPUT
'hello' in single quotes is: hello <class 'str'>
"hello" in double quotes is: hello <class 'str'>
''''hello'''' in triple quotes is: hello <class 'str'>
"""hello""" in triple quotes
VICAS is: hello <class 'str'> 10
List literal
Literal representation of a list object is done with one or more
items which are separated by comma and enclosed in square
brackets [].
L1=[1,"Ravi",75.50, True]
print (L1, type(L1))
OUTPUT
[1, 'Ravi', 75.5, True] <class 'list'>
VICAS 11
Tuple literal
Literal representation of a tuple object is done with one or more
items which are separated by comma and enclosed in
parentheses ().
T1=(1,"Ravi",75.50, True)
print (T1, type(T1))
OUTPUT
[1, 'Ravi', 75.5, True] <class tuple>
VICAS 12
Dictionary Literals
VICAS 13