Python Variables, Constants and Literals

This tutorial will focus on the python variables, constants, and literals and their use cases with examples.

What are variables in Python?

Variable is a named location used to store the data in memory while the program is running. These variables have a unique name know as an identifier. The value of the variables can be changed during the program.

How to declare variables in Python?

Unlike in other programming languages where we needed to declare the variable before using it, Python automatically assigns a memory location as we assign the variable with a value.

How to assign values to variables?

You can use the assignment operator “=” to assign the value to a variable. For example, let us assign “John Doe” value to “name” variable. Note that we do not need to declare that it is a string variable as Python automatically typecast the variable according to the value we assign.

Lets explore more variables,

How to assign multiple values to multiple variables?

We can use the assignment operator with a comma separation to assign multiple values to multiple variables.

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

What is constant in Python?

By definition, a constant is a type of variable whose value cannot be changed. They are usually declared in a module where a module is a file that can contain variables,  functions etc. which is imported to the main file.

Let’s see an example of using constants.

What are the rules and naming convention for variables and constants?

  1. You should not use special symbols like !, @, #, $, %, etc. in a variable name.
  2. Do not start the variable name with a digit
  3. Use capital letters where possible to declare a constant. For example PI, GRAVITY etc.
  4. Constants are put into Python modules and meant not be changed.
  5. 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 (_).

Guidelines derived from Guido’s Recommendations

Here are the guidelines that is derived from Guido that helps you better obtain uniform programming guidelines for your python projects.

Literals in Python

The data that is provided in the variable are known as literals in Python. Python supports the following literals:-

Python support the following literals:-

1) String Literals

2) Numeric Literals

3) Boolean Literals

4) Literal Collections such as List, Tuples, Dictionary

5) None Literal

1. String Literal

Single quotes or double quotes are used to defined by String literals. There are two kinds of strings supported in Python, single line and multiline string literals. Let us use some examples and ways by which we can define strings

2. Numeric Literals

A number can be directly assigned to variables in Python. They are immutable. Numeric literals can belong to following four different numerical types.

Int: numbers can be both positive and negative) with no fractional part. e.g. 432
Long: Integers of unlimited size followed by lowercase or uppercase L e.g. 1422L
Float: Real numbers with both integer and fractional part eg: -26.2
Complex: In the form of a+bj where a forms the real part and b forms the imaginary part of a complex number. eg: 1+2j

3. Boolean Literals

A Boolean Literal can have True or False value.

4.Literal Collections

There are 4 different literal collections List Literals, Tuple Literals, Dict Literals, and Set Literals. They represent more complex data and helps to provide extendibility to Python programs.

Let us use an example to see how these Literals function:-

5. None Literal

Python contains a special type of literal known as None.  It specifies the field that is not created. and also can indicate the end of lists in Python.

SHARE Python Variables, Constants and Literals

You may also like...

Leave a Reply

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

Share