Skip to main content

Python Variables and Constants

Python Variables

A variable is a named location used to store data in memory.

note

Variables can be thought of as a container of data that can be modified later in the program.

For example, create a variable called salary and assign the value 1500:

salary = 1500

In addition, it is possible to replace the value of a variable at any time:

salary = 2000
salary = 1750.99

Fundamental points

  • Python does not have a command to declare a variable, because a variable is automatically created the moment you assign it a value.
  • Python is a type-inferred language, so you don't have to explicitly define the variable type. The Python interpreter automatically knows the real time of a variable.
  • Variables do not need to be declared with a particular type and they can also change type after being set.
note

More in deep, Python doesn't actually assign values to the variables. Instead, it assigns the reference of the object(value) to the variable.

Assigning values to Variables in Python

The assignment operator = is used to assign a value to a variable.

For example, assign a value tutorialreference.com to the variable tutorial_website and then print out the value assigned to tutorial_website

tutorial_website = "tutorialreference.com`"
print(tutorial_website)

Changing the value of a variable

The assignment operator = is also used to change a value to a variable.

tutorial_website = "example.com"
print(tutorial_website) # print "example.com"

# assigning a new value to website
tutorial_website = "tutorialreference.com"

print(tutorial_website) # print "tutorialreference.com"

Assigning multiple values to multiple variables

If you want to assign different value to multiple variables at once, you can do in this way:

a, b, c = 1, 2.3, "Hello World"
print(a) # print 1
print(b) # print 2.3
print(c) # print "Hello World"

If you want to assign the same value to multiple variables at once, you can do in this way:

x = y = z = "same value"
print (x) # print "same value"
print (y) # print "same value"
print (z) # print "same value"

Constants

A constant is a type of variable whose value cannot be changed.

note

Constants can be thought of as a container of data that can't be modified later in the program.

Declaring and Assigning value to constant in Python

In Python, constants are usually declared and assigned in a module.

Python constants are written in all capital letters and underscores separating the words.

For example, create a new module file called constants.py and declare the following constants:

constants.py
PI = 3.14
GRAVITY = 9.8
E = 2.718

Then, you just need to import the constants module

othermodule.py
import constants

print(constant.PI) # print 3.14
print(constant.GRAVITY) # print 9.8
print(constant.E) # print 2.718
warning

Actually, constants in Python do not exist!

Naming in capital letters is a convention to separate them from variables, but it does not actually prevent reassignment.

Rules and Naming Convention for Variables and constants

Some suggestions:

  • Constant and variable names should have a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_). For example:
    snake_case
    MACRO_CASE
    camelCase
    CapWords
  • Constant and variable names are case-sensitive.
  • Create a name that makes sense. For example, counter makes more sense than c.
  • If you want to create a variable name with two words, use the underscore to separate them. For example:
    my_variable
    positive_counter
  • To declare a constant, use capital letters if possible. For example:
    PI
    G
    MASS
    SPEED_OF_LIGHT
    TEMP
  • Never use special symbols like !, @, #, $, %, etc.
  • Do not start the name of a variable with a digit.