Home ProgrammingPythonIntroduction to Python Python Variables, Constants and Literals – Way of Python

Python Variables, Constants and Literals – Way of Python

by WhereWhatHow
Python Variables

In this tutorial, we will learn about Python variables, constants, literals with the help of examples.

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

number =0

Here, number is a 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 = 'apple.com'
print(site_name)

Output

apple.com

In the above example, we assigned the value apple.com to the site_name variable. Then, we printed out the value assigned to site_name

Changing the Value of Python Variables

site_name = 'wherewhathow.net'
print(site_name)

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

print(site_name)

Output

wherewhathow.net
apple.com

Here, the value of site_name is changed from 'wherewhathow.net' 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  = 'wherewhathow.net'
print(site1)  # prints wherewhathow.net
print(site2)  # prints wherewhathow.net

Here, we have assigned the same string value 'wherewhathow.net' 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 Variables – 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.

Python Literals

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

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

site_name = 'wherewhathow.net'

In the above expression, site_name is a variable, and 'wherewhathow.net' is a literal.

Python Numeric Literals

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

Python Variables - Numeric Literals

Python Variables – Boolean Literals

There are two boolean literals: True and False.

For example,

pass = true 

Here, true is a boolean literal assigned to pass.

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.

Related Posts

Leave a Comment