In this tutorial, we are going to learn what are the Python variables, Constants & Literals with their use.

Table of Content

  • What are the Variables in Python?
  • Variable Assignment
  • Does Python have constants?
  • What are Literals in Python?
  • Numerical Literals
  • Boolean Literals
  • String Literals
  • Special Literals
  • Literal Collections

Python Variables

In Python, variables are those words which are used to store values of any data-type. In simple word whenever you create a variable it occupies some space in the memory according to the value assigned to it.

On the basis of the data-type, the Python interpreter allocates the memory to the variable. Variables are used to getting a reference for the values. For e.g., if you have a variable x with a value 40,000, instead of writing 40,000 you can just write x use the value 40,000.

x=40000
print(x)

#Output

40000

Variable declaration & assignment

If you have some experience with other High-level programming languages such as C++ or Java, you must have noticed that if you declared a variable, you need to write its data type along with it before assigning a value. In Python, you do not need to declare a data type along with the variable you can directly assign a value to it and the Python interpreter will itself understand what is the data type you have assigned to a variable.

Let’s understand it with an example

C++ Python
int x; // Variable declaration in C++
x=20; // Variable assignment in C++
cout<<x;
//Output of C++20
x = 20 # we do not declare the type of variable in Python
print(x)
#Output of Python Code20

In Python, we use equal to (=) to assign a value to a variable which is known as an Assignment operator. The variable should be on left of Assignment Operator and the value on the right. You do not have this feature of variable declaration in Python. You have to assign some value, and if you do not assign a value to the variable then the interpreter will throw an error.

You can assign any value to the variable, either it could be an object, an address, a number, a string, a function, a list, a class, a tuple or None, etc.

Variable Overwrite

You can overwrite the value of the variable by assigning them again in a new line. Take an example to understand it.

x = 20 # we have assigned a value 20 to a variable x
print(x)
x = 40 # Now we overwrite the value of x 20 by 40 now x has a value 40
print(x)

#Output

20
40

Multiple Assignments

Python gives you this cool feature of multiple assignments of the variable. You can assign multiple values to multiple variables with a single assignment operator. While using the multiple assignments be careful and must remember that the number of values on the right of the Assignment operator should be equal to the number of variables on the left of the Assignment operator.

Here is an example.

a, b, c, d="apple", "boy", "cat", "dog"
print(a)
print(b)
print(c)
print(d)

#Output

apple
boy
cat
dog

In Python, you can assign a single value to many variable at once.

e.g

steve_age = mark_age = keral_age = milli_age =20
print(steve_age)
print(keral_age)

#Output

20
20

Python CONSTANTS?

Unlike other programming languages, like C++ or Java, Python does not provide the constants. You can use the naming convention capitalize (PEP8) according to it. If you write a variable in upper case it termed as constant, but actually it’s a variable but we treat it as a constant.

Let’s understand it with an example:

normal_variable_all_lower_case = 100
CONSTANT_ALL_UPPER_CASE = 20

Though both are variable, and the variable name written in uppercase termed as constant in professional field.

Convention Rules for writing Variables and Constant:

  • Use full variable name instead of a letter for example use values instead of v.
  • Always use lowercase while writing a variable name e.g. variable=40
  • Always use UPPERCASE while using a Constant name e.g. CONSTANT=40
  • Never use special character while writing a variable name except for underscore (_)
  • Use underscore to separate the variable name e.g. steve_age = 30

Python Literals

Literals can be defined as the data is given to the variables and constants. This creates confusions let’s understand it with an example

string= “This is a Python string literal”

Here in this example, we assign a data which is a string to the variable string using an Assignment Operator, Here the Text (“This is a Python string literal”) is a string literal

There are many types of Literal in the Python

String Literals:

A given data to a variable or constant will be known as a string variable, if it is surrounded by the quotes and the quotes could be (“) or (‘)

With the string literal, we can use the escape character by using a special sign backslash(\).

e.g.

string_literal_1="Hello World inside double quote"
string_literal_2='Hello world inside single quote'
string_literal_3="""multi
line
string"""
string_literal_4="using \
escape character for \
every next line"
print(string_literal_1)
print(string_literal_2)
print(string_literal_3)
print(string_literal_4)

#Output

Hello World inside double quote
Hello world inside single quote
multi
line
string

using escape character for every next line

Boolean Literals

There are only two Boolean literals True and False. 0 is considered as False.

e.g.

bool_1=False
bool_2=True
print(bool_1)
print(bool_2)
print(0==False)

#Outputs

False
True
True

Numerical Literals:

Numerical literals are immutable and there are three kinds of Numeric Literals Integers, Floats, and Complex.

e.g.

integer = 23
float_contains_decimal_points= 23.0
complex_real_imag=23+7j
print(integer)
print(float_contains_decimal_points)
print(complex_real_imag.real)
print(complex_real_imag.imag)

#Output

23
23.0
23.0
7.0

Special Literal

Python has a special Literal known as None which mean nothing.

special_literal=None
print(special_literal)

#Output

None

Literal Collection:

The literal collection is the collection of literals there are 4 types of literal collection list, Dictionary, sets, and tuples.

e.g.

list_1=[1,"String",True,None]
dictionary={1:"String","string":3,4:True}
sets={1,True,"string",2.3}
tuples=("string",True,["list1","list2"])
print(list_1)
print(dictionary)
print(sets)
print(tuples)

#Output

[1, ‘String’, True, None] {1: ‘String’, ‘string’: 3, 4: True}
{2.3, 1, ‘string’}
(‘string’, True, [‘list1’, ‘list2’])

Python Tutorials:

Python Keywords & Identifiers

Leave a Reply

Your email address will not be published. Required fields are marked *