This is in continuation of Learn C-Python Programming . It covers Learning Python with Jupyter notebook, Dictionary, methods with dictionary, PIP, Jupyter Notebook installation with Anaconda
I discussed about data structure elements(Lists,Tuples) in last blog. Positive & negative indexing, slicing operator . List represented by [element1,element2] similar to C array and Tuples by (element1,element2).Tuple can be un-packed to different variables.
For ex : a,b,c=mytuple
Key Takeaway : This particular blog is to cover additional data structure techniques Dictionary and Set. Understanding the difference between all data structure techniques. This will also cover different installation options and overview of Python world.
Python Dictionary
- unordered key-value pairs . This is the difference from Lists and similar to real world dictionary.
- value can be any data type.
- Key must be immutable , so Tuple can also be Key but not the List
- dictionary format is {key1:val1, key2:val2}
- It is mutable so we can start from empty { } and that can grow later
- One simple example can be dictionary of student name and corresponding Identification number. Telephone directory can also be implemented using Dictionary.
>>>myclass={'Mark':10210,'Tony':10234,'Vikas':10237}
>> myclass[vikas]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'vikas' is not defined
#string must be in quote
>>> myclass['vikas']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'vikas'
#case sensitive key
>>> myclass['Vikas']
10237
Operations on Dictionary
#Adding new element
>>>>> myclass["suresh"]=20456
>>> myclass
{'Mark': 10210, 'Tony': 10234, 'Vikas': 10237, 'suresh': 20456}
#values of two elements can be same
>>> myclass["vivek"]=204566
>>> myclass["Raj"]=204566
>>> myclass
{'Mark': 10210, 'Tony': 10234, 'Vikas': 10237, 'suresh': 20456, 'vivek': 204566, 'Raj': 204566}
#Deleting an existing element pair
>>> del myclass["Mark"]
#lenth of dictionary using len() function
>>> len(myclass)
5
#checking presence of an element
>>> "Mark" in myclass
False
>>> "Mark" not in myclass
True
#You may pop() a particular element
>>> myclass.pop("Vikas")
10237
>>> myclass
{'Tony': 10234, 'suresh': 20456, 'vivek': 204566, 'Raj': 204566}
#POP can be used to POP the element and utilized its value in return
>>> test=myclass.pop("vivek")
>>> test
204566
>>>
# The CPython version 3.3 or later use LIFO for popitem()
>>> myclass
{'Tony': 10234, 'suresh': 20456, 'Raj': 204566}
>>> myclass.popitem()
('Raj', 204566)
>>> myclass.popitem()
('suresh', 20456)
>>> myclass.popitem()
('Tony', 10234)
>>> myclass.popitem()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'popitem(): dictionary is empty'
- OrderedDict keep track insertion order of items and their use when creating an iterator. It is default feature of CPython based Python 3.6+
Methods with Dictionary
Built-in functions like all()
, any()
, len()
, cmp()
, sorted()
etc. are commonly used with dictionary to perform different tasks.
>>> myseries={2:4,3:27,5:25,4:64,7:49,6:18}
>>> myseries
{2: 4, 3: 27, 5: 25, 4: 64, 7: 49, 6: 18}
>>> sorted(myseries)
[2, 3, 4, 5, 6, 7]
Which one should be used ?
- Lists are allocated in two blocks: the fixed one with all the Python object information and a variable sized block for the data.
- Tuples are stored in a single block of memory. Tuples are immutable so, It doesn’t require extra space to store new objects. Tuples can be used in pre-defined Tables. For example : Months
- It is the reason creating a tuple is faster than List.
- Indexing speed in tuples will be faster because of less pointer follow-up.
- There are some disadvantages of Tuples because sorting and updating not possible.
Lists, tuples, and strings are based on sequence but dictionary is a map based compound structure. One can’t index or slice a dictionary.
List, Tuples and Dictionary
A simple analogy to understand List,Tuples and Dictionary :
You may think List as customized Row Houses without any outside boundary, so can be modified or extended. Indexing can be done and particular row house can be accessed based on that.It may not necessary to have homogeneous.
Tuple can be considered as a building complex with boundary so no further modification/extension can be done. Indexing can be done and particular apartment can be accessed based on that.
Dictionary can be considered as city where you need building name(key) and house number(data).
Another non-primitive data structure element Set which is totally different from List,Tuple or Dictionary
Set
- A set is a collection of unordered and unindexed unique items . In Python sets are written with curly brackets. There is no key and index for Set, so only way to access by scanning the set.
- It is the mathematical notion of set. Set operations include Union(|),Intersection(&),Difference(-) etc
- A set cannot have a mutable element i.e. list,set or dictionary
- Empty set can be created by set() because {} is for empty Dictionary
- Frozensets are immutable sets and can be created by frozenset().
- There are many built-in functions like all(),len(),sum(),sorted() etc
Python World Overview
JuPyteR (formerly IPython) Notebook
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text.
It works as web application to run user code live and let visualize the instant result. It is replacement of IPython notebook. Code runs on a server (local or multi-user) and results are presented on page after converting to HTML. You may consider it as a lab notebook for recording all experiments and corresponding results with explanation.
Python Package Index (PyPI) is the online repository of Python modules/ packages in the form of archives called source distributions
PIP is recursive acronym of Pip Installs Packages or Pip Installs Python. It is used to install and manage packages written in Python and will be installed by default with Python . You may check pip –version and pip –help in Windows cmd window
Conda It is an open source Package, Dependency and Environment management system. It was initially developed for Python but now supports many languages —Python, R, Ruby, Lua, Scala, Java, JavaScript, C/ C++, FORTRAN. Please refer to this for Conda installation recommendations .
Anaconda is Python distribution for Data Science . It contains Python,Conda and Jupyter Notebook.
installation snapshots of Anaconda till Jupyter notebook Launch







Start new Python project page

This article is discussed at Learn Python with Jupyter Notebook video. Please subscribe to YouTube channel Embedkari for additional embedded related stuff