Python Variables

Rumman Ansari   Software Engineer   2023-03-28   823 Share
☰ Table of Contents

Table of Content:


  • What you will learn in this section:
    1. What is variable?
    2. Declaring and assigning value to a variable.
    3. Changing the value of a variable
    4. Assigning multiple values to multiple variables.
    5. Assign the same value to multiple variables at once
    6. Rules and Naming Convention for Variables and constants.

     


    What is variable

    Variable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value.

    It is helpful to think of variables as a container that holds data that can be changed later in the program.

    In Python, we don't need to specify the type of variable because Python is a infer language and smart enough to get variable type.

    Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore.

    It is recommended to use lowercase letters for the variable name. Rahul and rahul both are two different variables.


    Declaring and assigning value to a variable

    • The equal (=) operator is used to assign value to a variable.

    Code:

    number = 0
    product_weight = 2.1
    price_per_kilogram = 10
    filename = 'file.txt'
    trace = False
    sentence = "this is an example"
    
    print(number)
    print(product_weight)
    print(price_per_kilogram)
    print(trace)
    print(sentence)
    

    Output:

    0
    2.1
    10
    False
    this is an example
    

    Changing the value of a variable

    Code:

    website = "google.com"
    print(website)
     
    # assigning a new value to website 
    website = "atnyla.com"
    print(website)
    

    Output:

    google.com 
    atnyla.com
    

    Assigning multiple values to multiple variables

    variable1, variable2, variable3 =    5, 3.2, "Hello"           
    
    print (variable1)
    print (variable2)
    print (variable3)
    

    Output:

    5
    3.2
    Hello
    

     

    If we want to assign the same value to multiple variables at once, we can do this as:

    x  = y = z  =  "Kolkata" 
    
    print (x)
    print (y)
    print (z)
    

    Output:

    Kolkata
    Kolkata
    Kolkata
    

    Rules and Naming Convention for Variables and constants

    1. The first character of the variable must be an alphabet or underscore ( _ ).
    2. All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore, or digit (0-9).
    3. Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
    4. Identifier name must not be similar to any keyword defined in the language.
    5. Identifier names are case sensitive; for example, myname, and MyName is not the same.
    6. Create a name that makes sense. For example, my_age = 25 makes more sense than m = 25.
    7. Examples of valid identifiers: a123, _n, n_9, etc.
    8. Examples of invalid identifiers: 1a, n%4, n 9, etc.

     

    Constant

    Assigning value to constant in Python

    In Python, constants are usually declared and assigned in a module. Here, the module is a new file containing variables, functions, etc which is imported to the main file. Inside the module, constants are written in all capital letters and underscores separating the words.

    Declaring and assigning value to a constant

    constant.py

    PI = 3.14
    INTREST = 2.8  
    

    main.py

    import constant
    
    print(constant.PI)
    print(constant.INTREST)