Python Literals: Constants and Values

thumb_up 1  ·  sell in the assignment statement. x = 10 Here 10 is a literal as num, a literal is a notation for representing a fixed value in source, it is not literally included in source code. You can also declar, this is also an indirect way of instantiation and not with liter, Python internally treats it as of int type. # Using Octal notat, prefixed by 0x or 0X represents an integer in Hexedecimal form. , it will produce the following output − 0O34 in octal is 28 0X1, type(x)) # Using Hexadecimal notation x = 0X1c print ("0X1c in H, a decimal point symbol (.) separates these two parts in a litera, a scientific notation is used for a compact literal representati, x = 25.55 y = 0.05 z = -12.2345 For a floating point number whic, type(x)) # Using Scientific notation x = 1.23E5 print ("1.23E5 i, 1.23e-2 is equivalent to 0.0123 # Using normal floating point n, you will get the following output − 1.23 in normal float litera, a literal representation of a complex number takes a form x+yj. , type(x)) This code will produce the following output − 2+3j com, type(var1)) var3='''hello''' print ("''''hello'''' in triple quo, type(var1)) var4="""hello""" print ('"""hello""" in triple quote, double quotes ("hello") or triple quotes ('''hello''' or """hell, you will get the following output − 'hello' in single quotes is, string should be written in double quotes. var1='Welcome to "Py, the string itself should be put in single quotes. On the other h, True] Tuple Literal Tuple object in Python is a collection of o, which means a comma separated sequence without parentheses also , True] Default delimiter for Python sequence is parentheses, True] Dictionary Literal Like list or tuple, each of which is a key-value pair. Value is bound to key by the , string or tuple can be used as key. Key cannot appear more than , only the last one will be retained. Values can be of any data ty, Python literals explanation, Types of literals in Python, Python constant values

In computer science, a literal is a notation for representing a fixed value in source code. For example, in the assignment statement.

x = 10

Here 10 is a literal as numeric value representing 10 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 −

x = int(10)

However, this is also an indirect way of instantiation and not with literal.

You can create use literal representation for creating object of any built-in data type.

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.

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. A numeric representation with only eight digit symbols (0 to 7) but prefixed by 0o or 0O is an octal number.

x = 0O34

Similarly, a series of hexadecimal symbols (0 to 9 and a to f), prefixed by 0x or 0X represents an integer in Hexedecimal form.

x = 0X1C

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

 
# 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'>

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,

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.

For example, a number 1.23E05 is equivalent to 123000.00. Similarly, 1.23e-2 is equivalent to 0.0123

 
# 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))

Here, you will get the following 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''>

Complex Literal

A complex number comprises of a real and imaginary component. The imaginary component is any number (integer or floating point) multiplied by square root of "-1"

(√−1). In literal representation (√−1) is representation by "j" or "J". Hence, a literal representation of a complex number takes a form x+yj.

 
#Using literal notation of complex number x = 2+3j print ("2+3j complex literal is", x, type(x)) y = 2.5+4.6j print ("2.5+4.6j complex literal is", x, type(x))

This code will produce the following output −

2+3j complex literal is (2+3j) <class 'complex'>
2.5+4.6j complex literal is (2+3j) <class 'complex'>

String Literal

A string object is one of the sequence data types in Python. It is an immutable sequence of Unicode code points. Code point is a number corresponding to a character according to Unicode standard. Strings are objects of Python’s built-in class ‘str’.

String literals are written by enclosing a sequence of characters in single quotes ('hello'), double quotes ("hello") or triple quotes ('''hello''' or """hello""").

 
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))

Here, you will get the following 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 is: hello <class 'str'>

If it is required to embed double quotes as a part of string, the string itself should be put in single quotes. On the other hand, if single quoted text is to be embedded, string should be written in double quotes.

 
var1='Welcome to "Python Tutorial" from TutorialsPoint' print (var1) var2="Welcome to 'Python Tutorial' from TutorialsPoint" print (var2)

It will produce the following output −

Welcome to "Python Tutorial" from TutorialsPoint
Welcome to 'Python Tutorial' from TutorialsPoint

List Literal

List object in Python is a collection of objects of other data type. List is an ordered collection of items not necessarily of same type. Individual object in the collection is accessed by index starting with zero.

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))

It will produce the following output −

[1, 'Ravi', 75.5, True] <class 'list'>

Tuple Literal

Tuple object in Python is a collection of objects of other data type. Tuple is an ordered collection of items not necessarily of same type. Individual object in the collection is accessed by index starting with zero.

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))

It will produce the following output −

[1, 'Ravi', 75.5, True] <class tuple>

Default delimiter for Python sequence is parentheses, which means a comma separated sequence without parentheses also amounts to declaration of a tuple.

 
T1=1,"Ravi",75.50, True print (T1, type(T1))

Here too, you will get the same output −

[1, 'Ravi', 75.5, True] <class tuple>

Dictionary Literal

Like list or tuple, dictionary is also a collection data type. However, it is not a sequence. It is an unordered collection of items, each of which is a key-value pair. Value is bound to key by the ":" symbol. One or more key:value pairs separated by comma are put inside curly brackets to form a dictionary object.

capitals={"USA":"New York", "France":"Paris", "Japan":"Tokyo", "India":"New Delhi"} numbers={1:"one", 2:"Two", 3:"three",4:"four"} points={"p1":(10,10), "p2":(20,20)}

Key should be an immutable object. Number, string or tuple can be used as key. Key cannot appear more than once in one collection. If a key appears more than once, only the last one will be retained. Values can be of any data type. One value can be assigned to more than one keys. For example,

staff={"Krishna":"Officer", "Rajesh":"Manager", "Ragini":"officer", "Anil":"Clerk", "Kavita":"Manager"}
 
 
 
 
The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.
 

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back