Data types, Variables and Constants in python
Tags: beginners, programming, tutorial, python
Published: 2022-08-05 13:49:33

## Introduction
This is the second article of this series - **"Programming and python"**. And this covers everything that we need to know about datatypes especially the ones in python.

At the end of the previous article, we discussed about datatypes briefly. Now let's dive into it.

## Datatypes
Like we discussed in the previous article, every programs needs to store some amount of data in order to perform some operations. That data can be from user input, from previous operation in the same program and it can be from computer storage itself.

The term data itself is a high level abstraction. In general, data can be of lot of types. It can be numbers, letters, etc. In similar way, the data in a program can be of different types. They are:

### I. Boolean (bool)
A **boolean** type data item can have only 2 values. Either **True** or **False**. The first letter capital in both True and False is necessary in python. If we use true and false instead of True and False, we get an error. So, be careful with that.

### II. Numbers
Numbers can be still divided into 3 types. They are:

1. **Integer (int)**: Positive and negative numbers without fraction parts are considered as integers. Ex: 1, -200, 3456, -2345, etc.

2. **Floating point (float)**: Positive and negative numbers having fractional parts are considered as floating point numbers. Ex: 0.343, -234.689, 645.83, etc.

3. **Complex numbers (complex)**: Numbers having imaginary values associated with them are called complex numbers. We generally don't use them unless we are finding square root of negative numbers. Ex: 5+9i, -7-10i, etc.

### III. Strings (str)
A data item which is a character or a group of characters is considered as string in python. In some other programming languages, character and string are considered as different datatypes. Ex: 'a', 'this is a string', etc.

Okay, these all are basic datatypes. We have some complex datatypes, which are:

### I. List (list)
It is a data item which is a collection of other data items of any type including other lists even. We can change a list after initialisation at any point of time. This quality is called mutability. So, a list is a mutable collection of items and the items can be of any type. A list is denoted by `[]`. An example of list can be `[1, 2, 'a', 'bcd', [1.56, -0.23]]`.

### II. Tuple (tuple)
A tuple is just like a list but with one difference. A list is mutable which means we can make changes to a list once it is initialised. But a tuple is immutable, which means that we can't make changes to a tuple once it is initialised. A tuple is denoted by `()`. A tuple example can be `(1, 'fs', [1, 3], 1.1)`.

### III. Dictionary (dict)
A dictionary is a collection of key-value pairs. A key must be unique in a dictionary. There can't be two pairs with same key, but there can be multiple pairs with same value. A dictionary identifies a pair uniquely using keys. Let's assume the scenario of storing employees details. In this scenario, a key can be the `id` of employee and value can be their details. A dictionary is represented by `{}`. An example can be `{ 1: 'chiru', 2: 'vatsal', 3: 'chiranjeevi' }`.

### IV. Set (set)
A set is also a collection of data items similar to a dictionary but not in key-value pairs. It consists only individual values and doesn't allow duplicates just like mathematics set. A set is also represented using `{}`. Set only allows immutable data items like int, float, str, tuple etc into it. Also, sets doesn't allow duplicates. A value can only be once in it. Ex: `{1, '2', (2, 3)}`. A set can't allow a set into it as set itself is mutable.

### V. Frozenset (frozenset)
A frozenset is just a set which allows mutable items into it. Everything else is same. Ex: `{1, [2, 3, 4]}`.

These all are most used datatypes in python at least. Do comment if I miss anything here. 

Okay fine... These are the types of data we store. But,
##How do we actually store data?
We knew from last article that we store data required from running that program in memory which is main memory (RAM). And, we also saw what types of data we can store. Now, let's see how we can do that.

We may want to store the data in 2 ways. We might require our data may change over the period of time. For example, we are storing the time, and time changes every second, and we might want to update the time we stored every minute. So in this case, we store our data in a `variable`. Now, if we want to store name of a person, we may not want to change it in future as changing name doesn't make sense. So, in this case, we store data in a `constant`. Hence, `variable` and `constant` are two ways of storing a data.

**Storing some data in a variable or a constant is called initialisation**

## Let's initialise few variables and constants of all datatypes we discussed

The syntax (general form) of initialising a variable or a constant is `<variable name> = <value>` or `<constant name> = <value>`. These names are also called as **identifiers**.

In case if I forgot to tell you that "#" is used to write comments in code and comments are not considered as part of code while interpretation, now you know.

```python
var1 = 10 # int variable
var2 = 32.56 # float variable
var3 = "Hello" # string variable
var4 = [1, 4.4, var3] # list variable, and yes we can use variable like this
var5 = (2, 5, 1) # tuple variable
var6 = {1: "a", 2: "b", 3: "c"} # dict variable
var7 = {1, 2, 3} #set variable
var8 = {2, [1, 3]} # frozenset variable

const1 = 1 # int constant
const2 = 3.6 # float constant
const3 = "This is a constant" # string constant
const4 = [0, 4.423, const3] # list constant
const5 = (21, 25, 10) # tuple constant
const6 = {1: "z", 2: "y", 3: "x"} # dict constant
const7 = {11, 22, 33} #set constant
const8 = {4, [2, 1]} # frozenset constant
```
This is how we initialise variables and constants.

## Conclusion
That's it for this one. 

Do follow me for the next articles.
Let's connect on: [Twitter](https://twitter.com/VChiranjeeviAK) [LinkedIn](https://www.linkedin.com/in/chiranjeevi-tirunagari/) [Showwcase](https://www.showwcase.com/vchiranjeeviak)