Saturday, 15 October 2016

Python - Constants, Variables and Built-in Data types

Hello readers! This is the third article of our tutorial series on Python - 'Python in Terminal' and we will be discussing about Constants, Variables and Basic data types in Python, in this article. In the previous two articles, we have had a brief introduction to Python and learned about Python installation on Linux systems. These two articles were very general, but from now on, we would be discussing Python Programming. Lets start our discussion with the definition of a Program.


A program is a sequence of instructions, which process on input data and produces the desired output. Constant, variables and data types are the basic building blocks of any computer program. So, it is very essential to know about these terms and this is from where we start programming.


Constants are fixed values that do not change during the execution of a program. Their properties are very much same as those in Mathematics. Python supports two types of constants that are listed below:

1. Numeric constants : These are numbers, may it be positive number, negative number, complex number, floating point number or an exponential number. Thus, in Python, following numbers are valid numeric constants: 127, -128, 98.67, 4 + 3i, etc.

2. String constants : These are the sequence of alphanumeric characters, special characters and underscores enclosed in a single or double quotes. This makes following valid string constants : "Your Own Linux!", "myString", 'I<3Python!', '', etc.


A variable is a named location in memory that stores a value. In simpler words, when we create a variable and assign a value to it, the operating system reserves a memory location and stores the value in it. This memory location can then be identified or accessed with a variable name. Values can be numbers, text, images, audio clips or any other data. Unlike constants which remain unchanged during execution of a program, variables may take different values at different times during the execution of a program. Upon declaration, each variable belongs to a type that determines what type of value is stored in it, like numerals, literals or strings, etc. Whenever we choose a variable name, it should be meaningful for better readability of a code. For example, variable names such as ipAddr, firstName, team_size make more sense than variables ip, fname, ts.

When we are to choose a variable name to store a value, we cannot simply select and use any alphanumeric string as a variable name, it must follow certain criteria. Following things must be taken care of while selecting a variable name:

  • A variable name consist of alphabets, digits and underscores, and it must start with a letter or underscore
  • No whitespaces are allowed
  • It should not be a keyword
Note : Keywords are the reserved words in any programming language. They are predefined in libraries, have special meaning and purpose. Hence keywords are not allowed to be used as a variable names.

Below are the keywords used in Python:

and, assert, break, class, continue, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, yield

Considering above mentioned rules for a variable name, below are some valid and invalid variables names-

  • Valid variable names : firstName, test4, account_type, _serialNo
  • Invalid variable names : 24hours, #pin, total.20
Variable Declaration
We can assign a value to variable using the assignment operator =, using the syntax variable_name = value

>>> myValue = 5
>>> myValue

Built-in data types in Python

A data type is the type of value a Python object has. Depending upon this, we have different built-in data types in Python, which are - bool, int, float, long, complex. There are some more built-in data types such as str, list, dict and tuple, but they can be related to data organization in Python, so we can refer to them as 'Python data structures'. We will definitely study them in later part of the tutorial series and for now, we concentrate on very basic data types and know each of them one-by-one. Meanwhile, we can check what type the value belongs to using Python built-in type() function.

1. bool : A variable belonging to bool type can store only two possible values True and False. A bool type variable can be used only in logical operations.

>>> myBool = True
>>> type(myBool)
<type 'bool'>

>>> myBool = False
>>> type(myBool)
<type 'bool'> 

2. int : A variable belonging to int type stores only Integer values.

>>> myInt = 5
>>> type(myInt)
<type 'int'> 

3. float : A variable belonging to float type stores floating point value.

>>> myFloat = 10.50
>>> type(myFloat)
<type 'float'> 

4. long : A variable belonging to long type can store big big values. To declare value as a long, we have to append l to the numeric value.

>>> myLong = 10l
>>> type(myLong)
<type 'long'> 

5. complex : Complex numbers represents a real part and an imaginary part in the form (a+jb), a is real part and jb is imaginary part. Python provides built-in support for complex numbers.

>>> myComplex = 4 + 5j
>>> type(myComplex)
<type 'complex'>
>>> myComplex

Depending upon the type of the values, we have different types of operations those we can perform on them. So, knowing about Python built-in data types is of utmost importance.

That's it for the scope of this article. We learned about Constants, Variables and different data types in Python, in this article. We are yet to be introduced to some other data types, like strings, lists, dictionaries and tuples, which we would be covering in a separate article, as they are more inclined towards data organization. In the next article, we will learn about basic arithmetic and logical operations in Python. Please let us know about your views and opinions in the comment section below and stay tuned. Thank you.

This article is originally published at - Constants, Variables and Built-in Data types in Python


Post a Comment