Literals in Python
Generally, literals are a notation for representing a fixed value in source code. They can also be defined as raw value or data given in variables or constants.
Example:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Python3
# Numeric literalsx = 24y = 24.3z = 2+3jprint(x, y, z) |
24 24.3 (2+3j)
Here 24, 24.3, 2+3j are considered as literals.
Python has different types of literals.
- String literals
- Numeric literals
- Boolean literals
- Literal Collections
- Special literals
String literals
A string literal can be created by writing a text(a group of Characters ) surrounded by the single(”), double(“”), or triple quotes. By using triple quotes we can write multi-line strings or display in the desired way.
Example:
Python3
# string literals# in single quotes = 'geekforgeeks'# in double quotest = "geekforgeeks"# multi-line Stringm = '''geek for geeks'''print(s)print(t)print(m) |
geekforgeeks
geekforgeeks
geek
for
geeksHere geekforgeeks is string literal which is assigned to variable(s).
Character literal
It is also a type of string literals where a single character surrounded by single or double-quotes.
Example:
Python3
# character literal in single quotev = 'n'# character literal in double quotesw = "a"print(v)print(w) |
n a
Numeric literals
They are immutable and there are three types of numeric literal :
- Integer
- Float
- Complex.
Integer :
Both positive and negative numbers including 0. There should not be any fractional part.
Example:
Python3
# integer literal# Binary Literalsa = 0b10100# Decimal Literalb = 50# Octal Literalc = 0o320# Hexadecimal Literald = 0x12bprint(a, b, c, d) |
20 50 208 299
In the program above we assigned integer literals (0b10100, 50, 0o320, 0x12b) into different variables. Here, ‘a‘ is binary literal, ‘b’ is a decimal literal, ‘c‘ is an octal literal and ‘d‘ is a hexadecimal literal. But on using print function to display value or to get output they were converted into decimal.
Float
These are real numbers having both integer and fractional parts.
Example:
Python3
# Float Literale = 24.8f = 45.0print(e, f) |
24.8 45.0
24.8 and 45.0 are floating-point literals because both 24.8 and 45.0 are floating-point numbers.
Complex Literal
The numerals will be in the form of a + bj, where ‘a‘ is the real part and ‘b‘ is the complex part.
Example:
Python3
z = 7 + 5j# real part is 0 here.k = 7jprint(z, k) |
(7+5j) 7j
Boolean literals
There are only two Boolean literals in Python. They are true and false.
Example:
Python3
a = (1 == True)b = (1 == False)c = True + 3d = False + 7print("a is", a)print("b is", b)print("c:", c)print("d:", d) |
a is True b is False c: 4 d: 7
In python, True represents the value as 1 and False represents the value as 0. In the above example ‘a‘ is True and ‘b‘ is False because 1 equal to True.
Example:
Python3
x = (1 == True)y = (2 == False)z = (3 == True)r = (1 == True)a = True + 10b = False + 10print("x is", x)print("y is", y)print("z is", r)print("a:", a)print("b:", b) |
x is True y is False z is True a: 11 b: 10
Literal Collections
There are four different types of literal collections
- List literals
- Tuple literals
- Dict literals
- Set literals
List literals
List contains items of different data types. The values stored in List are separated by comma (,) and enclosed within square brackets([]). We can store different types of data in a List. Lists are mutable.
Example :
Python3
# List literalsnumber = [1, 2, 3, 4, 5]name = ['Amit', 'kabir', 'bhaskar', 2]print(number)print(name) |
[1, 2, 3, 4, 5] ['Amit', 'kabir', 'bhaskar', 2]
Tuple literals
A tuple is a collection of different data-type. It is enclosed by the parentheses ‘()‘ and each element is separated by the comma(,). It is immutable.
Example:
Python3
# Tuple literalseven_number = (2, 4, 6, 8)odd_number = (1, 3, 5, 7)print(even_number)print(odd_number) |
(2, 4, 6, 8) (1, 3, 5, 7)
Dictionary literals
Dictionary stores the data in the key-value pair. It is enclosed by curly-braces ‘{}‘ and each pair is separated by the commas(,). We can store different types of data in a dictionary. Dictionaries are mutable.
Example:
Python3
# Dict literalsalphabets = {'a': 'apple', 'b': 'ball', 'c': 'cat'}information = {'name': 'amit', 'age': 20, 'ID': 20}print(alphabets)print(information) |
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'name': 'amit', 'age': 20, 'ID': 20}Set literals
Set is the collection of the unordered data set. It is enclosed by the {} and each element is separated by the comma(,).
Example: we can create a set of vowels and fruits.
Python3
# Set literalsvowels = {'a', 'e', 'i', 'o', 'u'}fruits = {"apple", "banana", "cherry"}print(vowels)print(fruits) |
{'o', 'e', 'a', 'u', 'i'}
{'apple', 'banana', 'cherry'}Special literals
Python contains one special literal (None). ‘None’ is used to define a null variable. If ‘None’ is compared with anything else other than a ‘None’, it will return false.
Example:
Python3
# Special literalswater_remain = Noneprint(water_remain) |
None


