Posts

Python3 Strings DS

Python Data Structures : Strings Strings :              A string is a sequence of characters.   ***Computer do not deal with characters, they deal with numbers (binary) .Even though you may see characters on your screen, internally it is stored and manipulated as a combination of 0's and 1's.        This conversion of character to a number is called encoding, and the reverse process is decoding. ASCII and Unicode are some of the popular encoding used.   *   In python, string is a sequence of Unicode character. How to create a string?      ->   Strings can be created by enclosing characters inside a single quote or double quotes.     ->  Even triple quotes can be used in python but generally used to represent multiline strings and docstrings. Eg:        mystring='Hello'        print(mystring)          mystring="Hello"        print(mystring)        mystring=' ' 'Hello' ' '        print(mystring) output :  Hello                Hello

Python3 Sets DS

Python Data Structures:  Sets Sets :   -> A set is an unordered collection of items. Each element is unique (no duplicates).   -> The set itself is mutable .We can add or remove items from it.   -> Sets can be used to perform mathematical set operations like union ,intersection ,symmetric ,difference etc. Set Creation : Eg:         #set of integers        s={1,2,3}        print(s)        print(type(s)) output:   set([1,2,3])               < type 'set' >        #set does not allow duplicates. They store only one instance.        s={1,2,3,1,2}        print(s)   output:   {1,2,3} Eg:         #we can make set from a list        s=set([1,2,3,1])        print(s) output:  {1,2,3} Eg:        #initialize a set with set( ) method        s=set( )        print(type(s))   output:   < class 'set' > Add element to a Set : Eg:        #we can add single element using add( ) method and         #add multiple elements using update( ) method        s={1,2,3}       

Python3 Dictionaries DS

Python Data Structures : Dictionaries Dictionary          Python dictionary is an unordered collection of items. While other compound data types have only value as an element, a dictionary has a key : value pair. Dict Creation :   Eg:         #empty dictionary        my_dict={}         #dictionary with integers as keys        my_dict={1:'a',2:'b"}        print(my_dict)                 #dictionary with mixed keys        my_dict={'name':'satish',1:['abc','xy']}        print(my_dict)           #create empty dictionary using dict( )        my_dict=dict()        my_dict=dict([(1,'abc'),(2,'xy')])      #create a dict with list of tuples        print(my_dict) output:   {1:'a',2:'b'}               {'name':'satish',1:['abc','xy']}               {1:'abc',2:'xy'}    Dict Access : Eg:        my_dict={'name': 'suma','year':2020}        print(my_d

Python3 Tuples DS

Python Data Structures : Tuple  Data Structures : Tuple  Tuples    ->A tuple is similar to list.    ->The difference between the two is that we can't change the elements of the tuple(immutable) once it is assigned whereas in the list, elements can be changed. Tuple Creation : Eg :              #empty tuple        t=( )                #tuple having integers        t=(1,2,3)        print(t)          #tuple with mixed data types        t=(1,'rama',2.5,'a')        print(t)         #nested tuple        t=(1,(1,2),[1,'abc',3])        print(t) output:: (1,2,3)              (1,'rama',2.5,'a')              (1,(1,2),[1,'abc',3])         #only parentheses is not enough            t=('raja')            type(t) output: : str         # need a comma at the end             t=('raju',)             type(t)  output:: tuple        #paranthesis is optional               t="raja",              print(type(t))              

Python3 Lists DS

Python : Data Structures Data Structure Lists   Data structure:        A data structure is a collection of elements(such as numbers or character-or even other data structures) that is structured in some way, for example, by numbering the elements. The most basic data structure in Python is the "sequence". ->List is one of the sequence data structure ->Lists are collection of items(strings, integers or even other lists) ->Lists are enclosed within [ ] ->Each item in the list has an assigned index value. ->Each item in a list is separated by a comma( , ) ->Lists are mutable, which means they can be changed. List creation : Eg:        emptyList=[ ]        lst=['one', 'two', 'three']            #list of strings        lst2=[1,3,2,4]                            #list of integers        lst3=[[1,2],[3,4],5,6]]              #list of lists        lst4=[1,'krishna',4,6.5]             #list of different datatypes        print(lst4) outp