<?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: Manar Imad</title>
    <description>The latest articles on DEV Community by Manar Imad (@manarabdelkarim).</description>
    <link>https://dev.to/manarabdelkarim</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%2F690419%2F360a0122-135d-42bf-981f-bb3ed450f62e.jpeg</url>
      <title>DEV Community: Manar Imad</title>
      <link>https://dev.to/manarabdelkarim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manarabdelkarim"/>
    <language>en</language>
    <item>
      <title>Models' Relationship In Django</title>
      <dc:creator>Manar Imad</dc:creator>
      <pubDate>Sat, 25 Sep 2021 23:26:16 +0000</pubDate>
      <link>https://dev.to/manarabdelkarim/models-relationship-in-django-1c93</link>
      <guid>https://dev.to/manarabdelkarim/models-relationship-in-django-1c93</guid>
      <description>&lt;p&gt;Hello BackEnders &lt;/p&gt;

&lt;p&gt;Today, I decided to explain OneToOne , ManyToMany , and Foreignkey in Django &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/eXTue7sCt6ZvG/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/eXTue7sCt6ZvG/giphy.gif" alt="Django model"&gt;&lt;/a&gt;&lt;br&gt;
So let's get started:&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we use relationships?
&lt;/h2&gt;

&lt;p&gt;The first step to understand the relationships is to understand why we need them. Let's not talk too much about the concept of "Normalization", imagine if we have a table with students names, gender, and school events :&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;StudentID&lt;/th&gt;
&lt;th&gt;StudentName&lt;/th&gt;
&lt;th&gt;StudentGender&lt;/th&gt;
&lt;th&gt;StudentsClass&lt;/th&gt;
&lt;th&gt;StudentDesk&lt;/th&gt;
&lt;th&gt;SchoolEvents&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Natchos&lt;/td&gt;
&lt;td&gt;Male&lt;/td&gt;
&lt;td&gt;4A&lt;/td&gt;
&lt;td&gt;321&lt;/td&gt;
&lt;td&gt;Zoo Visit, Charity Day&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Manar&lt;/td&gt;
&lt;td&gt;Female&lt;/td&gt;
&lt;td&gt;5A&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;td&gt;Charity Day&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Kareem&lt;/td&gt;
&lt;td&gt;Male&lt;/td&gt;
&lt;td&gt;4A&lt;/td&gt;
&lt;td&gt;66&lt;/td&gt;
&lt;td&gt;Zoo Visits , Math Competition&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;you can notice here that every student has a desk "represented with a number" but why do we have this data in this table? Also, "Male" in StudentGender and "4A" in StudentsClass has been repeated twice in the table. And the worst is in the SchoolEvents column. Just think if we want to delete the Zoo Visit from Natchos's ongoing events, the Charity Day will be deleted too! &lt;br&gt;
Imagine if we have 100 or 1000 students!&lt;/p&gt;

&lt;p&gt;Instead, we have to divide the table into smaller tables "four tables in our case" &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;StudentID&lt;/th&gt;
&lt;th&gt;StudentName&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Natchos&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Manar&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Kareem&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;StudentGender&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Male&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Female&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;StudentsClass&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;4A&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;5A&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ID&lt;/th&gt;
&lt;th&gt;Events&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Zoo Visit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Charity Day&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Math Competition&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Perfect. But what is next? We need a way "a link" to tell the database that this particular student is in this class, has this gender and will go to these events.&lt;br&gt;
That is what we do with relationships. A relationship is an association between two entities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Foreignkey:
&lt;/h2&gt;

&lt;p&gt;I started with Foreignkey because it is the easiest to understand. &lt;/p&gt;

&lt;p&gt;Foreignkey in Django represents &lt;strong&gt;"one to many"&lt;/strong&gt; also called &lt;strong&gt;"many to one"&lt;/strong&gt; relationship in the database. &lt;br&gt;
it is just the same as its name one to many! I have one mother and my mother has many children. The country has many cities and every city is located in one country.&lt;br&gt;
In our example above, every student "normally" should have one gender. and every gender is for many students. &lt;br&gt;
Natchos and Kareem are two students "many", Male is one.&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%2Fwohg10xd98uksfvc1lq2.jpg" 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%2Fwohg10xd98uksfvc1lq2.jpg" alt="One To many"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Represent Foreignkey in Django:
&lt;/h3&gt;

&lt;p&gt;Because we can not represent all the "many" in the "one" table. It makes a lot of sense to represent the repationship from the many sides.. which means in our example we can't go to the Gender table add a row and say in it: we have a relationship with Natchos , and Kareem and John and Tom ..etc in the male row "record" .. instead we can add a row in the student row and called gender that says these students a relationship with the Gender table in "Male" or in "Female".&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%2Fon0jm6pfa6fbilmczp8v.jpg" 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%2Fon0jm6pfa6fbilmczp8v.jpg" alt="many to one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now to represent the ForeignKey will add it in the Student Field with one main parameter: referring to the model that has ForeignKey relationship "many to one" with it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.db import models

class StudentGender(models.Model):
    gender = models.CharField(max_length=10)
class Student(models.Model):
    name = models.CharField(max_length=30)
    gender = models.ForeignKey(StudentGender, 
   null=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Notice in class Student :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gender = models.ForeignKey(Gender, 
   on_delete=models.CASCADE)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That in Django we don't refer to the id of the other model but for the model "class" itself ( in other frameworks such as Flask, we refer to the other model's id). Also, we used null=True to allow the nulls so we can have some students with no gender specified.&lt;/p&gt;

&lt;h2&gt;
  
  
  OneToOne
&lt;/h2&gt;

&lt;p&gt;one to one is as easy as you can think. this pen is mine and this pen has one owner. I have one husband and my husband has one wife. In our example, every student setting on one desk and every desk is for one student "at least in my country" &lt;/p&gt;

&lt;h3&gt;
  
  
  Represent OneToOne in Django:
&lt;/h3&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%2Fvpiz7dffrltbgtu81yu2.jpg" 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%2Fvpiz7dffrltbgtu81yu2.jpg" alt="one to one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point and because we are going to add one value in either table, it doesn't matter where we add it. But because I think that it is a good idea to add in the Chair table since that the student is the main character, I will add the OneToOne field to the Chair :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class StudentGender(models.Model):
    gender = models.CharField(max_length=10)
    Student = models.OneToOneField(Student, 
   null=True)
class Student(models.Model):
    name = models.CharField(max_length=30)
    gender = models.ForeignKey(StudentGender, 
   null=True)

Notice in class Student :

gender = models.ForeignKey(StudentGender, 
   null=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Notice that here I also used "null=True" because as you know, if a chair has no student to set on, we don't have to throw it from the window. &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%2Fthumbs.gfycat.com%2FScalySelfassuredDuck-max-1mb.gif" 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%2Fthumbs.gfycat.com%2FScalySelfassuredDuck-max-1mb.gif" alt="one to one"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  ManyToMany
&lt;/h1&gt;

&lt;p&gt;Students enroll in many courses and every course is enrolled by many students. in our example, each student can attend many events and every event will be attended by many students. This is simply what a many to many relationships is. &lt;/p&gt;

&lt;h3&gt;
  
  
  Represent ManyToMany in Django:
&lt;/h3&gt;

&lt;p&gt;In ForeignKey we chose the "one" table and in OneToOne we chose either one. so what table can we choose in many to many? In the normal cases, we create a third table "more like a third wheel in the relationship" that has the id of the first table and the id of the second table as below:&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%2Fiye7rnmay7m5jebnlnz7.jpg" 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%2Fiye7rnmay7m5jebnlnz7.jpg" alt="many to many"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  But!
&lt;/h4&gt;

&lt;h3&gt;
  
  
  But!
&lt;/h3&gt;

&lt;h2&gt;
  
  
  But!
&lt;/h2&gt;

&lt;p&gt;Django made it easier for us. it is just as easy as adding ManyToOne or foreignKey!Again I will not choose the student.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student(models.Model):
    name = models.CharField(max_length=30)
    gender = models.ForeignKey(StudentGender, 
   null=True)

class Chair(models.Model):

chair_number = models.IntegerField()
student = models.ManyToManyField('student')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, It seems we have reached the end. &lt;br&gt;
Happy day to you and happy birthday to ME 🎂&lt;/p&gt;

&lt;h2&gt;
  
  
  References:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://sjlouji10.medium.com/django-models-with-relationships-one-to-one-relationship-f3ef8d77aec1" rel="noopener noreferrer"&gt;Django Models with Relationships — One to One Relationship&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.djangoproject.com/en/3.2/ref/models/fields/" rel="noopener noreferrer"&gt;Model field reference¶
&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>database</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Applying Confusion Matrix In Real-Life Scenario</title>
      <dc:creator>Manar Imad</dc:creator>
      <pubDate>Tue, 31 Aug 2021 11:41:35 +0000</pubDate>
      <link>https://dev.to/manarabdelkarim/how-to-apply-confusion-matrix-in-real-life-scenario-2i7a</link>
      <guid>https://dev.to/manarabdelkarim/how-to-apply-confusion-matrix-in-real-life-scenario-2i7a</guid>
      <description>&lt;h3&gt;
  
  
  Hello Scientist
&lt;/h3&gt;

&lt;p&gt;Before we start, this article is a follow-up to the previous one about (Sets) in Math and Python. &lt;/p&gt;

&lt;p&gt;To understand today’s post you should have knowledge of Sets or you can go and read &lt;a href="https://dev.to/manarabdelkarim/confusion-matrix-sets-in-math-and-python-5ahe"&gt;What Is Set In Math And Python&lt;/a&gt; &lt;br&gt;
Ok? Are you ready??&lt;/p&gt;

&lt;p&gt;Then let’s get started.&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%2Feu37k2sfmm42x2nw62p2.gif" 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%2Feu37k2sfmm42x2nw62p2.gif" alt="Confusion Matrix"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our first example is detecting Corona on People.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's call all the people that we chose as a test sample -&amp;gt; X&lt;/li&gt;
&lt;li&gt;And the set of people among X  who are sick (have Corona) -&amp;gt; S&lt;/li&gt;
&lt;li&gt;The set of people among X who are Healthy(don't have Corona) -&amp;gt; H&lt;/li&gt;
&lt;/ul&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%2Fsytr12mcuh0slyl7bmry.jpg" 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%2Fsytr12mcuh0slyl7bmry.jpg" alt="Confusion Matrix"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To represent S in mathematics we write:&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;S={x ∈ X: x has Corona}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;The small x means one of the X sets (a member of X).&lt;br&gt;
So the formula says an x "a person from the sample test" that is a member of the X "the set of all the people of the sample test" and that x "that person" has Corona (from the S sub set).&lt;/p&gt;

&lt;p&gt;And to represent H in mathematics we write:&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;H={x ∈ X: x doesn't have Corona}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now, what is the possibility that one of the X people has Corona and doesn't have Corona at the same time?&lt;br&gt;
Of course, this is impossible!&lt;/p&gt;

&lt;p&gt;So we know that |S| ∩ |H| = ∅ and |S| ∪ |H| = X (the people in the test sample are either sick or healthy so if we put all the sick and healthy people together they will be all of our samples)&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%2Fb236a3oszqvb9gleiz1e.jpg" 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%2Fb236a3oszqvb9gleiz1e.jpg" alt="Confusion Matrix"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's add our Corona detect prediction as we want to know how accurate is our CPR prediction.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;So let the people from the sample who tested positive -&amp;gt;P
And we represent them as P= {x ∈ X : x positive for Corona}&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that positive here means that the CPR predicts that someone has Corona.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The people from the sample who tested negative -&amp;gt;N
And we represent them as N= {x ∈ X : x negative for Corona}&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we have 4 probabilities:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;someone who has Corona and his test for Corona is positive.&lt;/li&gt;
&lt;li&gt;someone who does not have Corona and his test for Corona is negative.&lt;/li&gt;
&lt;li&gt;someone who has Corona but his test for Corona is negative.&lt;/li&gt;
&lt;li&gt;someone who does not have Corona but his test for Corona is positive.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;these probabilities are called the Confusion matrix&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%2Fj5rf9xdzffeqqv0y7sys.jpg" 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%2Fj5rf9xdzffeqqv0y7sys.jpg" alt="Confusion Matrix"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Confusion Matrix:
&lt;/h2&gt;

&lt;p&gt;A confusion matrix is a technique for summarizing the performance of a classification algorithm.&lt;/p&gt;

&lt;p&gt;Calculating a confusion matrix can give you a better idea of what your classification model is getting right and what types of errors it is making.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now Let's discuss the 4 possibility:&lt;/strong&gt;&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%2Fi.kym-cdn.com%2Fentries%2Ficons%2Foriginal%2F000%2F021%2F464%2F14608107_1180665285312703_1558693314_n.jpg" 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%2Fi.kym-cdn.com%2Fentries%2Ficons%2Foriginal%2F000%2F021%2F464%2F14608107_1180665285312703_1558693314_n.jpg" alt="math"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Someone who has Corona and his test for Corona is positive.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The representation of this possibility is: |S| ∩ |P|&lt;br&gt;
because S is the people who have Corona and P are the people who tested positive.&lt;br&gt;
someone who has Corona and tested positive means we want to find someone in both sets (S and P) which means the intersection.&lt;/p&gt;

&lt;p&gt;If we have a girl called Sara, Sara is in the P set which means the CPR predicted that Sara is in the S set ( the set of sick people)&lt;br&gt;
If Sara is tested positive we call this prediction "positive" and if she is sick "if she is in S set" then we say the prediction is True -&amp;gt; True Positive.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the prediction of what we are trying to find is right we call it positive.&lt;/li&gt;
&lt;li&gt;When the reality meets the prediction we call it True
So |S| ∩ |P|  = True Positive.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;someone who does not have Corona and his test for Corona is negative.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The representation of this possibility is: |H| ∩ |N|&lt;br&gt;
because H is the healthy people who don't have Corona and N are the people who tested negative.&lt;br&gt;
someone who doesn't have Corona and tested negative means we want to find someone in both sets (H and N) which means the intersection.&lt;/p&gt;

&lt;p&gt;Let's assume that the girl called Sara is in the N set which means the CPR predicted that Sara is in the H set ( the set of healthy people)&lt;br&gt;
If Sara is tested negative we call this prediction "negative" and if she is sick "if she is in S set" then we say the prediction is True -&amp;gt; True negative.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the prediction of what we are &lt;strong&gt;, not&lt;/strong&gt; trying to find (or the opposite of what we want to predict) is right we call it negative.
-When the reality meets the prediction we call it True&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So |H| ∩ |N| = True Negative&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Someone who has Corona but his test for Corona is negative.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The representation of this possibility is: |S| ∩ |N|&lt;br&gt;
because S is the sick people who do have Corona and N are the people who tested negative.&lt;/p&gt;

&lt;p&gt;Let's assume that the girl called Sara is in the N set which means the CPR predicted that Sara is in the H set ( the set of healthy people)&lt;br&gt;
If Sara is tested negative we call this prediction "negative" and because she is sick and not healthy "she is in S set" then we say the prediction is False -&amp;gt; False-negative.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the prediction of what we are &lt;strong&gt;, not&lt;/strong&gt; trying to find (or the opposite of what we want to predict) is right we call it negative.&lt;/li&gt;
&lt;li&gt;When the reality does not match the prediction we call it False&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So |H| ∩ |N| = False Negative&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;someone who does not have Corona but his test for Corona is positive.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The representation of this possibility is: |H| ∩ |P|&lt;br&gt;
because H are the healthy people who don't have Corona and P are the people who tested positive.&lt;/p&gt;

&lt;p&gt;Again, the poor Sara is in the P set which means the CPR predicted that Sara is in the N set ( the set of sick people)&lt;br&gt;
If Sara is tested positive we call this prediction "positive" and because she is healthy "she is in H set" and not sick then we say the prediction is False -&amp;gt; False positive.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When the prediction of what we are trying to find is right we call it positive.&lt;/li&gt;
&lt;li&gt;When the reality does not match the prediction we call it False&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So |H| ∩ |P| = False positive&lt;/p&gt;

&lt;p&gt;Note that to make our prediction accurate we want to increase the True positive and True negative and decrease the False-positive and the False-negative.&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%2F603gtct19tmcicy2gjfi.jpg" 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%2F603gtct19tmcicy2gjfi.jpg" alt="Confusion matrix"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I know that it is a bit of a headache so here is another example you can think of:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;we are making an app with machine learning algorithms that predict if a word is a bad - nasty word.&lt;/p&gt;

&lt;p&gt;Again the word would be either bad word or not bad word and&lt;/p&gt;

&lt;p&gt;Because we are searching for bad words, if we predict one we call the prediction positive, and if we predict the opposite "not bad" we call the production negative.&lt;/p&gt;

&lt;p&gt;If our prediction is True then we call the prediction True, and if our prediction is False then we call the production False.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;our sample X is the words in a sentence.&lt;/li&gt;
&lt;li&gt;G is the set of not bad words. -&amp;gt; G= {x ∈ X: X is not a bad word}.&lt;/li&gt;
&lt;li&gt;B is the set of bad words. -&amp;gt; B= {x ∈ X: X is a bad word}.&lt;/li&gt;
&lt;li&gt;P is for the probability of bad words -&amp;gt; P= {x ∈ X: positive for bad words}.&lt;/li&gt;
&lt;li&gt;P is for the probability of not bad words -&amp;gt; P= {x ∈ X: negative for bad words}.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;|B| ∩ |P|= True Positive&lt;/li&gt;
&lt;li&gt;|B| ∩ |N|= False Negative&lt;/li&gt;
&lt;li&gt;|G| ∩ |P|= False Positive&lt;/li&gt;
&lt;li&gt;|G| ∩ |N|= True Negative&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Bonus 🥳:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Let's represent the Confusion Matrix&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%2Fxfkwdmk1dwewsksqe8fb.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%2Fxfkwdmk1dwewsksqe8fb.png" alt="venn"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;References: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.coursera.org/learn/datasciencemathskills" rel="noopener noreferrer"&gt;Data Science Math Skills - Duke University | Coursera&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://machinelearningmastery.com/confusion-matrix-machine-learning/" rel="noopener noreferrer"&gt;What is a Confusion Matrix in Machine Learning&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>datascience</category>
      <category>machinelearning</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What Is Set In Math</title>
      <dc:creator>Manar Imad</dc:creator>
      <pubDate>Mon, 30 Aug 2021 20:34:21 +0000</pubDate>
      <link>https://dev.to/manarabdelkarim/confusion-matrix-sets-in-math-and-python-5ahe</link>
      <guid>https://dev.to/manarabdelkarim/confusion-matrix-sets-in-math-and-python-5ahe</guid>
      <description>&lt;h3&gt;
  
  
  Hello Scientists
&lt;/h3&gt;

&lt;p&gt;Recently I was applying for data science positions. At many interview and technical exams, you will be asked about some math basics that are important for any data Scientist. So today let's study math! What is Set , cardinality , intersection , and union, then, we will talk about confusion matrix&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%2Fck6jtqw8fgl3yy6ruimk.gif" 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%2Fck6jtqw8fgl3yy6ruimk.gif" alt="Cute Cat GIF • Purrfect pillow for Cat with a computer mouse and a calculator. Very soft and comfy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's get started:&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Set
&lt;/h2&gt;

&lt;p&gt;Set is a collection of things, set is made up of elements.&lt;/p&gt;

&lt;p&gt;Set can be a group of similar or different things. For example, you can have a set of books or a set of random things in a box. &lt;/p&gt;

&lt;h3&gt;
  
  
  let's look at some symbols that are commonly used with sets
&lt;/h3&gt;

&lt;p&gt;{}   -&amp;gt; The representation of set (In Python this is a dict but we will use it)&lt;br&gt;
∈    -&amp;gt; Membership/ Element of a set&lt;br&gt;
∉   -&amp;gt; not element of/ no set membership&lt;br&gt;
∅   -&amp;gt; Empty set (Phi)&lt;br&gt;
∪    -&amp;gt; Union&lt;br&gt;
∩    -&amp;gt; Intersection&lt;/p&gt;
&lt;h3&gt;
  
  
  Now let's take examples to understand the set and the symbols:
&lt;/h3&gt;

&lt;p&gt;If we have a set of students of a math course and their names are : Manar, Noor, Raneem, Noura.&lt;/p&gt;

&lt;p&gt;Then, to represent that set we write :&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;math_students = {Manar, Noor, Raneem, Noura}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Set representation in Python :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;math_students = {'Manar', 'Noor', 'Raneem', 'Noura'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that sets in Python can be from different types, such as :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_set = {'A',4,True,5.78}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And Set items in Python are unordered, unchangeable, and do not allow duplicate values.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;How many students above applied for the math course ?&lt;/p&gt;

&lt;p&gt;The answer is 4. To represent it mathematically we use the word&lt;br&gt;" cardinality" which just means the size of the set. So, the cardinality of math_students is 4 and that's how we represent it:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;|math_students| = 4&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To find the cardinality in Python we write:&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;cardinality = len(math_students)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note that the name cardinality here is just an example of a variable name&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now what is the relationship between Noura and math_students?&lt;/p&gt;

&lt;p&gt;Noura is a member of the math_students&lt;br&gt;
we represent it using ∈ like the way below :&lt;br&gt;
Noura ∈ math_students&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To check if there is a membership relationship in Python :&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;membership = "Noura" in math_students
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note that the name membership here is just an example of a variable name&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What is the relationship between Dario and math_students ?&lt;/p&gt;

&lt;p&gt;Dario is not a member of the math students set. That's how we represent it:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Dario ∉ math_students&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To check the if there is no membership relationship in Python :&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;no_membership = "Dario" not in math_students
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note that the name no_membership here is just an example of a variable name&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now we have another set failed_students for the students who failed in the exam, but fortunately, no student has fail. So our new set is empty. To represent our empty set we write:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;failed_students = ∅&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In Python, the empty set looks like :&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;failed_students = {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And its cardinality equal to zero :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(len(failed_students))

output : 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now we reached the fun Part (Intersection and Union):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And we will start with intersection:&lt;/p&gt;

&lt;p&gt;back to our math_class example, let's assume that we have another course which is Python, and the students who applied for the class are Manar, Tala, Dario, Raneem, Aseel&lt;br&gt;
So now we have :&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;math_students = {Manar, Tala, Raneem, Noura}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;Python_students = {Manar, Noor, Dario, Raneem, Aseel}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;If you check the students' name (assuming that they are the same people),we will find out that Manar and Raneem are attending both math and Python classes&lt;/p&gt;

&lt;p&gt;So we say the intersection between math_students and Python_students are Manar and Raneem .. To represent it mathematically:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;math_students ∩ Python_students = {Manar, Raneem}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note that the result of the intersection is a set&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In Python, we represent the intersection using &amp;amp; symbol :&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;math_students &amp;amp; Python_students

output: {'Manar','Raneem'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's imagine that we have another course for Geography, and the students are : Mark, Kitty, Tala and Keven&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;geography_studen = {Mark, Kitty, Tala, Keven}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;What is the intersection between geography_studen and python_students ?&lt;br&gt;
Well, the answer is: there is no intersection&lt;br&gt;
And What did we say about how to represent an empty set ? by using Phi ∅ . So:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;geography_studen ∩ python_students =  ∅&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;And in Python :&lt;br&gt;
&lt;/p&gt;

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

output: set()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now the Union&lt;/strong&gt;&lt;br&gt;
We have :&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;math_students = {Manar, Tala, Raneem, Noura}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;Python_students = {Manar, Noor, Dario, Raneem, Aseel}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;The union is all the members of all the sets&lt;br&gt;
which means here, all the members of math_students and all the members of Python_students.&lt;/p&gt;

&lt;p&gt;To represent the Union we said that we will use ∪.&lt;br&gt;
So, let's try to put them together :&lt;br&gt;
math_students ∪ Python_students = {Manar, Tala, Raneem, Noura, Manar, Noor, Dario, Raneem, Aseel}&lt;/p&gt;

&lt;p&gt;Ok that seems good but as we can notice, Manar and Raneem are duplicated, Why ? because Manar and Raneem are attending both the math and Python courses. in other words, because Manar and Raneem are the intersection between math_students and Python_students. So let's remove one Manar and one Raneem&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;math_students ∪ Python_students = {Manar, Tala, Raneem, Noura, Noor, Dario, Aseel}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now Our Union is correct. What we just did is something called Inculsion Exclusions Formula.&lt;/strong&gt;&lt;br&gt;
The mathematical representation of the formula is:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;|A ∪ B| = |A| + |B| - |A ∩ B|&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;And that's what we did. we added Python_students and math_students then we minus the interaction (remove the duplication)&lt;/p&gt;

&lt;p&gt;To represent union in Python:&lt;br&gt;
&lt;/p&gt;

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

output: {'Manar', 'Tala', 'Raneem', 'Noura', 'Noor', 'Dario', 'Aseel'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it for the symbols 😀&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus Info 🥳: What Is Venn Diagram?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Venn Diagram:
&lt;/h3&gt;

&lt;p&gt;Is illustration the uses circles to show the relationship among infinites groups of things.&lt;/p&gt;

&lt;p&gt;For Example:&lt;/p&gt;

&lt;p&gt;A= {1,3,5,7}     B= {2,3,4,5,6}      C= {9,15}&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%2Fiax61egan2bg4wr2bbvt.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%2Fiax61egan2bg4wr2bbvt.png" alt="venn"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, we reached the end. Hope you Enjoyed &lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/python/ref_set_intersection.asp" rel="noopener noreferrer"&gt;Python Set intersection() Method&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/python/ref_set_union.asp" rel="noopener noreferrer"&gt;Python Set union() Method&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://realpython.com/python-sets/" rel="noopener noreferrer"&gt;Sets in Python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.coursera.org/learn/datasciencemathskills" rel="noopener noreferrer"&gt;Data Science Math Skills - Duke University | Coursera&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.investopedia.com/terms/v/venn-diagram.asp" rel="noopener noreferrer"&gt;Venn Diagram&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>datascience</category>
      <category>python</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Reasons To Use Recursion and How It Works</title>
      <dc:creator>Manar Imad</dc:creator>
      <pubDate>Fri, 27 Aug 2021 17:04:19 +0000</pubDate>
      <link>https://dev.to/manarabdelkarim/why-and-how-to-solve-iterative-problems-recursively-the-smart-coding-4nj2</link>
      <guid>https://dev.to/manarabdelkarim/why-and-how-to-solve-iterative-problems-recursively-the-smart-coding-4nj2</guid>
      <description>&lt;h4&gt;
  
  
  Hello Engineers
&lt;/h4&gt;

&lt;p&gt;Today, I will talk about recursion in what, why, and how.&lt;br&gt;
What is recursion , why to use it, and how to solve problems with it.&lt;br&gt;
The examples will be in Python since Python is easy to understand and close to the algorithms ..&lt;/p&gt;

&lt;p&gt;Shall we start ? &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%2Fmedia1.giphy.com%2Fmedia%2FH4DjXQXamtTiIuCcRU%2F200.gif" 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%2Fmedia1.giphy.com%2Fmedia%2FH4DjXQXamtTiIuCcRU%2F200.gif" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is recursion?
&lt;/h3&gt;

&lt;p&gt;A more complex definition of recursion is: a technique that solve a problem by solving a smaller problems of the same type . We can simplify the definition by saying that recursion is a function that calls itself directly or indirectly. Don't worry for now, it will be much clearer when we start writing our examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why to use recursion?
&lt;/h3&gt;

&lt;p&gt;I choose this question over "when to use recursion" because many learners who studied recursion wanted to know why to use it since that all the problems we can solve in recursion , we can also solve it in normal iterative loops! So here is my answer:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1- Learning recursion makes you a better programmer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Learning and practicing recursion helps you improve your problem solving skills, because you will learn how to break down bit problems into smaller ones. Also, you will learn the principle of stack in data structure (A stack is an abstract data type that holds an ordered, linear sequence of items), the principle of LIFO (Last In First Out) and how to make use of the call stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2- Using recursion makes the code clearer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Recursion code is simpler and shorter than an iterative code. The recursion function will be written in less lines of code and will be easier for debugging.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3- Recursion is data structure's best friend:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As I mentioned above, recursive functions use the call stack of the interpreter. So, we can make use of the existing of call stack instead of writing code and creating a stack by ourselves or by using any external libraries. which means we can use recursion whenever we want to use stack. Two good implementation examples for using recursion in data structures are trees and graphs in-depth traversal.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to write recursive functions?
&lt;/h3&gt;

&lt;p&gt;Before writing a recursive function ,let's talk about a few factors to be determined :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1- Determine the size factor:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The size factor means that your problem should not exceed your static value or your memory allocation for that particular program. In simple language your program should not have large numbers and not too many iterations. Because again, recursion functions use call stack and the call stack has a limit. When the stack becomes full the program will crash and you will have an error.&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%2Fnabxiju4v37hs5pnbq81.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%2Fnabxiju4v37hs5pnbq81.png" alt="recursionerror"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2- Determine the base case/s:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The base cause could be a single or multiple values depending on your program. I will use a while loop here to explain the base case .. if we want to write a program that print "hello world" 5 times using while, we will write:&lt;/p&gt;

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

def display_hello(i):
    while(i &amp;gt; 0):
        print("hello world")
        i -=1


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

&lt;/div&gt;

&lt;p&gt;The base cause determines when the loop will stop .. in our example the base cause is i=0 because when the value of i becomes zero the loop will break and stop.&lt;/p&gt;

&lt;p&gt;Base case in recursion is that particular value that if we reach we want to stop recalling of the function and start removing and executing every function in the call stack.   &lt;/p&gt;

&lt;p&gt;Not having a base cause will make the function call itself until the call stack becomes full and the program crashes. Also, having a wrong base cause will result to a wrong output.&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%2Fz8zs2lqtl47178g04q46.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%2Fz8zs2lqtl47178g04q46.png" alt="base cause"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3- Determine the general case/s:&lt;/strong&gt;&lt;br&gt;
General cause is the one where the problem expressed as a small version of itself.  Which means, the program will continue iterate the general causes until it reaches the base cause &lt;/p&gt;

&lt;p&gt;In our example above, when i=0 considered as the base cause and we send 5 as a parameter , then we can think of:&lt;br&gt;
i=1, i=2, i=3, i=4, and i=5 as the general causes.&lt;/p&gt;

&lt;p&gt;Now, let's Write a recursive function and trace it using the previous problem:&lt;/p&gt;

&lt;p&gt;We want to write a function that write "hello world" 3 times:&lt;/p&gt;

&lt;p&gt;Let's analyze the problem:&lt;/p&gt;

&lt;p&gt;The function will check the base cause, if the current case doesn't match the base cause then the function will call itself one time  .. So the increasing is a liner (it could be O(n)) (it will be probably safe for the call stack size)&lt;br&gt;
let's try writing our function now by using our while loop example&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1- The base cause -&amp;gt; i =0&lt;/strong&gt;&lt;/p&gt;

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

def display_hello(i):
   if i&amp;gt;0:


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;2- The print command -&amp;gt; print("hello world")&lt;/strong&gt;&lt;/p&gt;

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

def display_hello(i):
   if i&amp;gt;0:
       print("hello world")


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;3- The changes in every iteration -&amp;gt; i-=1&lt;/strong&gt;&lt;br&gt;
to send the new value of the variable to the next function call we have to write it as a parameter &lt;/p&gt;

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

def display_hello(i):
   if i&amp;gt;0:
       print("hello world")
       display_hello(i-1)


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

&lt;/div&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%2Fjwk5w9akkavmuzy0f5j2.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%2Fjwk5w9akkavmuzy0f5j2.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it for this small problem .. Before tracing it let's add one print command after the function call and show the result at the end to make the tracing more interesting&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Assuming that the first invocation was :&lt;/strong&gt;&lt;/p&gt;

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

display_hello(3)


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;And our function is :&lt;/strong&gt;&lt;/p&gt;

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

def display_hello(i):
    if i &amp;gt; 0:
        print("hello world!")
        display_hello(i-1)
        print(f'The i value is {i}')


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Now we will trace it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the first call the function will enter the call stack &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%2Fvj8mo5zuuekz4ayia490.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%2Fvj8mo5zuuekz4ayia490.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then the program will execute the commands :&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%2Fa8dhpz3jf0qgvl9v2gre.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%2Fa8dhpz3jf0qgvl9v2gre.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When the execution reaches line 4 (the function recall) it will stop there (it will pause the execution of the rest commands) and add another function to the stack :&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%2Fx1m5v7och63bnjfzpz57.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%2Fx1m5v7och63bnjfzpz57.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then the program will execute the commands in the second recalled function :&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%2F93hjilm0ibrga4z42jpq.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%2F93hjilm0ibrga4z42jpq.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again , when the execution reaches line 4 (the function recall) it will stop there (it will pause the execution of the rest commands) and add another function to the stack :&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%2Fkg7tpzmyzh69a3liujba.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%2Fkg7tpzmyzh69a3liujba.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For this time too , our base cause has not satisfied yet because i is still bigger than zero, so the execution will continue until the function recall and then adds another function to the stack.&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%2Ft0yt5qcq5omqrlzlnvob.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%2Ft0yt5qcq5omqrlzlnvob.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now finally the parameter is 0 which is the value of i&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%2F0uhuk6ibng237irnxeie.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%2F0uhuk6ibng237irnxeie.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The execution will start until line 2 because the condition says that i should be bigger than zero, but i is equal to zero .. So we reached the base cause and we will not reach the recall "invocation" and the prints&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%2Fg5q8yflb26t7hp8jam6w.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%2Fg5q8yflb26t7hp8jam6w.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because this function finished all the commands that should be executed, it's the time to pop the function out of the stack :&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%2F60xym2sjxt6lucmn060w.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%2F60xym2sjxt6lucmn060w.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we returned to the previous function when the parameter was 1&lt;br&gt;
so now the program will execute the rest of the command/s:&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%2Fjiwaywbpajqg722pzr7r.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%2Fjiwaywbpajqg722pzr7r.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The function now is completed all the commands that should be executed, time to pop the function out of the stack :&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%2Fx1m5v7och63bnjfzpz57.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%2Fx1m5v7och63bnjfzpz57.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we returned to the function that its parameter is two and we will execute the rest of the commands :&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%2Fx58hd9upkojv3jz2wdqk.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%2Fx58hd9upkojv3jz2wdqk.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again, after we done the execution we will remove the function from the stack:&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%2Fvj8mo5zuuekz4ayia490.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%2Fvj8mo5zuuekz4ayia490.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have the first call which 3 was its parameter, and we will execute the rest of the commands:&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%2Fhg29vx3sqw27rno8od1q.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%2Fhg29vx3sqw27rno8od1q.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And here the last function will pop out from the stack and the call stack will become empty and here is the end of our program:&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%2Fsajlfw27cr15rk1p2r9d.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%2Fsajlfw27cr15rk1p2r9d.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's check if our tracing was right by executing the code :&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%2Flrfucdwiwjwclpofp57t.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%2Flrfucdwiwjwclpofp57t.png" alt="recursion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So here we reach the end of today's article &lt;/p&gt;

&lt;h2&gt;
  
  
  Happy recursion 😁
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Happy recursion 😁
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Happy recursion 😁
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Happy recursion 😁
&lt;/h5&gt;

&lt;h6&gt;
  
  
  Happy recursion 😁
&lt;/h6&gt;

&lt;h4&gt;
  
  
  Author Note:
&lt;/h4&gt;

&lt;p&gt;I didn't use Fibonacci as an example because I believe that it is not a good example for teaching recursion and recursion is one of the worst solutions to solve Fibonacci in terms of both time and space.&lt;br&gt;
View this article &lt;a href="https://dev.to/joaomcteixeira/fibonacci-without-recursiveness-in-python-a-better-way-4hm"&gt;Fibonacci without recursiveness in Python - a better way&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  References:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/recursion/" rel="noopener noreferrer"&gt;Recursion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@williambdale/recursion-the-pros-and-cons-76d32d75973a" rel="noopener noreferrer"&gt;Recursion: The Pros and Cons&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.udemy.com/course/recursion-in-python-and-c/" rel="noopener noreferrer"&gt;Learn All About Recursion in Python and C++ - Udemy&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Why Do Developers Choose Mobile First Approach</title>
      <dc:creator>Manar Imad</dc:creator>
      <pubDate>Tue, 24 Aug 2021 17:41:53 +0000</pubDate>
      <link>https://dev.to/manarabdelkarim/why-do-developers-choose-mobile-first-approach-4ckn</link>
      <guid>https://dev.to/manarabdelkarim/why-do-developers-choose-mobile-first-approach-4ckn</guid>
      <description>&lt;h4&gt;
  
  
  Hello Designers ..
&lt;/h4&gt;

&lt;p&gt;Today I was practicing front-end development, so that I thought that it would be a good idea to share some of what I learned about  mobile first approach and bootstrap in a small article so you can benefit too.&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%2Fi.gifer.com%2Fembedded%2Fdownload%2F72Oz.gif" 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%2Fi.gifer.com%2Fembedded%2Fdownload%2F72Oz.gif" alt="cat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's get started ..&lt;/p&gt;

&lt;h3&gt;
  
  
  The Idea of Mobile First Approach:
&lt;/h3&gt;

&lt;p&gt;The mobile first approach looks at designing a website for a mobile device first. So consider how you would satisfy the screen constraints of your mobile device. And then as your screen size expands, you would automatically start an app to your website to the larger and larger screen sizes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Mobile First Approach?
&lt;/h3&gt;

&lt;p&gt;The answer for this question is simple: mobile design is more limited. So if you start from a small screen you can focus in the main and important features that could fit a mobile screen, then perhaps you're willing more and more of the content. So as to take advantage of the increasing screen real estate, which means the same website when seeing on a mobile device might have only parts of the information being revealed to the user. But then when rendered on a full fledge desktop it might show a lot more detail version of the website.&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%2Fweb3canvas.com%2Fwp-content%2Fuploads%2F2013%2F03%2Fmobilefirst.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%2Fweb3canvas.com%2Fwp-content%2Fuploads%2F2013%2F03%2Fmobilefirst.png" alt="Mobile First Approach"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In addition, starting your website with mobile first and focusing more in the mobile design will make your website reachs more people because people tend to play more with their phone and we have 3.80 Billion phone users in the world! and the status of the world market share for mobile:55.77% comparing with Desktop:41.5%.&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%2Fydjd5ecuvkctfbopjy46.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%2Fydjd5ecuvkctfbopjy46.png" alt="mobile first"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fortunately we have many web UI frameworks including Bootstrap to work with the design and apply mobile first approach .. here is a list of some popular UI frameworks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Bootstrap        2. Tailwind CSS&lt;/li&gt;
&lt;li&gt;Semantic-UI    4. Foundation &lt;/li&gt;
&lt;li&gt;Material UI      6. Pure&lt;/li&gt;
&lt;li&gt;Skeleton          8. UIKit&lt;/li&gt;
&lt;li&gt;Milligram        10. Susy&lt;/li&gt;
&lt;li&gt;Materialize      12. Pure CSS&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Bonus Info 🎉: What is Bootstrap?
&lt;/h3&gt;

&lt;p&gt;Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web, because  Bootstrap makes the web development faster and easier, also, it includes HTML and CSS based design templates for typography, forms, buttons, tables, navigation, modals, image carousels and many other, as well as optional , in addition to JavaScript plugins.&lt;br&gt;
so we can create responsive designs with mobile first easily &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%2Fmedia3.giphy.com%2Fmedia%2FSI8DCsjjGnODwlwqW0%2Fgiphy.gif" 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%2Fmedia3.giphy.com%2Fmedia%2FSI8DCsjjGnODwlwqW0%2Fgiphy.gif" alt="Mobile First Approach"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Resources:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.invisionapp.com/inside-design/mobile-first-design/" rel="noopener noreferrer"&gt;What does mobile-first design mean for digital designers?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://getbootstrap.com/docs/3.3/" rel="noopener noreferrer"&gt;getbootstrap&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Front-End Web UI Frameworks and Tools: Bootstrap 4 Course by The Hong Kong University of Science and Technology - Coursera&lt;/li&gt;
&lt;li&gt;&lt;a href="https://bradfrost.com/blog/post/mobile-first-responsive-web-design/" rel="noopener noreferrer"&gt;mobile-first responsive web design&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a&gt;NUMBER OF MOBILE PHONE &amp;amp; SMARTPHONE USERS&lt;/a&gt;](Source: &lt;a href="https://www.bankmycell.com/blog/how-many-phones-are-in-the-world" rel="noopener noreferrer"&gt;https://www.bankmycell.com/blog/how-many-phones-are-in-the-world&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.statista.com/statistics/330695/number-of-smartphone-users-worldwide/" rel="noopener noreferrer"&gt;Number of smartphone users from 2016 to 2021&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>design</category>
      <category>css</category>
      <category>javascript</category>
      <category>html</category>
    </item>
    <item>
      <title>From Regular Functions to Arrow Functions</title>
      <dc:creator>Manar Imad</dc:creator>
      <pubDate>Tue, 24 Aug 2021 09:03:32 +0000</pubDate>
      <link>https://dev.to/manarabdelkarim/types-of-javascript-functions-a-bridge-from-regular-function-to-arrow-function-2bjc</link>
      <guid>https://dev.to/manarabdelkarim/types-of-javascript-functions-a-bridge-from-regular-function-to-arrow-function-2bjc</guid>
      <description>&lt;h4&gt;
  
  
  Hello World
&lt;/h4&gt;

&lt;p&gt;One of the struggles for new JavaSript learners is understanding the different ways of writing a function including arrow functions , so for today, I'm planning to explain all types of functions in JavaScript in detail&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%2Fwittykittydigital.com%2Fwp-content%2Fuploads%2F2019%2F03%2Fgiphy-6.gif" 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%2Fwittykittydigital.com%2Fwp-content%2Fuploads%2F2019%2F03%2Fgiphy-6.gif" alt="javascript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's get started...&lt;/p&gt;

&lt;p&gt;To explain the ways to write JavaSript functions let's divide them into two main types : Function Declaration and Function Expression.&lt;/p&gt;

&lt;h3&gt;
  
  
  1- Function Declaration
&lt;/h3&gt;

&lt;p&gt;This is the type that we know and is similar to other programming  languages' functions. Function declaration has two types :&lt;br&gt;
         &lt;strong&gt;a. Traditional Function:&lt;/strong&gt;&lt;br&gt;
    the traditional(Regular) function has this pattern :&lt;br&gt;
     &lt;code&gt;function&lt;/code&gt; functionName&lt;code&gt;(&lt;/code&gt;optionalParameter/s&lt;code&gt;)&lt;/code&gt;&lt;code&gt;{&lt;/code&gt;&lt;code&gt;}&lt;/code&gt;&lt;br&gt;
     for example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function print(){
console.log("hello world");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function add(number1,number2){
return number1 + number2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;     &lt;strong&gt;b. Shorthand Function:&lt;/strong&gt;&lt;br&gt;
The shorthand function is a regular function but for a class .. the differnce in the pattern is that we don't write the function keyword so it will be :&lt;br&gt;
  functionName&lt;code&gt;(&lt;/code&gt;optionalParameter/s&lt;code&gt;)&lt;/code&gt;&lt;code&gt;{&lt;/code&gt;&lt;code&gt;}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's see it in an example with a shorthand function we will call it login:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class user:
 constructor(name,email){
   this.userName = name;
   this.userEmail = email;
}
 login(){
 console.log(`Hello ${this.name} you have login successfully`);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2- Function Expression
&lt;/h3&gt;

&lt;p&gt;Function expression is just like a function declaration but we assign it to an object(variable)&lt;br&gt;
let's discuss its 4 types:&lt;/p&gt;

&lt;p&gt;     &lt;strong&gt;a. Regular FEs (Regular Function Expression)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's talk a bit about what that means:&lt;br&gt;
If we have this function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function print(){
console.log("hello world");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then when we want to invoke it we type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But in regular function expression we will save the function in a variable "assign the function to a variable" such as :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myPrint = function print(){
console.log("hello world");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now what will happen if we try to invoke print?&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%2F3ewut7md9kqvlu2rkdpe.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%2F3ewut7md9kqvlu2rkdpe.png" alt="function expression"&gt;&lt;/a&gt;&lt;br&gt;
That will cause an error telling us that print is not defined&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%2Fu3g3e5aduqpfe8jqkx0g.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%2Fu3g3e5aduqpfe8jqkx0g.png" alt="reference error"&gt;&lt;/a&gt;&lt;br&gt;
So how can we reach the function ?&lt;br&gt;
We can use the variable name (myPrint) that we assigned the function to.&lt;br&gt;
In our example we assign a function directly to the variable which means our variable is from type function, so let's try to invoke it&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%2Fkksjk4pd7x7pzxt9qhxg.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%2Fkksjk4pd7x7pzxt9qhxg.png" alt="function expression"&gt;&lt;/a&gt;&lt;br&gt;
Hooray it works 🥳!&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%2Fgmmyvfp954gqu80end7l.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%2Fgmmyvfp954gqu80end7l.png" alt="result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;     &lt;strong&gt;b. IIFE (Immediately Invoked Function Expression)&lt;/strong&gt;&lt;br&gt;
From its name IIFE will be invoked immediately, you can think of it as if the function invoked itself and will be executed.&lt;br&gt;
The syntax is simple .. we have a function :&lt;code&gt;function&lt;/code&gt; functionName&lt;code&gt;(){&lt;/code&gt;actions&lt;code&gt;}&lt;/code&gt; you will contain the function between parentheses or "round brackets" &lt;code&gt;(function&lt;/code&gt; functionName&lt;code&gt;(){&lt;/code&gt;actions&lt;code&gt;})&lt;/code&gt; and end the function with another parentheses in this way :&lt;br&gt;
&lt;code&gt;(function&lt;/code&gt; functionName&lt;code&gt;(){&lt;/code&gt;actions&lt;code&gt;})();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So the bet with you now is: this function will be invoked immediately when the file execution starts&lt;/p&gt;

&lt;p&gt;Let's write an example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function print(){
console.log("hello world");
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's start :&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%2Fp14sff7hgefqoddmbn3h.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%2Fp14sff7hgefqoddmbn3h.png" alt="IIFE"&gt;&lt;/a&gt;&lt;br&gt;
And let's execute it:&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%2Fech5xlcgra5by9mkgujf.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%2Fech5xlcgra5by9mkgujf.png" alt="Immediately Invoked Function Expression"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But wait .. We said we want to assign the function to a variable! Then let's do it :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myPrint = (function print(){
console.log("hello world");
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What will happen now ?Let's use it : &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%2F7p9pg9vj5nfbe3h6yjai.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%2F7p9pg9vj5nfbe3h6yjai.png" alt="IIFE"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why didn't it work ???&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%2Fi.gifer.com%2F9LrA.gif" 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%2Fi.gifer.com%2F9LrA.gif" alt="javascript"&gt;&lt;/a&gt;&lt;br&gt;
The simple answer is because the IIFE invoked itself .. which means when we assign the function to the variable , the function immediately invoked and what has been saved "assigned to" our myPrint variable is the result.. so first we have to remove the invoke parentheses and try again :&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%2Fd5v61g387p8sjbm8ppcw.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%2Fd5v61g387p8sjbm8ppcw.png" alt="IIFE"&gt;&lt;/a&gt;&lt;br&gt;
So now it works and the type of the variable is undefined&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%2Ffb33dggs2qwdb5pbcgsl.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%2Ffb33dggs2qwdb5pbcgsl.png" alt="IIFE"&gt;&lt;/a&gt;&lt;br&gt;
 Let's change the function to give the variable myPrint a string type:&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%2F31hbh3qoimp52ae3xeyb.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%2F31hbh3qoimp52ae3xeyb.png" alt="IIFE"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;     &lt;strong&gt;c. Anonymous Function&lt;/strong&gt;&lt;br&gt;
It is an anonymous function and that's it ! Just a function without a name 😁 .. Here is the syntax:&lt;br&gt;
[var|let|const] variableName = function(){actions}&lt;/p&gt;

&lt;p&gt;Let's try it with this example :&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%2Fnu6kfn3a5qz1hlr80ros.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%2Fnu6kfn3a5qz1hlr80ros.png" alt="Anonymous Function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that the function here should be assigned to a variable otherwise it will cause an error  &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%2F2bfhqog430pbsbdryu3e.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%2F2bfhqog430pbsbdryu3e.png" alt="Anonymous Function"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Extra example : let's try to combine both Anonymous Function and IIFE 😎:
&lt;/h4&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%2Fi5fnfu8lfwfto58pcw8e.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%2Fi5fnfu8lfwfto58pcw8e.png" alt="Anonymous Function and IIFE"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;     &lt;strong&gt;d. Arrow Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And we are here finally .. let's write the Arrow Function syntax in steps :&lt;/p&gt;

&lt;p&gt;First :let's steal the Anonymous function syntax from above&lt;br&gt;
&lt;code&gt;[var|let|const] variableName = function(){actions}&lt;/code&gt;&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%2Fimg.buzzfeed.com%2Fbuzzfeed-static%2Fstatic%2F2020-06%2F17%2F14%2Fasset%2F3f92c548489a%2Fsub-buzz-2045-1592403653-24.png%3Fdownsize%3D700%253A%252A%26output-quality%3Dauto%26output-format%3Dauto" 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%2Fimg.buzzfeed.com%2Fbuzzfeed-static%2Fstatic%2F2020-06%2F17%2F14%2Fasset%2F3f92c548489a%2Fsub-buzz-2045-1592403653-24.png%3Fdownsize%3D700%253A%252A%26output-quality%3Dauto%26output-format%3Dauto" alt="javascript"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next :delete the function keyword:&lt;br&gt;
&lt;code&gt;[var|let|const] variableName = (){actions}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finally: add (=&amp;gt;) sign after the parentheses :&lt;br&gt;
&lt;code&gt;[var|let|const] variableName = ()=&amp;gt;{actions}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And done .. this is our syntax so let's try it now :&lt;br&gt;
Our example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let myPrint = ()=&amp;gt; {
    return "hello world";
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's go!&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%2F1ze36eb3yrj0a6ge249d.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%2F1ze36eb3yrj0a6ge249d.png" alt="Arrow Function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we reached the end .. Happy invoking 😊&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>functional</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Deploy Django Project on Heroku Using Heroku CLI</title>
      <dc:creator>Manar Imad</dc:creator>
      <pubDate>Fri, 20 Aug 2021 22:41:30 +0000</pubDate>
      <link>https://dev.to/manarabdelkarim/deploy-django-project-on-heroku-using-heroku-cli-460h</link>
      <guid>https://dev.to/manarabdelkarim/deploy-django-project-on-heroku-using-heroku-cli-460h</guid>
      <description>&lt;h3&gt;
  
  
  Hello World..
&lt;/h3&gt;

&lt;p&gt;Every time I deploy my project on Heroku I make mistakes and forgot some steps. But not anymore, because today I decided to record them here for you and me.&lt;br&gt;
I will include everything I know that could cause an error or a fail in deployment.&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%2Fc.tenor.com%2FSmYr1-9rBTsAAAAC%2Fyoure-gonna-change-the-world-change-the-world.gif" 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%2Fc.tenor.com%2FSmYr1-9rBTsAAAAC%2Fyoure-gonna-change-the-world-change-the-world.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;so let's get started &lt;/p&gt;
&lt;h4&gt;
  
  
  prepare and check your files:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;STATICFILES_DIRS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;not having STATICFILES_DIRS in the settings of your project will cause a fail in deployment so make sure to add this line &lt;/p&gt;

&lt;p&gt;&lt;code&gt;STATICFILES_DIRS =[&lt;/code&gt;&lt;br&gt;
&lt;code&gt;os.path.join(BASE_DIR, 'static')&lt;/code&gt;&lt;br&gt;
&lt;code&gt;]&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Procfile&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;you have to add Procfile on the root directory and at the same level of your project and app/s to explicitly declare what command should be executed to start your app, add this line in the file &lt;/p&gt;

&lt;p&gt;&lt;code&gt;web: gunicorn myproject.wsgi&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and make sure to replace "myproject" with your project name and to install gunicorn , you can install it in many ways such as pip install gunicorn or poetry add gunicorn "if you are using poetry"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create requirements.txt file &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Heroku knows what libraries to install in its server by reading the requirements file so make sure to create one.&lt;br&gt;
here is a one-line you can type in the command-line to copy the libraries and their version to the file if you are using poetry&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry export -f requirements.txt --output requirements.txt --without-hashes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or this command if you are using pip :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; pip freeze &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;check the allowed hosts &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;if your allowed host list in your .env file and/or the settings are for only the localhost then that will cause a problem. So you can add the URL of your Heroku app to the list such as &lt;/p&gt;

&lt;p&gt;&lt;code&gt;ALLOWED_HOSTS = ['mydjangoapp.herokuapp.com']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;you can allow all hosts by using [*] then you are fine, and you can change it later after creating your Heroku app and have an actual URL link &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;make sure that everything is working just fine in your local &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;now we are ready to start, be sure you are standing in the right directory in your command line .. so here is the steps "commands" according to Heroku's official website:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;heroku login&lt;/li&gt;
&lt;li&gt;heroku create mydjangoapp --buildpack heroku/python&lt;/li&gt;
&lt;li&gt; git init&lt;/li&gt;
&lt;li&gt;heroku git:remote -a mydjangoapp&lt;/li&gt;
&lt;li&gt;git add .&lt;/li&gt;
&lt;li&gt;git commit -m "My first commit"&lt;/li&gt;
&lt;li&gt;git push heroku main&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;if your deployment was a success then congratulations on everything you deployed your app successfully.&lt;/p&gt;

&lt;p&gt;if you have an error then these are some suggestions :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; If you get an error message with collectstatic write this command :
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku config:set DISABLE_COLLECTSTATIC=1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;install whitenoise and psycopg2 + add whitenoise on the top of the middlewares list in the project settings &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;'whitenoise.middleware.WhiteNoiseMiddleware'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;now let's talk about having a database and you want to migrate it &lt;br&gt;
1- we need to add PostgreSQL to our Heruko project through CLi&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku addons:create heroku-postgresql:hobby-dev -a mydjangoapp

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

&lt;/div&gt;



&lt;p&gt;don't forget to see the PostgreSQL info and add them to the config vars &lt;br&gt;
you can use this command :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku config:get DATABASE_URL -a &amp;lt;your_heroku_app_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the order of the info that you will get from the URL is like this:&lt;br&gt;
&lt;code&gt;postgres://&amp;lt;username&amp;gt;:&amp;lt;password&amp;gt;@&amp;lt;hostname/server&amp;gt;/&amp;lt;databasename&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;next, here is the command to copy the .env vars to Heroku config vars :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku config:set $(cat .env | sed '/^$/d; /#[[:print:]]*$/d')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2- let's make migrations&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku run python manage.py makemigrations
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3- migrate to your Heroku database&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku run python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4- you can also create a super user&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku run python manage.py createsuperuser
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and here we reach the end .. have a good deploying 😊&lt;/p&gt;

&lt;h3&gt;
  
  
  References:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://devcenter.heroku.com/articles/git" rel="noopener noreferrer"&gt;https://devcenter.heroku.com/articles/git&lt;/a&gt; &lt;/p&gt;

</description>
      <category>django</category>
      <category>heroku</category>
      <category>database</category>
      <category>postgres</category>
    </item>
  </channel>
</rss>
