Showing posts with label Coding. Show all posts
Showing posts with label Coding. 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!

 

 

 

 

 

 


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.


Jun 7, 2020

#include iostream | #include iomanip | header files | C++ basics | CODING WINDS

#include<iostream>

Hello people, in this blog we are going to discuss the first line of our C/C++ code.


iostream stand for standard ‘input-output stream’.

This is nothing but a header file which is responsible for input and output stream.

Here, ‘input stream’ means when the task is performed in such a way that bytes travels from device(keyboard) to main memory, and ‘output stream’ means when the task is performed in such a way that bytes travel from main memory to your device.

Here “iostream” is a library which contains some predefined functions for example “cout” , “cin” , etc.

And “ #include ” is a word which makes the use of “iostream” possible in our code.

We just not only have “iostream” as a header file for input/output, but also have many other such as “iomanip” , “fstream”

1.    1. iomanip – This stands for ‘input ouput manipulation’. The methods in this file are used to manipulate the streams. This has predefined functions lihe setw, setprecision, etc

2.    2. fstream – This is defined as file stream. This header file is used to handle the data being read from a file as input or data being written into the file as output.

So this was all bookish knowledge, let’s try our hands on some codes showing the use of this ;

EXAMPLE 1 - Code using #include<iostream>


EXAMPLE 2 - Code using #include<iomanip>

 In this code we are going to see how to print '02' or '002' or '0002' in place of some int variable whose value is '2'.


Here the output of this code will be :
   20
00020
--20   

It also have many other pre-defined libraries(e.g. maths.h , cstdio, stdlib, etc ) in C++ language which we have not discussed here, we will discuss in the upcoming blogs.

Do you get why we write "using namespace std;" in every C++ code? If NO then check out our article on this by clicking here.

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

We also have something on "Dynamic Memory Allocation in C/C++", check it out by clicking here.


Jun 6, 2020

using namespace std; | std::cout | std::cin | C++ programming basics |

using namespace std;

Hey people in this blog you are going to read about “ using namespace std; ”, Namespaces are used in the C++ programming language to create a separate region for a group of variables, functions and classes etc.

Consider this, there are two students having same names, when we need to differentiate them, we would have to use some other information along with their names. Same goes with namespaces in C++, you might have a variable name, math, in your program, and there may be a library which contains a variable named math. Here, no way the compiler gonna recognize which version of variable value is called. That’s why namespace is designed to overcome this difficulty.

Now, namespace std:


std is an abbreviation, std is the standard namespace. “cout”, “cin” and a lot of other things are defined in it.

So whenever the execution will take place and come across cout, cin, endl or anything, it will read it as std::cout, std::cin, or std::endl.


A sample program :

Basically we only learn what we are taught. But coding isn’t just fluttering in a given consignment for the whole life, you have the sky to reach.

Namespaces are used for other purposes also.

Creating namespace:

The C++ language include the keyword “namespace” for creating namespaces



Take a look at the following code:



We can make the above code simple by doing this: using namespace measurements; 

e.g.  cout<<endl<<”Area is: “<< area;

Points to ponder on :

1) 1. We can have more than one namespace of the same name.

2) 2. We can have anonymous namespaces (namespace with no name). They are directly usable in the same program and are used for declaring unique identifiers. It also avoids making global static variable. The “anonymous” namespace you have created will only be accessible within the file you created it in.

For example look at the code below:


Check the result!!

 

Anyway, when there is a boon there is a bane too.

Most of the professional coders don’t prefer, “using namespace std” , as a good practice.

They say what if one or two of your function is names ‘Min()’ or ‘Max()’?

It will get collide with std::max, as using “using namespace std” is global.

In certain places, it is permitted to have a using declaration, which is of the form using ::foo::bar;

Look at the code below.


This is a little extreme with just one program, but you get the idea.

Imporant note:

Why we use, "int argc" and "char *argv" ?

They are used to control program from outside instead of hard coding those values inside the code. argv[argc] is a NULL pointer. argv[0] holds the name of the program. argv[1] points to the first command line argument and argv[n] points last argument.


We also have something on "Dynamic Memory Allocation in C/C++", check it out by clicking here.