Variables declaration and their uses in python development

Sharing is caring!

In my last tutorial, we have familiar with the python introduction to a quick start for development. We have learned how to install the python and set the python environment for all platform. If you not checked then I would be recommended to check this tutorial first for better understanding. Here is the link to the introduction and how to install and what is the best IDE can we use for development.

Now in this tutorial, we will learn how to declare a variable and their uses in python. So let’s get started to learn. Variables are just parts of your computer’s memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names. As we know that any programming language has two known variables that are called local and global. A local variable scope is inside the function definition while global variable can access every function of a class.

Variable Assignment:  We use the assignment operator (=) to assign values to a variable. For example, let’s see and string assigns into a variable.

my_var1 = "This is my first variable"  # string assign into variable name my_var1
print(my_var1)

Output result: This is my first variable

Similarly, we can be assigned the integer, float and boolean into the variable. Here is the example to understand.

my_var2 = 20;   # this is integer assign into variable name my_var2
print(my_var2)

my_var3 = 30.4   # this is double assign into variable name my_var3
print(my_var3)

my_var7 = True  # this is boolean assign into variable name my_var7
print(my_var7)

Output result: 20, 30.4 and True.

Let’s see how can use the sum and divide number and store into the variable?

my_var4 = 10/3
print(my_var4)

my_var5 = 4
my_var6 = 6
print(my_var5 + my_var6)

Output result: 3.3333333333333335 and 10

Even we can use the multiple variable assignments also.

a, b, c = 5, 3.2, "Hello"
 # or
x = y = z = "same string"

Let’s see the example of local and global variables to define in python.

# Local and Global Variable

my_global_var1 = "This is global variable."
print(my_global_var1)


def example():
    # Here I am define local variable
    my_local_var1 = "This is my local variable"
    print(my_local_var1)
    print(my_global_var1)

# Here I am calling this method or function
example()

Output result:

This is global variable.
This is my local variable
This is global variable.

Now let’s check with modified with a local and global variable for better understanding that how it works.

x = 6
def example2():
    # what we do here is defined x as a global variable now we can tell
    # that x is global other wise UnboundLocalError: local variable 'x' referenced before assignment:.
    global x
    print(x)
    x+=5
    print(x)
    x = 13    # Here x is treating as a local variable
    x+=5
    print(x)

example2()

Output result:

6
11
18

Great Now we have the clear understanding of the variable declaration in python and their uses. In my next tutorial, we will learn what are the data types available in python and their use cases.

If you are wondering to learn Android then Please Learn from Android category and Wondering to lean Kotlin then Kotlin Category will help you.

Please do subscribe your email to get the newsletter on this blog on below and if you like this post then do not forget to share like and comment on below section.

You can learn all articles of MobologicPlus from the android app, which is available to download below.

[appbox googleplay  mobi.androapp.mobologicplus.c7929]

Happy coding 🙂

0 0 votes
Article Rating

Similar Posts

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