Python

Variables in Python

Hello in this tutorial, we will explain variables in python programming.

python variables

1. Introduction

Variable is a name that refers to a location in the memory. It is also known as an identifier that contains a value. In python, it is not important.to specify the variable type as python is an infer language and is smart enough to under the data type on its own. Variables in python:

  • Must start with an underscore or letter
  • Cannot start with a number
  • Can contain alpha-numeric and underscore character
  • Are case sensitive in nature (i.e. name and NAME will be treated as different
  • Are of different types like Numbers, Boolean, Lists, Dictionaries and Strings
  • Each variable in python has it own unique identity which can be calculated with the help of the id() method

In this tutorial, we will focus on the variables in python and play around with them.

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Variables in Python

I am using JetBrains PyCharm as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Creating an implementation file

Add the below code to the python script. The script contains several methods (such as variable assignment, identity, local and global variables, re-define variables, etc) that will help understand the variables in python. You’re free to play around with these methods as per your wish.

jcg-assignment-variables.py

# jcg assignment
# variables in python

# local variables - defined and declared inside a method
# global variables - defined and declared outside a method

global_var = 'hello-world'


# creating variables
def create():
    # below represents the python assignment
    x = 5
    y = 'adam'
    print(x)
    print(y)


def redefine_var(number):
    print('existing value={}'.format(number))
    number = 5
    print('updated value={}'.format(number))


def single_value_assignment(orig):
    q = r = s = orig
    print('orig={}, q={}, r={}, s={}'.format(orig, q, r, s))


def multi_value_assignment():
    a, b, c = 1, 5.0, 'geek'
    print('a={}, b={}, c={}'.format(a, b, c))


def print_global_var():
    print('Value of global variable is={}'.format(global_var))


def object_identity():
    a = 10
    # id() is used to return the identity of an object.
    # the returned identity is guaranteed to be unique and constant for an object during its lifetime
    print('print of variable is={}'.format(id(a)))


if __name__ == '__main__':
    create()
    print('\n')
    redefine_var(10)
    print('\n')
    single_value_assignment(1)
    print('\n')
    multi_value_assignment()
    print('\n')
    print_global_var()
    print('\n')
    object_identity()

Run this python script and if everything goes well the output will be shown in the IDE console.

Console output

5
adam


existing value=10
updated value=5


orig=1, q=1, r=1, s=1


a=1, b=5.0, c=geek


Value of global variable is=hello-world


print of variable is=2110459701840

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned about the variables in python programming. You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial on how to use variables in python.

Download
You can download the full source code of this example here: Variables in Python

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button