Saturday 30 November 2019

Python Dictionary

PYTHON DICTIONARY


Introduction to Python – Dictionary


Dictionary is a collection of related data item pairs. 

A python dictionary is a collection of unordered items which are changeable and indexed. In Python dictionaries are written using curly brackets, and they have keys and values.


Important properties of Python - Dictionary


Un-ordered – Elements/items stored in a python dictionary are not kept in any particular order.
Changeable (mutable) – Structure and elements can be changed in place; elements can be added and removed.
Accessed by keys – Dictionary items are accessed by the keys, not by their position number (index).
Nesting Properties - Dictionaries can be nested, i.e. a dictionary can contain another dictionary in it.

Create a Python Dictionary


The dictionaries are created using curly brackets. Curly brackets contains pairs of key-value. 

>>> dist1 = {"brand":"Maruti", "model":"Desire", "year":2019}

>>> print(dist1)
{'brand': 'Maruti', 'model': 'Desire', 'year': 2019}

Accessing elements of a Python Dictionary


Following syntax can be used to access the individual elements of the python dictionary.

>>> print(dist1['brand'])
'Maruti'

>>> print(dist1['model'])
Desire

>>> print(dist1['year'])
2019

Following functions can be used to display all keys and values of the python dictionary.

>>> dist1.keys()
dict_keys(['brand', 'Model', 'Year'])

>>> dist1.values()
dict_values(['Maruti', 'Desire', 2019])


Sunday 10 November 2019

Python Set

Python Set


Introduction to Python – Set

A set is an unordered collection of elements. Every element is unique (no duplicate values in set) and are immutable (i.e. values cannot be changed). However, the set itself is mutable which means we can add or remove elements from set.

Sets is very useful to perform mathematical set operations like union and intersection.

Create a Python Set

A set is created by placing all the elements within curly braces { } and each elements separated by comma.

Set can have any number of elements with different data types like integer, float, string etc.

Example of set of an integers

>>> set1 = {1, 2, 3}
>>>set1
{1, 2, 3}

# set of mixed data types
>>>set2 = {101, "K Patel", 90.5}
>>>print(set2)
{'K Patel', 90.5, 101}

Set removes duplicate entries and arranges elements in order.

>>> set3 = {1,3,2,5,4,3}
>>> set3
{1, 2, 3, 4, 5}

Observe the following code:
>>> set2[0]
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
set2[0]
TypeError: 'set' object is not subscriptable

Note: 'set' object does not support indexing in python. We can’t access an element of set using indexing or slicing concept.

Changing Python Set Elements

Add() method can be used to add single element in to a set.

>>> set4 = {3, 5, 7}
>>> set4
{3, 5, 7}

>>> set4.add(6)
>>> set4
{3, 5, 6, 7}

Removing elements from Set

>>> set4.remove(7)
>>> set4
{3, 5, 6}

Note: Trying to remove element which does not exists in set will result in error. Observe the following code:

>>> set4(2)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
set4(2)
TypeError: 'set' object is not callable

Python Set Operations - Union and Intersection

Python sets can be used to perform mathematical set operations like union and intersection.

Python Set Union

Union of Set A and Set B is a set of all elements from both the python sets. Union operation can be performed using union operator( | ) or the method union().

>>> set5 = {2, 4, 6, 8}
>>> set6 = {6, 8, 10, 12}
>>> set5.union(set6)
{2, 4, 6, 8, 10, 12}

>>> print(set5 | set6)
{2, 4, 6, 8, 10, 12}

Python Set Intersection

Intersection of Set A and Set B is a set of elements that are common in both sets. Python intersection can be performed using intersection operator ( & ) or using the method intersection().

>>> set5 & set6
{8, 6}

>>> set5.intersection(set6)
{8, 6}


* * * * *

<  Back to Home Page >

Sunday 3 November 2019

Positive negative or zero number

Following Python practical will check whether entered number is positive or negative or zero. This program uses the concept of Python's nested if statement.

# Program will check whether entered number is positive or negative or zero.

number = int(input("Enter a number: "))
if number >= 0:
    if number == 0:
        print("It's Zero")
    else:
        print("It's Positive number")
else:
    print("It's Negative number")

Output of program

Enter a number: 5
It's Positive number

* * * * *

<  Back to Home Page >