Python libraries and Data Structures

Following are some data structures, which are used in Python-
Lists – Lists are one of the most versatile data structure in Python. A list can simply be defined by writing a list of comma separated values in square brackets. Lists might contain items of different types, but usually the items all have the same type. Python lists are mutable and individual elements of a list can be changed. 
Example#
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> my_list=[]
>>> my_list
[]
>>> my_list=[1,2,3]
>>> my_list
[1, 2, 3]
>>> my_list=[1,2,3, 'JavaTcode', 1.3]
>>> my_list=[1,2,3, 'JavaTcode', 1.3]
>>> my_list
[1, 2, 3, 'JavaTcode', 1.3]
>>> 


Strings – Strings can simply be defined by use of single ( ' ), double ( " ) or triple ( ''' ) inverted commas. Strings enclosed in tripe quotes ( ''' ) can span over multiple lines and are used frequently in doc-strings. \ is used as an escape character. Please note that Python strings are immutable, so you can not change part of strings. 
Example#

>>> #Single Quote String
... name='JavaTcode'
>>> 
>>> #Double quotes String
... name="This is sequence of text"
>>> name
'This is sequence of text'
>>> 
>>> #Be careful with quotes
... value='We can't do like this'
 File "<stdin>", line 2
   value='We can't do like this'
                 ^
SyntaxError: invalid syntax
>>> value="We can't do like this"
>>> value
"We can't do like this"
>>>
Read More


Tuples – A tuple is represented by a number of values separated by commas. Tuples are immutable and the output is surrounded by parentheses so that nested tuples are processed correctly. Additionally, even though tuples are immutable, they can hold mutable data if needed.

Since Tuples are immutable and can not change, they are faster in processing as compared to lists. Hence, if your list is unlikely to change, you should use tuples, instead of lists. 
Example#


Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> t=()
>>> t
()
>>> t=('a', 1)
>>> t
('a', 1)
>>> 
Read More


Dictionary – Dictionary is an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}. 

Example#
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> my_dictionary={'key':'value'}
>>> my_dictionary
{'key': 'value'}
>>> 
>>> my_dictionary['key']
'value'
>>> 
>>> 
>>> my_dictionary={'key1':'JavaTcode','key2':123}
>>> my_dictionary
{'key1': 'JavaTcode', 'key2': 123}
>>> my_dictionary={'key1':'JavaTcode','key2':123, 'list':[1,2,3]}
>>> my_dictionary
{'list': [1, 2, 3], 'key1': 'JavaTcode', 'key2': 123}
>>> my_dictionary['key3']='value3'
>>> my_dictionary
{'list': [1, 2, 3], 'key1': 'JavaTcode', 'key3': 'value3', 'key2': 123}
>>> 
>>> 
>>> my_dictionary['list']
[1, 2, 3]
>>> 
>>> my_dictionary['list'][0]
1
>>> 
>>> 
Read More


Python Iteration and Conditional Constructs

Like most languages, Python also has a FOR-loop which is the most widely used method for iteration. 
Learn More About Python

Comments