Code Pumpkin

Python Variables | Python

October 7, 2018
Posted by Dipen Adroja

In this article, we will explore more about python variables in detail. Python variable is a reserved memory location to store values. Every value in python has data types. Python Data types will be covered in subsequent articles (Datatypes: Numbers, List, Tuple, Strings, Dictionary, etc).

How to Declare and use a Variable?

Below is an example of variable declaration and access.  To declare a variable syntax would be

variable_name = variable_value

Below is the example for the same. We have assigned a string “Variable Example” to the variable a and then printed that value.

a=“Variable Example”

simple variable

In Python, we can re-declare a variable. In the previous example we have declared as a string now we can re-assign it as Number as below:

a=“Variable Example”

This will produce below output:

Reassign Variable | Python variables

Multiple Assignment

Python allows you to assign a single value to several variables simultaneously. For example −

a = b = c = 12345

Here, an integer object is created with the value 1, and all three variables are assigned to the same memory location. 

You can also assign multiple objects to multiple variables. For example − 

a,b,c = 1,2,"dipen"

Here, two integer objects with values 1 and 2 are assigned to variables a and b respectively, and one string object with the value "dipen" is assigned to the variable c.

Local & Global Variables

In Python when you want to use the same variable for rest of your program or module you declare it a global variable, while if you want to use the variable in a specific function or method, you use a local variable.

Let's understand this difference between local and global variable with the below program.

  1. Variable "foo” is global in scope and is assigned value “Global variable in python” which is printed in the output
  2. Variable foo is again declared in function and assumes local scope. It is assigned value “Local variable in Python." which is printed out as an output. This variable is different from the global variable "foo” defined earlier
  3. Once the function call is over, the local variable foo is destroyed. At line 12, when we again, print the value of "foo” is it displays the value of global variable foo=“Global variable in python”

The same is demonstrated below:

local global variable | Python Variables

We can also delete a variable using del keyword as below. 

Use del keyword | Python Variables

Standard Data Types

The data stored in memory can be of many types. For example, a person's age is stored as a numeric value and his or her address is stored as alphanumeric characters. Python has various standard data types that are used to define the operations possible on them and the storage method for each of them.

Python has five standard data types −

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

That's it for the variables for now. We will see about all datatypes in Python in subsequent articles.

That's all for this topic. If you guys have any suggestions or queries, feel free to drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here.

Happy Learning 🙂

If you like the content on CodePumpkin and if you wish to do something for the community and the planet Earth, you can donate to our campaign for planting more trees at CodePumpkin Cauvery Calling Campaign.

We may not get time to plant a tree, but we can definitely donate ₹42 per Tree.

About the Author

Coder, Blogger, Wanderer, Philosopher, Curious pumpkin

Tags: ,

Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
For more information on supported HTML tags in disqus comment, click here.
Total Posts : 124
follow us in feedly

Like Us On Facebook