In Python, literals are used to represent fixed values in your code.
Types of Literals:
Integers
A number with no fractions.
Octal Number – An integer with 0 in front of it. Each octal digit represents three binary bits with a base of 8.
Hexadecimal Number – An integer with 0x or 0X in front of it. Each number has a base of 16.
Floating point number
A number with nonempty decimal fractions.
Examples: 45.09, -6.00, 76.99, 1e-44, etc.
Strings
Text that is within “Glory” or ‘Glory’.
Ways to use quotes in the string:
“Yuri \" is\" the President”
“Yuri 'is' the President”
'Yuri “is” the President'
Booleans
Value is either True or False
True
or1
False
or0
Sequence literals:
i. List literals: An ordered collection of values enclosed in square brackets.
For example:
numbers = [1, 2, 3]
fruits = ['apple', 'banana', 'orange']
ii. Tuple literals: An ordered collection of values enclosed in parentheses.
For example:
coordinates = (10, 20)
Mapping literals:
i. Dictionary literals: Unordered collection of key-value pairs enclosed in curly braces.
For example:
person = {'name': 'John', 'age': 25}
ii. Set literals: Unordered collection of unique elements enclosed in curly braces.
For example:
unique_numbers = {1, 2, 3}
None literal
Represents the absence of a value and is denoted by the keyword None. It is often used to indicate a missing or undefined value.
For example
result = None
Example
#Numeric literals
x = 10 y = 3.14 z = 2 + 3j
#String literals
name = 'John' message = "Hello, World!"
#Boolean literals
is_true = True is_false = False
#List literals
numbers = [1, 2, 3] fruits = ['apple', 'banana', 'orange']
#Tuple literals
coordinates = (10, 20)
#Dictionary literals
person = {'name': 'John', 'age': 25}
#Set literals
unique_numbers = {1, 2, 3}
#None literal
empty_value = None