Site icon

Learning Python with Jupyter notebook

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

>>>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'


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, 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

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

Exit mobile version