Jun 29, 2020

Dictionary in Python | Initializing | Deletion | Addition | Coding Winds |

Python Dictionary

Hello guys, we will be discussing about Python Dictionary. It is an unordered representation and collection of items. Each item has a key/value pair. They are to obtain value when the key is known.

Initializing of dictionary

Initializing a dictionary requires the elements to be in between the {} brackets. Each item has a key and a corresponding value expressed as a pair. Representation is shown below,

Output

As seen we have used built-in function, dict(), to create a dictionary.

Accessing elements from dictionary

We use indexing to access other data types but in dictionary, we use keys. Keys can be used either inside square brackets [] or with the get() method.

KeyError is raised in case a key is not found in the dictionary. On the other hand, the get() method returns None if the key is not found.

Output

Changing and Adding of Elements

Dictionaries are mutable. We can easily add or change items using an assignment operator.

Output

Removal of Elements from Dictionary

We use pop() to remove a particular item from a dictionary. This removes an item with the provided key and returns the value. popitem()  can also be used, but this method selects the element randomly and return the item pair from the dictionary. clear() will empty the dictionary while del  will delete the dictionary or can be used to delete a specific item.

Output

Python Dictionary Methods

We have discussed few of the methods above like pop(), del, popitem(), get(), clear()

1)     Python Dictionary fromkeys()

It creates a new dictionary with the sequence of elements with a value provided by the user.

Syntax is,

dictionary.fromkeys(sequence, keys)

Sequence: - a sequence of elements which is to be used as keys for the new dictionary.

Value (optional): - values which will be set to each element of the dictionary by the user.

A dictionary from mutable object list,

Output

2)     Python Dictionary values()

It returns the list of all the values in the dictionary.

The syntax is,

dictionary.value()

This doesn’t take any parameters.

Output

What if the dictionary is modified?

Output

3)     Python Dictionary update()

The syntax is,

dict.update(other)

 This actually adds elements (from another dictionary, d1) if the key is not in dictionary, d. If the key is there then it updates the value of the key.

How update() works with an iterable?

Output

4)     Python Dictionary keys()

It returns the list of all the keys in the dictionary.

The syntax is,

dict.keys()

This doesn’t take any parameters

Look for the output on your own for better understanding.

5)     Python Dictionary items()

It returns the list of dictionary’s tuple pairs.

The syntax is,

dict.items()

This doesn’t take any parameters

Look for the output on your own for better understanding.

6)     Python Dictionary setdefault()

The setdefault() method returns the value of a key. If not, it inserts key with a value to the dictionary.

The syntax is,

dict.setdefault(key, defaultvalue)

Key: - key to be searched in the dictionary

Defaultvalue: - key with a value defaultvalue is inserted to the dictionary if key is not in the dictionary. If not provided, the defaultvalue will be None.

When key is the dictionary,

The output will be the value of the key which is 0.

When the key is not in the dictionary,

Check out the output.

 

PYTHON DICTIONARY COMPREHENSION

 

 Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.

Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


Do subscribe to our daily blog update by clicking here.


Thank You!

 

 

 

 

 

 


Sets in Python | Initializing | Deletion | Addition | Coding Winds |

Python Sets

Hello guys, we are going to learn all about Python sets. Set is a collection of unordered items. Basically, set has all its elements unique, i.e. no duplicates.

Initializing Sets

A set is created by placing all the elements between the {} braces. Conversion of list into tuple is also possible.

Output

We should be careful while initializing an empty set. Initializing empty {}

Updating sets

Sets are mutable but we cannot change or access elements are sets are unordered so there is no discussion about indexing.

However, we can add elements in a set. For adding one element at a time, we use add() and update() takes lists, strings, lists or any other set into the existing set. In both the cases, duplication is not allowed.

Output

Removing of elements

A particular item can be removed using remove() and discard().

Output

We can also remove an element using pop() but it will pop out randomly. We can also remove the every element by clear().

Predict the output.

Set Operations

Similar to math sets in python also work out operations like:-

  • 1)    Union


    Union in python is performed by |  this operator. This also can be done by union().

    Output

  • 2)     Intersection


    Intersection in python is performed by &  this operator. This also can be done by intersection()


    Output

  • 3)     Difference

    Difference of the set B from set A(A - B) is a set of elements that are only in A but not in B.                   Similarly, B - A is a set of elements in B but not in A.


    Difference is performed by this operator. The same can be done by difference()


    Output


  • 4)     Symmetric Difference

    Symmetric Difference of A and B is a set of elements in A and B but not in both (excluding the             intersection).

    

    Symmetric difference is performed by ^ this operator. Also the same can be done by                                symmetric_difference()

    Try it out  by your own, for better understanding.

More on sets will be python set methods

Few we have discussed earlier above

Method

Description

copy()

Returns a copy of the set

difference_update()

Removes all elements of another set from this set. Return none indicating the set is mutated.

intersection()

Returns the intersection of two sets as a new set

intersection_update()

Updates the set with the intersection of itself and another

isdisjoint()

Returns True if two sets have a null intersection

issubset()

Returns True if another set contains this set

issuperset()

Returns True if this set contains another set

symmetric_difference_update()

Updates a set with the symmetric difference of itself and another

Frozenset
Sets being mutable are unhashable, so they can't be used as dictionary keys. On the other hand, frozensets are hashable and can be used as keys to a dictionary.

Its syntax is just the same as sets,

a = frozenset([1,2,3,4,5])


 Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.

Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


Do subscribe to our daily blog update by clicking here.


Thank You!


 

 

 

 

 


Jun 26, 2020

List Comprehension | Coding Winds

List Comprehension

Hello guys, here we will talk about list comprehension.

List comprehension is a precise way to create a new list from the existing list in python.

Syntax is,

[expression for item in list if conditional]

This is equivalent to,

 for item in list:

            if conditional:

                        expression

Output

Let’s go through the statement,

new_list:- creates a new list.

i + (i+2):- expression, based on variable used in the old list.

for i in range (8):- iterating from 0 to 8 (can imagine it a list)

if i%2==0:- if condition for the new list


Jun 24, 2020

Python Language | Introduction | Coding Winds


INTRODUCTION

Python is an interpretedhigh-levelgeneral-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant white-space

Python is as simple as simple English, yet powerful. It is an object oriented programming language. We don’t have ‘;’ to end the statement instead we have indentation to worry about.

Look at the code below,




Python has built-in modules (written in c), we call libraries, that provide access to system functionality such as file I/O that would otherwise be inaccessible to programmers. Even modules written in python provide standardized solutions for many problems.

Look down to the code,





Like this, there are many libraries in built in python that can ease up the pain of problem solving questions.

Python is a very simple language with almost null complications. Before initializing, you don’t have to mention the data types. But you can easily find out what type of data type the user has input. We will study more deeply into this once we are done with the basics

Look down,

 


To study data types, click here.

 

In python, in data structures, we have:

1)    Lists

2)    Tuples

3)    Sets

4)    Dictionary

5)    Strings

Loops in Python:

1)    For loops: The statement is



2)    While loops: The statement is




Hello Python people, for this blog we have taken help from the book Python : The Complete Reference.


Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


Do subscribe to our daily blog update by clicking here.


Thank You!