# Variables and Data Types in Python

Learn: Through this article, learn about the **variables and their types in Python**. This article preferably describes about **different data types and their variable declaration**.

Submitted by Abhishek Jain, on August 09, 2017

When we create a program, we often like to store values so that it can be used later. We use objects to capture data, which then can be manipulated by computer to provide information. By now we know that object/ variable is a name which refers to a value.

**Every object has:**

**An Identity**- can be known using id (object)**A type**– can be checked using type (object) and**A value**

**Let us study all these in detail...**

**A) Identity of the object:** It is the object's address in memory and does not change once it has been created.

**B) Type (i.e. Data Type):** It is a set of values, and the allowable operations on those values. It can be one of the following:

**1. Number**

Number data type stores Numerical Values. This data type is immutable i.e. value of its object cannot be changed (we will talk about this aspect later). These are of three different types:

**Integer & Long****Float/floating point****Complex**

**i) Integers:**

Integers are the whole numbers consisting of + or – sign with decimal digits like 100000, -99, 0, 17.

When we want a value to be treated as very long integer value appends **L** to the value. Such values are treated as long integers by python.

>>> a = 10 >>> b = 5192L #example of supplying a very long value to a variable >>> c= 4298114 >>> type(c) # type ( ) is used to check data type of value

**ii) Floating Point:** Numbers with fractions or decimal point are called floating point numbers.

A floating point number will consist of sign (+,-) sequence of decimals digits and a dot such as 0.0, -21.9, 0.98333328, 15.2963.

**Example:** y= 12.36

**iii) Complex:** Complex number in python is made up of two floating point values, one each for real and imaginary part. For accessing different parts of variable (object) x; we will use x.real and x.image. Imaginary part of the number is represented by j instead of i, so 1+0j denotes zero imaginary part.

**Example:**

>>> x = 1+0j >>> print (x.real,x.imag) 1.0 0.0

**2. None**

This is special data type with single value. It is used to signify the absence of value/false in a situation. It is represented by **None**.

**3. Sequence**

A sequence is an ordered collection of items, indexed by positive integers. It is combination of mutable and non-mutable data types. Three types of sequence data type available in Python are Strings, Lists & Tuples.

**3.1) String:** is an ordered sequence of letters/characters. They are enclosed in single quotes **(' ')** or double **(" ")**. The quotes are not part of string. They only tell the computer where the string constant begins and ends.

If we are not sure, what is the data type of a value, Python interpreter can tell us:

>>>type ('Good Morning') <class'str'> >>> type ('3.2') <class'str'>

**3.2) Lists:** List is also a sequence of values of any type. Values in the list are called elements / items. These are mutable and indexed/ordered. List is enclosed in square brackets.

**Example:** >>>l = ['spam', 20.5,5]

**3.3) Tuples:**

Tuples are a sequence of values of any type, and are indexed by integers. They are immutable. Tuples are enclosed in (). We have already seen a tuple, in Example 2 (4, 2).

**4. Sets**

Set is an unordered collection of values, of any type, with no duplicate entry. Sets are immutable.

**Example:**

>>>s = set ([1,2,3,4]) >>>print(s) {1, 2, 3, 4 }

**5. Mapping**

This data type is unordered and mutable. Dictionaries fall under Mappings.

**5.1) Dictionaries:** Can store any number of python objects. What they store is a key – value pairs, which are accessed using key. Dictionary is enclosed in curly brackets.

**Example:** d = {1:'a',2:'b',3:'c'}

**C) Value of Object (variable)**

To bind value to a variable, we use assignment operator (=). This is also known as building of a variable.

**Example:** >>>pi = 3.1415

**Here,** value on RHS of **'='** is assigned to newly created **"pi"** variable.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.