Variables In Python

introschool

Introschool

Posted on October 11, 2021

Variables In Python

To understand a variable, think of it as a box or container. You can give a label to the box and store any value in it. Whenever you need that value in the program you can access it by its name.

language = 'Python'
print('We are learning', language)

# Output
# We are learning Python

Enter fullscreen mode Exit fullscreen mode

In the above example, the variable named language is created and the assignment operator(=) is used to assign a value ‘Python’.

Python is a dynamically typed language, it means that you don’t have to define a type of a variable before creating it. To create a variable, you just assign a value to it. Because it is dynamically typed, you can assign any type of data to the same name.

# Initially value 2 is assigned to variable x
x = 2
# Then x refer to string value "Hello World"
x = "Hello World"
# Now x is assigned a list.
x = [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

You can also create multiple variables in a single line. See the below code.

# Assign the same value to different variables.
x = y = z = 10

# Create multiple variables simultaneously.
x, y, z = 10, 20, 30
Enter fullscreen mode Exit fullscreen mode

Behind The Scene
For your initial understanding of variables, It is ok to consider them a box which stores a value, but what actually happens behind the scene?

In Python, every piece of data is an object. In the real world, anything that is well-defined is an object. For example, a pen, a car, a laptop, etc. You can easily differentiate one object from other objects because of their characteristics.

Similarly, in Python programming, each data item is an object. Each object has certain characteristics like its type, the actions you can perform on that object. For example an integer object, a string object, and a lot more that we will discuss later in the course.

See the code below

x = 20

text = 'Hello'

Enter fullscreen mode Exit fullscreen mode

When you write x = 20, Python interpreter does the following things:

  • It creates an integer object in the computer memory
  • Gives it a value 20
  • Assigns the variable x to point to that integer object. Alt Text

In a similar way, when you write text = ‘Hello World’

  • It creates a string object in computer memory.
  • Gives it a value ‘Hello World”
  • Assigns the variable text to point to the string object.

Alt Text
So the variable does not itself store a value, it is just a reference to the particular object. Whenever you need that value in your code, you can access it through the variable.

What will happen if you create another variable y and put it equal to x?

y = x
Enter fullscreen mode Exit fullscreen mode

In this case, Python interpreter will not create another object, it will just create another reference to the same object to which x is pointing.

Alt Text
What if you change the value of x. What will happen?

x = 40 
Enter fullscreen mode Exit fullscreen mode

In this case, Python interpreter will create another integer object with the value 40 and now variable x will point to integer object with value 40.
Alt Text
Now the integer object 20 does not have any reference. There is no way to access this object. When the Python interpreter notice that the object is no longer accessible, it takes back the allocated memory to the object so the memory can be used for something else.

Every object is assigned a unique id the interpreter. Two different objects can not have the same id. It is just like the address in the computer memory but not exactly the same. You can use Python’s built-in function id() to know that unique identification number.
Here you can see that id(x) and id(y) are not the same because variable x and y are pointing to two different objects but id(z) is exactly the same as id(x) because the object to which variable z is pointing is same as variable x.

x = 20
id(x)
# 10943616

y = 'Python is awesome'
id(y)
# 140399366

z = x
id(z)
# 10943616
Enter fullscreen mode Exit fullscreen mode

Constants
Constants are a type of variables whose value can not change throughout the code. For example the value of π(3.14), the value of g(9.8) or anything that is constant.

Use capital letters for creating constants.

PI = 3.14
GRAVITY = 9.8
Enter fullscreen mode Exit fullscreen mode

How to Name a Variable
Now you know what variables actually are they are just a reference to the object stored in memory. There are certain rules according to them you can give a name to variables. Let’s see them.

  • You can use uppercase(A-Z) and lowercase characters(a-z), digits(0-9) and underscore(_) character to give name a variable.
age = 68
Year = 2019
is_old = True
One23 = 123
Enter fullscreen mode Exit fullscreen mode
  • Do not use special symbols like !, @, #, $, %, etc.
  • The first character of the variable should not be a digit.
# This is not a valid variable name.
12three = 123

# It will give an error
Enter fullscreen mode Exit fullscreen mode
  • Variables are case sensitive, It means that Python interpreter treats uppercase(A-Z) and lowercase(a-z) character differently. In the example below, all are different variables. Be careful while accessing those values.
price = 100
Price = 120
Pri_ce = 250
Enter fullscreen mode Exit fullscreen mode
  • You can not use Python keywords as a variable name. If you do so, the interpreter will give an error.
  • To write variable name more than one-word use (_). For example, number_of_states, field_area, etc. When words are separated by an underscore, It is called Snake case.
  • Give an explicit name to a variable. n = 10 or number = 200 does not give you any context that what is the number about. Instead of that write it explicitly.
💖 💪 🙅 🚩
introschool
Introschool

Posted on October 11, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related