Monday, 6 March 2017

Python DataTypes

Value:
  • A value is one of the basic things a program works with, like a letter or a number.
  • example: 1,2.6, "Siva Krishna",....
  • These values belong to different types: 1 is an integer, 2.6 is an float,  and "Siva Krishna" is a string, because it contains a “string” of letters.


Data Type:
  • a data type or simply type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data. 
  • Most programming languages support various types of data.
  • Programming Languages can support either static data types or dynamic data types.

Static Datatype:
  •  Programmer should define data type of the variable explicitly and store the data into the variable. 
  • Once the data type is defined by the programmer for a variable then in the entire program we cannot change the data type of that variable.
  • C, CPP(C++), Java, .Net Languages supports static data types.

C-program example:

#include <stdio.h>
void main() 
{
int a=10,b=5,c;
c=a+b;
printf("result:%d",c);
}

output:
result:15



Java program example:

import java.io.*;
class demo
{
public static void main (String[] args) 
  {
          int a=2,b=3,c;
          c=a+b;
          System.out.println(c);
   }
}

output:
5


Dynamic Datatype:
  • Data type of variable will be decided based on the data which is assigned to the variable. 
  • During the execution of the program data type of the variable can be modified. 
  • Python, JavaScript and PHP are supporting dynamic data types.

Python program example:


>>> a=10

>>> b=20
>>> c=a+b
>>> print(c)
30






Python datatypes:-

  • Every data type in Python Language represents a class. 
  • Whenever we assigned data to the variable at the time of execution of the program corresponding data represented data type class Object will be created and that Object address will be assigned to the variable.


Two different Objects does not contain the same address (hash code value).

Python data types are categorized as:
  • Fundamental data types.
  • Collection data types.

Fundamental datatypes:

Fundamental data types represented classes Objects can store only one value at a time. 

Python supports the following fundamental types
  • int
  • float
  • complex
  • bool
  • str

Collection datatypes:

Collection data types represented classes Objects can store more than one value at a time.

Python supports the following Collection types
  • Sequences ( list and tuple )
  • set
  • dict
Note:
  • list and set are sequences, because list and set are insertion order is preserved

No comments:

Post a Comment