Breaking

Monday, 14 May 2018

Python : Datatypes


#Python03 

Python : Datatypes



As you know guys to learn any programming language you must know about the datatypes and operators of that programming language.so in this post we are going to cover some Datatypes and Operators of python.

Datatype:                                                                                                       
Every value has a datatype in python . Since everything is object in python programming,data types are actually classes and variables are instances(Object)  these classes. 
    Python has following datatypes:
    1.Number
    2.String
    3.List
    4.Tuple
    5.Dictionary 

Lets see each in detail,

1.Number                                                                                                       

    As there are separate datatype for integer ,float and double in other programming language but here in python this all datatypes are come under one category Number .Which works as per the which value is assigned to it. i.e Float is we assign any value having fractional part and integer when we assign a integer value.
  • Integer can be of any length
  • Floating point number is accurate up to 15 decimal places. 
Example : 
               a=5           (Integer)
               b=5.5        (float)
               c= 5+5j     (Complex)

2.String                                                                                                          
    String is a sequence of characters.String literals in python are surrounded by either single quotation marks, or double quotation marks. Strings can be output to screen using the print function

Example: 
              print("hello") or print('hello')
both will print the same string - hello 

Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters.
In python programming string elements can be access by both ends as it has both direction index,forward and backword index.

3.List                                                                                                              

List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets [ ].

Example: 
                 a= ['D',1,2.2,'Hello']

We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts form 0 in Python.
                  i.e a[3]= 'Hello'

Note: Lists are mutable, meaning, value of elements of a list can be altered.

4.Tuple                                                                                                           

Tuple is an ordered sequence of items same as list.The only difference is that tuples are immutable. Tuples once created cannot be modified. Tuples are used to write-protect data and are usually faster than list as it cannot change dynamically.
It is defined within parentheses () where items are separated by commas. 

Example :
               a = (1,'python5', 5+5j) 

We can use the slicing operator [] to extract items but we cannot change its value.

Note:Tuple is not mutable.

5.Dictionary                                                                                                   

Dictionary is an unordered collection of key-value pairs. It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving data. We must know the key to retrieve the value. 
In Python, dictionaries are defined within braces {} with each item being a pair in the form key:value. Key and value can be of any type. 

Example:
            a={'key':'Value'}
            a = {1:'One',2:'Two'} 

Here 1,2 are the keys and 'One','Two' are the values of keys respective. We use key to retrieve the respective value. But not the other way around.


Conversion between data types                                                                   

We can convert between different data types by using different type conversion functions like int(), float(), str() etc.

Example:
            int(10.6)
            10 
            float(10)
            10.0 
  • Conversion from float to int will truncate the value (make it closer to zero).
  • Conversion to and from string must contain compatible values.
  • We can even convert one sequence to another.
  • To convert to dictionary, each element must be a pair
To perform some operatioins on this datatypes python has so many operators which will coverinto next post.

Hope you are enjoying it.for any queries please comment below.

No comments:

Post a Comment