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!

Number

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.
Integers
>>> a = 10
>>> print a
10
>>> type (a) 
<type 'int'>

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

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

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

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

Complex
>>> a = 3+7j
>>> print a
(3+7j)
>>> 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!

Comments

Popular posts from this blog

Print in Python

Coding Digits

Calculating bits of bits.