Showing posts with label data type. Show all posts
Showing posts with label data type. Show all posts

Jul 3, 2020

Dictionary Comprehensions | Dictionary in Python | Coding Winds

Dictionary Comprehension

Hello guys, here we will talk about dictionary comprehension.

Dictionary comprehension is a precise way to create a new dictionary from the existing dictionary in python

Syntax is,

{key: expression for x in iterable if conditional}

Output

Another example,

Output

Single If statement

Output

We see that the conditional statement worked properly.

Similarly we can use multiple If loop and if-else also.

Nested dictionary with two dictionary comprehensions

Output

This code without dictionary comprehension will look like this,

We must be careful while using dictionary comprehension as:

1)     They can sometimes make the code run slower and consume more memory.

2)     They can also decrease the readability of the code.


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

Data Type Conversion in Python | Coding Winds

Data type conversion

Hello guys, we will here talk about data type conversion in python. We have almost entered in this world of python, and we should know that it is very easy to shift your data from one form to another of needed.

On simple level, we will look how we can change the data type from int to string, into to float and so on.


We have initiated the variable with an integer and gave commands to print out it in float, string and integer itself.

Let’s check out the output,


Another example,

Output

Now we will take a look how we will be changing list into tuple and set by using tuple() and set(). As set doesn’t contain any repeated items, so, repeated items will be discarded when changing a list to set.

Output

Conversion other than this like converting a list to dictionary, it’s a bit tricky. Click here, for more into this.