Python Variables, Constants and Literals

Python Variables

In programming, a variable is a container (storage area) to hold data. For example,

number = 10

Here, number is the variable storing the value 10.


Assigning values to Variables in Python

As we can see from the above example, we use the assignment operator = to assign a value to a variable.

# assign value to site_name variable
site_name = 'programiz.pro'

print(site_name)

# Output: programiz.pro

In the above example, we assigned the value 'programiz.pro' to the site_name variable. Then, we printed out the value assigned to site_name.

Note: Python is a type-inferred language, so you don't have to explicitly define the variable type. It automatically knows that programiz.pro is a string and declares the site_name variable as a string.


Changing the Value of a Variable in Python

site_name = 'programiz.pro'
print(site_name)

# assigning a new value to site_name
site_name = 'apple.com'

print(site_name)

Output

programiz.pro
apple.com

Here, the value of site_name is changed from 'programiz.pro' to 'apple.com'.


Example: Assigning multiple values to multiple variables

a, b, c = 5, 3.2, 'Hello'

print(a)  # prints 5
print(b)  # prints 3.2
print(c)  # prints Hello 

If we want to assign the same value to multiple variables at once, we can do this as:

site1 = site2  = 'programiz.com'

print(site1)  # prints programiz.com
print(site2)  # prints programiz.com

Here, we have assigned the same string value 'programiz.com' to both the variables site1 and site2.


Rules for Naming Python Variables

  • 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
  • Create a name that makes sense. For example, vowel makes more sense than v.
  • If you want to create a variable name having two words, use underscore to separate them. For example:
my_name
current_salary
  • Python is case-sensitive. So num and Num are different variables. For example,
var num = 5 
var Num = 55
print(num) # 5
print(Num) # 55
  • Avoid using keywords like if, True, class, etc. as variable names.

Python Constants

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

In Python, constants are usually declared and assigned in a module (a new file containing variables, functions, etc which is imported to the main file).

Let's see how we declare constants in separate file and use it in the main file,

Create a constant.py:

# declare constants 
PI = 3.14
GRAVITY = 9.8

Create a main.py:

# import constant file we created above
import constant

print(constant.PI) # prints 3.14
print(constant.GRAVITY) # prints 9.8

In the above example, we created the constant.py module file. Then, we assigned the constant value to PI and GRAVITY.

After that, we create the main.py file and import the constant module. Finally, we printed the constant value.

Note: In reality, we don't use constants in Python. Naming them in all capital letters is a convention to separate them from variables, however, it does not actually prevent reassignment.


Python Literals

Literals are representations of fixed values in a program. They can be numbers, characters, or strings, etc. For example, 'Hello, World!', 12, 23.0, 'C', etc.

Literals are often used to assign values to variables or constants. For example,

site_name = 'programiz.com'

In the above expression, site_name is a variable, and 'programiz.com' is a literal.


Python Numeric Literals

Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different numerical types: Integer, Float, and Complex.

Type Example Remarks
Decimal 5, 10, -68 Regular numbers.
Binary 0b101, 0b11 Start with 0b.
Octal 0o13 Start with 0o.
Hexadecimal 0x13 Start with 0x.
Floating-point Literal 10.5, 3.14 Containing floating decimal points.
Complex Literal 6 + 9j Numerals in the form a + bj, where a is real and b is imaginary part

Python Boolean Literals

There are two boolean literals: True and False.

For example,

result1 = True  

Here, True is a boolean literal assigned to result1.


String and Character Literals in Python

Character literals are unicode characters enclosed in a quote. For example,

some_character = 'S'

Here, S is a character literal assigned to some_character.

Similarly, String literals are sequences of Characters enclosed in quotation marks.

For example,

some_string = 'Python is fun' 

Here, 'Python is fun' is a string literal assigned to some_string.


Special Literal in Python

Python contains one special literal None. We use it to specify a null variable. For example,

value = None

print(value)

# Output: None

Here, we get None as an output as the value variable has no value assigned to it.


Literal Collections

There are four different literal collections List literals, Tuple literals, Dict literals, and Set literals.

# list literal
fruits = ["apple", "mango", "orange"] 
print(fruits)

# tuple literal
numbers = (1, 2, 3) 
print(numbers)

# dictionary literal
alphabets = {'a':'apple', 'b':'ball', 'c':'cat'} 
print(alphabets)

# set literal
vowels = {'a', 'e', 'i' , 'o', 'u'} 
print(vowels)

Output

['apple', 'mango', 'orange']
(1, 2, 3)
{'a': 'apple', 'b': 'ball', 'c': 'cat'}
{'e', 'a', 'o', 'i', 'u'}

In the above example, we created a list of fruits, a tuple of numbers, a dictionary of alphabets having values with keys designated to each value and a set of vowels.

To learn more about literal collections, refer to Python Data Types.

Video: Python Variables and print()

Did you find this article helpful?