Friday, 29 May 2015

Data Types In Python

By
Data types using we can different types of data stored in memory. For example, we can stored employee name using alphanumeric character  types and her age and id are  numeric types. These all types of data stored in python using data types.

Python Provide Five Types Of Data Types Such As :
  • Numbers
  • String
  • List
  • Tuple
  • Dictionary
Python Numbers  :

Python numbers data types store numeric values. In python number data types in we have use integer numbers, floating points numbers, and complex numbers.

Example

abc  =  10
abc1  =  20.52
abc2  =  45.e

Here example in we show first variable abc in we stored 10 numeric values, second variable abc1 in we stored 20.52 floating point values and third variable abc2 in stored 45.e complex number, so we understand numbers data types in we can stored integer, floating point and complex number.

Python String  :

String data types in python represents text information in our python program. Strings in python we can create using single quotes or double quotes. Anything within quotes is a string in python.

Example

abc  =  ‘Hello world’
abc1 =  “Hello friends”
print abc
print abc1

Output

Hello world
Hello friends

Python Lists  :

Python in list data type is mutable. This means that the elements in a list can be changed after it is created. The list can contain mixed data types. List contain items separated by commas and enclosed with in square brackets ([]).

List is similar functionality of arrays in c language but small difference is array stored same type of data and list is stored different data types.     
List in stored elements are access using the slice operator ( [ : ] ) with index position start with 0.

Example

lst = [123, ‘abc’, 50.20, ‘xyz’]
print lst
print lst[0]
print [0:2]
print[1:]

Output

[123,  ‘abc’, 50.20, ‘xyz’]
123
[123,  ’abc’]
[ ‘abc’, 50.20, ‘xyz’]

Python Tuples  :

Python in tuples is an immutable data types. A tuple consists of a number of values separated by commas, like list. Tuples are enclosed with parentheses ( () ).

The difference between tuples and list is :
  • Tuples enclosed with parentheses ( () ) and list enclosed with in square brackets( [] ).
  • Tuples is immutable type and list is mutable type of data types.


Example

tuple  =  ( 123, ‘abc’, 50.20, ‘xyz’, 286)       
print tuple                                   
print tuple [ 0 ]                            
print tuple [ 1 : 3 ]                      
print  [ 2 : ]                                   

Output

( 123, ‘abc’, 50.20, ‘xyz’, 286)
123
( ‘abc’, 50.20 )
( 50.20, ‘xyz’, 286 )

Python Dictionary  :

Python in dictionary data types are kind of hash table type. Dictionary is a group of key-value pairs. The elements in a dictionary are indexed by keys. Keys is unique in dictionary data types.
Dictionaries are enclosed by curly brackets ( { } ) and element of dictionary access using square brackets ( [ ] ).     

Example

dic  =  { }
dic [ ‘one’ ]  =  “First Position”
dic [3]  =  “Third Position”
dict1  =  { ‘ Name ’ :  ‘ James  ’, ‘ Pin  ’: 385001, ‘ Designation ’:  ‘ Engineers ’  }
print  dic [ ‘ one ’ ]
print dic [ 3 ]
print dict1
print dict1.keys()
print dict1.values()

Output

First Position
Third Position
{ ‘ Name ’ :  ‘ James  ’, ‘ Pin  ’: 385001, ‘ Designation ’:  ‘ Engineers ’  }
{ ‘ Designation ’, ‘ Pin  ’,  ‘ Name ’}
{ ‘ Engineers ’,  385001, ‘ James ’ }

0 comments:

Post a Comment