Showing posts with label Competitive Programming. Show all posts
Showing posts with label Competitive Programming. Show all posts

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

 CODING WINDS  |   We care about the future of future

In the life of computer programmer, there have been always some or the other things or topics which they use them daily and still don’t have adequate knowledge about it. So for clearing this mess up of you, here we are working voluntarily. Do have a watch on us regularly.

C++ Programming :

1.    #include<iostream> | #include<iomanip> | Header files

2.    using namespace std;

3.    Dynamic Memory Allocation | malloc() | calloc() |realloc() | free()

4.   Asymptotic Analysis 

5.  Space Complexity    

6. Time Complexity             

7. Time Complexity Analysis and Calculation  

8. Time Limit Exceeded (TLE)        

9. What is a Data Structure?  

10. OOPs Concepts    

11. Pointers in C and C++ 

12. Errors | Runtime Error | Compile Error | Logical Error   

13. Arrays in C/C++    

14. Passing Arrays in Functions as Arguments

15. Structure in C++

16. Structures and Functions

17. Strings in C++

18. C++20

19. NULL Pointers in C/C++

20. Strings Pre-defined Functions

21. Inheritance

22. Functions

23. Constructors

24. Ceil and Floor Functions

25. abs() , labs() and llabs() Functions

26. Polymorphism in C++

27. Templates in C++

28. Destructors in C++

Python Language :-

        1. Python - Introduction

        2. Data Types in Python

        3.  Strings in Python


        5. format() Advanced

        6. Python List

        7. Python Tuple

         8. Data Type Conversion in Python

         9. List Comprehension

        10. Sets

        11. Dictionary
        
        12. Dictionary Comprehension

        13. Python Operators
           
        14. User Input

        15. Exception handling

            16. Functions

            17. Lambda and Arrays

        18. Modules

            19. File Handling 
            
        20. Map, Filter and Reduce

            21. Metaclasses

Java Language :

In near future we are going to come with many other such articles, follow us on instagram (@coding.winds) for future updates.