Menu Close
python literals

Python Literals Tutorial

In this Python tutorial, we are going to learn about Python literals. Literals in Python are raw data that is given in variable or constant.
In the previous tutorial, we have seen detailed descriptions of all the Python keywords with appropriate examples. To learn about Python Keywords click here.

In this guide, we will see all about Python literals and their types with appropriate examples. Before going to this tutorial let’s see What is literals in Python?

What is Literals in Python ?

In Python, Literals is a raw data that is given in variable or constant. In Python, there are various types of Literals available. I think you understand, what are literals in Python.
Let’s see all the Python literals one by one with an example.

Type of Python Literals:

Here we will see a different type of literals in Python one by one with appropriate examples so that you can understand literals in Python in a better way.

Python String Literals:

In Python, A string literal is a sequence of characters surrounded by quotes is known as Python string literals.
you can use single, double, or triple quotes to create a string.

Example:

>>> x = 'Programming Funda'
>>> x
'Programming Funda'
>>> y = "Programming Funda"
>>> y
'Programming Funda'
>>> z = '''Programming Funda'''
>>> z
'Programming Funda'

Type of Python string literals:

Single line string:

Strings that are written inside single line known as single line string.

>>> z = "Programming Funda"
>>> z
Multiline String:

A string that is written in multi-line is known as a multi-line string. To use a multi-line string you have to use a backslash ( \ ) at the end of the string.

>>> x = "Programming \
...  Funda"
>>> x
'Programming  Funda'

Numeric Literals:

In Python, Numeric literals are immutable ( Unchangeable ).Numeric literals can belong to 4 different numeric types:

  • int
  • float
  • complex

How to use Numeric literals in Python ?

In Python, Numeric literals are very easy to learn, You can see following example.

>>> b = 100 # Decimal Literals
>>> a = 0b1010 # Binary Literals
>>> b = 100 # Decimal Literals
>>> c = 0o123 # Octal Literals
>>> d = 0x512 # Hexadecimal Literals
>>> a
10
>>> b
100
>>> c
83
>>> d
1298

Boolean Literals:

In Python, Boolean literal can have any of the two value: True or False.

How to use Python Boolean literals ?

To use Boolean literals you have to following example.

x  = (1 == True )
y = ( 1 == False )
a = True + 12
b = 10 - True

print(x)
print(y)
print(a)
print(b)

Output

True
False
13
9

Special Literals:

Python contains one special literal that is None. We use it to specify the field has not been created.

How to use Special in Python?

def testing():
    name = 'Programming Funda'
    category = None
    print(name)
    print(category)

testing()

Output

Programming Funda
None

Literals Colection:

In Python, There are four different literals collection ( List, Tuple, set, and dictionary ) in Python.

How to use literals collection in Python?

langaueg = ['Python', 'C', 'C++', 'PHP'] # List 
num = (1, 2, 3, 4, 5, 6) # Tuple
vowels = {'a', 'e', 'i', 'o', 'u'} # Set
alphabets = {'a': 'apple', 'b': 'ball', 'c': 'cat'} # Dictionary

print(langaueg)
print(num)
print(vowels)
print(alphabets)

Output

['Python', 'C', 'C++', 'PHP']
(1, 2, 3, 4, 5, 6)
{'a', 'e', 'u', 'i', 'o'}
{'a': 'apple', 'b': 'ball', 'c': 'cat'}

Conclusion:

In this Python literals tutorial, you have learned What is Python literals and their types with appropriate example.
So I think, you will don’t have any confusion regarding Python literals.literals in Python is nothing but it is raw data given in the variable or constant.

If you like this Python literals tutorial, Please share with your friends who want to learnPython programming from scratch to advanced.

Python Keywords Tutorial
Python Comments ( Introduction )

Was this post helpful?

Related Posts

Leave a Reply

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