Python Literals


A python literal is a source code representation of a fixed value. It can be assigned to any primitive type variable.

Python supports the following types of literals,

1. Numeric literals

A numeric literal can be created by providing a numeric value.

Example

    num1 = 121
    num2 = 123456789L
    num3 = 147.92
    num4 = 8+3j    


2. String literals

A string literal can be created by enclosing a sequence of characters in either single or double quotes.

Example

    #Single line string
    str1 = “TutorialsDuniya”
    #Multiple line string
    str2 = “Tutorials\		    #Using backslash at the end of each line
    Duniya”
    str3 = “””Tutorials	            #Using triple quotation marks
    Duniya”””    


3. Boolean literals

A boolean literal can be created using either True or False.

Example

    bool1 = True
    bool2 = False



4. Special Literals

In python, we have a special literal i.e. None which is used to specify the field that is not created. It is also used for end of lists.

Also none is stored when we try to store the return value of a function that does not return any value.

Example

    def myFunction()	# function that does not return any value
	fn = 12
    x = myFunction()	# stores ‘None’ in x
    print(x)		        # prints ‘None’


5. Literal Collections

Literal collections in python are the collections of Lists, Tuples and Dictionary.

Example

    list1 = [‘Tutorials’, 123, 17.4, ‘Abhi’]
    tuple1 = (‘Tutorials’, 123, 17.4, ‘Abhi’)
    dict = {1:’Tutorials’, ‘two’:’Duniya’}