Data Types in Python

What are data types?

As the name suggests, it is the type of data. A data type can be defined as the data storage format. To specify what value does a variable holds and how the user wants to manipulate it.
Some common data types are Integer, String, Boolean etc.

What are variables?

We have seen variables in mathematics; a quantity that is assumed to vary. In programming, variables are names of items whose value may vary through out a program. They are used to store values. 
In Python, we do not explicitly declare a variable with its data type. We only assign a value to it. Based on that value, declaration happens automatically. 
Values are assigned using equals sign (=). Like this,

>>>a = 10

Let's check out what different data types Python have in stock.

Data types in Python

Basic data types available in Python are:
  1. Number
  2. String
  3. List
  4. Tuple
  5. Dictionary
Besides these, Python also offers Set and Boolean types. I don't like my blog posts getting too long. And I also know you don't like it either. So we will see each of these in brief, one at a time. Let's begin!


Numeric values are stored under this data type. Integers, floating point numbers and complex numbers fall under this category. type() is a function that returns the data type of a variable.
  • int : This type stores real numbers of any length (memory is always a constraint). We can also prepend a string with numbers to indicate binary, octal or hexadecimal numbers. 
    • 0b or 0B : Number zero and letter b/B represent binary numbers.
    • 0o or 0O : Number zero and letter o/O represents octal numbers.
    • 0x or 0X : Number zero and letter x/X represents hexadecimal numbers.
  • float : This type stores floating point numbers that are accurate to 15 decimal places.
  • complex : This is used for complex numbers in a + bj form. Where a is real part, b is imaginary part followed by j or uppercase letter J.
>>> a = 10
>>> print a
>>> type (a) 
<type 'int'>

>>> a = 0b11
>>> print a
>>> type (a)
<type 'int'>

>>> a = 0o17
>>> print a
>>> type (a)
<type 'int'>

>>> a = 0x7A
>>> print a
>>> type (a)
<type 'int'>

>>> a = 12.4
>>> print a
>>> type (a)
<type 'float'>
>>> a = 12.2e4
>>> print a
>>> type (a)
<type 'float'>

>>> a = 3+7j
>>> print a
>>> type (a)
<type 'complex'>

We will see about Strings in the next post soon. If you find it useful please share with friends. Don't sit there left confused. You can ask questions in the comments.
Happy Weekend!


Popular posts from this blog

Print in Python

Coding Digits

Calculating bits of bits.