Variables in python

Thilakshids
4 min readNov 17, 2020

Using variables in Python

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers or characters in these variables.

The most special fact is unlike other programming languages, Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it. The example is given below.

Screenshot by Author(Assigning values to variables)

Another interesting fact about variables is, variables do not have any particular type, and can even change type after they have been set.

x = 12 (x is of type int)
x = “Blue” ( x is now of type str)
print(x)

In the above-given example, we can see that at first x variable is a type of int but after that, it has been changed to a string. The string type variable can be declared by using double quotes “ ” or single quotes ‘ ’. You can use double quotes either single quotes as you wish.

Variable names

When we study the variables in python there are various kinds of variables and when it comes to names there are several rules that we need to follow.

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • Variable names are case-sensitive (name, Name, and NAME are three different variables)
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0–9, and _ )

Assigning Values to Variables

Python variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.

The operand to the left of the equal sign(=) operator is the name of the variable and the operand to the right of the equal sign(=) operator is the value stored in the variable.

The amazing thing which python caries is that python allows you to assign values to multiple variables in one line :

x, y, z = “ Red ”, “ Blue ”, “ Purple ”
print(x)
print(y)
print(z)

And also you can assign the same value to multiple variables in one line:

x = y = z = “ Purple ”
print(x)
print(y)
print(z)

Output the variables

  • Python is a very readable programming language therefore in python we use the print word to output the variables.
  • For the concatenation, python uses the plus mark (+).

Eg :

x = “Thilakshi”
print(“Hello “ + x)

  • But when plus mark uses with numbers. It acts as a mathematical operator.

Eg :

x = 5
y = 10
print(x + y)

  • Another important fact is that when combining string and integer type variables it will give an error. Therefore it should be in the same variable type.

Global Variables.

Python Global Variables
  • Variables that are created outside of a function are known as global variables.
  • Global variables can be used by everyone, Inside of functions, and outside of the functions.

x = “The best”

def myfunction():
print(“Python is “ + x)

myfunction()

  • As you can see in the above example variable x is defined outside of the function called myfunction().
  • we can use the same variable name in and out of the function. This means we can use the same name for the global variable which we use inside of the function.
  • But if you want to change the value of the function there you have to use the keyword “ global ”.

x = “the best”

def myfunction():
global x
x = “fantastic”

myfunction()

print(“Python is “ + x)

  • Finally, we can say that A global variable is a variable with global scope, meaning that it is visible and accessible throughout the program unless shadowed. The set of all global variables is known as the global environment or global scope of the program.

Hope you enjoyed this article.

--

--