While reading materials for programming in python, you must have encountered certain phrases like keywords, variables, constants and literals. In this article, we will study the underlying concepts for definition and usage of literals in python.
What are Python literals?
Literals are the raw data that are assigned to variables or constants while programming. In python, we have different types of literals such as string literals, numeric literals, boolean literals and a special literal None. In the following sections, we will study each type of python literal and will also study about literal collections.
What are string literals in Python?
String literals are sequences of characters surrounded by single, double or triple quotes. In python we have two types of string literals namely single line strings and multiline strings.
A single line string is the string literal that terminates on encountering a newline. It can be defined by enclosing one or more characters inside single or double quotes as follows.
myString="This is a single line string" anotherString='This is also a single line string'
A multiline string can be defined by enclosing characters expanding to multiple lines in triple quotes as follows.
myString="""This is a multiline string. """
What are numeric literals?
Numeric literals are used to represent numbers in a python program.In python we have different types of numeric literals such as integers, floating point numbers and complex numbers.
Integers in python are numbers with no fractional component. An integer representing a decimal number can be defined as follows.
We can also define integers from other number systems. A binary number is represented as a numeric literal starting with ‘0b’ and consists of digits 0 and 1 as follows.
A number in the hexadecimal number system starts with ‘0x’ and can be represented in python as follows.
An integer literal in the octal number system starts with ‘0o’ and can be represented in python as follows.
A floating point literal in python represents a real number which consists of Integral as well as fractional part. A floating point number in the decimal number system can be represented as follows.
Complex numbers are in the form of a+bj where ‘a’ represents the real part and ‘b’ represents the imaginary part of the complex number. A numeric literal representing a complex number can be written as follows.
What are boolean literals in Python?
In python, there are two types of boolean literals namely True and False. They can be defined in a program as follows.
The special literal- None
The literal ‘None’ is used to specify that the variable to which it is assigned does not refer to any object. A variable with value ‘None’ can be defined as follows.
Literal collections in Python
There are different types of collection objects such as python dictionaries, lists, sets and tuples in which we can store the python literals.
We can store different types of data in a python list as follows.
We can even break a string literal at spaces using python string split operation and keep the substrings in a list as follows.
myStr="I am a single line string" myList=myStr.split() print("String literal is:",myStr) print("Created list is:",myList)
String literal is: I am a single line string Created list is: ['I', 'am', 'a', 'single', 'line', 'string']
A tuple also contains data just as lists but tuples are immutable and new elements cannot be added to it or any element cannot be deleted from a tuple. We can define a tuple as follows.
A python dictionary contains data in the form of key value pairs as follows.
A set in python can hold unordered and non duplicate data . Each element in a set is unique. We can define a set containing several elements as follows.
In this article, we have learnt about different types of python literals and we have also seen how collections such as lists, dictionaries, tuples and sets are used to store literals in python. Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.