Python3 Data types 2

Python Data types 2.


PYTHON LIST :

List is an ordered sequence of items. It is most used datatype in Python and more Flexible.
 
  *  Declaring a list is, items Separated by commas( , ) are enclosed within brackets [ ].

  eg:      a=[10,'hello',10.3]
             print(a[1])                #print index element
               
       output: 'hello' 


*  Lists are mutable ,meaning ,elements in a list can be altered.
 
  eg:     a=[21,30.3,'list']
            print(a)
            a[1]=3.3
            print(a)
  
       output:    [21,30.3,'list']
                      [21,3.3,'list']

For more information suggested video
 https://youtu.be/e1ruxK_6XZY


PYTHON 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.

Declaring a tuple ,elements are separated by commas( , ) and enclosed within parentheses  ( ).
 
 eg:       b=(10,2.5,'tuple')
             print(b)
             print(b[2])
             b[1]=2.2
             print(b)

       output:  (10,2.5,'tuple')
                    'tuple'
                    ------------------------------------------------------------------                      
                    TypeError 
                    TypeError:  'tuple' object does not support item assignment.


PYTHON SET :

   Set is an unordered collection of unique items. Set is defined by values separated by comma( , ) inside braces { }. Items in a set are not ordered.

  eg:     a={1,2,3,4,5}
            print(a)
            print(type(a))
  
       output:  {1,2,3,4,5}
                      <class  ' set ' >

 *  We can also perform set operations like union ,intersection on two sets. Set have unique values.

 eg :       s={12,13,14,13,14}
              print(s)          #automatically set won't consider duplicate elements.
              print(s[1])       #we can't print particular element in set because it is unordered.
 
       output:  {12,13,14}
                     ---------------------------------------------------------------
                     TypeError
                     TypeError : 'set'  object does not support indexing.



PYTHON DICTIONARY :

         Dictionary is  an unordered collection of key-value pairs.

In Python, Dictionaries are defined within braces { } with each item being a pair in the form of key : value. key and value can be of any type.

  eg:       d={'a' : "apple", 'b' : "bat"}
              print(d['a'])
 
       output:   apple

CONVERSION BETWEEN DATATYPES :

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

  eg:       float(4)
              int(2.5)
              str(21)
 
       output:   4.0
                       2
                       '21'

*  Conversion to and from string must contain compatible values.
 
  eg:       int('17g')

       output:   ------------------------------------------------------
                       --
                       ValueError
                      ValueError : invalid literal for int() with base 17 : '17g'


  eg:      user = "Rishitha"
             meters=100 

             print("Congrats," +user+"!You got selected for "+ str(meter)  +"meters running race competition.")

       output::   Congrats, Rishitha !You got selected for 100 meters running race competition.

*   Convert one sequence to another sequence:

  eg:      a=[2,3,4]
             print(type(a))

            #convert list of a to set 
             s=set(a)                        
             print(type(s))

           #convert string to list using list() method 
             list("hello")        


output :   < class  ' list' >
                 < class  'set' >
                  ['h', 'e', 'l', 'l', 'o']  




Comments

Popular posts from this blog

Python3 Function Arguments

Python3 File Handling

Python3