Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, December 2, 2019

Dictionaries and Sets

Index page for Python:
http://mylearningcafe.blogspot.com/2015/08/python-index-page.html


Dictionaries in python are data structures which associate keys with values.


>>> empl_name_id_dict = {"Nitin":1,"Jonathan":2,"Brien":3}


>>> briens_details = empl_name_id_dict["Brien"]
>>> briens_details
3

>>> briens_details = empl_name_id_dict["brien"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

KeyError: 'brien'

Keys are case sensitive.

Get method helps to get a default value for a non-existing key rather than an exception

>>> briens_details = empl_name_id_dict.get("brien")
>>> briens_details
>>> 

>>> briens_details = empl_name_id_dict.get("Brien")
>>> briens_details
3

//Adding a new element
>>> empl_name_id_dict ["Matt"] = 4
>>> empl_name_id_dict
{'Nitin': 1, 'Matt': 4, 'Jonathan': 2, 'Brien': 3}

//Get the list of keys or values

>>> list_of_keys = empl_name_id_dict.keys()
>>> list_of_keys
['Nitin', 'Matt', 'Jonathan', 'Brien']
>>> 
>>> list_of_values = empl_name_id_dict.values()
>>> list_of_values
[1, 4, 2, 3]

>>> list_of_items = empl_name_id_dict.items()
>>> list_of_items
[('Nitin', 1), ('Matt', 4), ('Jonathan', 2), ('Brien', 3)]


Sets is a data structure that represents a collection of distinct elements.

>>> list_of_items = [1,100,200,3,100,200,5]
>>> list_of_items
[1, 100, 200, 3, 100, 200, 5]
>>> 
>>> list_of_items_set = set(list_of_items)
>>> list_of_items_set

set([200, 1, 3, 100, 5])

Index page for Python:

Lists in Python

Index page for Python
http://mylearningcafe.blogspot.com/2015/08/python-index-page.html

What is a list?
Simply put, an ordered collection.

>>> list_of_integers = [1,4,5]
>>> len(list_of_integers)
3
>>> sum(list_of_integers)
10


How to get to an element in the list:

>>> list_of_integers[1]
4

Lists index starts from 0.

>>> list_of_integers[0]
1

Slice lists:

Slice lists using square brackets

>>> first_two_elements = list_of_integers[:2]
>>> first_two_elements
[1, 4]

Lets try some more commands:

>>> lists_of_value = [100,1,50,25,34,67,78,99]
>>> len(lists_of_value)
8

>>> first_4_elements = lists_of_value[:4]
>>> first_4_elements
[100, 1, 50, 25]

>>> last_3_elements = lists_of_value[-3:]
>>> last_3_elements
[67, 78, 99]

>>> copy_of_list_elements = lists_of_value[:]
>>> copy_of_list_elements
[100, 1, 50, 25, 34, 67, 78, 99]

>>> copy_of_list_elements.extend([200,300,400])
>>> copy_of_list_elements
[100, 1, 50, 25, 34, 67, 78, 99, 200, 300, 400]


Index Page for Python:
http://mylearningcafe.blogspot.com/2015/08/python-index-page.html



Monday, August 10, 2015

Python - Choosing an IDE

Refer index page for Python at the following URL:
http://mylearningcafe.blogspot.in/2015/08/python-index-page.html

 
Which is the best IDE for Python?
You can have a look at the following URL
https://wiki.python.org/moin/IntegratedDevelopmentEnvironments
for a list of IDE's for Python.

I have chosen Pycharm.
One can search google to find the installable for PyCharm.

Once downloaded, the installation is very simple.








After installation, when you run, we select







Select the theme of your choice and PyCharm is set up.




Refer index page for Python at the following URL:
http://mylearningcafe.blogspot.in/2015/08/python-index-page.html