<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Karan-Munjani</title>
    <description>The latest articles on DEV Community by Karan-Munjani (@karanmunjani).</description>
    <link>https://dev.to/karanmunjani</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F673239%2F2b8d9775-f358-4bb9-b6e1-0c2e107d2e94.jpg</url>
      <title>DEV Community: Karan-Munjani</title>
      <link>https://dev.to/karanmunjani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karanmunjani"/>
    <language>en</language>
    <item>
      <title>Playfair Cipher Encryption Program In Python</title>
      <dc:creator>Karan-Munjani</dc:creator>
      <pubDate>Fri, 01 Oct 2021 05:08:49 +0000</pubDate>
      <link>https://dev.to/karanmunjani/encryption-using-playfair-cipher-in-python-24l4</link>
      <guid>https://dev.to/karanmunjani/encryption-using-playfair-cipher-in-python-24l4</guid>
      <description>&lt;p&gt;In this post I'm going to explain you how to code playfair cipher to encrypt plaintext to cipher. So this post is all about encryption program and will soon add decryption program in this series also so stay tuned!&lt;/p&gt;

&lt;p&gt;If you don't know how the algorithm works checkout previous post in this series.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So Without further ado, LET'S BEGIN!!👨‍💻👩‍💻&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Can you recall what were the steps of algorithm?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Yes, You're right!&lt;/strong&gt; &lt;br&gt;
The Algorithm was consist of &lt;strong&gt;three&lt;/strong&gt; steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Convert plaintext into digraphs (i.e., into pair of two letters)&lt;/li&gt;
&lt;li&gt;Generate a Cipher Key Matrix&lt;/li&gt;
&lt;li&gt;Encrypt plaintext using Cipher Key Matrix and get ciphertext.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h1&gt;
  
  
  Programming Playfair Cipher:
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Step: 1 Creating function to convert plaintext into digraphs
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step: 2 Function to generate a cipher key matrix
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Now we need to locate index of particular character inside cipher key matrix to perform encryption:
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step: 3 Encrypt plaintext using cipher key matrix and get ciphertext
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Final Step! Running our code from main method:
&lt;/h2&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt; &lt;/p&gt;


&lt;center&gt; * * * * * * *&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Thanks a lot for reading out!
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;I hope the code with comments was actually sufficient to understand the logic.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's the repository link to the whole code file &lt;a href="https://github.com/Karan-Munjani/Cryptography_Using_Python/blob/main/Playfair%20Cipher.py"&gt;GitHub Repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But if you still have any doubts feel free to mention below in discussion section, I would love to answer them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Also drop a heart ❤ if you gained some knowledge!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>python</category>
      <category>programming</category>
      <category>security</category>
    </item>
    <item>
      <title>What is Playfair Cipher Encryption Algorithm</title>
      <dc:creator>Karan-Munjani</dc:creator>
      <pubDate>Thu, 30 Sep 2021 20:37:36 +0000</pubDate>
      <link>https://dev.to/karanmunjani/what-is-playfair-cipher-encryption-algorithm-4npk</link>
      <guid>https://dev.to/karanmunjani/what-is-playfair-cipher-encryption-algorithm-4npk</guid>
      <description>&lt;p&gt;This is going to be two or three part series where I'm going to explain the theory behind the Playfair cipher and how we can program it using most probably everyone's favourite language 🥁🥁🥁 (&lt;em&gt;"DRUM ROLLS"&lt;/em&gt;)  &lt;strong&gt;Python&lt;/strong&gt;, Yay!!&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h1&gt;
  
  
  Playfair cipher encryption Algorithm:
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;The Playfair cipher or Playfair square or Wheatstone–Playfair cipher is a manual symmetric encryption technique and was the first literal digram substitution cipher. The scheme was invented in 1854 by Charles Wheatstone, but bears the name of Lord Playfair for promoting its use. - Source: Wikipedia&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;So what we can grasp from above is that, this algorithm was developed by Charles Wheatstone but bears the name as Lord Playfair promoted it. Also we can depict that it was first practical digraph substitution cipher.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Digraph Cipher&lt;/strong&gt; : It simply means that we encrypt the plaintext to ciphertext with two characters at a time. (Will get more idea as I explain you through.)  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;center&gt; * * * * * * *&lt;/center&gt;

&lt;h2&gt;
  
  
  The Playfair Cipher Algorithm
&lt;/h2&gt;

&lt;p&gt;The Algorithm mainly consist of three steps: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Convert plaintext into digraphs (i.e., into pair of two letters)&lt;/li&gt;
&lt;li&gt;Generate a Cipher Key Matrix&lt;/li&gt;
&lt;li&gt;Encrypt plaintext using Cipher Key Matrix and get ciphertext.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;For the rest of our discussion we will be using below mentioned Key and Plaintext:&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Key&lt;/strong&gt; = DevToDiesel&lt;br&gt;
&lt;strong&gt;Plaintext&lt;/strong&gt; = Awesome Dev Community&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So let's move forward!!!&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step:1 Convert plaintext into digraphs (i.e., into pair of two letters)
&lt;/h2&gt;

&lt;p&gt;So our Plaintext &lt;code&gt;Awesome Dev Community&lt;/code&gt; will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[AW, ES, OM, ED, EV, CO, MX, MU, NI, TY] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now You might be wondering why &lt;code&gt;MX&lt;/code&gt; instead of &lt;code&gt;MM&lt;/code&gt; and where did the whitespaces go! Because there are three rules to be followed here and those are:&lt;/p&gt;

&lt;p&gt;1.While splitting text into pairs, if the letters are same in a pair then insert filler x. &lt;/p&gt;

&lt;p&gt;2.At the end if only one letter is left it has no pair we can insert our filler x&lt;/p&gt;

&lt;p&gt;3.Remove Whitespaces from the string as well as special characters apart from 25 alphabets.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;center&gt; * * * * * * *&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Step:2 Generate a Cipher Key Matrix
&lt;/h2&gt;

&lt;p&gt;Cipher Key Matrix is a 5×5 grid of alphabets that acts as the key for encrypting the plaintext.&lt;/p&gt;

&lt;p&gt;Each of the 25 alphabets must be unique and one letter of the alphabet (usually J) is omitted from the table (as the table can hold only 25 alphabets). &lt;/p&gt;

&lt;p&gt;If the plaintext contains J, then it is replaced by I.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;As you might expected! There are also rules to generate this key matrix. You're getting brainy now!&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Rules:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. If our key has repeating letters omit them.&lt;/strong&gt;&lt;br&gt;
So our Key = "DevToDiesel" Will be&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;D E V T O I S L
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Fill this unique characters key in our 5X5 Matrix&lt;/strong&gt;&lt;br&gt;
So it might look something like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgrif2fq5zro15h7ck9x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgrif2fq5zro15h7ck9x.png" alt="Half Mat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Fill the remaining places with unused alphabets without J&lt;/strong&gt; As it should only contain 25 letters, only omitting J would result in matrix like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtui4hbfoieyswnshv0d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frtui4hbfoieyswnshv0d.png" alt="Full Mat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;center&gt; * * * * * * *&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: Encrypt plaintext using Cipher Key Matrix and get ciphertext.
&lt;/h2&gt;

&lt;p&gt;Now we will encrypt our diagraph plaintext that is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[AW, ES, OM, ED, EV, CO, MX, MU, NI, TY]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;into Cipher text with the help of cipher key matrix, and following below listed rules.&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Rule: 1
&lt;/h3&gt;

&lt;p&gt;If both letters are not in same column and not in same row then draw a imaginary rectangle shape and take letters on the horizontal opposite corner of the rectangle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
Our first pair from plaintext is "AW", so A will be enciphered to S and W will enciphered to Y.&lt;br&gt;
i.e., &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A -&amp;gt; S&lt;/li&gt;
&lt;li&gt;W -&amp;gt; Y
resulting in,&lt;strong&gt;AW -&amp;gt; SY&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fev1lp2r8v7q5dqmdbnfp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fev1lp2r8v7q5dqmdbnfp.png" alt="Rule 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, pair OM will be enciphered to DR.&lt;br&gt;
i.e., &lt;strong&gt;OM -&amp;gt; DR&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6o1lbkr77y02gugevlye.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6o1lbkr77y02gugevlye.png" alt="Rule 1.2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  Rule: 2
&lt;/h3&gt;

&lt;p&gt;If both the letters are in the same column: Take the letter below each one (going back to the top if at the bottom).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt; &lt;br&gt;
Our Second pair is "ES", so according to above rule it will be enciphered to its below character in same column.&lt;br&gt;
i.e., &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;E -&amp;gt; S&lt;/li&gt;
&lt;li&gt;S -&amp;gt; F
thus, &lt;strong&gt;ES -&amp;gt; SF&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29f3of9b4b48zkir46ym.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29f3of9b4b48zkir46ym.png" alt="Rule 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;
  
  
  Rule: 3
&lt;/h3&gt;

&lt;p&gt;If both the letters are in the same row: Take the letter to the right of each one (going back to the leftmost if at the rightmost position)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt; &lt;br&gt;
The pair DE is in same row, so just choose the right next to each character from cipher key matrix.&lt;br&gt;
i.e., &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;D -&amp;gt; E&lt;/li&gt;
&lt;li&gt;E -&amp;gt; V
so, &lt;strong&gt;DE -&amp;gt; EV&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3j5t5cg7esa6m9dbt1f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz3j5t5cg7esa6m9dbt1f.png" alt="Rule 3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;center&gt; * * * * * * *&lt;/center&gt;

&lt;p&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Same way after performing above rules for whole the list of diagraph plain text, our cipher text will be generated.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Plaintext&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[AW, ES, OM, ED, EV, CO, MX, MU, NI, TY]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Ciphertext&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[SY, SF, DR, VE, VT, KD, PU, UD, MS, AT]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Plaintext = &lt;strong&gt;AWESOMEDEVCOMXMUNITY&lt;/strong&gt;&lt;br&gt;
Ciphertext = &lt;strong&gt;SYSFDRVEVTKDPUUDMSAT&lt;/strong&gt;&lt;/p&gt;

&lt;center&gt; * * * * * * *&lt;/center&gt;

&lt;h3&gt;
  
  
  In the next part of series I'll be showing how to code playfair cipher using python, so stay tuned.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Don't forget to leave your thoughts and doubts in discussion section below, also drop a heart ❤ if you gained some knowledge!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading out!😀😀&lt;/p&gt;

</description>
      <category>security</category>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>Ternary Operator and Short Circuit Conditionals in Python like JavaScript</title>
      <dc:creator>Karan-Munjani</dc:creator>
      <pubDate>Sun, 12 Sep 2021 11:31:36 +0000</pubDate>
      <link>https://dev.to/karanmunjani/ternary-operator-and-short-circuit-conditionals-in-python-like-javascript-24h7</link>
      <guid>https://dev.to/karanmunjani/ternary-operator-and-short-circuit-conditionals-in-python-like-javascript-24h7</guid>
      <description>&lt;p&gt;If you ever coded in JavaScript then you probably would be aware about content of cover image.&lt;/p&gt;

&lt;p&gt;Assuming you are aware that how much ternary operators are useful in programming languages such as C, C++, Go, Perl, Ruby, Java, JavaScript, etc. And Yes Python also Supports Ternary operator.&lt;/p&gt;

&lt;h3&gt;
  
  
  Python Introduced Ternary operator in Python 2.5.
&lt;/h3&gt;

&lt;p&gt;It's Called inline-if, Which has syntax something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a if condition else b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That Simply means if condition is true then result is a else b.&lt;/p&gt;

&lt;p&gt;But if you come from JavaScript Background then it seems odd at first, Cause we are used to code something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;variable = condition ? a : b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;But what if we can familiarize that odd ternary operator to feel like JavaScript one?&lt;/strong&gt;&lt;br&gt;
Yes We can do that, and that's not a rocket science. We can simply replace the ? with python logical &lt;code&gt;and&lt;/code&gt; operator and : with logical &lt;code&gt;or&lt;/code&gt; operator. &lt;/p&gt;

&lt;p&gt;Let's See how:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;variable = condition and a or b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After reading this you might be feel like, "hmmm! that was not much important and useful". But the aim of this article to explain that how logical conditional operators are helpful and just requires a different approach to it.&lt;/p&gt;

&lt;p&gt;Also We can use conditional short-circuiting in JavaScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let data = online &amp;amp;&amp;amp; getData();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What it does is if online is true then and then it calls the getData() method.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Short circuiting means that when we are evaluating an AND expression (&amp;amp;&amp;amp;), if the first operand is false it will short-circuit and not even look at the second operand.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So instead code in python like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(online):
  data = getData()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can simply use logical &lt;code&gt;and&lt;/code&gt; again to avoid writing if statement using one liner code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data = online and getData()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Yes I accept that this was not much smart article compare to others but Hope Some Novice programmer will get help form this🤞.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hope you gained some information from this article✨.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank You For reading it out😀
&lt;/h2&gt;

&lt;p&gt;Comment Your Thoughts Below.&lt;/p&gt;

</description>
      <category>python</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Remove a property of an object immutably in REDUX JavaScript</title>
      <dc:creator>Karan-Munjani</dc:creator>
      <pubDate>Fri, 20 Aug 2021 04:59:39 +0000</pubDate>
      <link>https://dev.to/karanmunjani/remove-a-property-of-an-object-immutably-in-redux-javascript-4igj</link>
      <guid>https://dev.to/karanmunjani/remove-a-property-of-an-object-immutably-in-redux-javascript-4igj</guid>
      <description>&lt;p&gt;If you're using Redux then you'll absolutely know about immutability, how much it's important to follow.&lt;/p&gt;

&lt;p&gt;So we know that Reducer is the place where all good state change occurs with returning a new state and without modifying the previous state.&lt;/p&gt;

&lt;p&gt;Imagine you're building a Bug Tracker App, and It stores all details about bug which are occurred.&lt;/p&gt;

&lt;p&gt;Suppose your initial store looks something like this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

[
 {
   id:"1",
   description:"Bug occured in Network call",
   resolved:false,
 },
 {
   id:"2",
   description:"Bug occured while Signup",
   resolved:false,
 }
]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Also We are logging on console every time store change using store.subscribe():&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

store.subscribe(() =&amp;gt; {
  console.log("store changed!", store.getState());
});


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Now Suppose you are dispatching action when some developers in working on the bug to resolve it but it's not resolved yet.
&lt;/h3&gt;

&lt;p&gt;Below is code of action which will take place&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

export const bugResolving = (id, status) =&amp;gt; ({
  type: actions.BUG_RESOLVING,
  payload: {
    id: id,
    status: status,
  },
});


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;em&gt;And from your code you're calling store.dispatch() something like this:&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

store.dispatch(bugAdded("Bug occured in Network call"));
store.dispatch(bugAdded("Bug occured while Signup"));
store.dispatch(bugResolving(1, "resolving in process by devDiesel"));


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;And from your code you're calling store.dispatch() something like this:&lt;/strong&gt; &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

store.dispatch(bugResolving(1, "resolving in process by devDiesel"));


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Thus your store will look something like this:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ferijcn1sq5e77oj7ww6t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ferijcn1sq5e77oj7ww6t.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in last store change we added status property in bug with id=1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now After a cup of coffee☕ the Dev was able to solve and bug was marked as solved🎯.&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Hooray!!🎉🎉&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;As you've guessed now we want to remove the status property from store object whose id is 1, and also update the resolved to true&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So in your reducer function you might write code like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

function reducer(state = [], action) {
  switch (action.type) {
   case actions.BUG_ADDED:
    //Some Code

   case actions.BUG_REMOVED:
    //Some Code

   case actions.BUG_RESOLVING:
    //Some Code

   case actions.BUG_RESOLVED:
    return state.map((bug)=&amp;gt; {
      if (bug.id === action.payload.id){
        delete bug.status;
        return { ... bug, resolved:true};
      }
      else return bug;

    default:
      return store;
    }
}



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So we will dispatch the action like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

store.dispatch(bugAdded("Bug occured in Network call"));
store.dispatch(bugAdded("Bug occured while Signup"));

store.dispatch(bugResolving(1, "resolving in process by devDiesel"));

store.dispatch(bugResolved(1)); //⬅This one


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So when the reducer runs BUG_RESOLVED it won't work as expected and will delete the status property from previous original bug state,instead of deleting where we wanted.&lt;/p&gt;

&lt;p&gt;And thus will only update resolved to true in last state.&lt;/p&gt;

&lt;p&gt;Which can be seen with the help of console logging of subscribe() method as described in starting.&lt;/p&gt;

&lt;p&gt;See In this picture:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbo2fa94xhctia3hubd75.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbo2fa94xhctia3hubd75.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  So Why this happened??
&lt;/h3&gt;

&lt;p&gt;As JavaScript is not purely immutable language when we return new state object using &lt;code&gt;return state.map((bug)=&amp;gt;{...})&lt;/code&gt; it does shallow copy of objects.&lt;/p&gt;

&lt;p&gt;That is the status property which we are created in previous state and status property which is we deleting are pointing to the same memory address.&lt;/p&gt;

&lt;p&gt;Thus when we delete this property it's get deleted from both object as its referring to same location inside memory&lt;/p&gt;

&lt;h3&gt;
  
  
  Then How to force immutability Now??
&lt;/h3&gt;

&lt;p&gt;We can do deep-copy of the object using Object.assign() method.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 case actions.BUG_RESOLVED:
      return state.map((bug) =&amp;gt; {
        let modifiedBug = Object.assign({}, bug);//1
        if (modifiedBug.id === action.payload.id) {
          modifiedBug.status = Object.assign({}, bug.status);//2
          delete modifiedBug.status;//3
          return { ...modifiedBug, resolved: true };//4
        } else return bug;
      });



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In above code:&lt;/p&gt;

&lt;p&gt;1.) We assigning new object using Object.assign() thus modifiedBug will get its own address in memory.&lt;/p&gt;

&lt;p&gt;2.) We setting modifiedBug.status property with new bug.status using Object.assign() this will also force to have it's separate memory address&lt;/p&gt;

&lt;p&gt;3.)Now we are deleting the modifiedBug.status which won't affect any previous bug object cause its pointing to totally different location.&lt;/p&gt;

&lt;p&gt;4.) In last we are appending resolved:true to modifiedBug Object and returning it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thus now our code will work as we expected&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm74tuvif4aswsx90nfg2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm74tuvif4aswsx90nfg2.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Thank You For Reading Out.😸
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Post any questions in comments if you have&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>redux</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
