<?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: Sharon-nyabuto</title>
    <description>The latest articles on DEV Community by Sharon-nyabuto (@sharonnyabuto).</description>
    <link>https://dev.to/sharonnyabuto</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3849348%2F063abdb2-e306-4721-a3a7-73ed0f20c060.png</url>
      <title>DEV Community: Sharon-nyabuto</title>
      <link>https://dev.to/sharonnyabuto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sharonnyabuto"/>
    <language>en</language>
    <item>
      <title>Writing Reusable code in Python</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Mon, 03 Aug 2026 21:41:41 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/writing-reusable-code-in-python-52ed</link>
      <guid>https://dev.to/sharonnyabuto/writing-reusable-code-in-python-52ed</guid>
      <description>&lt;h2&gt;
  
  
  Understanding Functions
&lt;/h2&gt;

&lt;p&gt;Previous articles in this series have guided us on how to make decisions, repeat tasks, and solve increasingly complex problems using Python. &lt;br&gt;
As our programs grow, another challenge begins to emerge; &lt;strong&gt;repeating codes we need to use more than once.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Say you've written a program that checks a dataset for missing values. Later on, you need to perform the same check on a different dataset. Do you copy and paste the same code, or is there a better way?&lt;br&gt;
Fortunately, Python allows us to group reusable code into a function, making it easy to use the same logic whenever we need it, entirely avoiding the repetition.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is a Function?
&lt;/h3&gt;

&lt;p&gt;A function is a &lt;strong&gt;reusable block of code that performs a specific task.&lt;/strong&gt; &lt;br&gt;
Functions allow us to package reusable code into a named block so we can call it whenever we need it, instead of writing the same logic repeatedly.&lt;br&gt;
Functions don't just save typing, they make code reusable. &lt;/p&gt;
&lt;h4&gt;
  
  
  Defining your first function
&lt;/h4&gt;

&lt;p&gt;Consider a task on analyzing several household survey datasets collected over different years. Before analyzing each one, you want Python to display the same message indicating that the analysis has started.&lt;/p&gt;

&lt;p&gt;Without functions, you would need to write the same line of code every time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Analyzing household survey dataset...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Analyzing household survey dataset...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Analyzing household survey dataset...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this works, repeating the same code easily becomes difficult to maintain. If you later decide to change the message, you would have to edit it everywhere it appears. &lt;/p&gt;

&lt;p&gt;Instead, Python allows us to define a function once and call it whenever we need it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;analyse_dataset&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Analysing household survey dataset...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break it down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;def&lt;/code&gt; Is the keyword that tells Python that you are defining a new function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;analyse_dataset&lt;/code&gt; is the function's name. It should be a descriptive name, i.e. it should explicitly describe what the function does.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;()&lt;/code&gt; is where any information the function needs (known as &lt;strong&gt;parameters&lt;/strong&gt;) will go. For now, the parentheses are empty because our function doesn't require any input.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:&lt;/code&gt; marks the beginning of the function body.&lt;/li&gt;
&lt;li&gt;The indented code underneath is the set of instructions Python executes each time the function is called.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice that nothing happens yet. This is because defining a function only tells Python that it exists. The code inside the function will not run until we explicitly &lt;em&gt;call&lt;/em&gt; it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Calling a function
&lt;/h4&gt;

&lt;p&gt;Once a function has been defined, you can execute it simply by writing its name followed by parentheses. This is what is known as &lt;strong&gt;calling&lt;/strong&gt; the function&lt;/p&gt;

&lt;p&gt;In the case of our example above, we simply write the name of the function whenever we need it, as many times as we would need it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;analyse_dataset&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;analyse_dataset&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Analysing household survey dataset...
Analysing household survey dataset...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's see how we can have a function performing a specific task.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Scenario: You've received a household survey dataset. Before analyzing it, you want to know how many records are complete.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# The dataset
&lt;/span&gt;&lt;span class="n"&gt;household_survey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH001&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH002&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH003&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH004&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH005&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;  

&lt;span class="c1"&gt;# The function
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;count_complete_records&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;complete&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;household_survey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;complete&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Complete records: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;complete&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Calling the function
&lt;/span&gt;&lt;span class="nf"&gt;count_complete_records&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What happens Step by Step:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python defines the function &lt;code&gt;count_complete_records()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When the function is called, it &lt;code&gt;loops&lt;/code&gt; through the dataset.&lt;/li&gt;
&lt;li&gt;Each row is checked to see if it contains any missing values &lt;code&gt;(None)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the row is &lt;strong&gt;complete&lt;/strong&gt;, the counter &lt;strong&gt;increases&lt;/strong&gt; by one.&lt;/li&gt;
&lt;li&gt;After checking every record, Python prints the &lt;strong&gt;total number of complete records&lt;/strong&gt;, i.e.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Complete records: 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function works well, but it has one limitation. It is written specifically for the &lt;code&gt;household_survey&lt;/code&gt; dataset because the dataset is hardcoded inside the function. &lt;br&gt;
If we needed to count complete records in a different survey, we would either have to edit the function or create another one. When working with multiple datasets, this becomes a little inefficient.&lt;br&gt;
Fortunately, it is possible to write one function that works with different datasets.&lt;/p&gt;
&lt;h4&gt;
  
  
  Parameters
&lt;/h4&gt;

&lt;p&gt;Rather than storing the dataset inside the function (as we did in the example above), Python allows us to &lt;em&gt;pass&lt;/em&gt; the dataset into the &lt;code&gt;function&lt;/code&gt; whenever it is called. We do this using &lt;strong&gt;parameters&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;parameter&lt;/code&gt; is a variable listed &lt;em&gt;inside&lt;/em&gt; a function's parentheses. It acts as a placeholder that receives the information passed to the function when it is called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;count_complete_records&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;complete&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;complete&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Complete records: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;complete&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the difference between this function and the previous one. This time, the function includes a parameter, &lt;code&gt;dataset&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
In this case, &lt;code&gt;dataset&lt;/code&gt; acts as a placeholder for the dataset we want the function to work with. Because the dataset is no longer hardcoded into the function, the same code can be used with different datasets.&lt;/p&gt;

&lt;p&gt;But where does the &lt;strong&gt;parameter&lt;/strong&gt; get its value?&lt;/p&gt;

&lt;p&gt;It receives its value when the function is called. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;count_complete_records&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;household_survey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice another difference here. This time, the parentheses contain the name of the dataset we want to analyse. This is known as an &lt;strong&gt;argument&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;When the function is called, Python takes the value of &lt;code&gt;household_survey&lt;/code&gt; and passes it to the parameter &lt;code&gt;dataset&lt;/code&gt;. &lt;br&gt;
From that point onwards, every time the function refers to &lt;code&gt;dataset&lt;/code&gt;, it is working with &lt;code&gt;household_survey&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

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

dataset   &amp;lt;─────── household_survey
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In simple terms, the &lt;strong&gt;parameter&lt;/strong&gt; is the placeholder inside the function, while the &lt;strong&gt;argument&lt;/strong&gt; is the actual value you provide when calling the function.&lt;/p&gt;

&lt;p&gt;Now if we needed to count the number of complete records in the economic impact survey below;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;economic_impact_survey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;B001&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Farmer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;B002&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;150000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Trader&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;B003&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Fisher&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;B004&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Farmer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;B005&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Farmer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; 
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;B006&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we would not need to modify or define the function all over again, but just pass the &lt;code&gt;argument&lt;/code&gt;(name of the dataset) when calling the function;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;count_complete_records&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;economic_impact_survey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Complete records: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the function uses a parameter, it can work with any dataset that follows the expected structure. &lt;/p&gt;

&lt;p&gt;An important point to remember is that simply existing does not make &lt;code&gt;functions&lt;/code&gt; reusable. It is the presence of parameters that allows them to work with different inputs.&lt;/p&gt;

&lt;h4&gt;
  
  
  Returning Values
&lt;/h4&gt;

&lt;p&gt;In the examples above, we have been displaying the results of our functions immediately using &lt;code&gt;print&lt;/code&gt;. This is helpful for when we want to see the result.&lt;br&gt;
In some situations, we do not want to see the result, but want to use the result elsewhere in the program. &lt;/p&gt;

&lt;p&gt;Suppose we wanted to compare the number of complete records in two different datasets. Printing the result alone wouldn't allow us to perform that comparison. We would need a way for the function to send the result back to the rest of the program. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;return&lt;/code&gt; statement sends a value back to the place where the function was called. &lt;br&gt;
Think of &lt;code&gt;print()&lt;/code&gt; as showing the result to the &lt;strong&gt;user&lt;/strong&gt;, while &lt;code&gt;return&lt;/code&gt; gives the result back to the &lt;strong&gt;program&lt;/strong&gt; so it can continue working with it.&lt;/p&gt;

&lt;p&gt;Let's modify our function;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;count_complete_records&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;complete&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;complete&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;complete&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that we've replaced:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Complete records: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;complete&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;complete&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, instead of displaying the result immediately, the function hands the value back to the program. These values can then be stored in variables;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;household_complete&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;count_complete_records&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;household_survey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;economic_complete&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;count_complete_records&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;economic_impact_survey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variables now contain the number of complete records for each dataset, and since the values have been stored, they can be printed, compared or used in other calculations.&lt;/p&gt;

&lt;p&gt;For example, we can compare the number of complete records in the two datasets as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;household_complete&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;economic_complete&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The household survey has more complete records.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The economic impact survey has more complete records.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;The economic impact survey has more complete records.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the &lt;code&gt;return&lt;/code&gt; statement makes functions far more flexible and reusable, because they can be stored in a variable and be used later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this article, we have seen how &lt;code&gt;functions&lt;/code&gt; enable us to write reusable code, by defining them once and calling  them whenever we need them. By using parameters, the same function can even work with different datasets, making our programs more flexible and easier to maintain.&lt;/p&gt;

&lt;p&gt;This brings us to the end of our introduction to Python series. Throughout this series, we have built the skills needed to write clear, reusable Python programs, from making decisions and repeating tasks to organizing code with functions. &lt;/p&gt;

&lt;p&gt;Continue practising beyond the examples we've explored, experiment with your own ideas, and challenge yourself with new problems. The confidence you build comes not from reading code, but from writing it.&lt;/p&gt;

&lt;p&gt;In future articles, we will shift our focus from learning Python itself to applying these skills to data analysis, showing how they come together to solve practical problems.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>beginners</category>
      <category>python</category>
    </item>
    <item>
      <title>Repeating Tasks Within Tasks</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Thu, 30 Jul 2026 06:28:44 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/repeating-tasks-within-tasks-5ghm</link>
      <guid>https://dev.to/sharonnyabuto/repeating-tasks-within-tasks-5ghm</guid>
      <description>&lt;h2&gt;
  
  
  Understanding nested loops in Python
&lt;/h2&gt;

&lt;p&gt;Think about an analog clock such as the one below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsw62wyvp03pknniy5mri.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsw62wyvp03pknniy5mri.jpg" alt="Analog clock Image" width="799" height="848"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Figure 1. An analog clock.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Image: Wikimedia Commons.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For every complete rotation of the second hand, the minute hand advances by one minute. Same goes for the hour hand; for every complete rotation of the minute hand, the hour hand advances by one hour. &lt;/p&gt;

&lt;p&gt;Programming sometimes requires this same kind of repetition. &lt;em&gt;But how can we tell Python to repeat one task inside another repeated task?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The answer lies in &lt;code&gt;nested&lt;/code&gt;loops. By definition, a &lt;code&gt;nested&lt;/code&gt; loop is simply a loop placed inside another loop. &lt;br&gt;
For every iteration of the outer loop, the inner loop runs from beginning to end before the outer loop moves to its next iteration.&lt;/p&gt;

&lt;p&gt;Much like the hour and minute hands on an analog clock, the outer loop advances &lt;strong&gt;one&lt;/strong&gt; step only after the inner loop has completed &lt;strong&gt;all&lt;/strong&gt; of its iterations.&lt;/p&gt;

&lt;p&gt;In data analysis, nested loops are particularly useful when you need to work through two levels of data, such as rows and columns in a dataset, households within counties, or regions and their characteristics.&lt;/p&gt;

&lt;p&gt;A nested loop can be made from any combination of &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loops. However, you'll most commonly use a &lt;code&gt;for&lt;/code&gt; loop inside another &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;
&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;outer_variable&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;outer_sequence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Outer loop
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;inner_variable&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;inner_sequence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Code to repeat
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Let us revisit the clock analogy in Python;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;hour&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hour &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;hour&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;minute&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;61&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; Minute &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;minute&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what happens, step by step;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python starts with the &lt;strong&gt;outer loop&lt;/strong&gt; and sets &lt;code&gt;hour&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Before moving to the next hour, Python enters the inner loop.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;inner loop&lt;/strong&gt; starts with &lt;code&gt;minute = 1&lt;/code&gt; and continues printing every minute until it reaches &lt;code&gt;60&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Once all &lt;code&gt;60 minutes&lt;/code&gt; have been printed, the inner loop finishes.&lt;/li&gt;
&lt;li&gt;Python returns to the outer loop and updates &lt;code&gt;hour&lt;/code&gt; to &lt;code&gt;2&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The inner loop starts again, printing minutes 1 through 60 for the second hour.&lt;/li&gt;
&lt;li&gt;The same process repeats for &lt;code&gt;hour = 3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;After the final hour has completed all &lt;code&gt;60 minutes&lt;/code&gt;, both loops end.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Result;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Hour&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
 &lt;span class="bp"&gt;...&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;58&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;
&lt;span class="n"&gt;Hour&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
 &lt;span class="p"&gt;..&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;58&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;
&lt;span class="n"&gt;Hour&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
 &lt;span class="bp"&gt;...&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;58&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;
 &lt;span class="n"&gt;Minute&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Notice that the minutes restart from 1 every time the hour changes. This is because the inner loop begins again from the start for every iteration of the outer loop, just as the minute hand completes a full revolution before the hour hand moves to the next hour.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Nested Loops Use cases
&lt;/h3&gt;

&lt;h3&gt;
  
  
  1. To find missing values (data cleaning)
&lt;/h3&gt;

&lt;p&gt;Imagine you're checking a dataset before analysis. You need to examine every row, and within each row, inspect every column for missing or invalid values. This is a situation for &lt;code&gt;nested&lt;/code&gt; loops because you're repeating one task (checking columns) inside another (moving through rows).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;dataset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH001&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Nairobi&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH002&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Kisumu&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH003&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Mombasa&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH004&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Nakuru&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;household_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;household_id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; has a missing value.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;HH002 has a missing value.
HH003 has a missing value.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. To identify duplicate records (Data Quality)
&lt;/h3&gt;

&lt;p&gt;Imagine you're cleaning survey data before analysis. Some respondents may have been entered twice. You can use nested &lt;code&gt;for&lt;/code&gt; loop to compare every respondent against every other respondent to identify duplicate IDs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;respondent_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R001&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R002&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R003&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R002&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R004&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;respondent_ids&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;respondent_ids&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;respondent_ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;respondent_ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Duplicate respondent found: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;respondent_ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Duplicate respondent found: R001
Duplicate respondent found: R002
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;&lt;strong&gt;i&lt;/strong&gt; and &lt;strong&gt;j&lt;/strong&gt; are index variables. The outer loop &lt;strong&gt;&lt;em&gt;i&lt;/em&gt;&lt;/strong&gt; selects one respondent at a time, while the inner loop &lt;strong&gt;&lt;em&gt;j&lt;/em&gt;&lt;/strong&gt; compares that respondent with every respondent that follows in the list. This ensures every unique pair is checked once, making it possible to detect duplicate IDs.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. To find a specific value in your table (Data exploration)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;dataset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;33&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;56&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;33&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;])):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;column&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: Found at Row &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, Column &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;column&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;33: Found at Row 2, Column 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although nested &lt;code&gt;for&lt;/code&gt; loops are the most common when working with collections of data, you can also combine &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loops. This is useful when you need to process a collection while repeating a task until a condition is met.&lt;/p&gt;

&lt;p&gt;Here are some examples that incorporate &lt;strong&gt;both the for and while&lt;/strong&gt; loops.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Data Validation (for + while)
&lt;/h4&gt;

&lt;p&gt;You're collecting survey data. Every respondent must provide a valid age before you move on to the next respondent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;respondents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH001&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH002&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH003&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;respondent&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;respondents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Recording data for &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;respondent&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Enter age: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invalid age. Please enter an age between 1 and 120.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Enter age: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Age recorded successfully.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The &lt;code&gt;for&lt;/code&gt; loop processes each respondent.&lt;br&gt;
The &lt;code&gt;while&lt;/code&gt; loop keeps asking until a valid value is entered.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
Recording data for HH001
0: Invalid age. Please enter an age between 1 and 120.
-2: Invalid age. Please enter an age between 1 and 120.
21: Age recorded successfully.

Recording data for HH002
68: Age recorded successfully.

Recording data for HH003
178: Invalid age. Please enter an age between 1 and 120.
89: Age recorded successfully.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Limitations of Nested Loops
&lt;/h3&gt;

&lt;p&gt;Like every other tool, &lt;code&gt;nested&lt;/code&gt; loops have situations where they're less efficient.&lt;/p&gt;

&lt;p&gt;As the amount of data grows, the inner loop must run for every iteration of the outer loop, increasing the total number of operations and potentially slowing down your program.&lt;/p&gt;

&lt;p&gt;Imagine comparing every household in a dataset of 10,000 records with every other household to identify duplicates. Although this approach works, it performs a very large number of comparisons. As datasets grow, there are more efficient techniques that avoid checking every possible pair.&lt;/p&gt;

&lt;p&gt;Nested loops are an excellent way to solve problems involving multiple levels of repetition. As we continue learning Python, we'll discover more efficient approaches for handling large datasets, particularly when working with libraries such as pandas.(We will cover these in a later series)&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this article, we've seen that &lt;code&gt;nested&lt;/code&gt; loops are a practical way to solve real-world problems. Whether you're validating survey responses, checking for missing values, or comparing records, nested loops help you work through data that has multiple levels or repeated relationships.&lt;/p&gt;

&lt;p&gt;As usual, keep experimenting by creating your own examples and thinking about tasks that involve one repeated process inside another. The more you practise, the more natural it will become to recognise when a nested loop is the right solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's next?
&lt;/h3&gt;

&lt;p&gt;"We've learned how to solve complex problems with loops, but what if we want to reuse that logic without writing it again? &lt;/p&gt;

&lt;p&gt;In the next article, we'll explore &lt;code&gt;functions&lt;/code&gt; and learn how to package reusable code into named blocks, making our programs less repetitive and easier to maintain. &lt;/p&gt;

&lt;p&gt;Until then, happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Controlling Loops with break and continue</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Thu, 23 Jul 2026 08:45:15 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/controlling-loops-with-break-and-continue-4ci0</link>
      <guid>https://dev.to/sharonnyabuto/controlling-loops-with-break-and-continue-4ci0</guid>
      <description>&lt;h2&gt;
  
  
  Knowing when to stop or skip a loop in Python
&lt;/h2&gt;

&lt;p&gt;In our last article we saw how we can efficiently repeat processes with &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loops.&lt;br&gt;
But what if we do not want our loop going all the way? How can we tell Python to stop a loop early or skip a single iteration, without ending the entire program?&lt;/p&gt;
&lt;h3&gt;
  
  
  The &lt;code&gt;break&lt;/code&gt; statement
&lt;/h3&gt;

&lt;p&gt;Loops are important, but sometimes we do not want every iteration to execute. This is where the &lt;strong&gt;break&lt;/strong&gt; statement is helpful.&lt;br&gt;
The &lt;code&gt;break&lt;/code&gt; statement immediately terminates a loop. Once Python encounters &lt;code&gt;break&lt;/code&gt; it exits the loop and goes on to execute the rest of the program.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;respondents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R001&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R002&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R003&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R004&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R005&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R006&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R007&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R008&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;respondent&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;respondents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;respondent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R005&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Respondent found!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Checking respondents : &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;respondent&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what happens step by step: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Python starts the &lt;code&gt;for&lt;/code&gt; loop and assigns the first value, &lt;code&gt;"R001"&lt;/code&gt;, to the variable &lt;code&gt;respondent&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It checks whether &lt;code&gt;respondent == "R005"&lt;/code&gt;, and since &lt;code&gt;"R001"&lt;/code&gt; is not equal to &lt;code&gt;"R005"&lt;/code&gt;, the condition is &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Python skips the break statement and executes &lt;code&gt;print("Checking", respondent)&lt;/code&gt;, displaying:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   Checking respondents: R001
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The loop repeats for &lt;code&gt;"R002"&lt;/code&gt; all through &lt;code&gt;"R004"&lt;/code&gt;, and since none of those are still ==R005, Checking respondent is still printed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;On the next iteration, respondent becomes &lt;code&gt;"R005"&lt;/code&gt;. This time, the condition &lt;code&gt;respondent == "R005"&lt;/code&gt;evaluates to &lt;code&gt;True&lt;/code&gt;. Python prints:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Respondent found!
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The break statement is executed, causing Python to immediately exit the loop. The remaining items, "R006" through "R008", are never processed. The output therefore becomes:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Checking respondents : R001
    Checking respondents : R002
    Checking respondents : R003
    Checking respondents : R004
    Respondent found!
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is important to note that the &lt;code&gt;break&lt;/code&gt; statement does not stop the entire program, it only stops the current loop. Python proceeds to execute the next tines of code after the loop.&lt;/p&gt;

&lt;p&gt;Here are a few more examples;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example 1:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Q1. A school bus can carry 30 students. Students keep boarding one by one. Stop boarding as soon as the bus is full. Print how many are on board after each student boards.
&lt;/span&gt;
&lt;span class="n"&gt;capacity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;capacity&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Students aboard: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; | Seats left: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt; 

&lt;span class="c1"&gt;# The break statement provides the loop with an exit condition by terminating it as soon as capacity reaches 0. Otherwise it would keep running infinitely beyond 0.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Students aboard: 1 | Seats left: 29
Students aboard: 2 | Seats left: 28
Students aboard: 3 | Seats left: 27
....
Students aboard: 28 | Seats left: 2
Students aboard: 29 | Seats left: 1
Students aboard: 30 | Seats left: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Example 2:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Q2:  A car park has 20 spaces. Cars keep arriving one by one. Ask if a car wants to park. If yes, reduce spaces by 1. Stop when the car park is full. Show spaces remaining after each car.
&lt;/span&gt;
&lt;span class="n"&gt;spaces&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="n"&gt;cars_parked&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;car_parking&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Do you want to park? (yes/no): &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;car_parking&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;cars_parked&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="n"&gt;spaces&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Go ahead and park.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Cars parked: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;cars_parked&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Spaces left: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;spaces&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;spaces&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The car park is now full.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Thank you. Have a good day!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are just two examples of where &lt;code&gt;break&lt;/code&gt; could be used. Other situations that would require &lt;code&gt;break&lt;/code&gt; include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Stop searching once a specific respondent ID is found.&lt;/li&gt;
&lt;li&gt;Exit a password prompt after the correct password is entered.&lt;/li&gt;
&lt;li&gt;Stop processing transactions once the daily limit has been reached.&lt;/li&gt;
&lt;li&gt;End a quiz when the user chooses to quit.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;continue&lt;/code&gt; statement
&lt;/h3&gt;

&lt;p&gt;We've seen that the&lt;code&gt;break&lt;/code&gt;statement terminates a loop as soon as a specified condition is met. The &lt;code&gt;continue&lt;/code&gt; statement behaves differently. Instead of ending the loop, it &lt;strong&gt;skips the rest of the current iteration&lt;/strong&gt; and immediately moves on to the next one, without terminating the loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To better understand the difference between break and continue, let's use the same initial example from the &lt;code&gt;break&lt;/code&gt; section. &lt;br&gt;
Notice how changing one statement will affect the behavior of the loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;respondents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R001&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R002&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R003&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R004&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R005&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R006&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R007&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R008&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;respondent&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;respondents&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;respondent&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;R005&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Checking respondents : &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;respondent&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is what happens step by step: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python starts the &lt;code&gt;for&lt;/code&gt; loop and assigns the first value, &lt;code&gt;"R001"&lt;/code&gt;, to the variable &lt;code&gt;respondent&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;It checks whether &lt;code&gt;respondent == "R005"&lt;/code&gt;. Since "R001" is not equal to "R005", the condition evaluates to &lt;code&gt;False&lt;/code&gt;.&lt;br&gt;
Python skips the continue statement and executes:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Checking respondent: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;respondent&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;displaying:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Checking respondent: R001
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The same process repeats for "R002", "R003", and "R004", with each respondent being printed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On the next iteration, respondent becomes "R005". This time, the condition &lt;code&gt;respondent == "R005"&lt;/code&gt; evaluates to &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python executes the &lt;code&gt;continue&lt;/code&gt; statement, immediately &lt;strong&gt;skipping&lt;/strong&gt; the &lt;code&gt;print()&lt;/code&gt; statement for this iteration. As a result, "R005" is &lt;strong&gt;NOT&lt;/strong&gt; displayed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instead of ending the loop, Python moves directly to the next iteration and continues processing "R006", "R007", and "R008" until all respondents have been checked.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The output therefore becomes;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Checking respondents : R001
Checking respondents : R002
Checking respondents : R003
Checking respondents : R004
Checking respondents : R006
Checking respondents : R007
Checking respondents : R008
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;As you will see from the output, the continue statement skips only the current iteration, which in our case is &lt;code&gt;"R005"&lt;/code&gt;, then proceeds with the remaining iterations until there are no more items to process.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here are a few more examples;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Scenario 1: A customer wants to withdraw money from an ATM. If they enter an amount greater than their account balance, ask them to enter another amount instead of ending the transaction.
&lt;/span&gt;
&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Enter withdrawal amount: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Insufficient balance.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;

    &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Successfully withdrawn Kshs. &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;. New balance: Ksh &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Scenario 2: Imagine you're analysing a survey dataset containing household incomes. Respondents who didnt disclose income are marked as -1. How can we skip these missing values while continuing to analyse the rest of the dataset?
&lt;/span&gt;&lt;span class="n"&gt;household_ids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH001&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH002&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH003&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH004&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH005&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH006&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HH007&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;household_incomes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;25000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;27000&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;household_ids&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;household_incomes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;household_ids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; | Income: Ksh &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;household_incomes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are just two examples of where &lt;code&gt;continue&lt;/code&gt; could be used. Other situations that would require &lt;code&gt;continue&lt;/code&gt; include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ignoring duplicate records when cleaning a dataset.&lt;/li&gt;
&lt;li&gt;Skipping products that are out of stock while processing customer orders.&lt;/li&gt;
&lt;li&gt;Ignoring files with an unsupported format when processing multiple files in a folder.&lt;/li&gt;
&lt;li&gt;Skipping weekends and holidays when generating a work schedule or calculating business days.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;At the beginning of the article we asked ; &lt;em&gt;How can we tell Python to stop a loop early or skip a single iteration, without ending the entire program?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We've now seen how the break and continue statements give us control over loops. While &lt;code&gt;break&lt;/code&gt; allows us to exit a loop as soon as a condition is met, &lt;code&gt;continue&lt;/code&gt; lets us skip the current iteration and move on to the next one. Together, they help us write programs that are efficient, flexible and more managable.&lt;/p&gt;

&lt;p&gt;As always, don't stop with the examples in this article. Modify them, experiment with different scenarios, and challenge yourself to find other situations where break and continue can simplify your code. &lt;/p&gt;

&lt;h3&gt;
  
  
  What's next?
&lt;/h3&gt;

&lt;p&gt;So far, we've learned how to control a single loop. But what happens when one loop isn't enough? &lt;/p&gt;

&lt;p&gt;In the next article, we'll answer that question as we explore &lt;code&gt;nested&lt;/code&gt; loops.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Repeating Tasks Without Repeating Code</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Fri, 17 Jul 2026 19:38:12 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/repeating-tasks-without-repeating-code-4fak</link>
      <guid>https://dev.to/sharonnyabuto/repeating-tasks-without-repeating-code-4fak</guid>
      <description>&lt;h2&gt;
  
  
  Using &lt;code&gt;for&lt;/code&gt; and &lt;code&gt;while&lt;/code&gt; loops in Python
&lt;/h2&gt;

&lt;p&gt;Now that Python can make decisions, how do we apply those decisions repeatedly?&lt;/p&gt;

&lt;p&gt;In the last article, we learned how to guide Python through multiple levels of decision-making using nested &lt;code&gt;if&lt;/code&gt; statements. But making decisions is only part of what makes Python pretty powerful.&lt;/p&gt;

&lt;p&gt;Picture this: You've just landed your first data analysis task. Your dataset contains 9,000 respondents, and you need to perform the same check on every single record. After writing the same line of code a few times, and dodging errors, you stop and think: &lt;em&gt;There has to be a better way than copying and pasting this thousands of times&lt;/em&gt;... right?&lt;/p&gt;

&lt;p&gt;There is. It's called a &lt;strong&gt;loop&lt;/strong&gt;, and by the end of this article, you'll know exactly how to use one, and get that task done in a fraction of that time.&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;strong&gt;FOR&lt;/strong&gt; Loop
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;for&lt;/code&gt; loop is used to repeat a block of code a &lt;strong&gt;specific&lt;/strong&gt; number of times or to iterate over a &lt;strong&gt;sequence&lt;/strong&gt;, such as a list, a string, or a range of numbers. &lt;/p&gt;

&lt;p&gt;The general syntax of a &lt;code&gt;for&lt;/code&gt; loop looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;variable&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Code to repeat
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;for&lt;/code&gt; : Indicates the beginning of the loop&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;variable&lt;/code&gt;: Stores the current value during each repetition. You can name it anything meaningful (&lt;code&gt;number&lt;/code&gt;, &lt;code&gt;student&lt;/code&gt;, &lt;code&gt;county&lt;/code&gt;, &lt;code&gt;letter&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;in&lt;/code&gt;: Tells Python to go through each item in the sequence.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sequence&lt;/code&gt;:  The collection being looped over, such as a range(), list, or string.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:&lt;/code&gt; Marks the beginning of the loop body.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Indentation&lt;/code&gt; : Indicates code blocks that are in the loop. Everything indented below the &lt;code&gt;for&lt;/code&gt; statement is repeated.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Example 1: Looping over a range&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The Result:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
1
2
3
4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens step by Step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python reads the statement &lt;code&gt;range(5)&lt;/code&gt;, which produces the numbers 0, 1, 2, 3, and 4.&lt;/li&gt;
&lt;li&gt;The first value, 0, is assigned to the variable &lt;code&gt;number&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Python executes the indented code and prints:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python returns to the top of the loop, assigns the next value (1) to &lt;code&gt;number&lt;/code&gt;, and prints it, the process continues for 2, 3, and 4.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once there are no more values left in the range, the loop ends and the program moves to the next line of code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;A quick note about range()&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You may have noticed that &lt;code&gt;range(5)&lt;/code&gt; prints the numbers 0 to 4, not 5. That's because the value passed to range() is the stopping point, and Python does not include it. In other words, &lt;code&gt;range(5)&lt;/code&gt; means start at 0 and stop before 5.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Example 2: Looping over a list&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Faith&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sheila&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bryan&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sharon&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Example 3: Combining a &lt;code&gt;for&lt;/code&gt; loop and conditional &lt;code&gt;if&lt;/code&gt; statement&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;temperatures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;33&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;temperatures&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Temperature is: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
         &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Temperature is: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;temperature&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; (below 30)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Output:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Temperature is: 22 (below 30)
Temperature is: 35
Temperature is: 18 (below 30)
Temperature is: 40
Temperature is: 29 (below 30)
Temperature is: 15 (below 30)
Temperature is: 33
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've seen how &lt;code&gt;for&lt;/code&gt; loops make it easy to repeat a block of code when we know what we're looping over, whether that's a range of numbers, a list, or another sequence. But what happens when we don't know in advance how many times a task needs to be repeated? That's where the &lt;code&gt;while&lt;/code&gt; loop comes in.&lt;/p&gt;

&lt;h3&gt;
  
  
  The WHILE loop
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;while&lt;/code&gt; loop repeatedly executes a block of code as long as a specified condition remains &lt;code&gt;True&lt;/code&gt;. Once the condition becomes &lt;code&gt;False&lt;/code&gt;, the loop stops.&lt;/p&gt;

&lt;p&gt;The general syntax of a &lt;code&gt;while&lt;/code&gt; loop looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Code to repeat
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;while&lt;/code&gt;:&lt;/strong&gt; Indicates the start of the &lt;code&gt;while&lt;/code&gt; loop&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;condition&lt;/code&gt;:&lt;/strong&gt; The expression that evaluates to either &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;:&lt;/code&gt;&lt;/strong&gt;  Marks the beginning of the loop body.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Indented code:&lt;/strong&gt; The block of code that is repeatedly executed for as long as the condition remains &lt;code&gt;True&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see how this works in practice &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example 1: printing the numbers from 1 to 5.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The Result:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
3
4
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens step by Step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;variable&lt;/code&gt; count is initialized with the value &lt;code&gt;1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python checks the condition &lt;code&gt;count &amp;lt;= 5.&lt;/code&gt; Since 1 is less than or equal to 5, the condition is &lt;code&gt;True&lt;/code&gt;, so the loop begins.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The statement &lt;code&gt;print(count)&lt;/code&gt; is executed, displaying:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The line &lt;code&gt;count += 1&lt;/code&gt; increases the value of count by 1, changing it from 1 to 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python returns to the top of the loop and checks the condition again. Since 2 &amp;lt;= 5 is still &lt;code&gt;True&lt;/code&gt;, the loop repeats.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This process continues until count becomes 6. At that point, the condition count &amp;lt;= 5 evaluates to &lt;code&gt;False&lt;/code&gt;, so the loop stops and the program moves on to the next line of code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Why do we need &lt;code&gt;count += 1&lt;/code&gt;/&lt;code&gt;count = count + 1&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Without this line, the value of count would never change. It would remain 1, meaning the condition count &amp;lt;= 5 would always be &lt;code&gt;True&lt;/code&gt;. As a result, Python would keep printing 1 forever, creating  an infinite loop.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;The Result:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;The loop never ends because &lt;code&gt;count&lt;/code&gt; is never updated. Since the condition always remains &lt;code&gt;True&lt;/code&gt;, Python keeps executing the loop forever. This is called an &lt;strong&gt;infinite loop&lt;/strong&gt;. &lt;br&gt;
Every &lt;code&gt;while&lt;/code&gt; loop should have a stopping condition so that it knows when to stop.&lt;/p&gt;

&lt;p&gt;Let's do a few more examples that demonstrate how while loops are commonly used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Q1: You have a starting balance of 5k, and you are withdrawing 1000. So you can keep withdrawing as long as the balance is greater than withdrawal
&lt;/span&gt;
&lt;span class="n"&gt;mpesa_balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;
&lt;span class="n"&gt;withdrawn_amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;mpesa_balance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;withdrawn_amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;new_balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mpesa_balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;withdrawn_amount&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Request successful.Your new Balance is: Kshs: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;new_balance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;mpesa_balance&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You have insufficient funds. Your Balance is: Ksks: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;new_balance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Q2. A savings account starts with Ksh 1,000. It earns 10% interest every month. How many months does it take to reach Ksh 2,000? Print the balance each month.
&lt;/span&gt;&lt;span class="n"&gt;start_amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;span class="n"&gt;interest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start_amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;
&lt;span class="n"&gt;month&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;start_amount&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;interest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start_amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;
    &lt;span class="n"&gt;start_amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;interest&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;start_amount&lt;/span&gt;
    &lt;span class="n"&gt;month&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="nf"&gt;print &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;After &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;month&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; months, your interest is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;interest&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;. The account balance is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;start_amount&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;At the beginning of this article, we imagined having to process thousands of survey responses without writing the same code over and over again. As we've seen, loops provide a much more efficient solution. &lt;br&gt;
Whether you use a &lt;code&gt;for&lt;/code&gt; loop when you know what you're iterating over, or a&lt;code&gt;while&lt;/code&gt; loop when repetition depends on a condition, both allow Python to &lt;strong&gt;automate repetitive tasks&lt;/strong&gt; efficiently and with far less effort and error than doing them manually.&lt;/p&gt;

&lt;p&gt;As you continue learning, try modifying the examples in this article and create a few of your own. Remember, the more you practise, the more natural it will become to recognise when a loop is the right tool for the job.&lt;/p&gt;

&lt;h3&gt;
  
  
  Up Next...
&lt;/h3&gt;

&lt;p&gt;We've learned how to repeat a single task. So far, our loops have executed until they naturally reached the end. In the next article, we'll learn how to stop a loop early or skip specific iterations using the &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; statements. I look forward to sharing it with you!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Making Decisions Within Decisions in Python</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Wed, 15 Jul 2026 06:11:48 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/taking-conditional-logic-further-with-nested-if-statements-94d</link>
      <guid>https://dev.to/sharonnyabuto/taking-conditional-logic-further-with-nested-if-statements-94d</guid>
      <description>&lt;h2&gt;
  
  
  Taking Conditional Logic Further with Nested if Statements
&lt;/h2&gt;

&lt;p&gt;Previously, we learnt how to use &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; to make decisions in Python. Those allowed our programs to choose between different paths depending on whether a condition evaluated to &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Most of the real world decisions depend on more than one condition.&lt;/p&gt;

&lt;p&gt;Think about logging into your bank account to withdraw money. Entering the correct PIN is only the first step. Once you're in, the system still needs to check whether you have sufficient funds before approving the withdrawal. &lt;br&gt;
How do we build that kind of layered decision-making in Python?&lt;br&gt;
That's exactly what we'll explore in this article as we learn about nested &lt;code&gt;if&lt;/code&gt; statements.&lt;/p&gt;
&lt;h3&gt;
  
  
  Nested &lt;code&gt;if&lt;/code&gt; Statements
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;nested &lt;code&gt;if&lt;/code&gt; statement&lt;/strong&gt; is simply an &lt;code&gt;if&lt;/code&gt; statement placed inside another &lt;code&gt;if&lt;/code&gt; statement. It allows a program to evaluate another condition only after the first condition has been met.&lt;/p&gt;
&lt;h3&gt;
  
  
  Syntax
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition_1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;condition_2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# Code to execute
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The inner &lt;code&gt;if&lt;/code&gt; statement is only evaluated if the outer &lt;code&gt;if&lt;/code&gt; statement is &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;Suppose you're analysing survey responses for a scholarship programme. Applicants must first be at least 18 years old. If they meet that requirement, then you can check whether their household income qualifies them for financial assistance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;
&lt;span class="n"&gt;monthly_income&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18000&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;monthly_income&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;30000&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Eligible for the scholarship.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Age requirement met, but income is above the eligibility threshold.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Applicant does not meet the minimum age requirement.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what happens step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Python stores the value &lt;code&gt;21&lt;/code&gt; in the variable &lt;code&gt;age&lt;/code&gt; and &lt;code&gt;18000&lt;/code&gt; in the variable &lt;code&gt;monthly_income&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It evaluates the first condition: &lt;code&gt;age &amp;gt;= 18&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since &lt;code&gt;21&lt;/code&gt; is greater than or equal to &lt;code&gt;18&lt;/code&gt;, the first condition evaluates to &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Because the first condition is &lt;code&gt;True&lt;/code&gt;, Python moves to the nested &lt;code&gt;if&lt;/code&gt; statement and evaluates the second condition: &lt;code&gt;monthly_income &amp;lt; 30000&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since &lt;code&gt;18000&lt;/code&gt; is less than &lt;code&gt;30000&lt;/code&gt;, the second condition also evaluates to &lt;code&gt;True&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python executes the nested &lt;code&gt;if&lt;/code&gt; block and prints:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Eligible for the scholarship.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because both conditions were satisfied, the two &lt;code&gt;else&lt;/code&gt; blocks are skipped and the program ends.&lt;/p&gt;

&lt;p&gt;Notice the order of the checks. Python only evaluates the applicant's income &lt;strong&gt;after&lt;/strong&gt; confirming they meet the minimum age requirement. &lt;br&gt;
If the applicant were under 18, the income would never be checked because the program would immediately execute the outer &lt;code&gt;else&lt;/code&gt; block. This is what makes nested &lt;code&gt;if&lt;/code&gt;statements useful; they allow one decision to depend on the outcome of another.&lt;/p&gt;

&lt;p&gt;Here are a few other examples, that demonstrate how multiple conditions can be evaluated one after another.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Scenario 1: Job Application Screener
&lt;/span&gt;
&lt;span class="c1"&gt;# Check whether the applicant has tertiary education. If they do, ask for their years of experience. 
# If they have at least 3 years of experience, ask for their expected salary. If the salary expectation is Ksh 80,000 or less, shortlist them for an interview; otherwise, indicate that their salary expectation is too high. 
# If they have fewer than 3 years of experience, print "Minimum 3 years of experience required." If they do not have tertiary education, print "Higher education is required for this position."
&lt;/span&gt;
&lt;span class="n"&gt;tertiary_education&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Do you have a diploma, degree or higher?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;degree&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;experience&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;How many years of experince&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;experience&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Enter your expected salary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;80000&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Shortlisted for interview&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Salary expectation too high&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Minimum 3 years experience required&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Higher education is required for this position&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;`&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Scenario 2: Mobile Money Transfer Validator
&lt;/span&gt;
&lt;span class="c1"&gt;# Write a program to validate a mobile money transfer. 
# If the user has an account, check that they have sufficient funds and that the transfer amount does not exceed the daily limit of Ksh 150,000. 
# If the transfer is successful, display the amount sent and the remaining balance. If the user does not have an account, print "Please register first."
&lt;/span&gt;
&lt;span class="n"&gt;mobile_checker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Do they have an active account? (Yes/No)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;mobile_checker&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;acc_balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is your account balance?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;acc_balance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;amount_to_send&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;How much do you want to send??&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;amount_to_send&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;acc_balance&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;new_balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;acc_balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;amount_to_send&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;amount_to_send&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;150000&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Amount exceeded daily limit&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Successful transfer&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Amount sent: Ksh., &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;amount_to_send&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, Your account balance is: Kshs., &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;new_balance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Insufficient funds: Kshs., &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;acc_balance&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You have a low balance&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Please register first.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
&lt;code&gt;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Scenario 3: School Report Card Generator
&lt;/span&gt;
&lt;span class="c1"&gt;# Write a program that generates a student's report card. Ask for the student's name and marks (out of 100) in Mathematics, English, Kiswahili, Science, and Social Studies. For each subject, assign a grade and remark using the grading scale provided. Then calculate the total marks (out of 500), the average score, and the overall grade based on the average using the same grading scale.
# Next, determine the student's promotion status: students with an average of 75 or above are promoted, those with an average between 50 and 74 are promoted but advised to work harder, while those below 50 should repeat the class.
# Finally, check whether the student received a grade E in any subject. If so, display a warning indicating that remedial lessons are required for the relevant subject(s). 
&lt;/span&gt;
&lt;span class="c1"&gt;# Print a neatly formatted report showing the student's name, subject grades, total marks, average, overall grade, promotion status and any remedial warning.
&lt;/span&gt;
&lt;span class="n"&gt;subjects&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Maths&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;English&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Kiswahili&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Science&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Social Studies&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;student_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is your name?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; : Performance Summary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;==================================&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;total_marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;""&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;subjects&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What was your score in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  A  - Excellent&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;74&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  B  - Very Good&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  C  - Good&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;49&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  D  - Pass&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  E  - Need Improvement&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="n"&gt;total_marks&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; | Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; | Grade:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  E  - Need Improvement&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Total marks: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;total_marks&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; / 500&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;NOTE: Remedial lessons required in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;failed&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total_marks&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subjects&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Average Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;average&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PROMOTED to next class&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;average&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;74&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PROMOTED -  work harder next term&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;REPEAT - see your class teacher&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mary Nancy : Performance Summary
==================================

Maths          | Score: 43 | Grade:  D  - Pass
English        | Score: 23 | Grade:  E  - Need Improvement
Kiswahili      | Score: 56 | Grade:  C  - Good
Science        | Score: 89 | Grade:  A  - Excellent
Social Studies | Score: 67 | Grade:  B  - Very Good
Total marks: 278 / 500

NOTE: Remedial lessons required in English 

Average Score: 55.6
PROMOTED - Work harder next term 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't stop with the examples in this article. Modify them, build your own, and experiment with different scenarios, because the best way to master programming is by writing and testing your own code. &lt;br&gt;
As you continue practicing, you may find yourself using nested conditionals whenever a problem requires multiple levels of decision-making. &lt;/p&gt;
&lt;h3&gt;
  
  
  Bonus Tip: f-Strings
&lt;/h3&gt;

&lt;p&gt;Before we wrap up, there's one small feature you've probably noticed throughout the examples in this article. Many of the &lt;code&gt;print()&lt;/code&gt; statements begin with the letter &lt;code&gt;f&lt;/code&gt;, like this;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; | Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; | Grade:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;f&lt;/code&gt; stands for &lt;strong&gt;formatted string&lt;/strong&gt; (or &lt;strong&gt;f-string&lt;/strong&gt;). It allows you to insert the value of a variable directly into a string by placing it inside curly braces &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Without an f-string, you would have to write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; | Score: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; | Grade: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;grade&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While both approaches work, f-strings are easier to read and become especially useful as your programs grow longer and your output includes multiple variables.&lt;/p&gt;

&lt;p&gt;As we continue through this series, we'll use f-strings regularly to produce cleaner and more readable output. &lt;/p&gt;

</description>
      <category>python</category>
      <category>learning</category>
    </item>
    <item>
      <title>Teaching Python to Make Decisions</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Mon, 13 Jul 2026 21:11:20 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/teaching-python-to-make-decisions-2k0k</link>
      <guid>https://dev.to/sharonnyabuto/teaching-python-to-make-decisions-2k0k</guid>
      <description>&lt;h2&gt;
  
  
  Understanding if, elif, and else
&lt;/h2&gt;

&lt;p&gt;In the previous article, we explored what Python is and why it's such a valuable tool for data analysts. We also wrote a few simple programs that followed instructions exactly as we wrote them.&lt;/p&gt;

&lt;p&gt;In this article, we will explore how to teach Python to make decisions using conditional statements. By the end, you'll understand how to use &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; to control the flow of your programs and hopefully solve simple real-world problems.&lt;/p&gt;




&lt;h3&gt;
  
  
  So what is a conditional statement?
&lt;/h3&gt;

&lt;p&gt;We make decisions all the time in our lives. If it rains, we carry an umbrella. If we're hungry, we eat. If an assignment is due tomorrow, we probably shouldn't leave it until midnight. Programs need to make decisions too, that is what conditional statements are for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conditional Statements&lt;/strong&gt; allow a program to evaluate conditions and choose what to do based on whether those conditions are &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False.&lt;/code&gt; &lt;br&gt;
The three keywords you'll most commonly use when writing conditional statements are &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Comparison Operators
&lt;/h3&gt;

&lt;p&gt;Before we can make decisions in Python, we need a way to compare values. &lt;br&gt;
Comparison operators evaluate two values and return either &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;, which are the outcomes that drive &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; statements. Common comparison operators are: &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operator&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;==&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;!=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Not equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Greater than&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Less than&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;gt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Greater than or equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;&amp;lt;=&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Less than or equal to&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# equal to        -&amp;gt; True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# not equal to    -&amp;gt; True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# greater than    -&amp;gt; True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# less than       -&amp;gt; False
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# greater or equal -&amp;gt; True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# less or equal    -&amp;gt; False
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: The &lt;code&gt;=&lt;/code&gt; operator assigns a value to a variable, (like age = 25), while &lt;code&gt;==&lt;/code&gt; checks whether two values are equal. This is one of the most common mix-ups I had when I started out.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  The IF statement
&lt;/h3&gt;

&lt;p&gt;The if statement is what lets your code actually make a decision : It runs a block of code only if a condition is &lt;strong&gt;True&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;re an adult.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what happens step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;age = 25&lt;/code&gt; - Python stores the value &lt;code&gt;25&lt;/code&gt; in the variable &lt;code&gt;age&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;age &amp;gt;= 18&lt;/code&gt; - It evaluates the condition. &lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;25&lt;/code&gt; is &lt;em&gt;greater than&lt;/em&gt; &lt;code&gt;18&lt;/code&gt; the condition  is &lt;code&gt;True&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The indented line is executed, and the output prints:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You're an adult.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The colon &lt;code&gt;:&lt;/code&gt; shows the end of the condition, and indicates everything after is what needs to be done if the condition is met.&lt;/p&gt;

&lt;p&gt;The indentation &lt;em&gt;(usually 4 spaces)&lt;/em&gt; is how Python knows which lines of code belong inside the &lt;code&gt;if&lt;/code&gt; statement.&lt;/p&gt;

&lt;h3&gt;
  
  
  The ELSE statement
&lt;/h3&gt;

&lt;p&gt;In an &lt;code&gt;if&lt;/code&gt; statement, you specify what needs to be done if the condition is True. &lt;br&gt;
You can also tell Python what to do if the condition is &lt;em&gt;not&lt;/em&gt; met, using &lt;code&gt;else&lt;/code&gt;. The statement then becomes;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;68&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;You have passed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Please retake the test&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what happens step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python stores the value &lt;code&gt;68&lt;/code&gt; in the variable &lt;code&gt;score&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It evaluates the condition &lt;code&gt;score &amp;gt;= 70&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;68&lt;/code&gt; is less than &lt;code&gt;70&lt;/code&gt;, the condition evaluates to &lt;code&gt;False&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Because the &lt;code&gt;if&lt;/code&gt; condition is &lt;code&gt;False&lt;/code&gt;, Python skips the &lt;code&gt;if&lt;/code&gt; block.&lt;/li&gt;
&lt;li&gt;Python then executes the &lt;code&gt;else&lt;/code&gt; block and prints:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Please retake the test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;else&lt;/code&gt; block acts as a fallback, and  is only executed when the condition in the &lt;code&gt;if&lt;/code&gt; statement evaluates to &lt;code&gt;False&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;if-else&lt;/code&gt; statement is always used when you have &lt;strong&gt;two&lt;/strong&gt; possible outcomes.&lt;/p&gt;

&lt;h3&gt;
  
  
  The ELIF statement
&lt;/h3&gt;

&lt;p&gt;Suppose there are more than two possible outcomes? &lt;br&gt;
The if-else statements will not sufficiently represent all of them. This is where the &lt;code&gt;elif&lt;/code&gt; statement becomes useful&lt;/p&gt;

&lt;p&gt;&lt;code&gt;elif&lt;/code&gt; ("else if") lets you check additional conditions in order.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Older Adults&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Youth&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Teens&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here's what happens step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python stores the value &lt;code&gt;34&lt;/code&gt; in the variable &lt;code&gt;age&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It evaluates the first condition: &lt;code&gt;age &amp;gt;= 40&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;34&lt;/code&gt; is less than &lt;code&gt;40&lt;/code&gt;, the first condition evaluates to &lt;code&gt;False&lt;/code&gt;, so Python moves to the next condition.&lt;/li&gt;
&lt;li&gt;Python evaluates the &lt;code&gt;elif&lt;/code&gt; condition: &lt;code&gt;age &amp;gt;= 20&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Since &lt;code&gt;34&lt;/code&gt; is greater than &lt;code&gt;20&lt;/code&gt;, the &lt;code&gt;elif&lt;/code&gt; condition evaluates to &lt;code&gt;True&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Python executes the &lt;code&gt;elif&lt;/code&gt; block and prints:
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Python then skips the &lt;code&gt;else&lt;/code&gt; block and the conditional statement ends.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Think of &lt;code&gt;elif&lt;/code&gt; as Python saying, "If the first condition wasn't true, check this one instead." &lt;br&gt;
The conditions are evaluated from top to bottom and only the first condition that evaluates to &lt;code&gt;True&lt;/code&gt; is executed.&lt;/p&gt;


&lt;h3&gt;
  
  
  Putting Conditionals into Practice
&lt;/h3&gt;

&lt;p&gt;Now that we've covered the basics of &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt;, it's time to see how they work together. &lt;br&gt;
The following examples are a little more practical, and demonstrate how Python can make decisions based on multiple conditions.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Q1. A cinema allows entry if: age &amp;gt;= 18 AND has_ticket == True. Write the condition and test it with different values.
&lt;/span&gt;
&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;How old are you?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# Ask for age
&lt;/span&gt;&lt;span class="n"&gt;has_ticket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;has_ticket&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Welcome to the Movies!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Restricted&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Q2. Build an electricity bill calculator. Units used: 0–50 → Ksh 12/unit, 51–200 → Ksh 15/unit, 201+ → Ksh 18/unit. Ask for units used and print the total bill.
&lt;/span&gt;
&lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;How many units have you used?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;electricity_units&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Your bill is &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Your bill is &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Your bill is &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;electricity_units&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Q3. Ask for a student's marks (0–100). Print their KCSE grade: A (75+), B (60–74), C (50–59), D (40–49), E (below 40).
&lt;/span&gt;&lt;span class="n"&gt;student_marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What were your marks?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;student_marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;A&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;student_marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;B&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;student_marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;C&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;student_marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;D&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;E&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't worry about counting your &lt;code&gt;elif&lt;/code&gt; statements! You can have as many as your program needs, as you can see in the last example. Python simply checks each condition in order and stops as soon as it finds one that's &lt;code&gt;True.&lt;/code&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Your turn, try it yourself!
&lt;/h3&gt;

&lt;p&gt;The examples in this article are just a starting point. Experiment with different values, create your own conditions, and see how changing the logic affects the output. Programming is one of those skills that's learned best by doing.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;p&gt;We've now learned how to make Python choose between different paths. Next, we'll build on that foundation with &lt;code&gt;nested if&lt;/code&gt; statements, where one condition is evaluated inside another. It's a small step in syntax, but a big step towards writing more realistic programs. &lt;/p&gt;

&lt;p&gt;Thanks for reading, and I hope you'll join me for the next part of the series. Until then, happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Python for Data Analysts: Getting Started</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Sun, 12 Jul 2026 14:44:43 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/python-for-data-analysts-getting-started-29o6</link>
      <guid>https://dev.to/sharonnyabuto/python-for-data-analysts-getting-started-29o6</guid>
      <description>&lt;h2&gt;
  
  
  Understanding what Python is, why it matters, and the concepts every beginner will encounter.
&lt;/h2&gt;

&lt;p&gt;A month ago, if you'd asked whether I could write Python, the answer would have been no, simply because Python just looked intimidating.&lt;/p&gt;

&lt;p&gt;As a research and data analyst however, I do love a challenge,  and I asked myself the practical question: &lt;em&gt;How could I actually use Python in my day-to-day work?&lt;/em&gt;&lt;br&gt;
A dive into Python revealed one certainty: Python is not &lt;em&gt;easy&lt;/em&gt;, but it's nowhere near as intimidating as I'd built it up to be. This is a look back at my first week learning Python: the assumptions I walked in with, and how quickly most of them fell apart.&lt;/p&gt;
&lt;h2&gt;
  
  
  So what is Python and why does it matter in Data analysis?
&lt;/h2&gt;

&lt;p&gt;Python is a programming language: a way of giving a computer clear, step-by-step instructions to perform tasks. The tasks can be as simple as basic calculations or as involved as cleaning data, analysing thousands of data entries, automating repetitive tasks, or building applications. &lt;/p&gt;

&lt;p&gt;One of the reasons Python is so popular among both beginners and data analysts is its readable syntax. It often resembles plain English, making it easier to learn while remaining powerful enough for real-world data analysis.&lt;/p&gt;

&lt;p&gt;For example, one of the first programs most people write looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, World!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It simply tells Python to display &lt;code&gt;Hello, World!&lt;/code&gt; on the screen. It's a tiny program, but it marks the beginning of almost every programmer's journey, including mine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does Python matter?
&lt;/h3&gt;

&lt;p&gt;If you've spent enough time working with spreadsheets, you've probably thought, &lt;em&gt;"There has to be an easier way to do this."&lt;/em&gt; Whether it's cleaning data, repeating the same steps every week, or working with increasingly large datasets, there comes a point where spreadsheets begin to show their limits. That's where Python comes in.&lt;/p&gt;

&lt;p&gt;In data analytics, Python is particularly useful for:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Automating repetitive tasks&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;Tasks you'd normally repeat manually (cleaning files, renaming columns, merging spreadsheets) can be scripted once and reused.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scale&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;As datasets grow, spreadsheets can become slow, cumbersome, or even unresponsive. Python is designed to handle much larger amounts of data efficiently. We'll look at the tools that make this possible including Python libraries, in a later article.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reproducibility&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;Every step of your analysis is written into a script. That means you can run the same code tomorrow, next month, or even next year and reproduce the same results, without having to memorize or manually repeat every step.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collaboration&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Because each step of the analysis is written into the script, colleagues can review, understand, and build on your work without having to guess what was done. This makes teams work seamlessly on the same project.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility in connectivity&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;Python can pull data directly from a database, an API, or a folder of raw files, instead of relying on someone exporting a CSV for you.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Specialized Tools&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;One of Python's greatest strengths is its ecosystem of libraries. Libraries are tools created by the Python community that extend what Python can do, without you having to build everything from scratch. &lt;em&gt;(We'll introduce some of them as we progress through the series)&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Learning why Python matters gave me the motivation to begin. Learning how to use it, however, came with a few surprises. Looking back, many of the things that intimidated me at first weren't really difficult, but simply unfamiliar.&lt;/p&gt;




&lt;h3&gt;
  
  
  Common Terms You'll Encounter in Python - Getting familiar with the Python Environment
&lt;/h3&gt;

&lt;p&gt;Far from jumping into data analysis, my first few days were spent learning the basic concepts and getting familiar with common terms, most of which had names that were new to me.&lt;/p&gt;

&lt;p&gt;Here are five that stood out to me when I was getting started;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Terminal:&lt;/strong&gt; I assumed one wrong command could break my computer, only to realize it's really just another way of talking to your machine, and it stops feeling like a 'black box' after a handful of basic commands.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integrated Development Environment/IDE:&lt;/strong&gt; This is where you actually write your code. Think of it as a text editor built specifically for writing and running programs, with extra features like error highlighting, autocomplete, and a built-in terminal. &lt;strong&gt;VS Code&lt;/strong&gt; is one of the most popular choices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Script:&lt;/strong&gt; This is the file containing Python code, usually with a .py extension. Instead of typing commands one at a time into the terminal, you write them all into a script and run the whole thing at once.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Library:&lt;/strong&gt; Pre-written code that someone else has already built, so you don't have to write everything from scratch. Need to work with dates, analyse data, or make charts? There's very likely a library for that — you just import it into your script and use it. &lt;strong&gt;Pandas&lt;/strong&gt; is one of the most common ones for data analysis.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Indentation:&lt;/strong&gt; Indentation isn't cosmetic in Python, it's part of the syntax, defining which lines belong together. It took me a couple of errors and examples before fully comprehending, but once that clicked, it stopped feeling confusing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Early on, I felt like I had to fully understand every new term,  variables, functions, loops, modules, packages, libraries, before I could write code. Turns out you only need to understand the concept you're using &lt;em&gt;today&lt;/em&gt;. The rest will fall into place as you keep writing code.&lt;/p&gt;

&lt;h2&gt;
  
  
  My first few lines of Python
&lt;/h2&gt;

&lt;p&gt;A variable is just a name that stores information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sharon&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nationality&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sharon&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Kenyan&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Kisumu&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; 
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;My name is &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;. I am a &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;nationality&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; and I live in &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python can do calculations for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;income&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25000&lt;/span&gt;
&lt;span class="n"&gt;expenses&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18000&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;income&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;expenses&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or take input from the user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;What is your name? &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Welcome, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While these may not be impressive on their own, together they show programming isn't about writing something complex from day one, but about stacking small blocks to solve a bigger problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Biggest lessons so far
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Progress comes from consistent practice.&lt;/li&gt;
&lt;li&gt;Errors are part of the workflow, not a verdict on your ability.&lt;/li&gt;
&lt;li&gt;Typing code yourself teaches more than copy-pasting it.&lt;/li&gt;
&lt;li&gt;Small projects teach more than another hour of tutorials.&lt;/li&gt;
&lt;li&gt;Looking things up is normal, sometimes even the best analysts do it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Programming isn't about knowing every answer. It's about knowing how to find one. This is the first article in my Python learning series, documenting the process as I go.&lt;/p&gt;

&lt;p&gt;In the next article, we'll look at how Python uses &lt;code&gt;if&lt;/code&gt; statements &lt;code&gt;nested if&lt;/code&gt; statements to make choices based on different conditions. This i think is one of the biggest steps towards writing programs that actually think before they act.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;If you're on the fence, don't wait until you feel "ready." Write one line of code and trust that understanding builds with reps.&lt;/p&gt;

&lt;p&gt;I'm still learning too, and that's exactly what this series is for.&lt;/p&gt;

</description>
      <category>analytics</category>
      <category>beginners</category>
      <category>datascience</category>
      <category>python</category>
    </item>
    <item>
      <title>Cleaning Messy Phone Numbers in PostgreSQL Using REGEXP_REPLACE</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Mon, 18 May 2026 22:34:00 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/cleaning-messy-phone-numbers-in-postgresql-using-regexp-replace-3n83</link>
      <guid>https://dev.to/sharonnyabuto/cleaning-messy-phone-numbers-in-postgresql-using-regexp-replace-3n83</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The first thing anyone that has worked with real world data will tell you is that rarely does data come in the clean, uniform format you expect. Phone numbers are one of the biggest culprits. &lt;/p&gt;

&lt;p&gt;One person enters &lt;code&gt;+254712345678&lt;/code&gt;, another writes &lt;code&gt;0712345678&lt;/code&gt;, someone else throws in &lt;code&gt;254-712-345-678&lt;/code&gt;, another &lt;code&gt;(254)-712345678&lt;/code&gt; and another &lt;code&gt;254 712 345 678&lt;/code&gt;.  Before you know it, your database has a different version of phone numbers in every row.&lt;/p&gt;

&lt;p&gt;In this article, we will explore how to fix that, using a powerful PostgreSQL function called &lt;code&gt;regexp_replace&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;By the end, you will understand what it does, how to build the pattern, and how to apply it to actually clean your data.&lt;/p&gt;




&lt;h2&gt;
  
  
  First, What Even Is &lt;code&gt;regexp_replace&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;First things first, we need to understand what this function is and what it does.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;regexp_replace&lt;/code&gt; is a PostgreSQL function that finds a pattern inside a string and replaces whatever matches that pattern with something else you specify. The word &lt;strong&gt;regexp&lt;/strong&gt; is short for &lt;strong&gt;regular expression&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The syntax looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;replacement_string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us break each part down simply:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Part&lt;/th&gt;
&lt;th&gt;What It Means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;source&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The string you are searching inside. In our case, this is the column &lt;code&gt;passenger_phone&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;pattern&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;What you are looking for : What do you want to find and remove or replace?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;replacement_string&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;What do you want to put in place of what you found? If you just want to delete it, you pass an empty string &lt;code&gt;''&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;flag&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Controls how the matching behaves. The two you will use most are &lt;code&gt;i&lt;/code&gt; for case-insensitive matching and &lt;code&gt;g&lt;/code&gt; for global, meaning it applies the replacement to everything it finds in the string, not just the first match&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now that we have the hang of it, let us move on to building the pattern.&lt;/p&gt;




&lt;h2&gt;
  
  
  Understanding the Special Characters in Patterns
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;pattern&lt;/code&gt; is basically a set of rules that tells PostgreSQL what to look for. It is like saying "find any character that is not a digit" or "find this pattern, but only at the beginning" etc. &lt;/p&gt;

&lt;p&gt;To write these rules/instructions, there are special characters that are used.&lt;/p&gt;

&lt;p&gt;Here are the ones we will use today:&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;^&lt;/code&gt; character
&lt;/h3&gt;

&lt;p&gt;Depending on where it is placed, this will do either of 2 things;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When &lt;code&gt;^&lt;/code&gt; is at the beginning of a pattern&lt;/strong&gt; it means: &lt;em&gt;start matching from the very beginning of the string.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;^'hello'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means: only match &lt;code&gt;hello&lt;/code&gt; if it appears at the start of the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;^[0-9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means: Only the &lt;strong&gt;first character&lt;/strong&gt; of a string is checked and matched if it is a digit. Anything after that is ignored.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When &lt;code&gt;^&lt;/code&gt; is inside square brackets &lt;code&gt;[ ]&lt;/code&gt;&lt;/strong&gt;, it means: &lt;em&gt;NOT these characters.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[^0-9]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means: &lt;strong&gt;Every character&lt;/strong&gt; in the string is checked, any one that is not a digit gets matched.&lt;/p&gt;

&lt;p&gt;Same symbol, two different purposes depending on its position.&lt;/p&gt;




&lt;h3&gt;
  
  
  The &lt;code&gt;\&lt;/code&gt; character (backslash)
&lt;/h3&gt;

&lt;p&gt;The backslash is used to tell PostgreSQL: &lt;em&gt;treat the next character as a character, not as a special operator.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For example, the &lt;code&gt;+&lt;/code&gt; sign in regular expressions has a special meaning (we will cover it in a moment). So if you want to find a plus sign &lt;code&gt;+&lt;/code&gt; in your data, like &lt;code&gt;+254&lt;/code&gt;, you write it as &lt;code&gt;\+&lt;/code&gt; to tell PostgreSQL &lt;em&gt;"I mean the actual plus sign, not the operator."&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Square brackets &lt;code&gt;[ ]&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Square brackets let you define a group of characters to match. For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;[0-9]&lt;/code&gt; means match any digit from 0 to 9&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[a-z]&lt;/code&gt; means match any lowercase letter&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[^0-9]&lt;/code&gt; means match anything that is NOT a digit (the &lt;code&gt;^&lt;/code&gt; inside means NOT, as we've mentioned above)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  The &lt;code&gt;+&lt;/code&gt; operator
&lt;/h3&gt;

&lt;p&gt;When used outside brackets, &lt;code&gt;+&lt;/code&gt; means: &lt;em&gt;keep matching one or more of the preceding character or group.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;[0-9]+&lt;/code&gt; means: Match the digits as long as they are all next to each other with no other characters in between. (Without the + , it stops after the first match)&lt;/p&gt;




&lt;h3&gt;
  
  
  The &lt;code&gt;$&lt;/code&gt; character
&lt;/h3&gt;

&lt;p&gt;Just like &lt;code&gt;^&lt;/code&gt; marks the start of a string, &lt;code&gt;$&lt;/code&gt; marks the end. When you write &lt;code&gt;$&lt;/code&gt; at the end of your pattern, you are saying: &lt;em&gt;the string must end here, nothing else allowed after this.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Putting them together: &lt;code&gt;^[0-9]+$&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now that you know each piece, read this pattern again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;^[0-9]+$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; → start from the beginning of the string&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;[0-9]&lt;/code&gt; → the characters must be digits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; → keep going, there can be one or more of them&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$&lt;/code&gt; → and this must be the end of the string, nothing else after&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All together: &lt;strong&gt;the entire string, from start to finish, must contain only digits.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the pattern we will use to identify which phone numbers are already clean and which ones need fixing.&lt;/p&gt;




&lt;h2&gt;
  
  
  Our Problem: What Does the Messy Data Look Like?
&lt;/h2&gt;

&lt;p&gt;Let us say when we run this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;dirty_safari_data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We get back a mix of things like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+254712345678
254712345678
0712345678
0712-345-678
+254 712 345 678
(0) 712345678

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

&lt;/div&gt;



&lt;p&gt;The goal is to standardize all of these into the local format starting with &lt;code&gt;0&lt;/code&gt;, containing only digits, like:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Two problems to solve:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Some numbers start with &lt;code&gt;+254&lt;/code&gt; or &lt;code&gt;254&lt;/code&gt; instead of &lt;code&gt;0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Some numbers have extra characters like brackets, spaces, dashes, or plus signs&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Step 1: Replace &lt;code&gt;+254&lt;/code&gt; or &lt;code&gt;254&lt;/code&gt; at the Start With &lt;code&gt;0&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The first thing we want to do is find any phone number that starts with &lt;code&gt;+254&lt;/code&gt; or &lt;code&gt;254&lt;/code&gt; and swap that prefix out for &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passenger_phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'^(&lt;/span&gt;&lt;span class="se"&gt;\+&lt;/span&gt;&lt;span class="s1"&gt;254|254)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;contacts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us read the pattern &lt;code&gt;^(\+254|254)&lt;/code&gt; carefully:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; → look at the start of the string only&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(&lt;/code&gt; &lt;code&gt;)&lt;/code&gt; → group what is inside together&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;\+254&lt;/code&gt; → literally the characters &lt;code&gt;+254&lt;/code&gt; (the backslash makes &lt;code&gt;+&lt;/code&gt; a literal character)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;|&lt;/code&gt; → OR&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;254&lt;/code&gt; → literally the characters &lt;code&gt;254&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the full pattern says: &lt;em&gt;at the start of the string, find either &lt;code&gt;+254&lt;/code&gt; or &lt;code&gt;254&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The replacement is &lt;code&gt;'0'&lt;/code&gt;, so wherever that prefix is found, replace it with &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The flag is &lt;code&gt;'g'&lt;/code&gt; for global.&lt;/p&gt;

&lt;p&gt;After this step, &lt;code&gt;+254712345678&lt;/code&gt; becomes &lt;code&gt;0712345678&lt;/code&gt; and &lt;code&gt;254712345678&lt;/code&gt; also becomes &lt;code&gt;0712345678&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Remove Any Character That Is Not a Digit
&lt;/h2&gt;

&lt;p&gt;Some numbers still have spaces, dashes, or other characters sitting in them. The second &lt;code&gt;regexp_replace&lt;/code&gt; cleans all of that up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passenger_phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'[^0-9]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;contacts&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pattern &lt;code&gt;[^0-9]&lt;/code&gt; means: &lt;em&gt;any character that is NOT a digit.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The replacement is &lt;code&gt;''&lt;/code&gt; (an empty string), so we are just deleting those characters.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;'g'&lt;/code&gt; flag means do this for every non-digit character found in the string, not just the first one.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;0712-345-678&lt;/code&gt; becomes &lt;code&gt;0712345678&lt;/code&gt; and &lt;code&gt;0712 345 678&lt;/code&gt; also becomes &lt;code&gt;0712345678&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3: The &lt;code&gt;WHERE&lt;/code&gt; Clause — Only Update What Needs Fixing
&lt;/h2&gt;

&lt;p&gt;We do not want to touch phone numbers that are already clean. That is where our &lt;code&gt;^[0-9]+$&lt;/code&gt; pattern comes in handy. We use it in the &lt;code&gt;WHERE&lt;/code&gt; clause to filter for records where the phone number is &lt;strong&gt;not&lt;/strong&gt; already a clean string of digits.&lt;/p&gt;

&lt;p&gt;You might be tempted to write it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s1"&gt;'^[0-9]+$'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this is not correct. The &lt;code&gt;!=&lt;/code&gt; operator just compares strings literally, so this would check if the phone number is literally equal to the text &lt;code&gt;^[0-9]+$&lt;/code&gt;, which is not what we want.&lt;/p&gt;

&lt;p&gt;In PostgreSQL, to check whether a string does NOT match a regular expression, you use the &lt;code&gt;!~&lt;/code&gt; operator. The corrected version should be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="o"&gt;!~&lt;/span&gt; &lt;span class="s1"&gt;'^[0-9]+$'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This now correctly says: &lt;em&gt;only return (or update) rows where the phone number does not match the pattern of being purely digits from start to finish.&lt;/em&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Putting It All Together
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The SELECT query (to preview your cleaned data before changing anything)
&lt;/h3&gt;

&lt;p&gt;Always preview before you update. This is a good habit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passenger_phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'^(&lt;/span&gt;&lt;span class="se"&gt;\+&lt;/span&gt;&lt;span class="s1"&gt;254|254)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="s1"&gt;'[^0-9]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;cleaned_phone&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;contacts&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="o"&gt;!~&lt;/span&gt; &lt;span class="s1"&gt;'^[0-9]+$'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how we nested one &lt;code&gt;regexp_replace&lt;/code&gt; inside another. The inner one runs first (fixing the prefix), and then the outer one runs on the result (removing non-digits). It is like running two cleaning steps in one go.&lt;/p&gt;




&lt;h3&gt;
  
  
  The UPDATE query (to actually apply the changes)
&lt;/h3&gt;

&lt;p&gt;Once you are happy with the preview, run the update:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;contacts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;regexp_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passenger_phone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'^(&lt;/span&gt;&lt;span class="se"&gt;\+&lt;/span&gt;&lt;span class="s1"&gt;254|254)'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="s1"&gt;'[^0-9]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'g'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;passenger_phone&lt;/span&gt; &lt;span class="o"&gt;!~&lt;/span&gt; &lt;span class="s1"&gt;'^[0-9]+$'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This updates only the rows that have messy phone numbers, leaving the already clean ones untouched.&lt;/p&gt;




&lt;h2&gt;
  
  
  Quick Recap
&lt;/h2&gt;

&lt;p&gt;Here is a summary of everything covered:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Pattern&lt;/th&gt;
&lt;th&gt;What It Does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;^&lt;/code&gt; at the start of a pattern&lt;/td&gt;
&lt;td&gt;Anchors the match to the start of the string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;^&lt;/code&gt; inside &lt;code&gt;[ ]&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Means NOT — match anything except what follows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;\+&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Treat &lt;code&gt;+&lt;/code&gt; as a literal plus sign, not an operator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;[0-9]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Match any digit from 0 to 9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;[^0-9]&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Match any character that is NOT a digit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;+&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;One or more of the preceding character/group&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;$&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Anchors the match to the end of the string&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;^[0-9]+$&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The entire string must be digits only, nothing else&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;g&lt;/code&gt; flag&lt;/td&gt;
&lt;td&gt;Apply the replacement globally (every match, not just the first)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Regular expressions can look intimidating and confusing at first, but once you understand what each character means and why it is there, they start making a lot of sense. The key takeaway here is to build your pattern step by step, rather than trying to write the whole thing at once.&lt;/p&gt;

&lt;p&gt;Also, always test with a &lt;code&gt;SELECT&lt;/code&gt; before running an &lt;code&gt;UPDATE&lt;/code&gt;. You can never be too careful when modifying data directly in a table.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>postgres</category>
      <category>beginners</category>
    </item>
    <item>
      <title>SQL Subqueries vs. CTEs: A Guide to Writing Better Queries</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Tue, 21 Apr 2026 19:54:31 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/sql-subqueries-vs-ctes-a-guide-to-writing-better-queries-1f44</link>
      <guid>https://dev.to/sharonnyabuto/sql-subqueries-vs-ctes-a-guide-to-writing-better-queries-1f44</guid>
      <description>&lt;p&gt;When you first learn SQL, queries are usually straightforward: you &lt;code&gt;SELECT&lt;/code&gt; some columns from a table and apply a &lt;code&gt;WHERE&lt;/code&gt; filter to narrow down the results.&lt;/p&gt;

&lt;p&gt;As your data questions become more complex, however, you realize sometimes you need the answer to one question before you can answer another. &lt;br&gt;
For example: &lt;em&gt;"Which employees earn above the company average?"&lt;/em&gt;, requires calculating the average salary first, then use the result to filter employees.&lt;/p&gt;

&lt;p&gt;To solve this, SQL provides two incredibly powerful tools: &lt;strong&gt;Subqueries&lt;/strong&gt; and &lt;strong&gt;Common Table Expressions (CTEs)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This guide will break down these tools and cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Part 1: Subqueries&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What subqueries are and how they work&lt;/li&gt;
&lt;li&gt;The different types of subqueries&lt;/li&gt;
&lt;li&gt;When to use subqueries&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Part 2: CTEs (Common Table Expressions)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What CTEs are and why they matter&lt;/li&gt;
&lt;li&gt;The different types of CTEs&lt;/li&gt;
&lt;li&gt;When to use CTEs&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Part 3: A clear comparison of Subqueries vs. CTEs&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt; PART 1: SUBQUERIES&lt;a&gt;&lt;/a&gt;  &lt;/h3&gt;

&lt;p&gt;Also known as an inner query or a nested query, a subquery is a query embedded within another SQL query.&lt;/p&gt;

&lt;p&gt;Subqueries are typically found nested inside standard SQL clauses such as &lt;code&gt;SELECT&lt;/code&gt;, &lt;code&gt;FROM&lt;/code&gt;, &lt;code&gt;WHERE&lt;/code&gt;, or &lt;code&gt;HAVING&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;SQL follows a strict order of execution:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The innermost subquery is always executed first. Its result is then passed on to be used by the outer query (the containing query).&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Subqueries allow you to use a single query rather than multiple queries, making your code more flexible and dynamic. They are categorized either by their output or by execution.&lt;/p&gt;

&lt;h4&gt; Subqueries Categorized by Output &lt;/h4&gt;

&lt;p&gt;One way to understand subqueries is by looking at the result they return.&lt;br&gt;
A subquery can return a single value, a list of values, or an entire table, depending on the query.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;h5&gt;&lt;em&gt; Scalar Subquery&lt;/em&gt;&lt;/h5&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Returns exactly one value (one row, one column).&lt;/p&gt;

&lt;p&gt;Scalar subqueries are used with single value comparison operators; &lt;strong&gt;=, &amp;lt;, &amp;gt;, &amp;lt;= &amp;amp; &amp;gt;=&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example: &lt;em&gt;"Which students scored above the class average on the math test?"&lt;/em&gt; requires a &lt;em&gt;scalar subquery&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Assuming the class average is 80.)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
               &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student_name&lt;/th&gt;
&lt;th&gt;score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Awino Judy&lt;/td&gt;
&lt;td&gt;95&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wanjiku   Stacy&lt;/td&gt;
&lt;td&gt;88&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kiprop  Bryan&lt;/td&gt;
&lt;td&gt;82&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Note: The scalar subquery &lt;code&gt;SELECT AVG(score) FROM students&lt;/code&gt; &lt;strong&gt;returns a single value&lt;/strong&gt; (80), then the outer query uses this value to filter students who scored above the class average&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;h5&gt;&lt;em&gt;Multi-row Subquery (List Subquery)&lt;/em&gt;&lt;/h5&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Returns a column of values, i.e. multiple rows but one column.&lt;br&gt;
It is often used with &lt;code&gt;IN&lt;/code&gt; and &lt;code&gt;NOT IN&lt;/code&gt; operators to filter results against a list of values.&lt;/p&gt;

&lt;p&gt;For example: "Which students registered for the math exam?" requires a &lt;em&gt;multi-row subquery&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;student_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt; 
&lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt; 
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;exam_registrations&lt;/span&gt; 
  &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;exam_subject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Math'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student_id&lt;/th&gt;
&lt;th&gt;student_name&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;Wanjiku Stacy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Kiprop  Bryan&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Awino  Judy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Mutua  Henry&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The list subquery first generates a list of student IDs from the exam_registrations table, then the outer query uses &lt;code&gt;IN&lt;/code&gt; to filter the students table against that list.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;h5&gt;&lt;em&gt;Table Subquery (Derived Table)&lt;/em&gt;&lt;/h5&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Returns multiple rows and columns, acting like a temporary table.&lt;br&gt;
This subquery is used in the &lt;code&gt;FROM&lt;/code&gt; clause, and must always have an alias.&lt;/p&gt;

&lt;p&gt;For example: &lt;em&gt;"Which students scored more than 160 total points in all 3 math tests?"&lt;/em&gt;  requires a  &lt;em&gt;table subquery&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total_score&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; 
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_score&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;exam_results&lt;/span&gt;
  &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;student_totals&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;total_score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;160&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student_name&lt;/th&gt;
&lt;th&gt;total_score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Awino   Nancy&lt;/td&gt;
&lt;td&gt;185&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wanjiku Stacy&lt;/td&gt;
&lt;td&gt;170&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kiprop  Bryan&lt;/td&gt;
&lt;td&gt;165&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The table subquery ran first to calculate everyone's total score, creating a temporary &lt;strong&gt;virtual table&lt;/strong&gt; which we aliased &lt;strong&gt;(student_totals)&lt;/strong&gt;. The outer query used this table to filter the results to get the students with more than 160 marks.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h4&gt; Subqueries Categorized by Execution &lt;/h4&gt;

&lt;p&gt;Subqueries can also be categorized based on how they work with the rest of the query. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;h4&gt;&lt;em&gt;Correlated Subquery&lt;/em&gt;&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;A correlated subquery is &lt;strong&gt;dependent&lt;/strong&gt; on data from the outer query. &lt;br&gt;
It runs for every row in the outer query, using values from the outer query to filter its result.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;/em&gt;&lt;/p&gt;&lt;h4&gt;&lt;em&gt;Uncorrelated (Independent) Subquery&lt;/em&gt;&lt;/h4&gt;&lt;p&gt;&lt;/p&gt;

&lt;p&gt;An uncorrelated subquery runs once, and is fully &lt;strong&gt;independent&lt;/strong&gt; of the outer query. &lt;br&gt;
This means that it evaluates its logic exactly &lt;strong&gt;once&lt;/strong&gt;, gets its result, and simply hands that result over to the main query to complete the task.&lt;/p&gt;

&lt;p&gt;If you're ever uncertain as to whether a subquery is correlated (dependent) or uncorrelated (independent), you can use the &lt;strong&gt;&lt;em&gt;highlight test&lt;/em&gt;&lt;/strong&gt; on your SQL editor.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Highlight &lt;strong&gt;just&lt;/strong&gt; the subquery and run it:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If it returns a result: It is an uncorrelated subquery. It can stand on its own.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If it throws an error: It is a correlated subquery. It depends on information from the outer query to make sense and run correctly.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;When to use Subqueries&lt;/h3&gt;

&lt;p&gt;Subqueries are useful when; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Calculating metrics like averages or totals on the fly, without hardcoding numbers.&lt;/li&gt;
&lt;li&gt;Filtering results using a dynamically generated list of values i.e. (List subqueries).&lt;/li&gt;
&lt;li&gt;Using row-specific logic&lt;/li&gt;
&lt;li&gt;There's need to aggregate data before filtering.&lt;/li&gt;
&lt;li&gt;Working with older legacy databases that don't support more modern features.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Subqueries are excellent for simple logic, but when they are deeply nested, they become difficult to read and more difficult to maintain. This is where Common Table Expressions (CTEs) come in.&lt;/p&gt;




&lt;h3&gt; PART 2: The CTE (COMMON TABLE EXPRESSION)&lt;a&gt;&lt;/a&gt; &lt;/h3&gt;

&lt;p&gt;A CTE is a temporary, named result set defined at the top of a query using the &lt;code&gt;WITH&lt;/code&gt; clause, that exists only for the duration of your query.&lt;/p&gt;

&lt;p&gt;Once it is defined, you can query and reference the CTE as you would a normal table in your database.&lt;/p&gt;

&lt;p&gt;Because a CTE is defined at the top of your script, it allows you to read the code logically from &lt;strong&gt;top-to-bottom&lt;/strong&gt; &lt;/p&gt;

&lt;h5&gt;The Basic Syntax of a CTE &lt;/h5&gt; 

&lt;p&gt;No matter how complex a query gets, almost every CTE follows this simple, two-part structure:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Define&lt;/strong&gt; the CTE using the &lt;code&gt;WITH&lt;/code&gt; clause at the top.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use&lt;/strong&gt; the CTE in your main query. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Syntax;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;-- Step 1: Write your query here. &lt;/span&gt;
    &lt;span class="c1"&gt;-- This creates your temporary, virtual table.&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column2&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;table1&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;-- Step 2: Write your main query here, reading from the CTE!&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column1&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we know the basic syntax, we can dive into the two types of CTEs commonly encountered: Recursive and Non-Recursive &lt;em&gt;(based on whether or not they reference themselves).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Non-Recursive CTEs&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
They are temporary result sets that &lt;em&gt;&lt;strong&gt;run once and don’t reference themselves&lt;/strong&gt;&lt;/em&gt;. &lt;br&gt;
They’re commonly used for breaking complex queries into modular blocks.&lt;/p&gt;

&lt;p&gt;The CTE syntax is;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="c1"&gt;-- Your SQL logic here (SELECT, JOIN, GROUP BY, etc.)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Replacing nested subqueries for better readability and reusability.&lt;/p&gt;

&lt;p&gt;Earlier when we wanted to find &lt;em&gt;students who scored a total of more than 160 points across all their maths tests&lt;/em&gt;, We solved it using a &lt;em&gt;Table Subquery&lt;/em&gt; in the &lt;code&gt;FROM&lt;/code&gt; clause.&lt;/p&gt;

&lt;p&gt;Let's try the exact same question, using a CTE.&lt;br&gt;
&lt;em&gt;"Which students scored more than 160 total points in all 3 math tests?"&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;student_totals&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_score&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;exam_scores&lt;/span&gt;
  &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;student_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total_score&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;student_totals&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;total_score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;160&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student_name&lt;/th&gt;
&lt;th&gt;total_score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Awino  Nancy&lt;/td&gt;
&lt;td&gt;185&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wanjiku Stacy&lt;/td&gt;
&lt;td&gt;170&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kiprop  Bryan&lt;/td&gt;
&lt;td&gt;165&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This CTE achieves the same result as our Table Subquery Example, in a cleaner, more readable way.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Recursive CTEs&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Recursive CTEs are temporary result sets that reference themselves to explore and map out hierarchical (tree-like) data (e.g., organizational charts, category trees).&lt;br&gt;
 Syntax;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="c1"&gt;-- Base case: Start with the root of the hierarchy&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column2&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;parent_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
  &lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
  &lt;span class="c1"&gt;-- Recursive case: Reference the CTE itself to traverse child rows&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;column2&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
  &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parent_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt; Exploring hierarchical or tree structured data (e.g., reporting structures or category hierarchies).&lt;/p&gt;

&lt;p&gt;For example: &lt;em&gt;"Alice is a manager at a Nairobi tech startup. Who are the employees in her team hierarchy?"&lt;/em&gt; requires a recursive CTE.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&lt;/span&gt; &lt;span class="n"&gt;team_hierarchy&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Alice'&lt;/span&gt;
  &lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
  &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;team_hierarchy&lt;/span&gt; &lt;span class="n"&gt;th&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;th&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;team_hierarchy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;employee_id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;manager_id&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;Alice&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Diana&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This result shows Alice, her direct reports (Bob, Charlie), and Bob’s direct reports (Diana, Eve)—the full team hierarchy.&lt;/em&gt;&lt;br&gt;
&lt;em&gt;The line &lt;code&gt;JOIN team_hierarchy th ON e.manager_id = th.employee_id&lt;/code&gt; is where the recursion happens.&lt;/em&gt; &lt;br&gt;
&lt;em&gt;&lt;code&gt;team_hierarchy&lt;/code&gt; is the CTE itself, we are joining the query to its own result. It takes every employee already in the CTE (e.g., Alice) and finds all employees who report to them. This repeats in a loop until no more reports are found&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;When to use CTEs&lt;/h3&gt;

&lt;p&gt;Subqueries are useful when; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A query has multiple logical steps&lt;/li&gt;
&lt;li&gt;You need to use the same data multiple times&lt;/li&gt;
&lt;li&gt;Querying hierarchical data&lt;/li&gt;
&lt;li&gt;You temporarily want to replace a view.&lt;/li&gt;
&lt;li&gt;When replacing nested subqueries.&lt;/li&gt;
&lt;/ul&gt;





&lt;h3&gt;PART 3: SUBQUERIES VS CTEs&lt;a&gt;&lt;/a&gt; &lt;/h3&gt;

&lt;p&gt;Here is a table comparison of subqueries and CTEs, based on architecture, readability and performance.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Subqueries&lt;/th&gt;
&lt;th&gt;CTEs&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Readability &amp;amp; Flow&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Reads &lt;strong&gt;inside-out&lt;/strong&gt;. Prone to creating "spaghetti code" if nested multiple times.&lt;/td&gt;
&lt;td&gt;Reads &lt;strong&gt;top-to-bottom&lt;/strong&gt;. Keeps code clean and highly modular.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Reusability&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Single-use only.&lt;/strong&gt; If you need the same data twice, you have to type the whole subquery again.&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;Highly reusable.&lt;/strong&gt; Can be referenced multiple times in the main query&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Recursion&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Cannot handle self-referencing or hierarchical data.&lt;/td&gt;
&lt;td&gt;Perfect for hierarchical/tree-structured data using &lt;strong&gt;Recursive CTEs&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Fast for basic filters. &lt;strong&gt;Correlated Subqueries&lt;/strong&gt; can be slow on large datasets because they loop row-by-row.&lt;/td&gt;
&lt;td&gt;Generally the &lt;strong&gt;same performance&lt;/strong&gt; (optimizers treat standard CTEs and subqueries equally). However, CTEs can be temporarily cached in memory in databases like PostgreSQL for speed boosts.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Best Use Case&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Quick, single-step math (Scalar), checking lists (&lt;code&gt;IN&lt;/code&gt; / &lt;code&gt;EXISTS&lt;/code&gt;), or creating simple derived tables.&lt;/td&gt;
&lt;td&gt;Multi-step data transformations, chaining queries, building org charts, or acting as temporary views.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;





&lt;h3&gt;&lt;/h3&gt;
&lt;br&gt;
SQL is not just about writing queries, it’s about writing queries that others can easily understand.

&lt;p&gt;When in doubt, choose the option that is easiest for the next developer to understand. &lt;/p&gt;

&lt;p&gt;If you only remember one thing from this guide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Subqueries&lt;/strong&gt; for quick, single-step tasks and simple filters.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;CTEs&lt;/strong&gt; for multi-step queries, reusability, and keeping your code readable from top to bottom.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;SQL is all about practice. Don’t get discouraged if subqueries and CTEs take a few tries to master, with consistent practice, they get and feel more intuitive.&lt;/p&gt;

&lt;p&gt;If you found this guide helpful, don't forget to &lt;strong&gt;save it for later&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Do you prefer using subqueries or CTEs in your work?&lt;/em&gt;&lt;br&gt;
Let me know in the comments below. Happy querying! &lt;/p&gt;

</description>
      <category>sql</category>
      <category>learning</category>
    </item>
    <item>
      <title>📘SQL JOINs Explained Simply - A Beginner's Guide</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Sun, 19 Apr 2026 03:47:31 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/sql-joins-explained-simply-a-beginners-guide-5edg</link>
      <guid>https://dev.to/sharonnyabuto/sql-joins-explained-simply-a-beginners-guide-5edg</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;SQL JOINs are used to &lt;strong&gt;combine data from two or more tables into a single result based on a shared column&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;They allow you to retrieve related data that is stored separately. &lt;br&gt;
For example, a database might have &lt;em&gt;customer names&lt;/em&gt; in one table and &lt;em&gt;purchase details&lt;/em&gt; in another. &lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Join&lt;/strong&gt; bridges the tables, giving insights that one table alone may not provide.&lt;/p&gt;

&lt;p&gt;In this article, we’ll break down the different types of SQL JOINs and when to use them. Specifically, we’ll cover:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;INNER JOIN&lt;/li&gt;
&lt;li&gt;LEFT JOIN&lt;/li&gt;
&lt;li&gt;RIGHT JOIN&lt;/li&gt;
&lt;li&gt;FULL OUTER JOIN&lt;/li&gt;
&lt;li&gt;CROSS JOIN&lt;/li&gt;
&lt;li&gt;SELF JOIN&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  🔗 INNER JOIN
&lt;/h2&gt;

&lt;p&gt;An &lt;code&gt;INNER JOIN&lt;/code&gt; combines records based on a related column and returns &lt;strong&gt;only matching rows&lt;/strong&gt; from both tables. &lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; columns&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; table1&lt;br&gt;
&lt;strong&gt;INNER JOIN&lt;/strong&gt; table2&lt;br&gt;
&lt;strong&gt;ON&lt;/strong&gt; table1.column = table2.column;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Example; If the &lt;em&gt;customers table&lt;/em&gt; and the &lt;em&gt;orders table&lt;/em&gt; share a column - the &lt;em&gt;customer_id,&lt;/em&gt; an &lt;code&gt;INNER JOIN&lt;/code&gt; is used to identify the customers who made orders;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;customer_id&lt;/th&gt;
&lt;th&gt;first_name&lt;/th&gt;
&lt;th&gt;last_name&lt;/th&gt;
&lt;th&gt;order_id&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;john&lt;/td&gt;
&lt;td&gt;doe&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;mary&lt;/td&gt;
&lt;td&gt;wanjiku&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;peter&lt;/td&gt;
&lt;td&gt;otieno&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;doe&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;lucy&lt;/td&gt;
&lt;td&gt;njeri&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;An &lt;code&gt;INNER JOIN&lt;/code&gt; can be used to combine more than two tables. &lt;/p&gt;

&lt;p&gt;For example, to find out the customers, books they purchased and the corresponding order IDs;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;th&gt;first_name&lt;/th&gt;
&lt;th&gt;last_name&lt;/th&gt;
&lt;th&gt;title&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;john&lt;/td&gt;
&lt;td&gt;doe&lt;/td&gt;
&lt;td&gt;Learning SQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;mary&lt;/td&gt;
&lt;td&gt;wanjiku&lt;/td&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;peter&lt;/td&gt;
&lt;td&gt;otieno&lt;/td&gt;
&lt;td&gt;Python for Data Science&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;doe&lt;/td&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;lucy&lt;/td&gt;
&lt;td&gt;njeri&lt;/td&gt;
&lt;td&gt;Advanced SQL Queries&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  ⬅️ LEFT JOIN/LEFT OUTER JOIN
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;LEFT JOIN&lt;/code&gt; returns &lt;strong&gt;all records from the left&lt;/strong&gt; table, and the &lt;strong&gt;matching records from the right&lt;/strong&gt; table. Where no match exists, a &lt;strong&gt;NULL&lt;/strong&gt; value is returned.&lt;/p&gt;

&lt;p&gt;In a &lt;code&gt;LEFT JOIN&lt;/code&gt;, the table specified in the &lt;code&gt;FROM&lt;/code&gt; clause is treated as the left table.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; columns&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; table1&lt;br&gt;
&lt;strong&gt;LEFT JOIN&lt;/strong&gt; table2&lt;br&gt;
&lt;strong&gt;ON&lt;/strong&gt; table1.column = table2.column;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To show all the books and the customers who ordered them, a &lt;code&gt;LEFT JOIN&lt;/code&gt; is used;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt; 
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;title&lt;/th&gt;
&lt;th&gt;first_name&lt;/th&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Learning SQL&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;td&gt;mary&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Python for Data Science&lt;/td&gt;
&lt;td&gt;peter&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;td&gt;john&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Advanced SQL Queries&lt;/td&gt;
&lt;td&gt;lucy&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Machine Learning Intro&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Notice that the row with &lt;strong&gt;Machine Learning Intro&lt;/strong&gt; has &lt;code&gt;NULL&lt;/code&gt; values because it was not ordered.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  ➡️ RIGHT JOIN/RIGHT OUTER JOIN
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;RIGHT JOIN&lt;/code&gt; works like the &lt;code&gt;LEFT JOIN&lt;/code&gt;, but it keeps &lt;strong&gt;all the rows from the right&lt;/strong&gt; table and &lt;strong&gt;matching rows from the left&lt;/strong&gt; table.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; columns&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; table1&lt;br&gt;
&lt;strong&gt;RIGHT JOIN&lt;/strong&gt; table2&lt;br&gt;
&lt;strong&gt;ON&lt;/strong&gt; table1.column = table2.column;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To show every book in the library, whether it was ordered or not;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;RIGHT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;book_id&lt;/th&gt;
&lt;th&gt;title&lt;/th&gt;
&lt;th&gt;order_id&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;Learning SQL&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Python for Data Science&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Advanced SQL Queries&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Machine Learning Intro&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Note:&lt;/strong&gt; For both LEFT and RIGHT outer joins, the order in which you write your tables determines which one returns all of its values. You need to be keen about which table you put on the left and which goes on the right to get your intended result.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🔄 FULL JOIN/FULL OUTER JOIN
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;FULL JOIN&lt;/code&gt; returns all rows from both tables.&lt;br&gt;
As is the case in both the right and Left outer joins, for the rows that do not have a match, &lt;code&gt;NULL&lt;/code&gt; is returned.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; columns&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; table1&lt;br&gt;
&lt;strong&gt;FULL OUTER JOIN&lt;/strong&gt; table2&lt;br&gt;
&lt;strong&gt;ON&lt;/strong&gt; table1.column = table2.column;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To retrieve all the customers and all the books, whether ordered or not, a &lt;code&gt;FULL JOIN&lt;/code&gt; will be used;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; 
&lt;span class="k"&gt;FULL&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;  
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; 
&lt;span class="k"&gt;FULL&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;book_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;th&gt;first_name&lt;/th&gt;
&lt;th&gt;last_name&lt;/th&gt;
&lt;th&gt;title&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;john&lt;/td&gt;
&lt;td&gt;doe&lt;/td&gt;
&lt;td&gt;Learning SQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;peter&lt;/td&gt;
&lt;td&gt;otieno&lt;/td&gt;
&lt;td&gt;Python for Data Science&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;lucy&lt;/td&gt;
&lt;td&gt;njeri&lt;/td&gt;
&lt;td&gt;Advanced SQL Queries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;mary&lt;/td&gt;
&lt;td&gt;wanjiku&lt;/td&gt;
&lt;td&gt;Data Analytics Basics&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;david&lt;/td&gt;
&lt;td&gt;kimani&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;Machine Learning Intro&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To retrieve only the matched or un-matched records, you can filter with the &lt;code&gt;WHERE&lt;/code&gt; clause.&lt;/p&gt;




&lt;h2&gt;
  
  
  ❌ CROSS JOIN
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;CROSS JOIN&lt;/code&gt; returns the combination of each row from one table with each row from another table. &lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; *&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; table1&lt;br&gt;
&lt;strong&gt;CROSS JOIN&lt;/strong&gt; table2;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A &lt;code&gt;CROSS JOIN&lt;/code&gt; does not have an &lt;code&gt;ON&lt;/code&gt; clause, because we are not looking for matching rows.&lt;/p&gt;

&lt;p&gt;If you have 2 tables; t-shirts color and t-shirt size tables, and wanted to find all possible combinations, a &lt;code&gt;CROSS JOIN&lt;/code&gt; is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;tshirt_colors&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="n"&gt;tshirt_sizes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;tshirt_colors&lt;/span&gt;
&lt;span class="k"&gt;CROSS&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;tshirt_sizes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;color_name&lt;/th&gt;
&lt;th&gt;size_name&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Black&lt;/td&gt;
&lt;td&gt;Small&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Black&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Black&lt;/td&gt;
&lt;td&gt;Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Black&lt;/td&gt;
&lt;td&gt;Extra Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;White&lt;/td&gt;
&lt;td&gt;Small&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;White&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;White&lt;/td&gt;
&lt;td&gt;Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;White&lt;/td&gt;
&lt;td&gt;Extra Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Navy&lt;/td&gt;
&lt;td&gt;Small&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Navy&lt;/td&gt;
&lt;td&gt;Medium&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Navy&lt;/td&gt;
&lt;td&gt;Large&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Navy&lt;/td&gt;
&lt;td&gt;Extra Large&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🔁 SELF JOIN
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;SELF JOIN&lt;/code&gt; is used when comparing rows within the same table. &lt;/p&gt;

&lt;p&gt;When using self joins, it is essential to use &lt;strong&gt;Aliases&lt;/strong&gt;, to distinguish the two "roles" of the same table for exact execution. &lt;br&gt;
&lt;em&gt;AS&lt;/em&gt; is used to introduce an alias&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SELECT&lt;/strong&gt; column name (s) as &lt;em&gt;alias&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;FROM&lt;/strong&gt; employees A&lt;br&gt;
&lt;strong&gt;JOIN&lt;/strong&gt; employees B &lt;br&gt;
&lt;strong&gt;ON&lt;/strong&gt; A.column = B.column;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To best illustrate self joins, we will use the &lt;em&gt;employees&lt;/em&gt; table below;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;employee_id&lt;/th&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;department_id&lt;/th&gt;
&lt;th&gt;manager_id&lt;/th&gt;
&lt;th&gt;salary&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;Alice&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;50000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;45000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;47000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Diana&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;60000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;40000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Brian&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;50000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Joy&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;35000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Luke&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;45000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To show employees and their managers, we use a &lt;code&gt;SELF JOIN&lt;/code&gt; as follows;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;manager&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; 
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;employee&lt;/th&gt;
&lt;th&gt;manager&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Brian&lt;/td&gt;
&lt;td&gt;Diana&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Joy&lt;/td&gt;
&lt;td&gt;Diana&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Luke&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;p&gt;I hope this guide made joins less intimidating! &lt;br&gt;
If you found this beginner's guide helpful, &lt;strong&gt;save it for later&lt;/strong&gt; or &lt;strong&gt;share&lt;/strong&gt; it with a friend who's just starting their SQL journey!  Happy learning!!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Are there any joins I left out? Do you have any join tricks you want to share?&lt;/em&gt;&lt;br&gt;
Let me know in the comment section below, I’d love to hear from you!&lt;/p&gt;

</description>
      <category>sql</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Applying SQL Concepts: DDL, DML, Filtering, and Data Transformation</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Sun, 12 Apr 2026 18:25:54 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/applying-sql-concepts-ddl-dml-filtering-and-data-transformation-30p7</link>
      <guid>https://dev.to/sharonnyabuto/applying-sql-concepts-ddl-dml-filtering-and-data-transformation-30p7</guid>
      <description>&lt;p&gt;&lt;strong&gt;Structured Query Language (SQL)&lt;/strong&gt; is the standard language used to interact with relational databases. &lt;br&gt;
SQL is a command type language. Whether you want to create, delete, update or read data, SQL provides commands to perform these operations. &lt;br&gt;
SQL commands are categorized based on their &lt;strong&gt;specific functionalities&lt;/strong&gt;, and allow for the creation, manipulation, retrieval and control of data and database structures.&lt;br&gt;
Among the command categories include; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data Definition Language (DDL)&lt;/li&gt;
&lt;li&gt;Data Manipulation Language (DML)&lt;/li&gt;
&lt;li&gt;Data Query Language (DQL)&lt;/li&gt;
&lt;li&gt;Data Control Language (DCL)&lt;/li&gt;
&lt;li&gt;Transaction Control Language (TCL)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we will look at Data Definition Language (DDL) and Data Manipulation Language (DML).&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;DDL (DATA DEFINITION LANGUAGE)&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Data Definition Language (DDL) is a set of commands used to &lt;strong&gt;define&lt;/strong&gt; and &lt;strong&gt;manage&lt;/strong&gt; the &lt;em&gt;structure&lt;/em&gt; of a database. &lt;br&gt;
It focuses on creating and modifying database objects such as tables, schemas, and indexes. &lt;br&gt;
DDL uses commands like &lt;strong&gt;&lt;em&gt;CREATE,&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;ALTER,&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;DROP,&lt;/em&gt;&lt;/strong&gt; to determine how data is organized and stored within the database. &lt;br&gt;
These commands affect the &lt;strong&gt;structure&lt;/strong&gt; of the database and not the data itself. &lt;br&gt;
DDL operations are often automatically committed, therefore changes take effect immediately without needing a manual &lt;em&gt;COMMIT&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;DML (DATA MANIPULATION LANGUAGE)&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Data Manipulation Language (DDL) is a set of SQL commands used to &lt;strong&gt;manage&lt;/strong&gt; and &lt;strong&gt;manipulate&lt;/strong&gt; the &lt;em&gt;data _stored within database tables. &lt;br&gt;
Unlike DDL, which focuses on defining the structure of the database, DML is concerned with performing operations on the &lt;strong&gt;data&lt;/strong&gt; itself. &lt;br&gt;
Common DML commands include &lt;strong&gt;INSERT,&lt;/strong&gt;&lt;/em&gt; &lt;strong&gt;&lt;em&gt;UPDATE&lt;/em&gt;,&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;DELETE&lt;/em&gt;,&lt;/strong&gt; which are used to add new records, modify existing data, and remove records from a table respectively. &lt;br&gt;
In most database systems, DML changes are not automatically committed, meaning they can be rolled back if necessary before being finalized.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Below are some SQL commands and how they are used;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;CREATE&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
DDL command used to create an object in a database, e.g a table&lt;/p&gt;

&lt;p&gt;The syntax; &lt;br&gt;
CREATE TABLE table_name(&lt;br&gt;
column1 datatype constraints,&lt;br&gt;
column2 datatype constraints,&lt;br&gt;
column3 datatype constraints&lt;br&gt;
);&lt;br&gt;
For example, to create a schema in a database for a school, and add a table called &lt;em&gt;students&lt;/em&gt; in the Schema you use &lt;em&gt;CREATE&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;primary&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;gender&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="n"&gt;date_of_birth&lt;/span&gt; &lt;span class="nb"&gt;DATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
&lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F2cnzby37arrc0hxvlm0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F2cnzby37arrc0hxvlm0l.png" alt="Table _students_ Created" width="608" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;INSERT&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This command is used to add a new record in a database.&lt;/p&gt;

&lt;p&gt;The syntax;&lt;br&gt;
INSERT INTO table_name (column1, column2, column3)&lt;br&gt;
VALUES &lt;br&gt;
(value1, value2, value3),&lt;br&gt;
(value1, value2, value3);&lt;/p&gt;

&lt;p&gt;To add student details to the students table created above, you use &lt;em&gt;INSERT&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;insert&lt;/span&gt; &lt;span class="k"&gt;into&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;gender&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;date_of_birth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;values&lt;/span&gt; 
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Amina'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Wanjiku'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'F'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2008-03-12'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Form 3'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Nairobi'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Brian'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Ochieng'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'M'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2007-07-25'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Form 4'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Mombasa'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Cynthia'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Mutua'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'F'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2008-11-05'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Form 3'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Kisumu'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'David'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Kamau'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'M'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2007-02-18'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Form 4'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Nairobi'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Esther'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Akinyi'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'F'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'2009-06-30'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Form 2'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Nakuru'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fl5oca738folscqftezfe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fl5oca738folscqftezfe.png" alt="_students_ table populated with student information" width="649" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
The update command is used to modify existing records in a database. &lt;/p&gt;

&lt;p&gt;The syntax;&lt;br&gt;
UPDATE table_name&lt;br&gt;
SET column1 = value1,&lt;br&gt;
WHERE condition;&lt;/p&gt;

&lt;p&gt;In the students table created, if student id number 2, moved to Nairobi from Mombasa, &lt;em&gt;UPDATE&lt;/em&gt; is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;update&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
&lt;span class="k"&gt;set&lt;/span&gt; &lt;span class="n"&gt;City&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Nairobi'&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fv7b6tqzq5dhlhtegy1cf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fv7b6tqzq5dhlhtegy1cf.png" alt="Result after city update" width="612" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;DELETE&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This deletes one or more records from a database object. &lt;br&gt;
It is used together with the _WHERE _clause to ensure that only specific rows are deleted.&lt;/p&gt;

&lt;p&gt;The syntax;&lt;/p&gt;

&lt;p&gt;DELETE FROM table_name&lt;br&gt;
WHERE condition;&lt;/p&gt;

&lt;p&gt;In the students table, if student_id 4 is no longer a student there, his records are removed using &lt;em&gt;DELETE&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;delete&lt;/span&gt; 
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;
 &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Ftbq8aw39rovam6uyq494.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftbq8aw39rovam6uyq494.png" alt="_students_ table with one record removed" width="613" height="140"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;u&gt;FILTERING WITH WHERE&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;WHERE&lt;/strong&gt; clause filters rows based on one or more conditions, so your query only modifies the records that match. &lt;br&gt;
It is often used across &lt;strong&gt;SELECT,&lt;/strong&gt; &lt;strong&gt;UPDATE,&lt;/strong&gt; and &lt;strong&gt;DELETE&lt;/strong&gt; statements.&lt;/p&gt;

&lt;p&gt;The Where clause is used with logical and comparison operators such as equal to &lt;strong&gt;(=)&lt;/strong&gt;, greater than &lt;strong&gt;(&amp;gt;)&lt;/strong&gt; and less than &lt;strong&gt;(&amp;lt;)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It can also be used with other operators including;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BETWEEN&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Where clause is used with the &lt;em&gt;BETWEEN&lt;/em&gt; operator, to filter records within a specified range.&lt;br&gt;
When using between, the result will include both the start and end values of the range.&lt;/p&gt;

&lt;p&gt;For example, to find students that scored between 55 and 95 marks from a results table, the values returned will include &lt;em&gt;55&lt;/em&gt; and &lt;em&gt;95&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;result_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;subject_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;grade&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;exam_results&lt;/span&gt; 
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="k"&gt;between&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F3xk2s9wghusj0dpcyw3m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3xk2s9wghusj0dpcyw3m.png" alt="Where clause used with Between " width="646" height="175"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;IN&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;WHERE&lt;/strong&gt; clause can be used with the &lt;em&gt;IN&lt;/em&gt; operator to filter records based on multiple values. &lt;br&gt;
It makes queries shorter and easier to understand, instead of writing multiple conditions using the (=) operator combined with OR.&lt;/p&gt;

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

&lt;p&gt;SELECT column1, column2&lt;br&gt;
FROM table_name&lt;br&gt;
WHERE column_name IN (value1, value2, value3);&lt;/p&gt;

&lt;p&gt;To find students from different cities in our students table, we use &lt;em&gt;IN&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; 
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Nairobi'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Nakuru'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Mombasa'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fxfuhnzi5d2s6y36vrbe4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxfuhnzi5d2s6y36vrbe4.png" alt="IN operator filtering our different cities" width="800" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LIKE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WHERE&lt;/strong&gt; clause is used with the &lt;strong&gt;LIKE&lt;/strong&gt; operator when searching for a particular pattern. &lt;br&gt;
When using &lt;em&gt;LIKE&lt;/em&gt;, a wildcard is used; &lt;br&gt;
&lt;strong&gt;‘%’&lt;/strong&gt;- signifies any number of characters including 0.&lt;br&gt;
 &lt;strong&gt;‘_’&lt;/strong&gt; wildcard is used for a specific number of characters.&lt;/p&gt;

&lt;p&gt;Syntax is;&lt;br&gt;
SELECT column_name&lt;br&gt;
FROM table_name&lt;br&gt;
WHERE column_name LIKE 'pattern';&lt;/p&gt;

&lt;p&gt;For example, to find the list of subjects that contain the word 'studies' in a subjects table, &lt;em&gt;LIKE&lt;/em&gt; is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;subject_name&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;subjects&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;subject_name&lt;/span&gt; &lt;span class="k"&gt;like&lt;/span&gt; &lt;span class="s1"&gt;'%Studies%'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F5udywjq3i6e2u04uq0fc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5udywjq3i6e2u04uq0fc.png" alt="Filtering using _Where_ clause and Like" width="800" height="153"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  CASE WHEN STATEMENT
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;CASE WHEN&lt;/strong&gt; statement is used to add conditional logic inside queries, similar to an &lt;em&gt;if-else&lt;/em&gt; structure.&lt;br&gt;
It checks conditions one by one and returns a value as soon as a matching condition is found.&lt;br&gt;
It helps categorize data dynamically and allows creation of new categories based on the specific conditions within a query.&lt;/p&gt;

&lt;p&gt;The syntax is;&lt;/p&gt;

&lt;p&gt;select column name, &lt;br&gt;
   case&lt;br&gt;
     when condition1 then 'result 1'&lt;br&gt;
     when condition2 then 'result 2'&lt;br&gt;
    else 'default result&lt;br&gt;
 end as new column_name &lt;/p&gt;

&lt;p&gt;For example, &lt;strong&gt;CASE WHEN&lt;/strong&gt; can be used to group marks into a new column called Performance, in the results table, as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;result_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s1"&gt;'Distinction'&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s1"&gt;'Merit'&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="n"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s1"&gt;'Pass'&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s1"&gt;'Fail'&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;Performance&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;exam_results&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fey0dt701fcwyrux0jkjq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fey0dt701fcwyrux0jkjq.png" alt="Case When statement" width="650" height="171"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  REFLECTION
&lt;/h2&gt;

&lt;p&gt;Learning Databases and SQL can seem difficult, but once you get into it, it becomes interesting especially if you are curious to see how you can create and modify your database along the way. &lt;/p&gt;

&lt;p&gt;The most interesting thing with SQL is that there are some rules for consistency and functionality across databases. Some of those rules are; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Always end your statements with a semicolon (;) &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SQL commands and key words such as SELECT and INSERT are not case-sensitive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Spaces and new lines are allowed for better readability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do not use SQL keywords as names, if you have to, write them in quotes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To include comments in your script, begin single line comments with --- and for multi-line comments you use /* at the beginning and */ at the end.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Commands like DROP and DELETE should be used with caution as they could result in permanent data loss without proper use of transactions or backups.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>sql</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>A Comprehensive Guide to Publishing and Embedding Power BI Reports on the Web with IFrames</title>
      <dc:creator>Sharon-nyabuto</dc:creator>
      <pubDate>Mon, 06 Apr 2026 23:25:01 +0000</pubDate>
      <link>https://dev.to/sharonnyabuto/a-comprehensive-guide-to-publishing-and-embedding-power-bi-reports-on-the-web-with-iframes-1004</link>
      <guid>https://dev.to/sharonnyabuto/a-comprehensive-guide-to-publishing-and-embedding-power-bi-reports-on-the-web-with-iframes-1004</guid>
      <description>&lt;p&gt;Microsoft PowerBI is one powerful tool when it comes to &lt;strong&gt;analyzing&lt;/strong&gt; and &lt;strong&gt;visualizing&lt;/strong&gt; data.&lt;/p&gt;

&lt;p&gt;Power BI allows users to connect to different data sources, transform data, build interactive dashboards and share insights with others.&lt;/p&gt;

&lt;p&gt;I have spent the last two weeks learning how to build queries, clean data on power query, use DAX functions, data modelling and relationships, as well as building dashboards and reports in Power BI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reports and dashboards in Power BI&lt;/strong&gt;&lt;br&gt;
Reports and dashboards are important for visualizing data. &lt;/p&gt;

&lt;p&gt;While &lt;strong&gt;dashboards **are often a one-page summary of key insights, **reports&lt;/strong&gt; are multiple pages and with a detailed analysis.&lt;br&gt;
In the day to day operations, reports have to be shared with managers, teams and other stakeholders.&lt;br&gt;
Instead of sending &lt;em&gt;‘.pbix’&lt;/em&gt; files, which require Power BI desktop, &lt;strong&gt;embedding reports&lt;/strong&gt; enables viewing them in browsers, interaction with filters and accessing live and updated data. &lt;/p&gt;

&lt;p&gt;Embedding reports is a necessary move from static reports, to interactive web-based dashboards, that no one has a challenge accessing.&lt;/p&gt;

&lt;p&gt;In this article, we will learn step by step how to;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a &lt;strong&gt;Workspace&lt;/strong&gt; in Power BI Services&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Upload&lt;/strong&gt; and &lt;strong&gt;Publish&lt;/strong&gt; a Power BI report from Power BI desktop&lt;/li&gt;
&lt;li&gt;Generate an &lt;strong&gt;Embed&lt;/strong&gt; code&lt;/li&gt;
&lt;li&gt;Embed the report on a web page using an &lt;strong&gt;iframe&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Brief Overview of Power BI and the Publishing Process&lt;/strong&gt;&lt;br&gt;
Power BI consists of two components; &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;- &lt;strong&gt;Power BI Desktop&lt;/strong&gt;– Where the data is loaded, cleaned and transformed, relationships and visuals are built and reports designed.&lt;/li&gt;
&lt;li&gt;- &lt;strong&gt;Power BI Service&lt;/strong&gt;- Where the reports are published, workspaces and embedded links are created, dashboards are shared and work is done in collaboration with others.&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  &lt;u&gt;Creating a Workspace in Power BI Services&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Workspace&lt;/strong&gt; is an &lt;em&gt;online folder&lt;/em&gt;, that is dedicated to &lt;em&gt;storing&lt;/em&gt; and &lt;em&gt;organizing&lt;/em&gt; your dashboards and reports.&lt;/p&gt;

&lt;p&gt;The default workspace is always ‘My Workspace’ which is often just for personal testing, and as a result, you cannot get a working public embed code if you generate your report here. Therefore, do &lt;strong&gt;NOT&lt;/strong&gt; use &lt;em&gt;My Workspace.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to create a Workspace step-by-step;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your browser and go to &lt;a href="https://app.powerbi.com" rel="noopener noreferrer"&gt;https://app.powerbi.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Log in using your Microsoft account&lt;/li&gt;
&lt;li&gt;On the left menu, click &lt;em&gt;Workspaces&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Click the &lt;em&gt;New Workspace&lt;/em&gt; button at the top right&lt;/li&gt;
&lt;li&gt;Enter a Workspace Name, &lt;em&gt;e.g. Electronics Sales&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;Apply&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fw5roapfy0tpefxa5r5e5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fw5roapfy0tpefxa5r5e5.png" alt="Creating a new Workspace" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once this is complete, your Workspace is ready. You can now publish your report.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;Uploading and publishing a Power BI report from Power BI desktop.&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Now we will upload the &lt;em&gt;.pbix&lt;/em&gt; file to the workspace we just created.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to publish a report to the Workspace step-by-step;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your report in Power BI desktop.&lt;/li&gt;
&lt;li&gt;On the top right corner of the home ribbon, click &lt;em&gt;‘Publish’&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Select the &lt;strong&gt;workspace&lt;/strong&gt; you just crated, &lt;em&gt;(e.g. Electronic Sales)&lt;/em&gt; from the list of Workspaces that Power BI will display.&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;Select&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Power BI will now upload your report. It may take a few seconds to 2 minutes, so give it some time. &lt;/li&gt;
&lt;li&gt;You will see a message that shows you have successfully published your report to Power BI Services.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fz5jb1cm9w6fzv0n21e5b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fz5jb1cm9w6fzv0n21e5b.png" alt="Notification for successful publishing" width="349" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Until you have confirmed that the report has been published, do not close your Power BI Desktop. &lt;br&gt;
If you close and there’s something wrong, you may have to start the process all over again.&lt;/p&gt;

&lt;p&gt;To confirm if the report has been uploaded to the Power BI Service;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go back to &lt;em&gt;&lt;a href="https://app.powerbi.com" rel="noopener noreferrer"&gt;https://app.powerbi.com&lt;/a&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Open your &lt;em&gt;Workspace&lt;/em&gt; and refresh. You should see your report&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fudn4rtowa9swvlia9fd5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fudn4rtowa9swvlia9fd5.png" alt="Uploaded Report on Workspace" width="769" height="265"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;u&gt;Generating the Embed Code.&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Embedding allows the report to be &lt;em&gt;displayed&lt;/em&gt; inside a website.&lt;br&gt;
Power BI provides an embed code, that can be copied and pasted in a website. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to generate a code, step-by-step;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your report in the Workspace.&lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;‘File’&lt;/em&gt; at the top of the page.&lt;/li&gt;
&lt;li&gt;Select &lt;em&gt;‘Embed Report’&lt;/em&gt;, then click on Website or portal&lt;/li&gt;
&lt;li&gt;Click on create &lt;em&gt;Embed code.&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Then click &lt;strong&gt;Confirm.&lt;/strong&gt; &lt;/li&gt;
&lt;li&gt;Copy the embed code to be used later.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You will now see a pop-up window with the link to embed, and a box with the embed code needed, this is the only correct place to get your embed code from.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fiepxd7vflh9sgio115ze.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fiepxd7vflh9sgio115ze.png" alt="Popup with the Iframe" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The embed code,  is an &lt;strong&gt;Iframe&lt;/strong&gt; &lt;em&gt;(inline frame)&lt;/em&gt; which is a HTML tag that allows you to display another webpage inside your webpage. &lt;/p&gt;

&lt;p&gt;A typical iframe will look like this;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;iframe&lt;/span&gt; &lt;span class="na"&gt;title=&lt;/span&gt;&lt;span class="s"&gt;"Electronic_Data"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"1140"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"541.25"&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://app.powerbi.com/reportEmbed?reportId=1e766837-7d75-4a37-bc3d-3e0ea1f56073&amp;amp;autoAuth=true&amp;amp;ctid=c95984e8-6a70-4512-a440-1c79bca9cc37"&lt;/span&gt; &lt;span class="na"&gt;frameborder=&lt;/span&gt;&lt;span class="s"&gt;"0"&lt;/span&gt; &lt;span class="na"&gt;allowFullScreen=&lt;/span&gt;&lt;span class="s"&gt;"true"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/iframe&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The width and height parts of the code are flexible, and you can change those to whatever you like. All the other parts however, should remain exactly as they are.&lt;/p&gt;

&lt;p&gt;A common mistake often made is using the &lt;em&gt;Share&lt;/em&gt; button to embed a report. &lt;br&gt;
The share button generates links accessible only to those that have accounts in your organization. It will therefore not generate an appropriate embed code.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;u&gt;Embedding the report on a webpage&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Embedding codes allow integration into websites.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How to do it, step-by-step;&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open any plain text editor, such as VS code or Notepad.  Do not use Microsoft Word. &lt;/li&gt;
&lt;li&gt;Create an empty &lt;strong&gt;.html&lt;/strong&gt; file.&lt;/li&gt;
&lt;li&gt;Add basic HTML structure;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;html
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Title of report e.g. Electronic Sales Data&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Heading e.g. Electronic Sales Dashboard&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;Power&lt;/span&gt; &lt;span class="na"&gt;BI&lt;/span&gt; &lt;span class="na"&gt;iframe&lt;/span&gt; &lt;span class="na"&gt;goes&lt;/span&gt; &lt;span class="na"&gt;here&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Click file &amp;gt; Save as, and name the file &lt;strong&gt;Index.html&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is it, if you double click on the saved html file, it will open on any browser and you can load your report, and see all the interactive report running perfectly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0tsbpfi4xzhr44298jvf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0tsbpfi4xzhr44298jvf.png" alt="Successfully embedded Report" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;u&gt;Troubleshooting common Issues&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;If something is not working, it could be one of the following; &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Report requires users to log in&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Fix:&lt;/em&gt;&lt;/strong&gt; New workspaces are sometimes set to private by default. Go back to your workspace, click &lt;em&gt;manage access&lt;/em&gt;, and add Everyone as &lt;strong&gt;Viewer&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Blank Screen after the publishing and embedding&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Fix:&lt;/em&gt;&lt;/strong&gt; Give it time. This is a normal occurrence, as Power BI can take a few minutes for the embed code to work, after publishing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; The report works on your end but no one else can see it.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;Fix:&lt;/em&gt;&lt;/strong&gt;It is possible you used ‘Share’ Button, when embedding, instead of generating the embed code. You might need to generate the embed code anew.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;u&gt;Key Insights and Conclusion&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;Now that you have learnt this process, you can go ahead and use it for your student projects, portfolios, and for work, absolutely free. &lt;/p&gt;

&lt;p&gt;You do not need a paid premium license to embed your Power BI reports. &lt;br&gt;
Every singe feature of your report will work exactly as it does on Power BI Desktop, the slicers, filters and interactions will work just fine inside the Iframe.&lt;/p&gt;

&lt;p&gt;In conclusion, you could build the best Power BI report, but if you cannot publish it and share it with other people, it's use may not be maximized, and a lot of insight could be lost.&lt;/p&gt;

</description>
      <category>analytics</category>
      <category>microsoft</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
