Literals

What are Python Literals?

Python literals or constants are the notation for representing a fixed value in source code. In contrast to variables, literals (123, 4.3, “Hello”) are static values or you can say constants which do not change throughout the operation of the program or application. For example, in the following assignment statement.

x =10

Here 10 is a literal as numeric value representing 10, which is directly stored in memory. However,

y = x*2

Here, even if the expression evaluates to 20, it is not literally included in source code. You can also declare an int object with built-in int() function. However, this is also an indirect way of instantiation and not with literal.

x =int(10)

Different Types of Python Literals
Python provides following literals which will be explained this tutorial:

Integer Literal
Float Literal
Complex Literal
String Literal
List Literal
Tuple Literal
Dictionary Literal
Python Integer Literal
Any representation involving only the digit symbols (0 to 9) creates an object of int type. The object so declared may be referred by a variable using an assignment operator.

Integer literals consist three different types of different literal values decimal, octal, and hexadecimal literals.

1. Decimal Literal
Decimal literals represent the signed or unsigned numbers. Digitals from 0 to 9 are used to create a decimal literal value.

Look at the below statement assigning decimal literal to the variable −

x = 10
y = -25
z = 0
2. Octal Literal
Python allows an integer to be represented as an octal number or a hexadecimal number. A numeric representation with only eight digit symbols (0 to 7) but prefixed by 0o or 0O is an octal number in Python.

Look at the below statement assigning octal literal to the variable −

x = 0O34
3. 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.

Look at the below statement assigning hexadecimal literal to the variable −

x = 0X1C
However, it may be noted that, even if you use octal or hexadecimal literal notation, Python internally treats them as of int type.

Example
# 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))
When you run this code, it will produce the following output −

0O34 in octal is 28 <class 'int'>
0X1c in Hexadecimal is 28 <class 'int'>
Python Float Literal
A floating point number consists of an integral part and a fractional part. Conventionally, a decimal point symbol (.) separates these two parts in a literal representation of a float. For example,

Example of Float Literal
x = 25.55
y = 0.05
z = -12.2345
For a floating point number which is too large or too small, where number of digits before or after decimal point is more, a scientific notation is used for a compact literal representation. The symbol E or e followed by positive or negative integer, follows after the integer part.

Example of Float Scientific Notation Literal
For example, a number 1.23E05 is equivalent to 123000.00. Similarly, 1.23e-2 is equivalent to 0.0123

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *