Showing posts with label engineering. Show all posts
Showing posts with label engineering. 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 4, 2020

Dynamic Memory Allocation | malloc() | calloc() | free () | realloc() | Array of undefined size | Changing the size of an array using realloc() | CODING WINDS

  Dynamic Memory Allocation

 

Hey people in this blog you are going to read about “Dynamic Memory Allocation”, you need to know before you are using functions like “malloc()”, “calloc()”, “free()”, etc. in your C/C++ code.

To change or define the size of any data structure during the runtime is called “Dynamic Memory Allocation”.

To use Dynamic memory allocation we need to use several library functions

malloc() , calloc() , free() , realloc().  These operations are including in the library <stdlib.h> .

1.  malloc() function :

The name “malloc” stands for memory allocation.

SYNTAX  :  ptr = (CastType *)malloc(size);

The malloc() function reserves a block of memory of the specified number of bytes. And, it returns a pointer of void which can be casted into pointers of any form.

2.  calloc() function :

The name “calloc” stands for contiguous allocation.

The malloc() function allocates memory and leaves the memory uninitialized. Whereas, the calloc() function allocates memory and initializes all bits to zero.

 

SYNTAX  :  ptr = (CastType *)calloc(n,elements_size);

Where, n – number of elements

3.  free() function :

The dynamically allocated memory created either with either calloc() or malloc() does not get freed upon themselves. You must  explicitly use free() to release the space.

SYNTAX  :  free(ptr);

4.  realloc() function :

The name “realloc” stands for re-allocation.

“realloc” method in C is used to dynamically change the memory allocation of a previously allocated memory. In other words, if the memory previously allocated with the help of “malloc” or “calloc” is insufficient, “realloc” can be used to dynamically re-allocate memory. Re-allocation of memory maintains the already present value and new blocks will be initialized with default garbage value.

 

SYNTAX  :  ptr = realloc(ptr, NewSize);

 

It is hard to understand this topic by just reading this bookish deifinition.

So, let’s just look into some working examples of this, which might help get insight of this topic in a better way :

1.    Declaring an array of a variable size, using “malloc()“ function.

2.    Intializing an array of variable size with a zero value, using “calloc()” function.

3.    Deleting a memory space occupied by any data structure, using “free()” function.

4.    Re-defining the size of an array, using “realloc()”  function.

 

 

1.   Declaring an array of a variable size, using “malloc()“ function.

 

 

2.   Intializing an array of variable size with a zero value, using “calloc()” function.

 


 

3.   Deleting a memory space occupied by any data structure, using “free()” function.

 


4.   Re-defining the size of an array, using “realloc()”  function.