Python Variables, Constants and Literals

    In this Python tutorial, we are going to discuss Python variables, constants, and literals. What are they, their types, and how to use Python variables, constants, and literals.

    Python Variables

    In Python, variables are those entities that are used to store values of a particular data type. In simple words, whenever you create a variable, it occupies some space in the memory according to the value assigned to it. Based on the data type, the Python interpreter allocates memory to the variable. Variables are used to get references for the values. For example, if you have a variable x with a value 40,000 then instead of writing 40,000 you can just write x to use the value .

    x=40000
    print(x)

    #Output

    40000

    Variable Declaration and Assignment

    If you have some experience with other high-level programming languages , such as C++ or Java, you must have noticed that if you declared a variable, you need to write its data type along with it before assigning a value. In Python, however, you do not need to declare a data type along with the variable. You can directly assign a value to it and the Python interpreter will itself understand the data type you have assigned to the variable.

    Let’s understand this with an example:

    C++ Python
    int x;
    
    // Variable declaration in C++
    x=20;
    
    // Variable assignment in C++
    cout<<x;

    //Output of C++

    20

    x = 20 # we do not declare the type of variable in Python
    
    print(x)

    #Output of Python Code

    20

    In Python, we use equal to (=) to assign a value to a variable which is known as an assignment operator . The variable should be on the left of the assignment operator and the value on the right. You do not have this feature of variable declaration in Python. Here, you have to assign some value, and if you do not assign a value to the variable then the interpreter will throw an error. You can assign any value to the variable, either it could be an object, an address, a number, a string, a function, a list, a class, a tuple, etc.

    Variable Overwrite

    You can overwrite the value of the variable by assigning it a value again in a new line. Take the following example to understand it:

    x = 20 # we have assigned a value 20 to a variable x
    print(x)
    x = 40 # Now we overwrite the value of x 20 by 40 now x has a value of 40
    print(x)

    #Output

    20 40

    Multiple Assignments

    Python gives you this cool feature of multiple assignments of the variable. You can assign multiple values to multiple variables with a single assignment operator. While using the multiple assignments be careful and must remember that the number of values on the right of the Assignment operator should be equal to the number of variables on the left of the Assignment operator. Here is an example:

    a, b, c, d="apple", "boy", "cat", "dog"
    print(a)
    print(b)
    print(c)
    print(d)

    #Output

    apple
    boy
    cat
    dog

    In Python, you can assign a single value to many variables at once. e.g.

    steve_age = mark_age = keral_age = milli_age =20
    print(steve_age)
    print(keral_age)

    #Output

    20 20

    Python CONSTANTS?

    Unlike other programming languages, like C++ or Java, Python does not support constants. You can use the naming convention to capitalize ( PEP 8 ). According to it, if you write a variable in upper case it is termed as a constant, but actually, it’s a variable but we treat it as a constant.

    Let’s understand this with an example:

    normal_variable_all_lower_case = 100
    CONSTANT_ALL_UPPER_CASE = 20

    Though both are variables, the variable name written in uppercase is termed as a constant in the professional field.

    Convention Rules for Writing Variables and Constants:

    • Use a full variable name instead of a letter. For example, use values instead of v .
    • Always use lowercase while writing a variable name. e.g. variable=40.
    • Always use UPPERCASE while using a constant name. e.g. CONSTANT=40.
    • Never use a special character while writing a variable name except for underscore (_).
    • Use underscore to separate the variable name. e.g. steve_age = 30

    Python Literals

    Literals can be defined as the data is given to the variables and constants. This creates confusion. Let's understand it with an example:

    string= "This is a Python string literal"

    Here in this example, we assign data, which is a string to the variable string using an assignment operator. Here the text (“ This is a Python string literal”) is a string literal.

    Types of Literals in Python

    1. String Literals

    A given data to a variable or constant will be known as a string variable, if it is surrounded by the quotes and the quotes could be (“) or (‘). With the string literal, we can use the escape character by using a special sign backslash (\). e.g.

    string_literal_1="Hello World inside double quote"
    string_literal_2='Hello world inside single quote'
    string_literal_3="""multi
                          line
                          string"""
    string_literal_4="using \
                       escape character for \
                       every next line"
    print(string_literal_1)
    print(string_literal_2)
    print(string_literal_3)
    print(string_literal_4)

    #Output

    Hello World inside double quote
    Hello world inside single quote
    multi
    line
    string
    using escape character for every next line

    2. Boolean Literals

    There are only two Boolean literals True and False. 0 is considered as False. e.g.

    bool_1=False
    bool_2=True
    print(bool_1)
    print(bool_2)
    print(0==False)

    #Outputs

    False
    True
    True

    3. Numerical Literals

    Numerical literals are immutable and there are 3 kinds of numerical literals:

    1. Integer,
    2. Float, and
    3. Complex.

    e.g.

    integer = 23
    float_contains_decimal_points= 23.0
    complex_real_imag=23+7j
    print(integer)
    print(float_contains_decimal_points)
    print(complex_real_imag.real)
    print(complex_real_imag.imag)

    #Output

    23
    23.0
    23.0
    7.0

    4. Special Literal (None)

    Python has a special literal known as None, which means nothing.

    special_literal=None
    print(special_literal)

    #Output

    None

    5. Literal Collection

    The literal collection is the collection of literals. There are 4 types of literal collections:

    1. List,
    2. Dictionary,
    3. Sets, and
    4. Tuples.

    e.g.

    list_1=[1,"String",True,None]
    dictionary={1:"String","string":3,4:True}
    sets={1,True,"string",2.3}
    tuples=("string",True,["list1","list2"])
    print(list_1)
    print(dictionary)
    print(sets)
    print(tuples)

    #Output

    [1, 'String', True, None]
    {1: 'String', 'string': 3, 4: True}
    {2.3, 1, 'string'}
    ('string', True, ['list1', 'list2'])

    Conclusion

    That was all about Python variables, constants, and literals. These are fundamental concepts in any programming language. Therefore, knowing them is important to make the most out of a program.