<?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: Akhilesh</title>
    <description>The latest articles on DEV Community by Akhilesh (@yakhilesh).</description>
    <link>https://dev.to/yakhilesh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1358056%2Fe7b59a04-a112-43df-8884-da4ea809ee35.jpg</url>
      <title>DEV Community: Akhilesh</title>
      <link>https://dev.to/yakhilesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yakhilesh"/>
    <language>en</language>
    <item>
      <title>When Code Breaks: How to Handle Errors Like a Pro</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Sun, 19 Apr 2026 02:17:21 +0000</pubDate>
      <link>https://dev.to/yakhilesh/when-code-breaks-how-to-handle-errors-like-a-pro-3g02</link>
      <guid>https://dev.to/yakhilesh/when-code-breaks-how-to-handle-errors-like-a-pro-3g02</guid>
      <description>&lt;p&gt;Your program has been running perfectly for ten minutes.&lt;/p&gt;

&lt;p&gt;Then someone passes in a string where you expected a number. Or the file you're trying to read got deleted. Or the internet went down mid-request. Or a user typed nothing when your code expected input.&lt;/p&gt;

&lt;p&gt;Your program crashes. A wall of red text appears. Everything stops.&lt;/p&gt;

&lt;p&gt;This is not bad luck. This is normal. Errors happen in every real program. The question is not how to avoid them entirely. The question is how to catch them before they bring everything down.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Actually Happens When Code Breaks
&lt;/h2&gt;

&lt;p&gt;When Python hits a problem it cannot resolve, it raises an exception. If nothing catches that exception, the program stops and prints a traceback, that red wall of text.&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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;2&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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;/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="nc"&gt;Traceback &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;main.py&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;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;numbers&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="nb"&gt;IndexError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three pieces of information here. The file and line where it broke. The exact code that caused it. The type of error and what went wrong.&lt;/p&gt;

&lt;p&gt;Read tracebacks from the bottom up. The last line is the actual error. Everything above it is the path Python took to get there.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try and Except
&lt;/h2&gt;

&lt;p&gt;Wrap risky code in a &lt;code&gt;try&lt;/code&gt; block. Tell Python what to do if something goes wrong in the &lt;code&gt;except&lt;/code&gt; block.&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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;2&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="k"&gt;try&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;numbers&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="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;IndexError&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;That index doesn&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;t exist in the list.&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;That index doesn't exist in the list.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Program keeps running. No crash. No red text.&lt;/p&gt;

&lt;p&gt;Python tries the code inside &lt;code&gt;try&lt;/code&gt;. If an &lt;code&gt;IndexError&lt;/code&gt; happens, it jumps to &lt;code&gt;except IndexError&lt;/code&gt; and runs that block instead. If no error happens, the &lt;code&gt;except&lt;/code&gt; block is skipped entirely.&lt;/p&gt;




&lt;h2&gt;
  
  
  Catching Different Error Types
&lt;/h2&gt;

&lt;p&gt;Different problems raise different error types. You can handle each one differently.&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;divide&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ZeroDivisionError&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;Cannot divide by zero.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;TypeError&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;Both inputs must be numbers.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;divide&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="mi"&gt;2&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="nf"&gt;divide&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="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="nf"&gt;divide&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;five&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;5.0
Cannot divide by zero.
Both inputs must be numbers.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each &lt;code&gt;except&lt;/code&gt; catches a specific error type. &lt;code&gt;ZeroDivisionError&lt;/code&gt; when someone divides by zero. &lt;code&gt;TypeError&lt;/code&gt; when the wrong type gets passed in. Both get handled cleanly without crashing.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Errors You'll See Most Often
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# NameError: used a variable that doesn't exist
&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;username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# username was never defined
&lt;/span&gt;
&lt;span class="c1"&gt;# TypeError: wrong type for the operation
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;

&lt;span class="c1"&gt;# ValueError: right type, wrong value
&lt;/span&gt;&lt;span class="n"&gt;number&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# "hello" can't become an integer
&lt;/span&gt;
&lt;span class="c1"&gt;# IndexError: index out of range
&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&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;2&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# KeyError: key doesn't exist in dictionary
&lt;/span&gt;&lt;span class="n"&gt;data&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;name&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;Alex&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;data&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&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# FileNotFoundError: file doesn't exist
&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;missing.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# ZeroDivisionError: dividing by zero
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These seven cover probably 80% of the errors you'll encounter in the next few months. When you see one, you now know the category of problem.&lt;/p&gt;




&lt;h2&gt;
  
  
  Getting the Error Message
&lt;/h2&gt;

&lt;p&gt;Sometimes you want to know exactly what went wrong, not just that something went wrong.&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;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;number&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;not a number&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&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;Something went wrong: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Something went wrong: invalid literal for int() with base 10: 'not a number'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;as e&lt;/code&gt; captures the error object. &lt;code&gt;e&lt;/code&gt; contains the full error message. Useful for logging problems or showing the user something specific.&lt;/p&gt;




&lt;h2&gt;
  
  
  Else and Finally
&lt;/h2&gt;

&lt;p&gt;Two optional additions to try/except that come up in real code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;else&lt;/code&gt; runs only if no error happened.&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;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;number&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;42&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&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;That&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s not a valid number.&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;Successfully converted: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;number&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Successfully converted: 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;finally&lt;/code&gt; runs no matter what. Error or no error.&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;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;data.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;FileNotFoundError&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;File not 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;finally&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;This always runs.&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;code&gt;finally&lt;/code&gt; is useful for cleanup code that needs to run regardless of what happened. Closing database connections. Releasing locks. Logging that an operation completed. Even if an exception occurred and was not caught, &lt;code&gt;finally&lt;/code&gt; still runs before Python propagates the error.&lt;/p&gt;




&lt;h2&gt;
  
  
  Raising Your Own Errors
&lt;/h2&gt;

&lt;p&gt;You can raise errors intentionally. Useful when someone uses your function in a way that doesn't make sense.&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;set_age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&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;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="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&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 cannot be negative.&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;age&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;That age is not realistic.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;set_age&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&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;Invalid input: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Invalid input: Age cannot be negative.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;raise ValueError("message")&lt;/code&gt; creates and throws an error. The caller's try/except catches it. Your function refuses to work with bad data and tells you exactly why.&lt;/p&gt;

&lt;p&gt;This is how professional code is written. Functions validate their inputs and raise clear errors when something is wrong. Far better than silently producing wrong answers.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Real Pattern: Retrying on Failure
&lt;/h2&gt;

&lt;p&gt;This shows up in data pipelines and API calls constantly.&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;load_number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;

&lt;span class="n"&gt;raw_data&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;42&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;hello&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;99&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;abc&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;17&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;?&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;clean_numbers&lt;/span&gt; &lt;span class="o"&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;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;raw_data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;load_number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&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;result&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;clean_numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&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;Skipped invalid value: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;item&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;Clean numbers: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;clean_numbers&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Skipped invalid value: hello
Skipped invalid value: abc
Skipped invalid value: ?
Clean numbers: [42, 99, 17]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Real data is messy. Some values won't convert. Some files won't exist. Some network calls will fail. Handle the failures, keep the successes, move on.&lt;/p&gt;




&lt;h2&gt;
  
  
  The One Mistake to Avoid
&lt;/h2&gt;

&lt;p&gt;Catching everything silently.&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;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;some_complicated_code&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This catches every possible error and does nothing about it. Your code fails silently. You have no idea what went wrong or where. Bugs become invisible. Do not do this.&lt;/p&gt;

&lt;p&gt;At minimum, print the error. Better, log it. Best, handle specific error types and let unexpected ones crash loudly so you know they exist.&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;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;some_complicated_code&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&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;Unexpected error: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&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;That bare &lt;code&gt;except:&lt;/code&gt; with no type and no message is a trap. Avoid it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try This
&lt;/h2&gt;

&lt;p&gt;Create &lt;code&gt;errors_practice.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Write a function called &lt;code&gt;safe_divide&lt;/code&gt; that takes two numbers and returns the result of dividing the first by the second. Handle the case where the second number is zero. Handle the case where either input is not a number. Return &lt;code&gt;None&lt;/code&gt; for invalid inputs and print a clear message explaining what went wrong.&lt;/p&gt;

&lt;p&gt;Then write a function called &lt;code&gt;load_profile&lt;/code&gt; that takes a filename, opens it, reads the JSON inside using &lt;code&gt;json.load&lt;/code&gt;, and returns the data as a dictionary. Handle the case where the file doesn't exist. Handle the case where the file exists but is not valid JSON. Return &lt;code&gt;None&lt;/code&gt; for failures.&lt;/p&gt;

&lt;p&gt;Test both functions with inputs that work and inputs that should fail. Make sure your program never crashes, just handles problems cleanly and keeps going.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;Your code now handles failure gracefully. Next is modules and packages, how Python lets you use code that other people wrote, which is how you'll eventually use NumPy, Pandas, PyTorch, and every other library in this series.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>productivity</category>
      <category>python</category>
    </item>
    <item>
      <title>Reading Files Without Breaking Everything</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Sun, 19 Apr 2026 02:11:39 +0000</pubDate>
      <link>https://dev.to/yakhilesh/reading-files-without-breaking-everything-37k7</link>
      <guid>https://dev.to/yakhilesh/reading-files-without-breaking-everything-37k7</guid>
      <description>&lt;p&gt;Every program you have written so far forgets everything the moment it stops running.&lt;/p&gt;

&lt;p&gt;You store names in variables. You calculate scores. You build lists. Then the program ends and all of it is gone. Next time you run it, you start from zero again.&lt;/p&gt;

&lt;p&gt;Files are how programs remember things between runs. Files are also how programs talk to the outside world. Real data lives in files. CSV files full of numbers. Text files full of sentences. JSON files full of structured data. Your AI models will read thousands of files. You need to know how this works.&lt;/p&gt;




&lt;h2&gt;
  
  
  Opening and Reading a File
&lt;/h2&gt;

&lt;p&gt;First, create a file to practice with. Make a new file called &lt;code&gt;notes.txt&lt;/code&gt; in the same folder as your Python files. Put this inside it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alex
Priya
Sam
Jordan
Lisa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just five names, one per line. Save it.&lt;/p&gt;

&lt;p&gt;Now read it with 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="nb"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;notes.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;close&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;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alex
Priya
Sam
Jordan
Lisa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three steps. Open the file. Do something with it. Close it.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;"r"&lt;/code&gt; means read mode. You're opening the file to read it, not change it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file.read()&lt;/code&gt; grabs the entire file content as one big string.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file.close()&lt;/code&gt; is important. An open file uses system resources. If you forget to close it, you can run into problems, especially if your program opens many files.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Better Way: with
&lt;/h2&gt;

&lt;p&gt;Forgetting &lt;code&gt;file.close()&lt;/code&gt; is a common mistake. Python has a cleaner approach that closes the file automatically, no matter what happens.&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;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;notes.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&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;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same result. But now you don't need to call &lt;code&gt;.close()&lt;/code&gt;. When the &lt;code&gt;with&lt;/code&gt; block ends, Python closes the file automatically. Even if your code crashes inside the block. Even if an error happens. The file always gets closed.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;with&lt;/code&gt; every time you work with files. It's the standard way.&lt;/p&gt;




&lt;h2&gt;
  
  
  Reading Line by Line
&lt;/h2&gt;

&lt;p&gt;Sometimes you don't want the whole file as one string. You want each line separately.&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;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;notes.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readlines&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;lines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alex&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Priya&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Sam&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Jordan&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Lisa&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.readlines()&lt;/code&gt; gives you a list where each item is one line. But notice the &lt;code&gt;\n&lt;/code&gt; at the end of each name. That's the newline character, the invisible marker that tells Python where one line ends and the next begins.&lt;/p&gt;

&lt;p&gt;Strip it off with &lt;code&gt;.strip()&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="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;notes.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readlines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&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;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;lines&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;names&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alex&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;Priya&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;Sam&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;Jordan&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;Lisa&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;Clean list. No newline characters. This is the pattern you'll use constantly when loading data from text files.&lt;/p&gt;

&lt;p&gt;Or loop directly through the file line by line, which is more memory-efficient for large files.&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;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;notes.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&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;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;file&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;line&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;strip&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alex
Priya
Sam
Jordan
Lisa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Writing to a File
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;output.txt&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;w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&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 from Python&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="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;This is a second line&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="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&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 a third&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run this. Check your folder. A new file called &lt;code&gt;output.txt&lt;/code&gt; appeared with those three lines inside.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;"w"&lt;/code&gt; means write mode. Here is the dangerous part about &lt;code&gt;"w"&lt;/code&gt;. If &lt;code&gt;output.txt&lt;/code&gt; already existed, Python deleted all its old content and started fresh. No warning. No confirmation. Just gone.&lt;/p&gt;

&lt;p&gt;If you want to add to an existing file without destroying what's already there, use &lt;code&gt;"a"&lt;/code&gt; for append mode.&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;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;output.txt&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;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;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;This line gets added at the end&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run this a few times. Open the file. You'll see the new line added each time without losing the original content.&lt;/p&gt;




&lt;h2&gt;
  
  
  Writing Multiple Lines at Once
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;names&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;Alex&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;Priya&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;Sam&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;Jordan&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;names.txt&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;w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&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;name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&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="se"&gt;\n&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;Loop through a list, write each item as its own line. You'll use this shape when saving results, exporting data, logging outputs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Working With JSON
&lt;/h2&gt;

&lt;p&gt;Plain text files are useful but JSON is where things get interesting. JSON is a format for storing structured data, basically Python dictionaries and lists, saved as text.&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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="n"&gt;person&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;name&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;Alex&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;age&lt;/span&gt;&lt;span class="sh"&gt;"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&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;Mumbai&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;skills&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;Python&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;Machine Learning&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;SQL&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;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;person.json&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;w&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&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;Open &lt;code&gt;person.json&lt;/code&gt; in your editor. It looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alex"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"age"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"city"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Mumbai"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"skills"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"Python"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"Machine Learning"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="s2"&gt;"SQL"&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;indent=4&lt;/code&gt; makes it readable with nice formatting. Without it, everything is on one line.&lt;/p&gt;

&lt;p&gt;Now read it back.&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;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;person.json&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&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;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;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="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;skills&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;Output:&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;Alex&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Python&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;Machine Learning&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;SQL&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;code&gt;json.dump()&lt;/code&gt; converts Python to JSON and saves it. &lt;code&gt;json.load()&lt;/code&gt; reads JSON and converts it back to Python. The dictionary comes back exactly as you saved it.&lt;/p&gt;

&lt;p&gt;This matters enormously in AI work. Model configurations are JSON. API responses are JSON. Dataset metadata is JSON. You'll read and write JSON constantly.&lt;/p&gt;




&lt;h2&gt;
  
  
  When the File Doesn't Exist
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;missing.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&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 console"&gt;&lt;code&gt;&lt;span class="go"&gt;FileNotFoundError: [Errno 2] No such file or directory: 'missing.txt'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your program crashes. Handle it properly.&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;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;missing.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&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;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;FileNotFoundError&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;That file doesn&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;t exist.&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;That file doesn't exist.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Program keeps running. You'll learn more about try/except in the next post. For now, just know this pattern exists and use it whenever opening a file that might not be there.&lt;/p&gt;




&lt;h2&gt;
  
  
  File Paths
&lt;/h2&gt;

&lt;p&gt;So far you've used just a filename like &lt;code&gt;"notes.txt"&lt;/code&gt;. That works only when the file is in the same folder as your Python script.&lt;/p&gt;

&lt;p&gt;For files in other locations, you need the full path.&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;# Windows
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&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="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Users&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Alex&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;Documents&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;data.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Mac and Linux
&lt;/span&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/home/alex/documents/data.txt&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;r&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The double backslash on Windows is because &lt;code&gt;\&lt;/code&gt; has special meaning in Python strings. You need &lt;code&gt;\\&lt;/code&gt; to mean a literal backslash. On Mac and Linux, forward slashes work fine.&lt;/p&gt;

&lt;p&gt;A cleaner approach that works on all systems is using Python's &lt;code&gt;pathlib&lt;/code&gt; module. But that's a more advanced topic. For now, keeping files in the same folder as your script keeps things simple.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try This
&lt;/h2&gt;

&lt;p&gt;Create &lt;code&gt;files_practice.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Part one: write a list of at least eight cities to a file called &lt;code&gt;cities.txt&lt;/code&gt;, one city per line.&lt;/p&gt;

&lt;p&gt;Part two: read &lt;code&gt;cities.txt&lt;/code&gt; back. Clean up the newline characters. Print how many cities are in the file.&lt;/p&gt;

&lt;p&gt;Part three: create a dictionary with information about yourself, at least five keys. Save it to &lt;code&gt;profile.json&lt;/code&gt; using &lt;code&gt;json.dump&lt;/code&gt;. Then read it back and print each key and value on its own line.&lt;/p&gt;

&lt;p&gt;Part four: try to open a file called &lt;code&gt;doesnt_exist.txt&lt;/code&gt; and handle the error gracefully so the program prints a helpful message instead of crashing.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;Files can fail. Networks can fail. Users can type the wrong thing. Programs break in all kinds of ways. The next post is about handling errors properly so your code fails gracefully instead of crashing at the worst moment.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>productivity</category>
      <category>python</category>
    </item>
    <item>
      <title>Build Your Own Types: Classes Explained Simply</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Sun, 19 Apr 2026 02:06:57 +0000</pubDate>
      <link>https://dev.to/yakhilesh/build-your-own-types-classes-explained-simply-26dj</link>
      <guid>https://dev.to/yakhilesh/build-your-own-types-classes-explained-simply-26dj</guid>
      <description>&lt;p&gt;You have been using types this whole time.&lt;/p&gt;

&lt;p&gt;Strings. Integers. Lists. Dictionaries. Every one of those is a type. And they don't just hold data. They also come with built-in behaviors. Strings have &lt;code&gt;.upper()&lt;/code&gt;. Lists have &lt;code&gt;.append()&lt;/code&gt;. Dictionaries have &lt;code&gt;.get()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now here is the question. What if the type you need doesn't exist yet? What if you want a &lt;code&gt;Student&lt;/code&gt; type that holds a name, age, and scores, and also knows how to calculate its own average?&lt;/p&gt;

&lt;p&gt;That is exactly what classes let you build.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Cookie Cutter Idea
&lt;/h2&gt;

&lt;p&gt;A class is a blueprint. It describes what something looks like and what it can do. By itself it's just a description, it doesn't actually exist yet.&lt;/p&gt;

&lt;p&gt;An object is what you get when you use the blueprint to create something real. You can create as many objects from one class as you want. Same structure, different data.&lt;/p&gt;

&lt;p&gt;Cookie cutter is the class. The cookies are the objects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Your First Class
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;age&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="n"&gt;self&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="n"&gt;age&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;class Student:&lt;/code&gt; defines a new type called Student.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def __init__&lt;/code&gt; is a special method that runs automatically when you create a new Student. Think of it as the setup instructions.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self&lt;/code&gt; refers to the specific object being created. When you make a Student named Alex, &lt;code&gt;self&lt;/code&gt; is that Alex object. When you make a Student named Priya, &lt;code&gt;self&lt;/code&gt; is that Priya object. Same class, different &lt;code&gt;self&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.name = name&lt;/code&gt; stores the name on this specific object. Without &lt;code&gt;self.&lt;/code&gt;, the value disappears when &lt;code&gt;__init__&lt;/code&gt; finishes.&lt;/p&gt;

&lt;p&gt;Now create some students.&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;student1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alex&lt;/span&gt;&lt;span class="sh"&gt;"&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="n"&gt;student2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Priya&lt;/span&gt;&lt;span class="sh"&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student1&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student2&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alex
Priya
25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two objects. Same blueprint. Different data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Adding Methods
&lt;/h2&gt;

&lt;p&gt;Methods are functions that belong to a class. They define what the object can do.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;age&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="n"&gt;self&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="n"&gt;age&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;Hi, I&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;m &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; and I&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;m &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; years old.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;birthday&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&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="n"&gt;self&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;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;Happy birthday &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;! You are now &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&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;/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;student1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alex&lt;/span&gt;&lt;span class="sh"&gt;"&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="n"&gt;student1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;student1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;birthday&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;student1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hi, I'm Alex and I'm 25 years old.
Happy birthday Alex! You are now 26.
Hi, I'm Alex and I'm 26 years old.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;birthday()&lt;/code&gt; changed &lt;code&gt;self.age&lt;/code&gt; on that specific object. &lt;code&gt;student1&lt;/code&gt; is now 26. Any other Student object you created is still whatever age they were. The change only affected &lt;code&gt;student1&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  A More Real Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BankAccount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;,&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;0&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&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;balance&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&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;self&lt;/span&gt;&lt;span class="p"&gt;.&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;Deposited &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: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&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;def&lt;/span&gt; &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&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;self&lt;/span&gt;&lt;span class="p"&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;Not enough money.&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&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;self&lt;/span&gt;&lt;span class="p"&gt;.&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;Withdrew &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: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&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;def&lt;/span&gt; &lt;span class="nf"&gt;show_balance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s balance: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&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;/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;account&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BankAccount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alex&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_balance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alex's balance: 1000
Deposited 500. New balance: 1500
Withdrew 200. New balance: 1300
Not enough money.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the object remembers its state between method calls. &lt;code&gt;self.balance&lt;/code&gt; keeps updating. Each call to &lt;code&gt;deposit&lt;/code&gt; or &lt;code&gt;withdraw&lt;/code&gt; works with the current balance, not some starting value. That persistent memory is what makes objects powerful.&lt;/p&gt;




&lt;h2&gt;
  
  
  Multiple Objects, Independent State
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;account1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BankAccount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alex&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;account2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BankAccount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Priya&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;account1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deposit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;account2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;account1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_balance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;account2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_balance&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Deposited 200. New balance: 1200
Withdrew 1000. New balance: 4000
Alex's balance: 1200
Priya's balance: 4000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two completely separate accounts. Operations on one never touch the other. That independence is the whole point of objects.&lt;/p&gt;




&lt;h2&gt;
  
  
  The &lt;strong&gt;str&lt;/strong&gt; Method
&lt;/h2&gt;

&lt;p&gt;When you print an object, Python normally shows something ugly like &lt;code&gt;&amp;lt;__main__.Student object at 0x10b3f2a90&amp;gt;&lt;/code&gt;. You can fix that.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;age&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="n"&gt;self&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="n"&gt;age&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&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;Student: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, Age: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;student1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alex&lt;/span&gt;&lt;span class="sh"&gt;"&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Student: Alex, Age: 25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;__str__&lt;/code&gt; is another special method like &lt;code&gt;__init__&lt;/code&gt;. Python calls it automatically whenever it needs to display your object as text. Return a string from it and that's what gets printed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Three Things That Confuse Everyone
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Forgetting self in method definitions.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&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;age&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;     &lt;span class="c1"&gt;# missing self
&lt;/span&gt;        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: Student.__init__() takes 2 positional arguments but 3 were given
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every method in a class must have &lt;code&gt;self&lt;/code&gt; as the first parameter. Every single one. Even if you don't use &lt;code&gt;self&lt;/code&gt; inside the method. Python passes the object automatically as the first argument, so &lt;code&gt;self&lt;/code&gt; has to be there to receive it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Forgetting self when storing data.&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;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;       &lt;span class="c1"&gt;# wrong, this just creates a local variable
&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&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;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;  &lt;span class="c1"&gt;# correct, this stores it on the object
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without &lt;code&gt;self.&lt;/code&gt;, the data exists only while &lt;code&gt;__init__&lt;/code&gt; is running and then vanishes. Always use &lt;code&gt;self.&lt;/code&gt; to store anything you need to keep.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using the class itself instead of creating an object.&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="n"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;      &lt;span class="c1"&gt;# wrong, Student is the blueprint
&lt;/span&gt;&lt;span class="n"&gt;student1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;     &lt;span class="c1"&gt;# correct, student1 is the actual object
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Try This
&lt;/h2&gt;

&lt;p&gt;Create &lt;code&gt;classes_practice.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Build a class called &lt;code&gt;Product&lt;/code&gt; for an online store.&lt;/p&gt;

&lt;p&gt;It should store: name, price, and quantity in stock.&lt;/p&gt;

&lt;p&gt;It should have these methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;show_info()&lt;/code&gt; that prints all the product details neatly&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sell(quantity)&lt;/code&gt; that reduces the stock by that amount, but prints "not enough stock" if the quantity requested is more than what's available&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;restock(quantity)&lt;/code&gt; that adds to the stock&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;apply_discount(percent)&lt;/code&gt; that reduces the price by that percentage and prints the new price&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create two different products. Call different methods on each. Make sure the state changes correctly and the two products stay independent.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;Objects are great for organizing data and behavior together. But Python also needs to work with data that lives in files, not just in memory. Next post is about reading and writing files, which is how your programs start talking to the real world.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>productivity</category>
      <category>ai</category>
    </item>
    <item>
      <title>Dictionaries: When a List Just Isn't Enough</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Sun, 19 Apr 2026 02:05:31 +0000</pubDate>
      <link>https://dev.to/yakhilesh/dictionaries-when-a-list-just-isnt-enough-27o9</link>
      <guid>https://dev.to/yakhilesh/dictionaries-when-a-list-just-isnt-enough-27o9</guid>
      <description>&lt;p&gt;You have a list of five numbers.&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;person&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;Alex&lt;/span&gt;&lt;span class="sh"&gt;"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Mumbai&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;alex@email.com&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;Quick question. What is &lt;code&gt;person[3]&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;You have to count. Index 0 is the name. Index 1 is age. Index 2 is city. Index 3 is... the boolean? What does True mean here? Is that married? Is that a student? Is that something else entirely?&lt;/p&gt;

&lt;p&gt;You wrote this code yourself and you already can't remember what index 3 means.&lt;/p&gt;

&lt;p&gt;Now imagine coming back to this code three weeks later.&lt;/p&gt;




&lt;p&gt;This is the exact problem dictionaries solve. Instead of accessing data by position, you access it by name. Instead of &lt;code&gt;person[3]&lt;/code&gt; you write &lt;code&gt;person["is_student"]&lt;/code&gt;. The name tells you everything.&lt;/p&gt;




&lt;h2&gt;
  
  
  What a Dictionary Looks Like
&lt;/h2&gt;

&lt;p&gt;Curly braces. Key and value pairs separated by colons. Pairs separated by commas.&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;person&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;name&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;Alex&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;age&lt;/span&gt;&lt;span class="sh"&gt;"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&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;Mumbai&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;is_student&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;email&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;alex@email.com&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;Now access any value by its key.&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;person&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;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="n"&gt;person&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&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;person&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;is_student&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alex
25
True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No counting. No guessing. &lt;code&gt;person["age"]&lt;/code&gt; tells you exactly what you're getting.&lt;/p&gt;




&lt;h2&gt;
  
  
  Adding, Changing, Removing
&lt;/h2&gt;

&lt;p&gt;A dictionary is not frozen after you create it. You can change it freely.&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;person&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;name&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;Alex&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;age&lt;/span&gt;&lt;span class="sh"&gt;"&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="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;"&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;Mumbai&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;        &lt;span class="c1"&gt;# add new key
&lt;/span&gt;&lt;span class="n"&gt;person&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&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;               &lt;span class="c1"&gt;# update existing key
&lt;/span&gt;&lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;               &lt;span class="c1"&gt;# delete a key
&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;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&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;Alex&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;age&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assigning to a key that exists updates it. Assigning to a key that doesn't exist creates it. Clean and consistent.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Safe Way to Get Values
&lt;/h2&gt;

&lt;p&gt;What happens when you ask for a key that doesn't exist?&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;person&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;name&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;Alex&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;age&lt;/span&gt;&lt;span class="sh"&gt;"&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&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 plaintext"&gt;&lt;code&gt;KeyError: 'city'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your program crashes. Not ideal.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;.get()&lt;/code&gt; instead. It returns &lt;code&gt;None&lt;/code&gt; if the key doesn't exist instead of crashing.&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;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&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;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                         &lt;span class="c1"&gt;# None
&lt;/span&gt;
&lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&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;Unknown&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;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                         &lt;span class="c1"&gt;# Unknown
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second argument to &lt;code&gt;.get()&lt;/code&gt; is a default value. If the key exists, you get the real value. If it doesn't, you get the default. Your program keeps running.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;.get()&lt;/code&gt; whenever the key might not be there. Use &lt;code&gt;person["key"]&lt;/code&gt; only when you're certain the key exists.&lt;/p&gt;




&lt;h2&gt;
  
  
  Looping Through a Dictionary
&lt;/h2&gt;

&lt;p&gt;Three ways. Each one useful for different situations.&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;scores&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;Alex&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;92&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Priya&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sam&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Jordan&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;95&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;key&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;scores&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;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alex
Priya
Sam
Jordan
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just the keys. Now both:&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;key&lt;/span&gt;&lt;span class="p"&gt;,&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;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&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;key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Alex&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;92&lt;/span&gt;
&lt;span class="na"&gt;Priya&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;88&lt;/span&gt;
&lt;span class="na"&gt;Sam&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;75&lt;/span&gt;
&lt;span class="na"&gt;Jordan&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;95&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.items()&lt;/code&gt; gives you each key-value pair as a tuple. The &lt;code&gt;for key, value&lt;/code&gt; unpacks it. This is the one you'll use most.&lt;/p&gt;

&lt;p&gt;Just the values if you need them:&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;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;scores&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;values&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;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;92
88
75
95
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Checking If a Key Exists
&lt;/h2&gt;

&lt;p&gt;Before you access a key, sometimes you need to know if it's there.&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;person&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;name&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;Alex&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;age&lt;/span&gt;&lt;span class="sh"&gt;"&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="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;person&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;person&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;city&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;No city found&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;No city found
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;in&lt;/code&gt; works with dictionaries just like with lists. Checks keys, not values.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dictionaries Inside Lists
&lt;/h2&gt;

&lt;p&gt;This is the pattern you will see constantly in real data work.&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="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&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;Alex&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;score&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;92&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;passed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&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;name&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;Priya&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;score&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;58&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;passed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;False&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;name&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;Sam&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;score&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;passed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&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;name&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;Jordan&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;score&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;passed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;False&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;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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;passed&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&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; passed with &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;student&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="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="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; needs to retake the exam&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alex passed with 92
Priya needs to retake the exam
Sam passed with 75
Jordan needs to retake the exam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A list of dictionaries. Every item in the list is a dictionary with the same keys. This is exactly how JSON data looks. This is how APIs return data. This is how database records get processed in Python.&lt;/p&gt;

&lt;p&gt;Get comfortable with this shape. You'll work with it for the rest of your programming life.&lt;/p&gt;




&lt;h2&gt;
  
  
  Counting Things With a Dictionary
&lt;/h2&gt;

&lt;p&gt;One classic use case. Count how many times each item appears.&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;words&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;python&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;is&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;fun&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;python&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;is&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;great&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;python&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;counts&lt;/span&gt; &lt;span class="o"&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;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;words&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;word&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&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="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;word&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;python&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;is&lt;/span&gt;&lt;span class="sh"&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;fun&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;great&lt;/span&gt;&lt;span class="sh"&gt;'&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Walk through what happens. First time Python sees "python", it's not in counts yet. So &lt;code&gt;counts["python"] = 1&lt;/code&gt;. Second time it sees "python", it IS in counts. So &lt;code&gt;counts["python"] = counts["python"] + 1&lt;/code&gt; which is &lt;code&gt;1 + 1 = 2&lt;/code&gt;. Third time, &lt;code&gt;2 + 1 = 3&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This exact pattern, counting occurrences, shows up in text analysis, data cleaning, building histograms, and more.&lt;/p&gt;




&lt;h2&gt;
  
  
  The One Thing That Catches Everyone
&lt;/h2&gt;

&lt;p&gt;Dictionary keys must be unique. If you add the same key twice, the second value overwrites the first. Silently. No error.&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;data&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;name&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;Alex&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;age&lt;/span&gt;&lt;span class="sh"&gt;"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&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;Alexander&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;      &lt;span class="c1"&gt;# duplicate key
&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;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name&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;Output:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;"Alex" is gone. "Alexander" replaced it without any warning. Python doesn't complain. This causes bugs that are genuinely hard to find because nothing crashes.&lt;/p&gt;

&lt;p&gt;Double check your keys when creating a dictionary manually.&lt;/p&gt;




&lt;h2&gt;
  
  
  Dictionary vs List: When to Use Which
&lt;/h2&gt;

&lt;p&gt;Use a list when order matters and you're dealing with a sequence of similar things. A list of names. A list of scores. A sequence of steps.&lt;/p&gt;

&lt;p&gt;Use a dictionary when you want to label your data. A user profile. A config file. Anything where you need to look something up by name instead of by position.&lt;/p&gt;

&lt;p&gt;In data science and AI, you'll use both constantly, often together. Models output dictionaries. Datasets are lists of dictionaries. Configuration is a dictionary. Results are stored in dictionaries.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try This
&lt;/h2&gt;

&lt;p&gt;Create &lt;code&gt;dictionaries_practice.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Build a dictionary for a product in an online store. It should have at least six keys: name, price, category, in_stock (boolean), rating, and number of reviews.&lt;/p&gt;

&lt;p&gt;Then write code that does all of this:&lt;/p&gt;

&lt;p&gt;Print a nicely formatted product description using the values from your dictionary.&lt;/p&gt;

&lt;p&gt;Add a "discount_percent" key with a value of 10.&lt;/p&gt;

&lt;p&gt;Calculate and print the discounted price using the values already in the dictionary.&lt;/p&gt;

&lt;p&gt;Write a loop that goes through the dictionary and prints every key and value on its own line.&lt;/p&gt;

&lt;p&gt;Finally, try accessing a key that doesn't exist using &lt;code&gt;.get()&lt;/code&gt; with a sensible default value.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;Lists and dictionaries store collections of data. But what if you want to create your own custom data type, something that bundles both data and behavior together? That's what classes and objects are for, and that's exactly where we're heading.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>productivity</category>
      <category>python</category>
    </item>
    <item>
      <title>Lists: The Most Useful Thing in Python</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Sun, 19 Apr 2026 02:05:11 +0000</pubDate>
      <link>https://dev.to/yakhilesh/lists-the-most-useful-thing-in-python-4jpk</link>
      <guid>https://dev.to/yakhilesh/lists-the-most-useful-thing-in-python-4jpk</guid>
      <description>&lt;p&gt;Every program you will ever write works with collections of things.&lt;/p&gt;

&lt;p&gt;A list of users. A list of prices. A list of predictions your model made. A list of words in a sentence. A list of images to process.&lt;/p&gt;

&lt;p&gt;You cannot avoid lists. They are everywhere. Learn them well now and everything else gets easier.&lt;/p&gt;




&lt;h2&gt;
  
  
  Making a List
&lt;/h2&gt;

&lt;p&gt;Square brackets. Items separated by commas.&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;fruits&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;apple&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;banana&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;mango&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;grape&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;82&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;67&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;54&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;71&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;mixed&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;Alex&lt;/span&gt;&lt;span class="sh"&gt;"&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="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;3.14&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;empty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A list can hold anything. Strings, numbers, booleans, even other lists. And yes, mixing types in one list is allowed, though in practice most lists hold one type of thing.&lt;/p&gt;




&lt;h2&gt;
  
  
  Getting Items Out
&lt;/h2&gt;

&lt;p&gt;Every item in a list has a position number called an index. And here is the thing that trips up almost everyone at the start.&lt;/p&gt;

&lt;p&gt;Indexes start at 0, not 1.&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;fruits&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;apple&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;banana&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;mango&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;grape&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;fruits&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="c1"&gt;# apple
&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;fruits&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="c1"&gt;# banana
&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;fruits&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="c1"&gt;# mango
&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;fruits&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="c1"&gt;# grape
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apple
banana
mango
grape
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First item is index 0. Second is index 1. Always one less than you expect.&lt;/p&gt;

&lt;p&gt;Negative indexes count from the end. &lt;code&gt;-1&lt;/code&gt; is always the last item.&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;fruits&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="c1"&gt;# grape
&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;fruits&lt;/span&gt;&lt;span class="p"&gt;[&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;span class="c1"&gt;# mango
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Incredibly useful when you need the last item and don't know how long the list is.&lt;/p&gt;

&lt;p&gt;Try to access an index that doesn't exist and Python tells you clearly.&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;fruits&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IndexError: list index out of range
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The list has 4 items. Index 10 does not exist.&lt;/p&gt;




&lt;h2&gt;
  
  
  Changing Items
&lt;/h2&gt;

&lt;p&gt;Lists are mutable. That means you can change them after creating them.&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;fruits&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;apple&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;banana&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;mango&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;grape&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;fruits&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="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orange&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;fruits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&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;orange&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;mango&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;grape&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;Banana is gone. Orange took its place at index 1.&lt;/p&gt;




&lt;h2&gt;
  
  
  Adding and Removing
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fruits&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;apple&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;banana&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;mango&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;grape&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# adds to the end
&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;fruits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orange&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# inserts at index 1
&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;fruits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;banana&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# removes by value
&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;fruits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;           &lt;span class="c1"&gt;# removes and returns last item
&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;last&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;fruits&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&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;banana&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;mango&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;grape&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;apple&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;orange&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;banana&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;mango&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;grape&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;apple&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;orange&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;mango&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;grape&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;grape&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&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;orange&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;mango&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;code&gt;append&lt;/code&gt; is the one you'll use most. Every time you build a list dynamically, you start with an empty list and append to it.&lt;/p&gt;




&lt;h2&gt;
  
  
  Useful Things to Know About Your List
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&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;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&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;9&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;7&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="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;       &lt;span class="c1"&gt;# 7, how many items
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;       &lt;span class="c1"&gt;# 1, smallest
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;       &lt;span class="c1"&gt;# 9, largest
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;       &lt;span class="c1"&gt;# 34, total
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;    &lt;span class="c1"&gt;# [1, 2, 3, 4, 7, 8, 9], sorted copy
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# True, is 7 in the list?
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# False
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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="mi"&gt;7&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt;
&lt;span class="mi"&gt;34&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;2&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;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;in&lt;/code&gt; is one of the cleaner bits of Python. Check if something exists in a list in plain English.&lt;/p&gt;




&lt;h2&gt;
  
  
  Slicing: Getting a Chunk
&lt;/h2&gt;

&lt;p&gt;You can grab a section of a list using slicing. Start index and end index separated by a colon inside brackets.&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;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&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;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&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="n"&gt;numbers&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="c1"&gt;# index 1 up to but not including 4
&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;numbers&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="c1"&gt;# from the start up to index 3
&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;numbers&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="c1"&gt;# from index 4 to the end
&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;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:])&lt;/span&gt;    &lt;span class="c1"&gt;# last three items
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&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="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;]&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="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;]&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="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same rule as range: the end number is not included. &lt;code&gt;[1:4]&lt;/code&gt; gives you indexes 1, 2, and 3.&lt;/p&gt;




&lt;h2&gt;
  
  
  Looping Over a List
&lt;/h2&gt;

&lt;p&gt;You saw this in the loops post. Now it makes more sense.&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;names&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;Alex&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;Priya&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;Sam&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;Jordan&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;name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;names&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;Hello, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Alex!
Hello, Priya!
Hello, Sam!
Hello, Jordan!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you also need the index, use &lt;code&gt;enumerate&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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;names&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;index&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0: Alex
1: Priya
2: Sam
3: Jordan
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;enumerate&lt;/code&gt; gives you both the position and the value on every pass. You'll use this more than you think.&lt;/p&gt;




&lt;h2&gt;
  
  
  Building a List Dynamically
&lt;/h2&gt;

&lt;p&gt;The most common real-world pattern. Start empty, add things as you go.&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;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;92&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;78&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="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;97&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;passing&lt;/span&gt; &lt;span class="o"&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;score&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;scores&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;score&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="n"&gt;passing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;passing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;92&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;78&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;61&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;97&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start with an empty list. Loop through the original. Check the condition. Append if it passes. End up with a filtered list. This pattern is everywhere in data processing.&lt;/p&gt;




&lt;h2&gt;
  
  
  One Mistake That Gets Everyone
&lt;/h2&gt;

&lt;p&gt;Copying a list the wrong way.&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;original&lt;/span&gt; &lt;span class="o"&gt;=&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;2&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="n"&gt;copy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;          &lt;span class="c1"&gt;# this is NOT a copy
&lt;/span&gt;
&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&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="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# [1, 2, 3, 4] -- original changed too!
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you do &lt;code&gt;copy = original&lt;/code&gt;, you're not creating a second list. Both variables point to the exact same list in memory. Change one, both change.&lt;/p&gt;

&lt;p&gt;To make a real copy:&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;original&lt;/span&gt; &lt;span class="o"&gt;=&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;2&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="n"&gt;copy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   &lt;span class="c1"&gt;# actual copy
&lt;/span&gt;
&lt;span class="n"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&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="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# [1, 2, 3] -- original unchanged
&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;copy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;              &lt;span class="c1"&gt;# [1, 2, 3, 4]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This trips people up badly in data work. Always use &lt;code&gt;.copy()&lt;/code&gt; when you need an independent copy of a list.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try This
&lt;/h2&gt;

&lt;p&gt;Create &lt;code&gt;lists_practice.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Start with this list of temperatures in Celsius:&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;temps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;38&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;41&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&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;27&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write code that does all of these without using any built-in shortcut functions where noted:&lt;/p&gt;

&lt;p&gt;First: print the highest and lowest temperature using &lt;code&gt;max()&lt;/code&gt; and &lt;code&gt;min()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Second: build a new list containing only temperatures above 30. Use a loop and append.&lt;/p&gt;

&lt;p&gt;Third: convert every temperature to Fahrenheit and store in a new list. Formula: &lt;code&gt;(c * 9/5) + 32&lt;/code&gt;. Use the function you wrote in post 5 if you saved it.&lt;/p&gt;

&lt;p&gt;Fourth: print how many temperatures are above 30 without using &lt;code&gt;len()&lt;/code&gt; on your filtered list. Count them with a counter variable inside a loop.&lt;/p&gt;

&lt;p&gt;Run it. Check the outputs make sense.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;Lists hold items in order with a numeric index. But sometimes you need to label your data with names instead of numbers. That's what dictionaries are for and they're coming up next.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
      <category>ai</category>
    </item>
    <item>
      <title>Functions: Stop Writing the Same Code Twice</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Sat, 18 Apr 2026 13:34:46 +0000</pubDate>
      <link>https://dev.to/yakhilesh/functions-stop-writing-the-same-code-twice-5ehn</link>
      <guid>https://dev.to/yakhilesh/functions-stop-writing-the-same-code-twice-5ehn</guid>
      <description>&lt;p&gt;You have written the same &lt;code&gt;print(f"Hello, {name}!")&lt;/code&gt; line three times in your code already.&lt;/p&gt;

&lt;p&gt;Not a big deal with one line. But what happens when the thing you keep repeating is 15 lines long? What happens when you find a bug in it and have to fix it in six different places? What happens when you forget to fix one of them?&lt;/p&gt;

&lt;p&gt;This is not a hypothetical. This is what happens in real codebases when people don't use functions. Code gets copied, pasted, modified slightly, and then something breaks and nobody knows which copy is the right one.&lt;/p&gt;

&lt;p&gt;Functions solve this. Write the code once, use it anywhere.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Simplest Function Possible
&lt;/h2&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;greet&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;Hello, welcome!&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;code&gt;def&lt;/code&gt; tells Python you're defining a function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;greet&lt;/code&gt; is the name you're giving it. You choose the name, same rules as variable names.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;()&lt;/code&gt; is where parameters go. Empty for now.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;:&lt;/code&gt; ends the line, same as if and for.&lt;/p&gt;

&lt;p&gt;The indented block is the function body. The code that runs when you call the function.&lt;/p&gt;

&lt;p&gt;But here's the thing. Running that code does nothing visible. You defined the function. You haven't used it yet.&lt;/p&gt;

&lt;p&gt;To actually run the code inside, you call 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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&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;Hello, welcome!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, welcome!
Hello, welcome!
Hello, welcome!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three calls, three outputs. The function ran three times. You wrote the print statement once.&lt;/p&gt;




&lt;h2&gt;
  
  
  Parameters: Giving the Function Information
&lt;/h2&gt;

&lt;p&gt;A function that always does the exact same thing is useful but limited. Parameters let you pass different information to the function each time you call 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;greet&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="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;Hello, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;name&lt;/code&gt; inside the parentheses is a parameter. It's a variable that exists only inside the function and gets its value when you call 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;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alan&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Priya&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sam&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Alan! Welcome.
Hello, Priya! Welcome.
Hello, Sam! Welcome.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same function. Three different results. The parameter is what makes it flexible.&lt;/p&gt;

&lt;p&gt;You can have multiple parameters. Separate them with commas.&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;introduce&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;age&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="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;My name is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;. I am &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; years old and I live in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="si"&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="nf"&gt;introduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alan&lt;/span&gt;&lt;span class="sh"&gt;"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Mumbai&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;introduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Priya&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Delhi&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;My name is Alan. I am 25 years old and I live in Mumbai.
My name is Priya. I am 30 years old and I live in Delhi.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Order matters here. When you call the function, Python matches your arguments to the parameters left to right. First argument goes to first parameter, second to second, and so on.&lt;/p&gt;




&lt;h2&gt;
  
  
  Return: Getting Something Back
&lt;/h2&gt;

&lt;p&gt;So far every function just prints something. But often you want a function to calculate something and give you the result back, not just display it.&lt;/p&gt;

&lt;p&gt;That's what &lt;code&gt;return&lt;/code&gt; does.&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&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;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add&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="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;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;The function takes two numbers, adds them, and returns the answer. You can store that answer in a variable, use it in another calculation, pass it to another function, anything.&lt;/p&gt;

&lt;p&gt;Here's the key difference. &lt;code&gt;print()&lt;/code&gt; inside a function just displays text. The value disappears after that. &lt;code&gt;return&lt;/code&gt; sends the value back to whoever called the function so they can do something with 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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;

&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add&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;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# x is now 7
&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&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="c1"&gt;# y is now 14
&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;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;The output of one function fed straight into another. This is how real programs are built. Small functions, chained together.&lt;/p&gt;




&lt;h2&gt;
  
  
  Default Parameters
&lt;/h2&gt;

&lt;p&gt;Sometimes you want a parameter to have a default value if the caller doesn't provide one.&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;greet&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;greeting&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&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;greeting&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&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="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alan&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Priya&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;Good morning&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sam&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;Hey&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Alan!
Good morning, Priya!
Hey, Sam!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you call &lt;code&gt;greet("Alan")&lt;/code&gt; without a second argument, &lt;code&gt;greeting&lt;/code&gt; falls back to &lt;code&gt;"Hello"&lt;/code&gt;. When you provide one, that value is used instead.&lt;/p&gt;

&lt;p&gt;Default parameters must always come after non-default ones. This doesn't work:&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;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello&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="c1"&gt;# wrong
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python needs to know which argument is which. Put defaults last.&lt;/p&gt;




&lt;h2&gt;
  
  
  Variables Inside Functions Stay Inside
&lt;/h2&gt;

&lt;p&gt;This one surprises people.&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;calculate&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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;Inside function: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;result&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;calculate&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;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# this breaks
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Inside function: 100
NameError: name 'result' is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable &lt;code&gt;result&lt;/code&gt; was created inside the function. It only exists while the function is running. Once the function finishes, &lt;code&gt;result&lt;/code&gt; is gone. Code outside the function cannot see it.&lt;/p&gt;

&lt;p&gt;This is called scope. Variables have a scope, which is the region of code where they exist.&lt;/p&gt;

&lt;p&gt;The fix is to return the value.&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;calculate&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;

&lt;span class="n"&gt;answer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate&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;answer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;result&lt;/code&gt; is returned and stored in &lt;code&gt;answer&lt;/code&gt;. &lt;code&gt;answer&lt;/code&gt; lives outside the function, so the outside code can use it.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Function That Does Something Real
&lt;/h2&gt;

&lt;p&gt;Let's write a function that calculates a grade based on a score. You built something similar with if/else in post 3. Now let's make it reusable.&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;get_grade&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;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;90&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&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="k"&gt;elif&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;80&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&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="k"&gt;elif&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="k"&gt;return&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="k"&gt;elif&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;60&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&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="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;F&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;scores&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;95&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;82&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;67&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;54&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;71&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;score&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;scores&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="nf"&gt;get_grade&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="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;Score &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;score&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Score 95: Grade A
Score 82: Grade B
Score 67: Grade D
Score 54: Grade F
Score 71: Grade C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at how clean this is. The grading logic lives in one place. The loop just calls the function for each score. If you ever need to change how grades are calculated, you change it in one function and it updates everywhere.&lt;/p&gt;

&lt;p&gt;That's the real reason functions exist. Not just to avoid repetition. To keep related logic together so your code is easier to read, fix, and change.&lt;/p&gt;




&lt;h2&gt;
  
  
  Two Mistakes Everyone Makes
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Mistake 1: Forgetting to return and wondering why you get None&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;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;     &lt;span class="c1"&gt;# calculated but never returned
&lt;/span&gt;
&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add&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="mi"&gt;3&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;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;The function calculated 8. But it never sent that value back. Python functions that don't explicitly return something return &lt;code&gt;None&lt;/code&gt; automatically. &lt;code&gt;None&lt;/code&gt; means nothing. Not zero. Literally nothing.&lt;/p&gt;

&lt;p&gt;Fix: add &lt;code&gt;return result&lt;/code&gt; at the end.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mistake 2: Calling before defining&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="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alan&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# call happens first
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;greet&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="c1"&gt;# definition comes after
&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;Hello, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&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;Error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NameError: name 'greet' is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python reads top to bottom. When it hits &lt;code&gt;greet("Alan")&lt;/code&gt;, it hasn't seen the definition yet. Define functions before you call them.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try This
&lt;/h2&gt;

&lt;p&gt;Create a file called &lt;code&gt;functions_practice.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Write three separate functions.&lt;/p&gt;

&lt;p&gt;First one: takes a temperature in Celsius and returns it converted to Fahrenheit. The formula is &lt;code&gt;(celsius * 9/5) + 32&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Second one: takes a person's name and birth year and prints a message like "Alan was born in 1999 and is 25 years old." Calculate the age inside the function. You'll need to hardcode the current year or store it in a variable.&lt;/p&gt;

&lt;p&gt;Third one: takes a list of numbers and returns the largest one without using Python's built-in &lt;code&gt;max()&lt;/code&gt; function. Think through how you'd find the largest number by going through the list one item at a time.&lt;/p&gt;

&lt;p&gt;Then call each function at least twice with different inputs to make sure they work correctly.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;You now know how to organize code into reusable blocks. The next post is about lists, which are the most important data structure you'll use in Python. You've seen them briefly but now you'll learn everything: how to add to them, remove from them, search them, sort them, and loop through them in useful ways.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Loop That Ate My Computer</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Sat, 18 Apr 2026 09:12:53 +0000</pubDate>
      <link>https://dev.to/yakhilesh/the-loop-that-ate-my-computer-46g7</link>
      <guid>https://dev.to/yakhilesh/the-loop-that-ate-my-computer-46g7</guid>
      <description>&lt;p&gt;Let me tell you what happened the first time I wrote a loop.&lt;/p&gt;

&lt;p&gt;I wanted to print numbers from 1 to 10. Simple enough. I had just read about while loops. I thought I understood them. So I wrote 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="n"&gt;number&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;number&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&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;I ran it.&lt;/p&gt;

&lt;p&gt;Numbers started printing. 1, 2, 3... wait. Same numbers? 1, 1, 1, 1, 1, 1, 1, 1...&lt;/p&gt;

&lt;p&gt;The terminal filled up. Lines kept coming. The computer fan started spinning faster. I sat there frozen for three full seconds before I remembered to press Ctrl+C to stop it.&lt;/p&gt;

&lt;p&gt;I had created an infinite loop. And I had no idea what I did wrong.&lt;/p&gt;

&lt;p&gt;That moment is where this post starts.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Even Is a Loop
&lt;/h2&gt;

&lt;p&gt;You already know Python runs your code top to bottom, one line at a time. Loops break that rule in a useful way. They say: run this block of code multiple times before moving on.&lt;/p&gt;

&lt;p&gt;Without loops, if you wanted to print numbers 1 through 10, you'd 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="mi"&gt;1&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="mi"&gt;2&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="mi"&gt;3&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="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="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="mi"&gt;6&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="mi"&gt;7&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="mi"&gt;8&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="mi"&gt;9&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="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's fine for 10. What about 1000? What about a million? What if you don't know the number in advance?&lt;/p&gt;

&lt;p&gt;That's where loops come in.&lt;/p&gt;

&lt;p&gt;Python has two kinds. The &lt;code&gt;for&lt;/code&gt; loop and the &lt;code&gt;while&lt;/code&gt; loop. They do similar jobs but in different situations. Learn both.&lt;/p&gt;




&lt;h2&gt;
  
  
  The For Loop
&lt;/h2&gt;

&lt;p&gt;A for loop goes through a collection of things, one by one, and runs your code for each one.&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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&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;Output:&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
6
7
8
9
10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ten lines. One instruction. That's the power.&lt;/p&gt;

&lt;p&gt;Let's break down what each part means.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for&lt;/code&gt; starts the loop.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;number&lt;/code&gt; is a variable you create right here. It gets a new value on each pass through the loop. You can name it anything. &lt;code&gt;n&lt;/code&gt;, &lt;code&gt;i&lt;/code&gt;, &lt;code&gt;num&lt;/code&gt;, whatever makes sense.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;in&lt;/code&gt; connects the variable to the collection.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;range(1, 11)&lt;/code&gt; creates a sequence of numbers starting at 1 and going up to but not including 11. So 1 through 10. The second number is always exclusive. Feels weird at first. You get used to it.&lt;/p&gt;

&lt;p&gt;The colon and indentation work the same as if statements. Everything indented under the &lt;code&gt;for&lt;/code&gt; line runs on each pass.&lt;/p&gt;




&lt;h2&gt;
  
  
  Range Has Three Versions
&lt;/h2&gt;

&lt;p&gt;Worth knowing all three now so they don't confuse you later.&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;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="c1"&gt;# 0, 1, 2, 3, 4  (starts at 0 by default)
&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;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# 1, 2, 3, 4, 5
&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;0&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# 0, 2, 4, 6, 8  (third number is the step)
&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="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="mi"&gt;0&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="mi"&gt;2&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;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;The step can also go backwards.&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;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="mi"&gt;10&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="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;print&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Looping Over a List
&lt;/h2&gt;

&lt;p&gt;Range isn't the only thing you can loop over. You can loop over any collection, including a list of items.&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;fruits&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;apple&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;banana&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;mango&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;grape&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;fruit&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;fruits&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;fruit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apple
banana
mango
grape
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable &lt;code&gt;fruit&lt;/code&gt; takes on each value in the list, one by one. First pass it's "apple", second pass "banana", and so on. The loop runs exactly four times because there are four items.&lt;/p&gt;

&lt;p&gt;This is something you'll do constantly in real code. Go through a list, do something with each item.&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;names&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;Alex&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;Priya&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;Sam&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;Jordan&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;name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;names&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;Hello, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Alex!
Hello, Priya!
Hello, Sam!
Hello, Jordan!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  The While Loop
&lt;/h2&gt;

&lt;p&gt;A while loop is different. Instead of going through a collection, it keeps running as long as a condition stays true.&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;number&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;number&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;number&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;Output:&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
6
7
8
9
10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same result as the for loop example. Different mechanism.&lt;/p&gt;

&lt;p&gt;The while loop checks the condition before every single pass. Is &lt;code&gt;number &amp;lt;= 10&lt;/code&gt;? Yes? Run the block. Check again. Yes? Run again. At some point &lt;code&gt;number&lt;/code&gt; becomes 11. Is &lt;code&gt;11 &amp;lt;= 10&lt;/code&gt;? No. Stop.&lt;/p&gt;

&lt;p&gt;Now you see my mistake from the opening. I forgot &lt;code&gt;number = number + 1&lt;/code&gt;. The number never changed. It stayed 1. The condition &lt;code&gt;1 &amp;lt; 10&lt;/code&gt; was always true. The loop never stopped.&lt;/p&gt;

&lt;p&gt;The golden rule for while loops: always make sure something inside the loop will eventually make the condition false. If nothing changes the condition, you loop forever.&lt;/p&gt;




&lt;h2&gt;
  
  
  Breaking Out Early
&lt;/h2&gt;

&lt;p&gt;Sometimes you want to stop a loop before it naturally finishes. &lt;code&gt;break&lt;/code&gt; does that.&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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&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;number&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;7&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;Found 7, stopping&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="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;Output:&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
6
Found 7, stopping
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The loop was set to run from 1 to 19. But when it hit 7, the &lt;code&gt;break&lt;/code&gt; statement stopped the whole loop immediately. Everything after 7 never ran.&lt;/p&gt;




&lt;h2&gt;
  
  
  Skipping One Pass
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;continue&lt;/code&gt; is different from &lt;code&gt;break&lt;/code&gt;. Instead of stopping the whole loop, it skips only the current pass and moves to the next one.&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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&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;number&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;5&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="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;Output:&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
6
7
8
9
10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5 is missing. When &lt;code&gt;number&lt;/code&gt; was 5, &lt;code&gt;continue&lt;/code&gt; fired and Python jumped straight to the next iteration. The &lt;code&gt;print(number)&lt;/code&gt; below it never ran for that one pass.&lt;/p&gt;




&lt;h2&gt;
  
  
  Keeping Count Inside a Loop
&lt;/h2&gt;

&lt;p&gt;A really common pattern: start a counter at 0, add to it inside 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;total&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;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;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;number&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;Sum of 1 to 10: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;total&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sum of 1 to 10: 55
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variable &lt;code&gt;total&lt;/code&gt; lives outside the loop but gets updated inside it on every pass. After the loop finishes, &lt;code&gt;total&lt;/code&gt; holds the final answer.&lt;/p&gt;

&lt;p&gt;This pattern shows up everywhere. In real AI code you'll use it to track losses, scores, counts, totals. The shape of the pattern is always the same: set up a variable before the loop, update it inside, use it after.&lt;/p&gt;




&lt;h2&gt;
  
  
  Nested Loops
&lt;/h2&gt;

&lt;p&gt;You can put a loop inside a loop. The inner one runs completely for every single pass of the outer one.&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;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="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="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;col&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;Row &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, Col &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;col&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Row 1, Col 1
Row 1, Col 2
Row 1, Col 3
Row 2, Col 1
Row 2, Col 2
Row 2, Col 3
Row 3, Col 1
Row 3, Col 2
Row 3, Col 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Outer loop runs 3 times. Each time it runs, the inner loop runs 3 times. Total: 9 lines.&lt;/p&gt;

&lt;p&gt;This matters more in AI than you'd expect. When you work with grids, matrices, images, you're often looping over rows and columns exactly like this.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Practical Example
&lt;/h2&gt;

&lt;p&gt;Let's use loops for something real. A simple number guessing game.&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;secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="n"&gt;attempts&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;guesses&lt;/span&gt; &lt;span class="o"&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;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&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;guess&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;guesses&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;attempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;attempts&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;if&lt;/span&gt; &lt;span class="n"&gt;guess&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;secret&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;Correct! You got it in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;attempts&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; attempt(s)&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;elif&lt;/span&gt; &lt;span class="n"&gt;guess&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;secret&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;Guess &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;guess&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: too low&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;Guess &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;guess&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: too high&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Guess 3: too low
Guess 9: too high
Correct! You got it in 3 attempt(s)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This combines everything you've learned so far. Variables, data types, conditions, and now loops. Notice how the pieces fit together.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Use Which
&lt;/h2&gt;

&lt;p&gt;A simple way to decide:&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;for&lt;/code&gt; when you know what you're looping over. A list of names. A range of numbers. A known collection of things.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;while&lt;/code&gt; when you don't know how many times you'll loop. Keep going until the user types "quit." Keep going until you find the answer. Keep going until some condition changes.&lt;/p&gt;

&lt;p&gt;Most of the time in Python, &lt;code&gt;for&lt;/code&gt; is what you want. &lt;code&gt;while&lt;/code&gt; is for specific situations where the end condition isn't a fixed number of steps.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Three Things That Go Wrong
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Going infinite.&lt;/strong&gt; You write a while loop and forget to update the condition. Solution: always find the line inside your while loop that changes the variable being checked. If that line doesn't exist, your loop runs forever.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Off by one.&lt;/strong&gt; &lt;code&gt;range(1, 10)&lt;/code&gt; gives you 1 through 9, not 1 through 10. The end number is not included. This bites everyone at least once. If your loop seems to stop one too early, add 1 to your range end.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modifying a list while looping over it.&lt;/strong&gt; This is a sneaky one. If you add or remove items from a list while a for loop is going through it, weird things happen. Loop over a copy instead, or collect your changes and apply them after the loop. You'll see why this matters more once you're working with data.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try This
&lt;/h2&gt;

&lt;p&gt;Create a file called &lt;code&gt;loops_practice.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Write code that does all three of these things separately.&lt;/p&gt;

&lt;p&gt;First: use a loop to print a multiplication table for any number you choose. So if you pick 5, it prints "5 x 1 = 5", "5 x 2 = 10" all the way to "5 x 10 = 50".&lt;/p&gt;

&lt;p&gt;Second: use a loop to add up all even numbers between 1 and 100 and print the total.&lt;/p&gt;

&lt;p&gt;Third: use a loop to go through this list and print only the names longer than 4 characters.&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;names&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;Alex&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;Priya&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;Sam&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;Jordan&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;Li&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;Valentina&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;Tom&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;Hints: the &lt;code&gt;%&lt;/code&gt; operator tells you if a number is even (even numbers have zero remainder when divided by 2). The &lt;code&gt;len()&lt;/code&gt; function tells you how long a string is.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Comes Next
&lt;/h2&gt;

&lt;p&gt;You can print 1000 items with one loop now. But what if you want to take a block of code and run it from different places in your program, with different inputs each time? That's a function. That's next.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>productivity</category>
      <category>python</category>
    </item>
    <item>
      <title>How Your Code Makes Decisions (And Gets Them Wrong)</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Sat, 18 Apr 2026 04:09:23 +0000</pubDate>
      <link>https://dev.to/yakhilesh/how-your-code-makes-decisions-and-gets-them-wrong-3l0g</link>
      <guid>https://dev.to/yakhilesh/how-your-code-makes-decisions-and-gets-them-wrong-3l0g</guid>
      <description>&lt;p&gt;&lt;em&gt;Picture this&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You're building a simple app. It checks if someone is old enough to sign up. Sounds easy. You have their age. You know the minimum age. You just need the program to say yes or no.&lt;/p&gt;

&lt;p&gt;But you don't know how to make the program choose. Right now your code just runs top to bottom, every line, every time. It doesn't skip anything. It doesn't branch. It has no judgment.&lt;/p&gt;

&lt;p&gt;That changes today.&lt;/p&gt;




&lt;p&gt;The ability to make decisions is what separates a real program from a list of instructions. Without it, every program would do the exact same thing every single time, no matter what. No reactions. No logic. No intelligence.&lt;/p&gt;

&lt;p&gt;If/else is how you give your code judgment.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Basic Idea
&lt;/h2&gt;

&lt;p&gt;An if statement says: check this condition. If it's true, run this code. If it's not true, skip it.&lt;/p&gt;

&lt;p&gt;That's really all it is.&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;20&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 can sign up&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You can sign up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now change age to 15 and run it again.&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;15&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 can sign up&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;Output:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Nothing. The condition was false so Python skipped the whole block.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Structure, Explained Line by Line
&lt;/h2&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;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 can sign up&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;code&gt;if&lt;/code&gt; is the keyword that starts a decision.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;age &amp;gt;= 18&lt;/code&gt; is the condition. Python checks this. It becomes either &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The colon &lt;code&gt;:&lt;/code&gt; at the end of the if line is required. Forget it and you get an error.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("You can sign up")&lt;/code&gt; is indented with 4 spaces. This is the block of code that runs when the condition is true. The indentation is how Python knows which code belongs to the if.&lt;/p&gt;

&lt;p&gt;This is important. Python uses indentation to understand structure. Not brackets, not semicolons, spaces. If your indentation is wrong, your code either crashes or does the wrong thing.&lt;/p&gt;




&lt;h2&gt;
  
  
  Adding the Else
&lt;/h2&gt;

&lt;p&gt;What if you want something to happen when the condition is false too?&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;15&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 can sign up&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;Sorry, you need to be 18 or older&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sorry, you need to be 18 or older
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;else&lt;/code&gt; catches everything the &lt;code&gt;if&lt;/code&gt; didn't. It runs when the condition is false. No condition needed for else, it's the default path.&lt;/p&gt;

&lt;p&gt;Change age to 25. The if runs, the else gets skipped. Change it to 10. The if gets skipped, the else runs. The two blocks are mutually exclusive. Only one ever runs.&lt;/p&gt;




&lt;h2&gt;
  
  
  More Than Two Options: elif
&lt;/h2&gt;

&lt;p&gt;Sometimes two options aren't enough.&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;72&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;90&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;Grade: 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;score&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="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;Grade: 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;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;Grade: 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;score&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;Grade: 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;Grade: F&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Grade: C
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;elif&lt;/code&gt; means "else if." It gives you additional conditions to check after the first one fails.&lt;/p&gt;

&lt;p&gt;Python checks from top to bottom and stops at the first true condition. Score is 72. Is it 90 or above? No. Is it 80 or above? No. Is it 70 or above? Yes. Print "Grade: C." Stop. The remaining elif and the else are ignored.&lt;/p&gt;

&lt;p&gt;This is why order matters. If you put &lt;code&gt;score &amp;gt;= 60&lt;/code&gt; first, a score of 95 would print "Grade: D" because 95 is also above 60 and Python would stop there.&lt;/p&gt;

&lt;p&gt;Try flipping the order and see what happens. Breaking things on purpose is one of the best ways to understand how they work.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Comparison Operators
&lt;/h2&gt;

&lt;p&gt;You've seen &lt;code&gt;&amp;gt;=&lt;/code&gt; already. Here are all the ways you can compare things.&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# equal to: 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;a&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# not equal to: 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;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# greater than: 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;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# less than: 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;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# greater than or equal to: 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;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# less than or equal to: False
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The one that trips people up most is &lt;code&gt;==&lt;/code&gt;. In Python, one equals sign &lt;code&gt;=&lt;/code&gt; means assignment. Put a value into a variable. Two equals signs &lt;code&gt;==&lt;/code&gt; means comparison. Are these two things equal?&lt;/p&gt;

&lt;p&gt;Inside an if statement, you almost always want &lt;code&gt;==&lt;/code&gt;. Using &lt;code&gt;=&lt;/code&gt; inside an if is one of the most common early mistakes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Combining Conditions
&lt;/h2&gt;

&lt;p&gt;What if you need two things to be true at the same 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="n"&gt;age&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;has_id&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_id&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;Come on in&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;Can&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;t let you in&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Come on in
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;and&lt;/code&gt; requires both conditions to be true. If either one is false, the whole thing is false.&lt;/p&gt;

&lt;p&gt;What if either condition being true is enough?&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;is_member&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="n"&gt;has_coupon&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;is_member&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;has_coupon&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 get a discount&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;Full price for you&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;You get a discount
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;or&lt;/code&gt; only needs one condition to be true.&lt;/p&gt;

&lt;p&gt;And &lt;code&gt;not&lt;/code&gt; flips a boolean. &lt;code&gt;not True&lt;/code&gt; becomes &lt;code&gt;False&lt;/code&gt;. &lt;code&gt;not False&lt;/code&gt; becomes &lt;code&gt;True&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="n"&gt;is_raining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;is_raining&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;Good day for a walk&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Good day for a walk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Conditions Inside Conditions
&lt;/h2&gt;

&lt;p&gt;You can put if statements inside other if statements. This is called nesting.&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;20&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="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;has_ticket&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;Enjoy the show&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 need a ticket&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;Must be 18 or older&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Enjoy the show
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each level of nesting gets another level of indentation. Works fine, but don't go too deep. After three or four levels it becomes hard to read. You'll learn cleaner ways to handle complex conditions as you go.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Real Example
&lt;/h2&gt;

&lt;p&gt;Let's build something slightly more real. A simple login check.&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;correct_username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;alex123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;correct_password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;python2024&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;alex123&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;wrongpassword&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;correct_username&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;correct_password&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;Login successful. Welcome back!&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;username&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;correct_username&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;correct_password&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;Wrong password. Try again.&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;Username not found.&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Wrong password. Try again.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change the password to &lt;code&gt;"python2024"&lt;/code&gt; and run again. Now try a completely wrong username. See how each path behaves differently.&lt;/p&gt;




&lt;h2&gt;
  
  
  Three Mistakes That Will Definitely Happen
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Mistake 1: Using = instead of == inside a condition&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="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;=&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;# wrong
&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;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;Error:&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="nb"&gt;SyntaxError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;invalid&lt;/span&gt; &lt;span class="n"&gt;syntax&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside an if condition, you almost never want &lt;code&gt;=&lt;/code&gt;. You want &lt;code&gt;==&lt;/code&gt; to compare.&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;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;# correct
&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;Exactly 18&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;strong&gt;Mistake 2: Missing the colon&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;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="c1"&gt;# wrong, no colon
&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;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;Error:&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="nb"&gt;SyntaxError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;expected&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;elif&lt;/code&gt;, and &lt;code&gt;else&lt;/code&gt; line ends with a colon. No exceptions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mistake 3: Wrong indentation&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;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;Adult&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# wrong, not indented
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Error:&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="nb"&gt;IndentationError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;indented&lt;/span&gt; &lt;span class="n"&gt;block&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;if&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="n"&gt;statement&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code inside an if block must be indented. Four spaces is the standard. VS Code will do this automatically when you press enter after a colon.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try This Yourself
&lt;/h2&gt;

&lt;p&gt;Create a file called &lt;code&gt;decisions.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Write a program that acts as a basic ticket pricing system. Store a person's age in a variable. Then print their ticket price based on these rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Under 5 years old: free&lt;/li&gt;
&lt;li&gt;5 to 12 years old: half price (pick any price you want)&lt;/li&gt;
&lt;li&gt;13 to 17: student price (a bit more)&lt;/li&gt;
&lt;li&gt;18 to 64: full adult price&lt;/li&gt;
&lt;li&gt;65 and above: senior discount&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Print a message that says the category and the price. Something like "Child ticket: 200 rupees" or whatever makes sense.&lt;/p&gt;

&lt;p&gt;Then change the age variable to five different ages and make sure each one prints the right thing.&lt;/p&gt;

&lt;p&gt;Hint: you need elif for this. Multiple conditions, in the right order.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;Your code can make decisions now. But right now it only makes that decision once. What if you want it to repeat something a hundred times without writing it a hundred times? That's what loops do, and that's the next post.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>ai</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Why "Hello" and 42 Are Completely Different Things</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Sat, 18 Apr 2026 04:07:07 +0000</pubDate>
      <link>https://dev.to/yakhilesh/why-hello-and-42-are-completely-different-things-1g9l</link>
      <guid>https://dev.to/yakhilesh/why-hello-and-42-are-completely-different-things-1g9l</guid>
      <description>&lt;p&gt;You finished the last post. You stored your name in a variable. You stored your age. Both worked.&lt;/p&gt;

&lt;p&gt;But here's something that probably confused you. When you stored your name, you used quotes. When you stored your age, you didn't. And when you tried to combine them in a print statement, Python complained unless you added that &lt;code&gt;str()&lt;/code&gt; thing.&lt;/p&gt;

&lt;p&gt;Why? Your name and your age are both just information. Why does Python care about the difference?&lt;/p&gt;

&lt;p&gt;Because to Python, they are not the same kind of thing at all. And understanding this, really understanding it, will save you from dozens of confusing errors in the future.&lt;/p&gt;




&lt;h2&gt;
  
  
  What You'll Actually Get From This Post
&lt;/h2&gt;

&lt;p&gt;By the end, you'll know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What data types are and why they exist&lt;/li&gt;
&lt;li&gt;The four main types you'll use constantly: strings, integers, floats, and booleans&lt;/li&gt;
&lt;li&gt;How to check what type something is&lt;/li&gt;
&lt;li&gt;How to convert between types&lt;/li&gt;
&lt;li&gt;Why type errors happen and how to read them&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why Types Exist
&lt;/h2&gt;

&lt;p&gt;Think about a calculator. You can add two numbers. You get a number back.&lt;/p&gt;

&lt;p&gt;Now try to add a name and a number. What's "Alex" plus 25? It makes no sense. There's no meaningful answer.&lt;/p&gt;

&lt;p&gt;Your calculator doesn't let you do that because it knows numbers and text are different kinds of things. Python thinks the same way.&lt;/p&gt;

&lt;p&gt;When Python stores a value, it doesn't just store the value. It also remembers what kind of value it is. A number behaves differently from text. A yes/no value behaves differently from a decimal. Tracking this is what types are for.&lt;/p&gt;

&lt;p&gt;There are four types you'll use in almost every program you ever write. Learn these four well before worrying about anything else.&lt;/p&gt;




&lt;h2&gt;
  
  
  Type 1: Strings (Text)
&lt;/h2&gt;

&lt;p&gt;A string is any text. A name. A sentence. A word. An email address. A tweet. Even a single letter.&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;Akhil&lt;/span&gt;&lt;span class="sh"&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;Mumbai&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I am learning Python&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;single_letter&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&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;number_as_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;42&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The quotes are what make something a string. Single quotes or double quotes, both work. Pick one style and stick with 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="n"&gt;name1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Akhil&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;    &lt;span class="c1"&gt;# double quotes
&lt;/span&gt;&lt;span class="n"&gt;name2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Akhil&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;    &lt;span class="c1"&gt;# single quotes
# both are identical strings
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strings can be combined with &lt;code&gt;+&lt;/code&gt;. This is called concatenation. You're just sticking text together.&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;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Good&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;morning&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;combined&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first&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="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;second&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;combined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;You can also repeat a string with &lt;code&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="n"&gt;line&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="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;20&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;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;Silly example but you'll actually use this to format output in real programs.&lt;/p&gt;

&lt;p&gt;One more thing. Strings have a length. &lt;code&gt;len()&lt;/code&gt; tells you how many characters are in a string.&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;Jack&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="nf"&gt;len&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;Output:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Type 2: Integers (Whole Numbers)
&lt;/h2&gt;

&lt;p&gt;An integer is any whole number. No decimals. No quotes.&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="n"&gt;score&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;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2024&lt;/span&gt;
&lt;span class="n"&gt;number_of_posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;130&lt;/span&gt;
&lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Negative numbers work fine. Just put a minus sign.&lt;/p&gt;

&lt;p&gt;Integers support all the math you'd expect.&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# addition: 13
&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;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# subtraction: 7
&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;a&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# multiplication: 30
&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;a&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# division: 3.3333...
&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;a&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# floor division: 3 (drops the decimal)
&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;a&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&gt;# modulo: 1 (the remainder)
&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;a&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# power: 1000 (10 to the power of 3)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;13
7
30
3.3333333333333335
3
1
1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;//&lt;/code&gt; operator is useful when you need a whole number result from division. The &lt;code&gt;%&lt;/code&gt; operator gives you the remainder after division. You'll use both more than you think.&lt;/p&gt;




&lt;h2&gt;
  
  
  Type 3: Floats (Decimal Numbers)
&lt;/h2&gt;

&lt;p&gt;A float is any number with a decimal point.&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;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;99.99&lt;/span&gt;
&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;5.11&lt;/span&gt;
&lt;span class="n"&gt;pi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;3.14159&lt;/span&gt;
&lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;36.6&lt;/span&gt;
&lt;span class="n"&gt;small&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.001&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Floats work with all the same math operators as integers.&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;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;99.99&lt;/span&gt;
&lt;span class="n"&gt;discount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.10&lt;/span&gt;
&lt;span class="n"&gt;final&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;discount&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;Final price: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;final&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Final price: 89.991
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One thing to know. Computers store decimals in a way that sometimes causes tiny rounding errors.&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="mf"&gt;0.1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;That's not a bug in your code. That's how computers handle decimal math internally. For most purposes it doesn't matter. If you're building something that needs precise decimal handling, like financial software, Python has tools for that. But don't worry about it now.&lt;/p&gt;




&lt;h2&gt;
  
  
  Type 4: Booleans (True or False)
&lt;/h2&gt;

&lt;p&gt;A boolean has exactly two possible values: &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;. Capital T and capital F. That's important.&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;is_raining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;has_finished&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="n"&gt;is_logged_in&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Booleans seem too simple to be useful. They're actually one of the most important types in programming. Every decision your code makes comes down to a boolean. Every &lt;code&gt;if&lt;/code&gt; statement (which you'll learn in the next post) checks whether something is &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can create booleans directly like above, or they come from comparisons.&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;&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;# 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;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&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;!=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    &lt;span class="c1"&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;# False
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

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

&lt;/div&gt;



&lt;p&gt;Note: &lt;code&gt;==&lt;/code&gt; checks if two things are equal. One &lt;code&gt;=&lt;/code&gt; assigns a value. Two &lt;code&gt;==&lt;/code&gt; compares values. This trips up beginners constantly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Checking the Type of Something
&lt;/h2&gt;

&lt;p&gt;Python has a built-in function called &lt;code&gt;type()&lt;/code&gt; that tells you what type something is.&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;Akhil&lt;/span&gt;&lt;span class="sh"&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;22&lt;/span&gt;
&lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;5.11&lt;/span&gt;
&lt;span class="n"&gt;is_student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&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="nf"&gt;type&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&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="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;height&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="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_student&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;class 'str'&amp;gt;
&amp;lt;class 'int'&amp;gt;
&amp;lt;class 'float'&amp;gt;
&amp;lt;class 'bool'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;str&lt;/code&gt; means string. &lt;code&gt;int&lt;/code&gt; means integer. &lt;code&gt;float&lt;/code&gt; means float. &lt;code&gt;bool&lt;/code&gt; means boolean.&lt;/p&gt;

&lt;p&gt;You'll use &lt;code&gt;type()&lt;/code&gt; a lot when debugging. When something behaves unexpectedly, checking what type it is often reveals the problem immediately.&lt;/p&gt;




&lt;h2&gt;
  
  
  Converting Between Types
&lt;/h2&gt;

&lt;p&gt;This is where a lot of beginner errors come from. Python won't automatically convert between types when it doesn't know what you want. You have to tell it.&lt;/p&gt;

&lt;p&gt;The conversion functions are &lt;code&gt;str()&lt;/code&gt;, &lt;code&gt;int()&lt;/code&gt;, and &lt;code&gt;float()&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="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="c1"&gt;# integer
&lt;/span&gt;&lt;span class="n"&gt;age_as_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;      &lt;span class="c1"&gt;# convert to string: "25"
&lt;/span&gt;
&lt;span class="n"&gt;price_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;99.99&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;        &lt;span class="c1"&gt;# string
&lt;/span&gt;&lt;span class="n"&gt;price&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="n"&gt;price_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# convert to float: 99.99
&lt;/span&gt;
&lt;span class="n"&gt;score_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;42&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;           &lt;span class="c1"&gt;# string
&lt;/span&gt;&lt;span class="n"&gt;score&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="n"&gt;score_text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# convert to integer: 42
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A very common situation: you have a number you want to put inside a sentence.&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="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I am &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; years old&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;   &lt;span class="c1"&gt;# this breaks
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: can only concatenate str (not "int") to str
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python says: you're trying to combine text and a number with &lt;code&gt;+&lt;/code&gt;. I don't know what you want. I won't guess.&lt;/p&gt;

&lt;p&gt;Fix:&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="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I am &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&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; years old&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;   &lt;span class="c1"&gt;# works
&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;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I am 25 years old
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, the easier way, use an f-string. f-strings handle the conversion automatically.&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="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I am &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; years old&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# works, no str() needed
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I am 25 years old
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get comfortable with f-strings early. They prevent this whole category of errors.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Most Common Type Mistakes
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Mistake 1: Forgetting that numbers in quotes are strings&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="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;25&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;          &lt;span class="c1"&gt;# this is a string, not a number
&lt;/span&gt;&lt;span class="n"&gt;next_year&lt;/span&gt; &lt;span class="o"&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;1&lt;/span&gt;  &lt;span class="c1"&gt;# breaks
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: can only concatenate str (not "int") to str
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The number 25 is in quotes, so Python treats it as text. You can't do math with text.&lt;/p&gt;

&lt;p&gt;Fix:&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="c1"&gt;# no quotes, it's a number now
&lt;/span&gt;&lt;span class="n"&gt;next_year&lt;/span&gt; &lt;span class="o"&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;1&lt;/span&gt;  &lt;span class="c1"&gt;# works: 26
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Mistake 2: Using = instead of == when comparing&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="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;# wrong
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SyntaxError: invalid syntax
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;=&lt;/code&gt; assigns. &lt;code&gt;==&lt;/code&gt; compares. Use &lt;code&gt;==&lt;/code&gt; to check if two things are equal.&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;# correct: True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Mistake 3: Wrong case on True and False&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="n"&gt;is_done&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt;   &lt;span class="c1"&gt;# wrong
&lt;/span&gt;&lt;span class="n"&gt;is_done&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;   &lt;span class="c1"&gt;# correct
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python is case-sensitive. &lt;code&gt;true&lt;/code&gt; is not the same as &lt;code&gt;True&lt;/code&gt;. &lt;code&gt;True&lt;/code&gt; with a capital T is the boolean. &lt;code&gt;true&lt;/code&gt; with a lowercase t is just a variable name Python doesn't know about.&lt;/p&gt;




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

&lt;p&gt;Here's a short program that uses all four types.&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;Akhil&lt;/span&gt;&lt;span class="sh"&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="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;5.9&lt;/span&gt;
&lt;span class="n"&gt;is_student&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&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="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Name: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&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;Age: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;age&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;Height: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; feet&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;Is a student: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;is_student&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;Type of name: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;type&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="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;Type of age: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&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;span class="n"&gt;next_age&lt;/span&gt; &lt;span class="o"&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;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;Next year I&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ll be &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;next_age&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;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Hello, &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&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;!&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;greeting&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name: Akhil
Age: 25
Height: 5.9 feet
Is a student: True
Type of name: &amp;lt;class 'str'&amp;gt;
Type of age: &amp;lt;class 'int'&amp;gt;
Next year I'll be 26
Hello, Akhil!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type everything. Run it. Change the values. Break it on purpose. See what errors come up. Fix them.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try This Yourself
&lt;/h2&gt;

&lt;p&gt;Create a file called &lt;code&gt;types_practice.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Make variables for all of this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your full name (string)&lt;/li&gt;
&lt;li&gt;Your age (integer)&lt;/li&gt;
&lt;li&gt;Your height in any unit (float)&lt;/li&gt;
&lt;li&gt;Whether you have a pet (boolean)&lt;/li&gt;
&lt;li&gt;The year you were born (integer)&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Print each one with a label using f-strings&lt;/li&gt;
&lt;li&gt;Calculate how many years until you turn 50 and print it&lt;/li&gt;
&lt;li&gt;Print the type of each variable using &lt;code&gt;type()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Expected output should look something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Name: Akhilesh Yadav
Age: 22
Height: 5.9 feet
Has a pet: False
Birth year: 1999
Years until 50: 25
Type of name: &amp;lt;class 'str'&amp;gt;
Type of age: &amp;lt;class 'int'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hints: subtraction works with &lt;code&gt;-&lt;/code&gt;. The year you turn 50 is your birth year plus 50.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;You can store information now. The next step is making your code take different actions depending on what that information is. That's what if/else statements do, and it's where programming starts to feel like actual decision-making.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>productivity</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Your First Python Program Runs. Now What?</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Sat, 18 Apr 2026 04:04:18 +0000</pubDate>
      <link>https://dev.to/yakhilesh/your-first-python-program-runs-now-what-514c</link>
      <guid>https://dev.to/yakhilesh/your-first-python-program-runs-now-what-514c</guid>
      <description>&lt;p&gt;You downloaded Python. You watched the installation video. You followed along. You opened the terminal and typed that command.&lt;br&gt;&lt;br&gt;
And it worked.  &lt;/p&gt;

&lt;p&gt;"Hello, World!" appeared on the screen.  &lt;/p&gt;

&lt;p&gt;You stared at it for a second. Then you thought: okay, but what just happened? And what do I do now?  &lt;/p&gt;

&lt;p&gt;That's exactly where this post starts. Not from "let's learn programming theory." From the moment after it works, when the real questions begin. :contentReference[oaicite:0]{index=0}&lt;/p&gt;


&lt;h2&gt;
  
  
  What You'll Actually Get From This Post
&lt;/h2&gt;

&lt;p&gt;By the time you finish reading and writing the code yourself, you'll know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What Python actually is and what "running a program" means
&lt;/li&gt;
&lt;li&gt;How to create a Python file and run it from the terminal
&lt;/li&gt;
&lt;li&gt;What variables are and how to use them
&lt;/li&gt;
&lt;li&gt;The three most common beginner mistakes and how to fix them
&lt;/li&gt;
&lt;li&gt;How to use &lt;code&gt;print()&lt;/code&gt; to see what's happening in your code
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it. No loops. No functions. No classes. Just the foundation, done properly.&lt;/p&gt;


&lt;h2&gt;
  
  
  What Python Actually Is
&lt;/h2&gt;

&lt;p&gt;Python is a language. Like English or Spanish, but for talking to computers instead of people.  &lt;/p&gt;

&lt;p&gt;Your computer only understands one thing at the deepest level: numbers. Specifically, zeros and ones. Everything your computer does, every app, every website, every video game, is ultimately zeros and ones being processed by hardware.  &lt;/p&gt;

&lt;p&gt;Writing zeros and ones directly would be insane. So over time, people invented layers on top. Assembly language. Then C. Then higher-level languages like Python. Each layer makes it easier for humans to write instructions, and something else handles translating those instructions into what the machine actually understands.  &lt;/p&gt;

&lt;p&gt;Python is one of the highest-level, most readable programming languages that exists. When you write Python, it reads almost like English. That's not an accident. It was designed that way.  &lt;/p&gt;

&lt;p&gt;When you "run a Python program," here's what actually happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You write code in a &lt;code&gt;.py&lt;/code&gt; file. It's just text. Plain text in a file that ends with &lt;code&gt;.py&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;You tell Python to run that file.
&lt;/li&gt;
&lt;li&gt;Python reads your file from top to bottom, line by line.
&lt;/li&gt;
&lt;li&gt;It executes each line and produces output.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's the whole model. Top to bottom. One line at a time. That's how Python thinks.&lt;/p&gt;


&lt;h2&gt;
  
  
  Setting Up: The Two Things You Need
&lt;/h2&gt;

&lt;p&gt;You need two things installed: Python and a place to write code.  &lt;/p&gt;

&lt;p&gt;For writing code, use VS Code. It's free, it's what most developers use, and it makes Python easier to work with.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;YT:&lt;/strong&gt; How to install Python and VS Code for beginners 2024  &lt;/p&gt;

&lt;p&gt;Once both are installed, open VS Code. Open a folder somewhere on your computer where you'll keep your code. Something like &lt;code&gt;ai_ml_journey&lt;/code&gt; in your Documents folder. Create a new file inside it. Name it &lt;code&gt;lesson01.py&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.py&lt;/code&gt; extension tells VS Code and your computer: this is a Python file.&lt;/p&gt;


&lt;h2&gt;
  
  
  Your First Real Program
&lt;/h2&gt;

&lt;p&gt;Open &lt;code&gt;lesson01.py&lt;/code&gt; and type this exactly. Don't paste it. Type 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;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;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 Akhil&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;I am learning Python&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;Save the file.  &lt;/p&gt;

&lt;p&gt;Now open your terminal (in VS Code you can press &lt;code&gt;Ctrl + backtick&lt;/code&gt; on Windows/Linux, or &lt;code&gt;Cmd + backtick&lt;/code&gt; on Mac). Type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python lesson01.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Press enter. You should see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, World!
My name is Akhil
I am learning Python
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's a program. Three instructions. Python reads the first line, executes it, prints the text. Then reads the second, executes it, prints. Then the third.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;print()&lt;/code&gt; is a function. It takes whatever you put inside the parentheses and displays it on the screen. The text inside the quotes is called a string. More on that in the next post.  &lt;/p&gt;

&lt;p&gt;Change "Akhil" to your actual name if needed. Save. Run it again. See your name appear. That's your first customization of a program.&lt;/p&gt;




&lt;h2&gt;
  
  
  Variables: Storing Information
&lt;/h2&gt;

&lt;p&gt;Every program needs to remember things. What's the user's name? What number did they enter? What was the result of a calculation?  &lt;/p&gt;

&lt;p&gt;Variables are how you store information and give it a name.&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;Akhil&lt;/span&gt;&lt;span class="sh"&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;22&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;Mumbai&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;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="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;city&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run this. Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Akhil
22
Mumbai
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A variable is a name that points to a value. When you write &lt;code&gt;name = "Akhil"&lt;/code&gt;, you're telling Python: create a box, label it &lt;code&gt;name&lt;/code&gt;, and put the value &lt;code&gt;"Akhil"&lt;/code&gt; inside it. Later, when you write &lt;code&gt;print(name)&lt;/code&gt;, Python goes to the box labeled &lt;code&gt;name&lt;/code&gt;, takes out what's inside, and prints it.  &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;=&lt;/code&gt; sign in Python doesn't mean "equals" the way it does in math. It means "assign." Put the right side into the box labeled on the left side.&lt;/p&gt;




&lt;h2&gt;
  
  
  Using Variables Together
&lt;/h2&gt;

&lt;p&gt;Variables become powerful when you combine them.&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;Akhil&lt;/span&gt;&lt;span class="sh"&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;22&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;Mumbai&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="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;name&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;I am &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&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; years old&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;I live in &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;My name is Akhil
I am 22 years old
I live in Mumbai
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice &lt;code&gt;str(age)&lt;/code&gt;. The age variable holds a number, not text. When you try to combine a number and text with &lt;code&gt;+&lt;/code&gt;, Python gets confused. &lt;code&gt;str()&lt;/code&gt; converts the number into text so you can combine them. This is one of the most common errors beginners hit.  &lt;/p&gt;

&lt;p&gt;There's also a cleaner way to do this. Python has something called f-strings. They're easier to read and you'll use them constantly.&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;Akhil&lt;/span&gt;&lt;span class="sh"&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;22&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;Mumbai&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;My name is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&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;I am &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; years old&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;I live in &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;city&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; before the opening quote turns it into an f-string. Anything inside &lt;code&gt;{}&lt;/code&gt; gets replaced with the value of that variable. Same output, cleaner code. Use f-strings.&lt;/p&gt;




&lt;h2&gt;
  
  
  Changing Variables
&lt;/h2&gt;

&lt;p&gt;Variables can change. That's literally why they're called 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;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;score&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;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&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;Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;score&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;score&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&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;Score: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;score&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Score: 0
Score: 10
Score: 15
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Line 3 is interesting. &lt;code&gt;score = score + 5&lt;/code&gt; means: take the current value of &lt;code&gt;score&lt;/code&gt; (which is 10), add 5 to it, and store the result back in &lt;code&gt;score&lt;/code&gt;. So &lt;code&gt;score&lt;/code&gt; becomes 15.  &lt;/p&gt;

&lt;p&gt;This pattern shows up constantly in programming. You'll use it for counters, running totals, tracking progress.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Everyone Gets Wrong at the Start
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mistake 1: Forgetting quotes around text
&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Akhil&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python sees &lt;code&gt;Akhil&lt;/code&gt; without quotes and thinks it's a variable. It doesn't exist.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NameError: name 'Akhil' is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&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="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;Akhil&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Mistake 2: Mixing text and numbers without converting
&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&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;I am &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; years old&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;strong&gt;Error:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: can only concatenate str (not "int") to str
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&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="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;I am &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; years old&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;
  
  
  Mistake 3: Wrong indentation
&lt;/h3&gt;

&lt;p&gt;Python uses indentation (spaces at the start of a line) to understand structure.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IndentationError: unexpected indent
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Remove extra spaces at the start.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Full Working Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;first_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;Akhil&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;last_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;Yadav&lt;/span&gt;&lt;span class="sh"&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;22&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;Mumbai&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;learning&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Python&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;full_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first_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; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;last_name&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;Name: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;full_name&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;Age: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;age&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;City: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;city&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;Currently learning: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;learning&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;Goal: Become an AI engineer&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;Name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Akhil Yadav&lt;/span&gt;
&lt;span class="na"&gt;Age&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;22&lt;/span&gt;
&lt;span class="na"&gt;City&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Mumbai&lt;/span&gt;
&lt;span class="na"&gt;Currently learning&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Python&lt;/span&gt;
&lt;span class="na"&gt;Goal&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Become an AI engineer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice &lt;code&gt;full_name = first_name + " " + last_name&lt;/code&gt;. The &lt;code&gt;" "&lt;/code&gt; adds a space between first and last name.&lt;/p&gt;




&lt;h2&gt;
  
  
  Try This Yourself
&lt;/h2&gt;

&lt;p&gt;Create a new file called &lt;code&gt;about_me.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Write a program that stores your real information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name
&lt;/li&gt;
&lt;li&gt;age
&lt;/li&gt;
&lt;li&gt;city
&lt;/li&gt;
&lt;li&gt;interest
&lt;/li&gt;
&lt;li&gt;goal
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hi, my name is [your name].
I'm [your age] years old and I live in [your city].
I'm interested in [your interest].
My goal is [your goal].
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make everything come from variables.&lt;/p&gt;

&lt;p&gt;Run it. Change a variable. Run again. Notice how output changes.&lt;/p&gt;




&lt;h2&gt;
  
  
  What's Next
&lt;/h2&gt;

&lt;p&gt;The next post covers &lt;strong&gt;data types&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Right now you've been storing text and numbers without really understanding what's different about them.  &lt;/p&gt;

&lt;p&gt;And that difference matters more than it seems.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programmingforbeginners</category>
      <category>learntocode</category>
      <category>codingjourney</category>
    </item>
    <item>
      <title>From Zero to AI Engineer: Here's the Exact Path (And Why Most People Never Finish)</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Fri, 17 Apr 2026 06:13:31 +0000</pubDate>
      <link>https://dev.to/yakhilesh/from-zero-to-ai-engineer-heres-the-exact-path-and-why-most-people-never-finish-4kd4</link>
      <guid>https://dev.to/yakhilesh/from-zero-to-ai-engineer-heres-the-exact-path-and-why-most-people-never-finish-4kd4</guid>
      <description>&lt;h1&gt;
  
  
  The Real Roadmap to Learning AI/ML
&lt;/h1&gt;

&lt;p&gt;There’s a search millions of people have made:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"How to learn AI."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You probably made it too.&lt;/p&gt;

&lt;p&gt;You get back a wall of options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Courses
&lt;/li&gt;
&lt;li&gt;Bootcamps
&lt;/li&gt;
&lt;li&gt;YouTube playlists
&lt;/li&gt;
&lt;li&gt;Reddit debates about needing a PhD
&lt;/li&gt;
&lt;li&gt;Blog posts promising fast results
&lt;/li&gt;
&lt;li&gt;Roadmaps jumping straight into advanced tools
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You try one. It’s too advanced.&lt;br&gt;&lt;br&gt;
You try another. It assumes prior knowledge.&lt;br&gt;&lt;br&gt;
You try a beginner tutorial. It feels too basic.&lt;/p&gt;

&lt;p&gt;After a few days, you’re overwhelmed and stuck.&lt;/p&gt;

&lt;p&gt;This isn’t a content problem.&lt;br&gt;&lt;br&gt;
It’s a clarity problem.&lt;/p&gt;

&lt;p&gt;This is that clarity.&lt;/p&gt;




&lt;h1&gt;
  
  
  What This Series Actually Is
&lt;/h1&gt;

&lt;p&gt;This is a &lt;strong&gt;complete, step-by-step roadmap&lt;/strong&gt; to becoming an AI Engineer.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No shortcuts
&lt;/li&gt;
&lt;li&gt;No fluff
&lt;/li&gt;
&lt;li&gt;No “buy my course later”
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You’ll go from:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Installing Python → Building real AI systems&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Machine Learning
&lt;/li&gt;
&lt;li&gt;Deep Learning
&lt;/li&gt;
&lt;li&gt;Transformers
&lt;/li&gt;
&lt;li&gt;LLMs
&lt;/li&gt;
&lt;li&gt;AI Agents
&lt;/li&gt;
&lt;li&gt;MLOps
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Total: 130 focused posts&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Each post = one concept.&lt;/p&gt;

&lt;p&gt;By the end:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You’ll build real projects
&lt;/li&gt;
&lt;li&gt;You’ll understand how systems actually work
&lt;/li&gt;
&lt;li&gt;You’ll have a strong GitHub portfolio
&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  The Honest Reality Before You Start
&lt;/h1&gt;

&lt;p&gt;Let’s be real.&lt;/p&gt;

&lt;p&gt;Most people quit.&lt;/p&gt;

&lt;p&gt;Not because they’re not smart&lt;br&gt;&lt;br&gt;
But because they hit a wall.&lt;/p&gt;

&lt;h3&gt;
  
  
  What to expect:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This will take &lt;strong&gt;6–9 months&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Some parts will feel slow&lt;/li&gt;
&lt;li&gt;Some parts will feel hard&lt;/li&gt;
&lt;li&gt;Some parts will make no sense at first&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s normal.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Confusion = you're learning something new&lt;/p&gt;
&lt;/blockquote&gt;

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

&lt;ul&gt;
&lt;li&gt;Don’t skip phases
&lt;/li&gt;
&lt;li&gt;Don’t just read → &lt;strong&gt;write code&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Don’t rush → focus on understanding
&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  The 11 Phases
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Phase 1: Python That Actually Works
&lt;/h2&gt;

&lt;p&gt;Learn Python in a &lt;strong&gt;practical way&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables, loops, conditions
&lt;/li&gt;
&lt;li&gt;Functions, classes
&lt;/li&gt;
&lt;li&gt;File handling
&lt;/li&gt;
&lt;li&gt;Error handling
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Build scripts that read, process, and save data&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;15 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 2: Math That Makes AI Possible
&lt;/h2&gt;

&lt;p&gt;Not theory. Just what you need.&lt;/p&gt;

&lt;p&gt;Topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vectors &amp;amp; matrices
&lt;/li&gt;
&lt;li&gt;Dot products
&lt;/li&gt;
&lt;li&gt;Matrix multiplication
&lt;/li&gt;
&lt;li&gt;Derivatives &amp;amp; gradient descent
&lt;/li&gt;
&lt;li&gt;Probability &amp;amp; statistics
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Understand what your model is doing internally&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;11 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 3: Data Handling &amp;amp; Exploration
&lt;/h2&gt;

&lt;p&gt;Most real AI work = data work.&lt;/p&gt;

&lt;p&gt;Tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NumPy
&lt;/li&gt;
&lt;li&gt;Pandas
&lt;/li&gt;
&lt;li&gt;Matplotlib / Seaborn / Plotly
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Skills:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cleaning data
&lt;/li&gt;
&lt;li&gt;Exploring datasets
&lt;/li&gt;
&lt;li&gt;Finding patterns
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Turn raw data into usable insights&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;13 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 4: SQL for Data Work
&lt;/h2&gt;

&lt;p&gt;Data lives in databases.&lt;/p&gt;

&lt;p&gt;Topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SELECT, WHERE
&lt;/li&gt;
&lt;li&gt;Joins
&lt;/li&gt;
&lt;li&gt;Aggregations
&lt;/li&gt;
&lt;li&gt;Subqueries
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Work with real-world data&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;6 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 5: Dev Tools That Matter
&lt;/h2&gt;

&lt;p&gt;Essential tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Git &amp;amp; GitHub
&lt;/li&gt;
&lt;li&gt;Jupyter Notebook
&lt;/li&gt;
&lt;li&gt;Google Colab
&lt;/li&gt;
&lt;li&gt;Virtual environments
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Make your workflow reliable&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;5 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 6: Machine Learning Core
&lt;/h2&gt;

&lt;p&gt;Core algorithms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linear Regression
&lt;/li&gt;
&lt;li&gt;Logistic Regression
&lt;/li&gt;
&lt;li&gt;Decision Trees
&lt;/li&gt;
&lt;li&gt;Random Forest
&lt;/li&gt;
&lt;li&gt;XGBoost
&lt;/li&gt;
&lt;li&gt;SVM
&lt;/li&gt;
&lt;li&gt;KNN
&lt;/li&gt;
&lt;li&gt;Naive Bayes
&lt;/li&gt;
&lt;li&gt;K-Means, PCA
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model evaluation
&lt;/li&gt;
&lt;li&gt;Overfitting
&lt;/li&gt;
&lt;li&gt;Feature engineering
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Build your first complete ML project&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;21 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 7: Deep Learning
&lt;/h2&gt;

&lt;p&gt;Topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Neural Networks
&lt;/li&gt;
&lt;li&gt;Backpropagation
&lt;/li&gt;
&lt;li&gt;CNNs
&lt;/li&gt;
&lt;li&gt;RNNs, LSTMs
&lt;/li&gt;
&lt;li&gt;Autoencoders, GANs
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Framework:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PyTorch
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Understand how deep learning actually works&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;15 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 8: NLP &amp;amp; LLMs
&lt;/h2&gt;

&lt;p&gt;Topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tokenization
&lt;/li&gt;
&lt;li&gt;Embeddings
&lt;/li&gt;
&lt;li&gt;Attention mechanism
&lt;/li&gt;
&lt;li&gt;Transformers
&lt;/li&gt;
&lt;li&gt;BERT, GPT
&lt;/li&gt;
&lt;li&gt;HuggingFace
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chatbots
&lt;/li&gt;
&lt;li&gt;RAG systems
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Build and understand modern AI systems&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;16 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 9: AI Applications
&lt;/h2&gt;

&lt;p&gt;From model → real product&lt;/p&gt;

&lt;p&gt;Tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LangChain
&lt;/li&gt;
&lt;li&gt;FastAPI
&lt;/li&gt;
&lt;li&gt;Streamlit
&lt;/li&gt;
&lt;li&gt;Docker
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Build and deploy real apps&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;8 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 10: AI Agents
&lt;/h2&gt;

&lt;p&gt;Next-gen AI systems&lt;/p&gt;

&lt;p&gt;Topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function calling
&lt;/li&gt;
&lt;li&gt;Multi-step reasoning
&lt;/li&gt;
&lt;li&gt;Memory systems
&lt;/li&gt;
&lt;li&gt;Multi-agent systems
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Build intelligent systems that take actions&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;10 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Phase 11: MLOps (Production AI)
&lt;/h2&gt;

&lt;p&gt;Topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MLflow
&lt;/li&gt;
&lt;li&gt;DVC
&lt;/li&gt;
&lt;li&gt;CI/CD
&lt;/li&gt;
&lt;li&gt;Monitoring
&lt;/li&gt;
&lt;li&gt;A/B testing
&lt;/li&gt;
&lt;li&gt;Airflow
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Deploy real-world AI systems&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;10 posts&lt;/strong&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  How to Use This Roadmap
&lt;/h1&gt;

&lt;p&gt;Follow this approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the concept
&lt;/li&gt;
&lt;li&gt;Write the code yourself
&lt;/li&gt;
&lt;li&gt;Break things and fix them
&lt;/li&gt;
&lt;li&gt;Rebuild from memory
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After each phase:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build a small project
&lt;/li&gt;
&lt;li&gt;Upload it to GitHub
&lt;/li&gt;
&lt;li&gt;Write a README
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When stuck:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Stay with the problem longer than comfortable&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s where real learning happens.&lt;/p&gt;




&lt;h1&gt;
  
  
  One More Thing
&lt;/h1&gt;

&lt;p&gt;You &lt;em&gt;can&lt;/em&gt; learn AI.&lt;/p&gt;

&lt;p&gt;The real question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Will you keep going when it gets hard?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because it will.&lt;/p&gt;

&lt;p&gt;Every AI engineer you see today:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Got stuck
&lt;/li&gt;
&lt;li&gt;Felt confused
&lt;/li&gt;
&lt;li&gt;Wanted to quit
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They just didn’t stop.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>learning</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Complete AI/ML Engineer Guide for 2026</title>
      <dc:creator>Akhilesh</dc:creator>
      <pubDate>Mon, 16 Feb 2026 15:35:05 +0000</pubDate>
      <link>https://dev.to/yakhilesh/complete-aiml-engineer-guide-for-2026-p1l</link>
      <guid>https://dev.to/yakhilesh/complete-aiml-engineer-guide-for-2026-p1l</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
Introduction and Career Overview
&lt;/li&gt;
&lt;li&gt;
Mathematical Foundations
&lt;/li&gt;
&lt;li&gt;
Programming Fundamentals
&lt;/li&gt;
&lt;li&gt;
Classical Machine Learning
&lt;/li&gt;
&lt;li&gt;
Deep Learning Fundamentals
&lt;/li&gt;
&lt;li&gt;
Natural Language Processing
&lt;/li&gt;
&lt;li&gt;
Large Language Models (LLMs) and Modern NLP
&lt;/li&gt;
&lt;li&gt;
Computer Vision
&lt;/li&gt;
&lt;li&gt;
Advanced AI/ML Concepts (2026)
&lt;/li&gt;
&lt;li&gt;
MLOps and Production Systems
&lt;/li&gt;
&lt;li&gt;
Tools and Frameworks
&lt;/li&gt;
&lt;li&gt;
Building Your First AI/ML Project
&lt;/li&gt;
&lt;li&gt;
Specific Project Ideas with Implementation Guides
&lt;/li&gt;
&lt;li&gt;
Interview Preparation
&lt;/li&gt;
&lt;li&gt;
Learning Resources and Roadmap
&lt;/li&gt;
&lt;li&gt;
Adversarial Machine Learning and Model Security
&lt;/li&gt;
&lt;li&gt;
Cost Optimization and Resource Management
&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Introduction and Career Overview
&lt;/h2&gt;

&lt;p&gt;1.1 &lt;strong&gt;What is an AI/ML Engineer in 2026?&lt;/strong&gt;&lt;br&gt;
An AI/ML Engineer in 2026 is a professional who combines software engineering skills with machine learning expertise to build, deploy, and maintain intelligent systems. The role has evolved significantly with the rise of large language models, autonomous agents, and production-grade AI systems.&lt;br&gt;
&lt;strong&gt;Core Responsibilities&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Design and implement machine learning solutions&lt;/li&gt;
&lt;li&gt;Build and optimize data pipelines&lt;/li&gt;
&lt;li&gt;Deploy models to production environments&lt;/li&gt;
&lt;li&gt;Monitor and maintain AI systems&lt;/li&gt;
&lt;li&gt;Collaborate with data scientists and software engineers&lt;/li&gt;
&lt;li&gt;Stay updated with rapidly evolving AI technologies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Difference from Data Scientist&lt;/strong&gt;:&lt;br&gt;
While data scientists focus on analysis, experimentation, and model development, AI/ML engineers focus on productionizing models, building scalable systems, and engineering robust AI applications.&lt;/p&gt;

&lt;p&gt;1.2 &lt;strong&gt;Skills Required in 2026&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Technical Skills&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strong programming (Python, increasingly Rust for performance)&lt;/li&gt;
&lt;li&gt;Mathematics and statistics&lt;/li&gt;
&lt;li&gt;Machine learning algorithms and theory&lt;/li&gt;
&lt;li&gt;Deep learning and neural networks&lt;/li&gt;
&lt;li&gt;LLM application development&lt;/li&gt;
&lt;li&gt;MLOps and deployment&lt;/li&gt;
&lt;li&gt;Cloud platforms (AWS, GCP, Azure)&lt;/li&gt;
&lt;li&gt;Version control and software engineering practices&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Emerging Skills (2026-specific)&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Agent orchestration frameworks&lt;/li&gt;
&lt;li&gt;Retrieval-Augmented Generation (RAG)&lt;/li&gt;
&lt;li&gt;Prompt engineering and optimization&lt;/li&gt;
&lt;li&gt;Vector databases&lt;/li&gt;
&lt;li&gt;Fine-tuning large models&lt;/li&gt;
&lt;li&gt;Multi-modal AI systems&lt;/li&gt;
&lt;li&gt;AI safety and alignment&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Soft Skills&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Problem-solving&lt;/li&gt;
&lt;li&gt;Communication&lt;/li&gt;
&lt;li&gt;Continuous learning&lt;/li&gt;
&lt;li&gt;Project management&lt;/li&gt;
&lt;li&gt;Ethical AI considerations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;1.3 &lt;strong&gt;Career Path and Levels&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Junior AI/ML Engineer (0-2 years)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implement existing models&lt;/li&gt;
&lt;li&gt;Data preprocessing and feature engineering&lt;/li&gt;
&lt;li&gt;Basic model training and evaluation&lt;/li&gt;
&lt;li&gt;Learn production deployment basics&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Mid-Level AI/ML Engineer (2-5 years)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Design ML architectures&lt;/li&gt;
&lt;li&gt;Optimize model performance&lt;/li&gt;
&lt;li&gt;Build end-to-end ML pipelines&lt;/li&gt;
&lt;li&gt;Deploy and monitor production systems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Senior AI/ML Engineer (5+ years)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lead technical projects&lt;/li&gt;
&lt;li&gt;Research and implement cutting-edge techniques&lt;/li&gt;
&lt;li&gt;Architect complex AI systems&lt;/li&gt;
&lt;li&gt;Mentor junior engineers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Specialist Tracks&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LLM Engineer&lt;/li&gt;
&lt;li&gt;Computer Vision Engineer&lt;/li&gt;
&lt;li&gt;NLP Engineer&lt;/li&gt;
&lt;li&gt;MLOps Engineer&lt;/li&gt;
&lt;li&gt;Research Engineer&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2. Mathematical Foundations
&lt;/h2&gt;

&lt;p&gt;Mathematics is the bedrock of machine learning. You need strong fundamentals to understand how algorithms work, debug issues, and innovate.&lt;br&gt;
2.1 &lt;strong&gt;Linear Algebra&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Why it matters&lt;/strong&gt;:&lt;br&gt;
Neural networks, dimensionality reduction, and most ML algorithms rely heavily on linear algebra operations.&lt;br&gt;
Core Concepts:&lt;br&gt;
&lt;strong&gt;Vectors and Matrices&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vector operations (addition, scalar multiplication, dot product)&lt;/li&gt;
&lt;li&gt;Matrix operations (addition, multiplication, transpose)&lt;/li&gt;
&lt;li&gt;Identity and inverse matrices&lt;/li&gt;
&lt;li&gt;Matrix decomposition (eigenvalues, eigenvectors)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical Understanding&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A vector represents a point in n-dimensional space&lt;/li&gt;
&lt;li&gt;Matrix multiplication represents linear transformations&lt;/li&gt;
&lt;li&gt;Neural network weights are matrices&lt;/li&gt;
&lt;li&gt;Data is often represented as matrices (rows = samples, columns = features)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Operations You Must Know&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dot Product&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Measures similarity between vectors&lt;/li&gt;
&lt;li&gt;Used in neural network forward propagation&lt;/li&gt;
&lt;li&gt;Formula: a · b = sum(ai * bi)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Matrix Multiplication&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Core operation in neural networks&lt;/li&gt;
&lt;li&gt;Non-commutative (AB ≠ BA)&lt;/li&gt;
&lt;li&gt;Used to apply transformations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Transpose&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flips matrix dimensions&lt;/li&gt;
&lt;li&gt;Essential for gradient calculations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Eigenvalues and Eigenvectors&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used in PCA (Principal Component Analysis)&lt;/li&gt;
&lt;li&gt;Understanding data variance&lt;/li&gt;
&lt;li&gt;Dimensionality reduction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advanced Concepts&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Singular Value Decomposition (SVD)&lt;/li&gt;
&lt;li&gt;Matrix factorization&lt;/li&gt;
&lt;li&gt;Norms (L1, L2)&lt;/li&gt;
&lt;li&gt;Orthogonality and orthonormalization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical Application&lt;/strong&gt;:&lt;br&gt;
When you multiply input data by weights in a neural network, you are performing matrix multiplication. Understanding this helps you debug shape mismatches and optimize computations.&lt;/p&gt;

&lt;p&gt;2.2 &lt;strong&gt;Calculus&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Why it matters&lt;/strong&gt;:&lt;br&gt;
Optimization algorithms (gradient descent) and backpropagation rely on calculus.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core Concepts&lt;/strong&gt;:&lt;br&gt;
&lt;strong&gt;Derivatives&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rate of change of a function&lt;/li&gt;
&lt;li&gt;Slope of a tangent line&lt;/li&gt;
&lt;li&gt;Used to find minima/maxima&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Partial Derivatives&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Derivative with respect to one variable&lt;/li&gt;
&lt;li&gt;Used when functions have multiple inputs&lt;/li&gt;
&lt;li&gt;Essential for gradient calculation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Chain Rule&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Derivative of composite functions&lt;/li&gt;
&lt;li&gt;Foundation of backpropagation&lt;/li&gt;
&lt;li&gt;How gradients flow through neural networks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Gradient&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vector of partial derivatives&lt;/li&gt;
&lt;li&gt;Points in direction of steepest increase&lt;/li&gt;
&lt;li&gt;Negative gradient used for optimization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Concepts You Must Know&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gradient Descent&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Algorithm to minimize loss functions&lt;/li&gt;
&lt;li&gt;Uses gradient to update parameters&lt;/li&gt;
&lt;li&gt;Learning rate controls step size&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Backpropagation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Algorithm to compute gradients efficiently&lt;/li&gt;
&lt;li&gt;Uses chain rule repeatedly&lt;/li&gt;
&lt;li&gt;Enables training deep networks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Optimization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finding minimum of loss function&lt;/li&gt;
&lt;li&gt;Local vs global minima&lt;/li&gt;
&lt;li&gt;Saddle points and plateaus&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Important Derivatives&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;d/dx (x^n) = n * x^(n-1)&lt;/li&gt;
&lt;li&gt;d/dx (e^x) = e^x&lt;/li&gt;
&lt;li&gt;d/dx (ln(x)) = 1/x&lt;/li&gt;
&lt;li&gt;d/dx (sin(x)) = cos(x)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Multivariable Calculus&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gradients in multiple dimensions&lt;/li&gt;
&lt;li&gt;Hessian matrix (second derivatives)&lt;/li&gt;
&lt;li&gt;Jacobian matrix&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical Application&lt;/strong&gt;:&lt;br&gt;
When training a neural network, you compute the gradient of the loss with respect to each weight. This tells you how to adjust weights to reduce error.&lt;/p&gt;

&lt;p&gt;2.3 &lt;strong&gt;Probability and Statistics&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Why it matters&lt;/strong&gt;:&lt;br&gt;
ML is fundamentally about learning patterns from data with uncertainty. Probability theory provides the mathematical framework.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core Concepts&lt;/strong&gt;:&lt;br&gt;
&lt;strong&gt;Probability Basics&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sample space and events&lt;/li&gt;
&lt;li&gt;Probability axioms&lt;/li&gt;
&lt;li&gt;Conditional probability&lt;/li&gt;
&lt;li&gt;Bayes theorem&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Distributions&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Discrete Distributions&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bernoulli (binary outcomes)&lt;/li&gt;
&lt;li&gt;Binomial (number of successes)&lt;/li&gt;
&lt;li&gt;Poisson (rare events)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.&lt;strong&gt;Continuous Distributions&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normal/Gaussian (bell curve)&lt;/li&gt;
&lt;li&gt;Uniform (equal probability)&lt;/li&gt;
&lt;li&gt;Exponential (time between events)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Statistical Concepts&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Mean, Median, Mode&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Central tendency measures&lt;/li&gt;
&lt;li&gt;Mean sensitive to outliers&lt;/li&gt;
&lt;li&gt;Median robust to outliers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.&lt;strong&gt;Variance and Standard Deviation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Measure of spread&lt;/li&gt;
&lt;li&gt;Variance = average squared deviation&lt;/li&gt;
&lt;li&gt;Std dev = square root of variance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.&lt;strong&gt;Covariance and Correlation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Relationship between variables&lt;/li&gt;
&lt;li&gt;Covariance can be any value&lt;/li&gt;
&lt;li&gt;Correlation normalized to [-1, 1]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Bayes Theorem&lt;/strong&gt;:&lt;br&gt;
P(A|B) = P(B|A) * P(A) / P(B)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Foundation of Bayesian inference&lt;/li&gt;
&lt;li&gt;Used in Naive Bayes classifier&lt;/li&gt;
&lt;li&gt;Probabilistic reasoning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Statistical Inference&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Hypothesis Testing&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Null and alternative hypotheses&lt;/li&gt;
&lt;li&gt;p-values and significance levels&lt;/li&gt;
&lt;li&gt;Type I and Type II errors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.&lt;strong&gt;Confidence Intervals&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Range of plausible values&lt;/li&gt;
&lt;li&gt;Uncertainty quantification&lt;/li&gt;
&lt;li&gt;Different from prediction intervals&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.&lt;strong&gt;Maximum Likelihood Estimation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parameter estimation method&lt;/li&gt;
&lt;li&gt;Finds parameters that maximize probability of observed data&lt;/li&gt;
&lt;li&gt;Foundation of many ML algorithms&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Important Probability Rules&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sum rule: P(A or B) = P(A) + P(B) - P(A and B)&lt;/li&gt;
&lt;li&gt;Product rule: P(A and B) = P(A|B) * P(B)&lt;/li&gt;
&lt;li&gt;Independence: P(A and B) = P(A) * P(B)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical Application&lt;/strong&gt;:&lt;br&gt;
When building a spam classifier, you use Bayes theorem to compute the probability that an email is spam given certain words appear in it.&lt;/p&gt;

&lt;p&gt;2.4 &lt;strong&gt;Optimization Theory&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Why it matters&lt;/strong&gt;:&lt;br&gt;
Training ML models is an optimization problem - finding parameters that minimize loss.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core Concepts&lt;/strong&gt;:&lt;br&gt;
&lt;strong&gt;Convex Optimization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Convex functions have single global minimum&lt;/li&gt;
&lt;li&gt;Easier to optimize&lt;/li&gt;
&lt;li&gt;Linear regression is convex&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Non-Convex Optimization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple local minima&lt;/li&gt;
&lt;li&gt;Neural networks are non-convex&lt;/li&gt;
&lt;li&gt;Harder but more powerful&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Optimization Algorithms&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Gradient Descent&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iteratively move in direction of negative gradient&lt;/li&gt;
&lt;li&gt;Step size controlled by learning rate&lt;/li&gt;
&lt;li&gt;Batch gradient descent uses all data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.&lt;strong&gt;Stochastic Gradient Descent (SGD)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses single sample per iteration&lt;/li&gt;
&lt;li&gt;Faster but noisier&lt;/li&gt;
&lt;li&gt;Better for large datasets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.&lt;strong&gt;Mini-Batch Gradient Descent&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses subset of data&lt;/li&gt;
&lt;li&gt;Balance between speed and stability&lt;/li&gt;
&lt;li&gt;Most common in practice&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.&lt;strong&gt;Advanced Optimizers (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adam: Adaptive learning rates&lt;/li&gt;
&lt;li&gt;AdamW: Adam with weight decay&lt;/li&gt;
&lt;li&gt;Lion: More memory efficient&lt;/li&gt;
&lt;li&gt;Sophia: Second-order optimization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Learning Rate Strategies&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Constant learning rate&lt;/li&gt;
&lt;li&gt;Learning rate decay&lt;/li&gt;
&lt;li&gt;Cyclic learning rates&lt;/li&gt;
&lt;li&gt;Warm-up strategies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Regularization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;L1 regularization (Lasso): Encourages sparsity&lt;/li&gt;
&lt;li&gt;L2 regularization (Ridge): Prevents large weights&lt;/li&gt;
&lt;li&gt;Elastic Net: Combination of L1 and L2&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical Application&lt;/strong&gt;:&lt;br&gt;
Choosing the right optimizer and learning rate schedule can dramatically reduce training time and improve model performance.&lt;/p&gt;

&lt;p&gt;2.5 &lt;strong&gt;Information Theory&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Why it matters&lt;/strong&gt;:&lt;br&gt;
Concepts like entropy and information gain are fundamental to decision trees, neural networks, and compression.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;**Core Concepts&lt;/em&gt;&lt;em&gt;:&lt;br&gt;
Entropy&lt;/em&gt;*:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Measure of uncertainty/randomness&lt;/li&gt;
&lt;li&gt;Higher entropy = more unpredictable&lt;/li&gt;
&lt;li&gt;Formula: H(X) = -sum(P(x) * log(P(x)))&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cross-Entropy&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Measures difference between distributions&lt;br&gt;
Used as loss function in classification&lt;br&gt;
Lower cross-entropy = better predictions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;KL Divergence&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Measures how one distribution differs from another&lt;/li&gt;
&lt;li&gt;Non-symmetric&lt;/li&gt;
&lt;li&gt;Used in variational inference&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Mutual Information&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Measures dependence between variables&lt;/li&gt;
&lt;li&gt;Used in feature selection&lt;/li&gt;
&lt;li&gt;Zero if variables are independent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical Application&lt;/strong&gt;:&lt;br&gt;
Cross-entropy loss in neural networks measures how far predicted probabilities are from true labels. Minimizing this makes predictions more accurate.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Programming Fundamentals
&lt;/h2&gt;

&lt;p&gt;3.1** Python Mastery**&lt;br&gt;
Python is the dominant language for AI/ML in 2026. You need more than basic syntax - you need to write efficient, production-quality code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core Python Concepts&lt;/strong&gt;:&lt;br&gt;
&lt;strong&gt;Data Types and Structures&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lists, tuples, sets, dictionaries&lt;/li&gt;
&lt;li&gt;List comprehensions&lt;/li&gt;
&lt;li&gt;Generator expressions&lt;/li&gt;
&lt;li&gt;Understanding mutability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Object-Oriented Programming&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Classes and objects&lt;/li&gt;
&lt;li&gt;Inheritance and polymorphism&lt;/li&gt;
&lt;li&gt;Encapsulation&lt;/li&gt;
&lt;li&gt;Abstract classes and interfaces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Functional Programming&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lambda functions&lt;/li&gt;
&lt;li&gt;Map, filter, reduce&lt;/li&gt;
&lt;li&gt;Decorators&lt;/li&gt;
&lt;li&gt;Higher-order functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advanced Python&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.&lt;strong&gt;Context Managers&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With statements&lt;/li&gt;
&lt;li&gt;Resource management&lt;/li&gt;
&lt;li&gt;Custom context managers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.&lt;strong&gt;Iterators and Generators&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memory-efficient iteration&lt;/li&gt;
&lt;li&gt;Yield keyword&lt;/li&gt;
&lt;li&gt;Generator pipelines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.&lt;strong&gt;Decorators&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function modification&lt;/li&gt;
&lt;li&gt;Logging and timing&lt;/li&gt;
&lt;li&gt;Caching (memoization)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.&lt;strong&gt;Type Hints&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Static type checking&lt;/li&gt;
&lt;li&gt;Better code documentation&lt;/li&gt;
&lt;li&gt;IDE support&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Python for ML Specific:&lt;/strong&gt;&lt;br&gt;
1.&lt;strong&gt;NumPy&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Array operations&lt;/li&gt;
&lt;li&gt;Broadcasting&lt;/li&gt;
&lt;li&gt;Vectorization&lt;/li&gt;
&lt;li&gt;Linear algebra functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.&lt;strong&gt;Pandas:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DataFrames and Series&lt;/li&gt;
&lt;li&gt;Data manipulation&lt;/li&gt;
&lt;li&gt;GroupBy operations&lt;/li&gt;
&lt;li&gt;Merging and joining&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.&lt;strong&gt;Matplotlib and Seaborn&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data visualization&lt;/li&gt;
&lt;li&gt;Plot customization&lt;/li&gt;
&lt;li&gt;Statistical plots&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code Quality&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PEP 8 style guide&lt;/li&gt;
&lt;li&gt;Docstrings and documentation&lt;/li&gt;
&lt;li&gt;Unit testing (pytest)&lt;/li&gt;
&lt;li&gt;Linting (pylint, flake8)&lt;/li&gt;
&lt;li&gt;Type checking (mypy)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*&lt;em&gt;Performance Optimization:&lt;br&gt;
*&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Profiling code&lt;/li&gt;
&lt;li&gt;Vectorization over loops&lt;/li&gt;
&lt;li&gt;Using appropriate data structures&lt;/li&gt;
&lt;li&gt;Memory management&lt;/li&gt;
&lt;li&gt;Multiprocessing and threading&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Bad: Slow loop-based approach
result = []
for i in range(len(data)):
    result.append(data[i] * 2)

# Good: Vectorized NumPy approach
import numpy as np
result = np.array(data) * 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.2 &lt;strong&gt;Essential Libraries and Frameworks&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Data Manipulation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NumPy: Numerical computing&lt;/li&gt;
&lt;li&gt;Pandas: Data analysis&lt;/li&gt;
&lt;li&gt;Polars: Faster alternative to Pandas (2026 trend)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Visualization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Matplotlib: Basic plotting&lt;/li&gt;
&lt;li&gt;Seaborn: Statistical visualization&lt;/li&gt;
&lt;li&gt;Plotly: Interactive plots&lt;/li&gt;
&lt;li&gt;Altair: Declarative visualization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Machine Learning&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scikit-learn: Classical ML algorithms&lt;/li&gt;
&lt;li&gt;XGBoost: Gradient boosting&lt;/li&gt;
&lt;li&gt;LightGBM: Fast gradient boosting&lt;/li&gt;
&lt;li&gt;CatBoost: Handling categorical features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Deep Learning&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PyTorch: Research and production&lt;/li&gt;
&lt;li&gt;TensorFlow: Production deployment&lt;/li&gt;
&lt;li&gt;JAX: High-performance numerical computing&lt;/li&gt;
&lt;li&gt;Hugging Face Transformers: Pre-trained models&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;LLM and Modern AI (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LangChain: LLM application framework&lt;/li&gt;
&lt;li&gt;LangGraph: Agent orchestration&lt;/li&gt;
&lt;li&gt;LlamaIndex: Data framework for LLMs&lt;/li&gt;
&lt;li&gt;Haystack: NLP pipelines&lt;/li&gt;
&lt;li&gt;DSPy: Programming LLMs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Vector Databases&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pinecone: Managed vector database&lt;/li&gt;
&lt;li&gt;Weaviate: Open-source vector search&lt;/li&gt;
&lt;li&gt;Chroma: Embedding database&lt;/li&gt;
&lt;li&gt;Qdrant: Vector search engine&lt;/li&gt;
&lt;li&gt;Milvus: Scalable vector database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.3 &lt;strong&gt;Version Control and Collaboration&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Git Fundamentals&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repositories and commits&lt;/li&gt;
&lt;li&gt;Branching and merging&lt;/li&gt;
&lt;li&gt;Pull requests&lt;/li&gt;
&lt;li&gt;Resolving conflicts&lt;/li&gt;
&lt;li&gt;Git workflows (Gitflow, trunk-based)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;GitHub/GitLab&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Repository management&lt;/li&gt;
&lt;li&gt;Issue tracking&lt;/li&gt;
&lt;li&gt;CI/CD pipelines&lt;/li&gt;
&lt;li&gt;Code review practices&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;DVC (Data Version Control):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Versioning datasets&lt;/li&gt;
&lt;li&gt;Experiment tracking&lt;/li&gt;
&lt;li&gt;Pipeline management&lt;/li&gt;
&lt;li&gt;Remote storage integration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.4 &lt;strong&gt;Software Engineering Best Practices&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Code Organization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modular design&lt;/li&gt;
&lt;li&gt;Separation of concerns&lt;/li&gt;
&lt;li&gt;Configuration management&lt;/li&gt;
&lt;li&gt;Logging and monitoring&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Testing&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unit tests&lt;/li&gt;
&lt;li&gt;Integration tests&lt;/li&gt;
&lt;li&gt;Test-driven development&lt;/li&gt;
&lt;li&gt;Continuous integration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Documentation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;README files&lt;/li&gt;
&lt;li&gt;API documentation&lt;/li&gt;
&lt;li&gt;Code comments&lt;/li&gt;
&lt;li&gt;Architecture diagrams&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Design Patterns:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Factory pattern&lt;/li&gt;
&lt;li&gt;Singleton pattern&lt;/li&gt;
&lt;li&gt;Observer pattern&lt;/li&gt;
&lt;li&gt;Strategy pattern&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  4. Classical Machine Learning
&lt;/h2&gt;

&lt;p&gt;Before deep learning dominated, classical machine learning algorithms were (and still are) essential for many tasks. They are faster, more interpretable, and require less data.&lt;/p&gt;

&lt;p&gt;4.1 &lt;strong&gt;Supervised Learning&lt;/strong&gt;&lt;br&gt;
What is Supervised Learning?&lt;br&gt;
Learning from labeled data where each example has input features and a known output label. The goal is to learn a mapping from inputs to outputs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Types of Supervised Learning&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Classification: Predicting discrete categories&lt;/li&gt;
&lt;li&gt;Regression: Predicting continuous values&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;4.1.1 &lt;strong&gt;Linear Regression&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Concept&lt;/strong&gt;:&lt;br&gt;
Predicting continuous output using linear relationship between features and target.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mathematical Formulation&lt;/strong&gt;:&lt;br&gt;
y = w1x1 + w2x2 + ... + wn*xn + b&lt;br&gt;
Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;y = predicted value&lt;/li&gt;
&lt;li&gt;xi = input features&lt;/li&gt;
&lt;li&gt;wi = weights (learned parameters)&lt;/li&gt;
&lt;li&gt;b = bias term&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How it Works&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize weights randomly&lt;/li&gt;
&lt;li&gt;Make predictions&lt;/li&gt;
&lt;li&gt;Calculate error (Mean Squared Error)&lt;/li&gt;
&lt;li&gt;Update weights using gradient descent&lt;/li&gt;
&lt;li&gt;Repeat until convergence&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Assumptions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linear relationship between features and target&lt;/li&gt;
&lt;li&gt;Independence of errors&lt;/li&gt;
&lt;li&gt;Homoscedasticity (constant variance)&lt;/li&gt;
&lt;li&gt;Normally distributed errors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Variants:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ridge Regression (L2 regularization)&lt;/li&gt;
&lt;li&gt;Lasso Regression (L1 regularization)&lt;/li&gt;
&lt;li&gt;Elastic Net (L1 + L2)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When to Use&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple baseline model&lt;/li&gt;
&lt;li&gt;Interpretable predictions needed&lt;/li&gt;
&lt;li&gt;Linear relationships in data&lt;/li&gt;
&lt;li&gt;Feature importance analysis&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical Tips&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Feature scaling improves convergence&lt;/li&gt;
&lt;li&gt;Check for multicollinearity&lt;/li&gt;
&lt;li&gt;Visualize residuals&lt;/li&gt;
&lt;li&gt;Use regularization to prevent overfitting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.1.2 &lt;strong&gt;Logistic Regression&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Concept&lt;/strong&gt;:&lt;br&gt;
Classification algorithm that predicts probability of binary outcomes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mathematical Formulation&lt;/strong&gt;:&lt;br&gt;
P(y=1|x) = 1 / (1 + e^-(w·x + b))&lt;br&gt;
This is the sigmoid function that outputs values between 0 and 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it Works&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Linear combination of features&lt;/li&gt;
&lt;li&gt;Apply sigmoid activation&lt;/li&gt;
&lt;li&gt;Output interpreted as probability&lt;/li&gt;
&lt;li&gt;Threshold (usually 0.5) for classification&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Loss Function&lt;/strong&gt;:&lt;br&gt;
Binary Cross-Entropy (Log Loss)&lt;br&gt;
&lt;strong&gt;Extensions&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multinomial Logistic Regression (multi-class)&lt;/li&gt;
&lt;li&gt;Ordinal Logistic Regression (ordered categories)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When to Use&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Binary classification problems&lt;/li&gt;
&lt;li&gt;Need probability estimates&lt;/li&gt;
&lt;li&gt;Baseline classification model&lt;/li&gt;
&lt;li&gt;Interpretable results required&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Practical Tips&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Feature scaling improves performance&lt;/li&gt;
&lt;li&gt;Check class imbalance&lt;/li&gt;
&lt;li&gt;Regularization prevents overfitting&lt;/li&gt;
&lt;li&gt;ROC-AUC for model evaluation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.1.3 &lt;strong&gt;Decision Trees&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Concept&lt;/strong&gt;:&lt;br&gt;
Tree-structured model that makes decisions based on feature values.&lt;br&gt;
&lt;strong&gt;How it Works&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start with all data at root&lt;/li&gt;
&lt;li&gt;Find best feature to split on&lt;/li&gt;
&lt;li&gt;Split data based on threshold&lt;/li&gt;
&lt;li&gt;Recursively repeat for each branch&lt;/li&gt;
&lt;li&gt;Stop when stopping criteria met&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Splitting Criteria:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gini Impurity (classification)&lt;/li&gt;
&lt;li&gt;Information Gain / Entropy (classification)&lt;/li&gt;
&lt;li&gt;Mean Squared Error (regression)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy to interpret and visualize&lt;/li&gt;
&lt;li&gt;Handles non-linear relationships&lt;/li&gt;
&lt;li&gt;No feature scaling needed&lt;/li&gt;
&lt;li&gt;Can handle mixed data types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prone to overfitting&lt;/li&gt;
&lt;li&gt;Unstable (small data changes cause different trees)&lt;/li&gt;
&lt;li&gt;Biased toward features with many values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hyperparameters&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;max_depth: Maximum tree depth&lt;/li&gt;
&lt;li&gt;min_samples_split: Minimum samples to split node&lt;/li&gt;
&lt;li&gt;min_samples_leaf: Minimum samples in leaf&lt;/li&gt;
&lt;li&gt;max_features: Features to consider for split&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When to Use&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Need interpretable model&lt;/li&gt;
&lt;li&gt;Mixed feature types&lt;/li&gt;
&lt;li&gt;Non-linear relationships&lt;/li&gt;
&lt;li&gt;Quick baseline model&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.1.4 &lt;strong&gt;Random Forests&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Concept&lt;/strong&gt;:&lt;br&gt;
Ensemble of decision trees trained on random subsets of data and features.&lt;br&gt;
&lt;strong&gt;How it Works&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Bootstrap sampling (random sample with replacement)&lt;/li&gt;
&lt;li&gt;Train decision tree on each sample&lt;/li&gt;
&lt;li&gt;Random feature selection at each split&lt;/li&gt;
&lt;li&gt;Average predictions (regression) or vote (classification)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Why it Works&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduces overfitting through averaging&lt;/li&gt;
&lt;li&gt;Reduces variance while maintaining low bias&lt;/li&gt;
&lt;li&gt;Each tree sees different data and features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Robust to overfitting&lt;/li&gt;
&lt;li&gt;Handles high-dimensional data&lt;/li&gt;
&lt;li&gt;Feature importance estimates&lt;/li&gt;
&lt;li&gt;Good default performance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Less interpretable than single tree&lt;/li&gt;
&lt;li&gt;Can be slow on large datasets&lt;/li&gt;
&lt;li&gt;Memory intensive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hyperparameters&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;n_estimators: Number of trees&lt;/li&gt;
&lt;li&gt;max_depth: Maximum tree depth&lt;/li&gt;
&lt;li&gt;min_samples_split: Minimum samples to split&lt;/li&gt;
&lt;li&gt;max_features: Features per split&lt;/li&gt;
&lt;li&gt;bootstrap: Whether to use bootstrap samples&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When to Use&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Default choice for tabular data&lt;/li&gt;
&lt;li&gt;Need robust performance&lt;/li&gt;
&lt;li&gt;Feature importance analysis&lt;/li&gt;
&lt;li&gt;Can afford computational cost&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.1.5 &lt;strong&gt;Gradient Boosting&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Concept&lt;/strong&gt;:&lt;br&gt;
Sequentially train weak learners to correct errors of previous models.&lt;br&gt;
&lt;strong&gt;How it Works&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Train initial model (often simple)&lt;/li&gt;
&lt;li&gt;Calculate residuals (errors)&lt;/li&gt;
&lt;li&gt;Train new model to predict residuals&lt;/li&gt;
&lt;li&gt;Add to ensemble with learning rate&lt;/li&gt;
&lt;li&gt;Repeat for specified iterations&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Key Idea&lt;/strong&gt;:&lt;br&gt;
Each new model focuses on examples the ensemble currently gets wrong.&lt;br&gt;
&lt;strong&gt;Popular Implementations&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.XGBoost (Extreme Gradient Boosting):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Regularization to prevent overfitting&lt;/li&gt;
&lt;li&gt;Parallel processing&lt;/li&gt;
&lt;li&gt;Handling missing values&lt;/li&gt;
&lt;li&gt;Tree pruning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.LightGBM:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gradient-based One-Side Sampling&lt;/li&gt;
&lt;li&gt;Exclusive Feature Bundling&lt;/li&gt;
&lt;li&gt;Faster training&lt;/li&gt;
&lt;li&gt;Lower memory usage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.CatBoost:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Native categorical feature handling&lt;/li&gt;
&lt;li&gt;Ordered boosting&lt;/li&gt;
&lt;li&gt;Robust to overfitting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Often highest performance on tabular data&lt;/li&gt;
&lt;li&gt;Handles complex patterns&lt;/li&gt;
&lt;li&gt;Feature importance&lt;/li&gt;
&lt;li&gt;Can handle missing values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prone to overfitting if not tuned&lt;/li&gt;
&lt;li&gt;Longer training time&lt;/li&gt;
&lt;li&gt;More hyperparameters to tune&lt;/li&gt;
&lt;li&gt;Less interpretable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key Hyperparameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;learning_rate: Shrinkage parameter&lt;/li&gt;
&lt;li&gt;n_estimators: Number of boosting rounds&lt;/li&gt;
&lt;li&gt;max_depth: Tree complexity&lt;/li&gt;
&lt;li&gt;subsample: Fraction of samples per tree&lt;/li&gt;
&lt;li&gt;colsample_bytree: Fraction of features per tree&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kaggle competitions&lt;/li&gt;
&lt;li&gt;Need maximum performance&lt;/li&gt;
&lt;li&gt;Tabular data&lt;/li&gt;
&lt;li&gt;Can afford tuning time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.1.6 Support Vector Machines (SVM)&lt;br&gt;
Concept:&lt;br&gt;
Find optimal hyperplane that maximally separates classes.&lt;br&gt;
How it Works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Map data to higher dimensional space&lt;/li&gt;
&lt;li&gt;Find hyperplane with maximum margin&lt;/li&gt;
&lt;li&gt;Support vectors are closest points to boundary&lt;/li&gt;
&lt;li&gt;Decision boundary defined by support vectors&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Kernel Trick:&lt;br&gt;
Implicitly map data to high-dimensional space without computing transformations.&lt;br&gt;
&lt;strong&gt;Common Kernels&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linear: For linearly separable data&lt;/li&gt;
&lt;li&gt;Polynomial: For polynomial decision boundaries&lt;/li&gt;
&lt;li&gt;RBF (Radial Basis Function): Most common, handles non-linear&lt;/li&gt;
&lt;li&gt;Sigmoid: Similar to neural networks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Effective in high dimensions&lt;/li&gt;
&lt;li&gt;Memory efficient (only support vectors matter)&lt;/li&gt;
&lt;li&gt;Versatile (different kernels)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slow on large datasets&lt;/li&gt;
&lt;li&gt;Sensitive to feature scaling&lt;/li&gt;
&lt;li&gt;Difficult to interpret&lt;/li&gt;
&lt;li&gt;Choosing right kernel is tricky&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hyperparameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C: Regularization parameter&lt;/li&gt;
&lt;li&gt;kernel: Type of kernel function&lt;/li&gt;
&lt;li&gt;gamma: Kernel coefficient&lt;/li&gt;
&lt;li&gt;degree: Polynomial degree (if polynomial kernel)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Small to medium datasets&lt;/li&gt;
&lt;li&gt;High-dimensional data&lt;/li&gt;
&lt;li&gt;Clear margin of separation&lt;/li&gt;
&lt;li&gt;Text classification&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.1.7 K-Nearest Neighbors (KNN)&lt;br&gt;
Concept:&lt;br&gt;
Classify based on majority vote of k nearest neighbors.&lt;br&gt;
&lt;strong&gt;How it Works&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Store all training data&lt;/li&gt;
&lt;li&gt;For new point, find k nearest neighbors&lt;/li&gt;
&lt;li&gt;Classification: majority vote&lt;/li&gt;
&lt;li&gt;Regression: average of neighbors&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Distance Metrics&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Euclidean: Standard distance&lt;/li&gt;
&lt;li&gt;Manhattan: Sum of absolute differences&lt;/li&gt;
&lt;li&gt;Minkowski: Generalization of Euclidean&lt;/li&gt;
&lt;li&gt;Cosine: Angle between vectors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple to understand&lt;/li&gt;
&lt;li&gt;No training phase&lt;/li&gt;
&lt;li&gt;Naturally handles multi-class&lt;/li&gt;
&lt;li&gt;Non-parametric (no assumptions)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slow prediction (must search all data)&lt;/li&gt;
&lt;li&gt;Memory intensive (stores all data)&lt;/li&gt;
&lt;li&gt;Sensitive to feature scaling&lt;/li&gt;
&lt;li&gt;Curse of dimensionality&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hyperparameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;k: Number of neighbors&lt;/li&gt;
&lt;li&gt;distance_metric: How to measure distance&lt;/li&gt;
&lt;li&gt;weights: uniform vs distance-weighted&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Small datasets&lt;/li&gt;
&lt;li&gt;Need simple baseline&lt;/li&gt;
&lt;li&gt;Non-linear decision boundaries&lt;/li&gt;
&lt;li&gt;Recommender systems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.2 &lt;strong&gt;Unsupervised Learning&lt;/strong&gt;&lt;br&gt;
What is Unsupervised Learning?&lt;br&gt;
Learning patterns from unlabeled data without explicit output labels.&lt;br&gt;
Main Types:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Clustering: Grouping similar data points&lt;/li&gt;
&lt;li&gt;Dimensionality Reduction: Reducing feature space&lt;/li&gt;
&lt;li&gt;Anomaly Detection: Finding unusual patterns&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;4.2.1 K-Means Clustering&lt;br&gt;
Concept:&lt;br&gt;
Partition data into k clusters by minimizing within-cluster variance.&lt;br&gt;
Algorithm:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize k cluster centers randomly&lt;/li&gt;
&lt;li&gt;Assign each point to nearest center&lt;/li&gt;
&lt;li&gt;Update centers to mean of assigned points&lt;/li&gt;
&lt;li&gt;Repeat until convergence&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Choosing k:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Elbow method: Plot inertia vs k&lt;/li&gt;
&lt;li&gt;Silhouette score: Measure cluster quality&lt;/li&gt;
&lt;li&gt;Domain knowledge&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple and fast&lt;/li&gt;
&lt;li&gt;Scales to large datasets&lt;/li&gt;
&lt;li&gt;Easy to implement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Must specify k beforehand&lt;/li&gt;
&lt;li&gt;Sensitive to initialization&lt;/li&gt;
&lt;li&gt;Assumes spherical clusters&lt;/li&gt;
&lt;li&gt;Sensitive to outliers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Variants:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;K-Means++: Better initialization&lt;/li&gt;
&lt;li&gt;Mini-Batch K-Means: Faster for large data&lt;/li&gt;
&lt;li&gt;K-Medoids: More robust to outliers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Customer segmentation&lt;/li&gt;
&lt;li&gt;Image compression&lt;/li&gt;
&lt;li&gt;Data preprocessing&lt;/li&gt;
&lt;li&gt;Quick clustering baseline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.2.2 Hierarchical Clustering&lt;br&gt;
Concept:&lt;br&gt;
Build tree of clusters through iterative merging or splitting.&lt;br&gt;
&lt;strong&gt;Types&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Agglomerative (Bottom-Up):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with each point as cluster&lt;/li&gt;
&lt;li&gt;Merge closest clusters&lt;/li&gt;
&lt;li&gt;Continue until single cluster&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Divisive (Top-Down):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with all points in one cluster&lt;/li&gt;
&lt;li&gt;Split recursively&lt;/li&gt;
&lt;li&gt;Less common&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Linkage Methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single: Minimum distance between clusters&lt;/li&gt;
&lt;li&gt;Complete: Maximum distance&lt;/li&gt;
&lt;li&gt;Average: Average distance&lt;/li&gt;
&lt;li&gt;Ward: Minimize within-cluster variance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No need to specify number of clusters&lt;/li&gt;
&lt;li&gt;Dendrogram provides visualization&lt;/li&gt;
&lt;li&gt;Can reveal hierarchical structure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Computationally expensive O(n^3)&lt;/li&gt;
&lt;li&gt;Not suitable for large datasets&lt;/li&gt;
&lt;li&gt;Sensitive to noise&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Small datasets&lt;/li&gt;
&lt;li&gt;Hierarchical structure expected&lt;/li&gt;
&lt;li&gt;Need dendrogram visualization&lt;/li&gt;
&lt;li&gt;Don't know number of clusters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.2.3 &lt;strong&gt;DBSCAN (Density-Based Clustering)&lt;/strong&gt;&lt;br&gt;
Concept:&lt;br&gt;
Cluster based on density of points. Points in dense regions form clusters.&lt;br&gt;
&lt;strong&gt;Parameters&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;eps: Neighborhood radius&lt;/li&gt;
&lt;li&gt;min_samples: Minimum points for core point&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How it Works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Core points: Have min_samples within eps&lt;/li&gt;
&lt;li&gt;Border points: Within eps of core point&lt;/li&gt;
&lt;li&gt;Noise points: Neither core nor border&lt;/li&gt;
&lt;li&gt;Connect core points to form clusters&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finds arbitrary-shaped clusters&lt;/li&gt;
&lt;li&gt;Robust to outliers&lt;/li&gt;
&lt;li&gt;No need to specify number of clusters&lt;/li&gt;
&lt;li&gt;Identifies noise points&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sensitive to parameters&lt;/li&gt;
&lt;li&gt;Struggles with varying densities&lt;/li&gt;
&lt;li&gt;Not suitable for high dimensions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arbitrary cluster shapes&lt;/li&gt;
&lt;li&gt;Noise in data&lt;/li&gt;
&lt;li&gt;Don't know number of clusters&lt;/li&gt;
&lt;li&gt;Spatial data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.2.4 &lt;strong&gt;Principal Component Analysis (PCA)&lt;/strong&gt;&lt;br&gt;
Concept:&lt;br&gt;
Reduce dimensionality by projecting data onto directions of maximum variance.&lt;br&gt;
How it Works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Standardize data&lt;/li&gt;
&lt;li&gt;Compute covariance matrix&lt;/li&gt;
&lt;li&gt;Calculate eigenvectors and eigenvalues&lt;/li&gt;
&lt;li&gt;Sort by eigenvalues (descending)&lt;/li&gt;
&lt;li&gt;Select top k eigenvectors&lt;/li&gt;
&lt;li&gt;Project data onto new axes&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Principal Components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New orthogonal axes&lt;/li&gt;
&lt;li&gt;PC1: Direction of maximum variance&lt;/li&gt;
&lt;li&gt;PC2: Second most variance (orthogonal to PC1)&lt;/li&gt;
&lt;li&gt;And so on&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduces dimensionality&lt;/li&gt;
&lt;li&gt;Removes correlated features&lt;/li&gt;
&lt;li&gt;Speeds up algorithms&lt;/li&gt;
&lt;li&gt;Visualization (2D or 3D)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linear transformation only&lt;/li&gt;
&lt;li&gt;Loses interpretability&lt;/li&gt;
&lt;li&gt;Sensitive to scaling&lt;/li&gt;
&lt;li&gt;May lose important information&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Choosing Number of Components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explained variance ratio&lt;/li&gt;
&lt;li&gt;Scree plot&lt;/li&gt;
&lt;li&gt;Domain knowledge&lt;/li&gt;
&lt;li&gt;Cross-validation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High-dimensional data&lt;/li&gt;
&lt;li&gt;Feature correlation&lt;/li&gt;
&lt;li&gt;Visualization&lt;/li&gt;
&lt;li&gt;Preprocessing for other algorithms&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.2.5 t-SNE (t-Distributed Stochastic Neighbor Embedding)&lt;br&gt;
Concept:&lt;br&gt;
Non-linear dimensionality reduction for visualization.&lt;br&gt;
How it Works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Model pairwise similarities in high dimensions&lt;/li&gt;
&lt;li&gt;Model pairwise similarities in low dimensions&lt;/li&gt;
&lt;li&gt;Minimize difference between distributions&lt;/li&gt;
&lt;li&gt;Uses gradient descent&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reveals clusters and patterns&lt;/li&gt;
&lt;li&gt;Non-linear relationships&lt;/li&gt;
&lt;li&gt;Great for visualization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Computationally expensive&lt;/li&gt;
&lt;li&gt;Different runs give different results&lt;/li&gt;
&lt;li&gt;Cannot transform new data&lt;/li&gt;
&lt;li&gt;Not for general dimensionality reduction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hyperparameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;perplexity: Balance local vs global structure&lt;/li&gt;
&lt;li&gt;learning_rate: Step size&lt;/li&gt;
&lt;li&gt;n_iterations: Number of optimization steps&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visualizing high-dimensional data&lt;/li&gt;
&lt;li&gt;Exploring data structure&lt;/li&gt;
&lt;li&gt;Presentation purposes&lt;/li&gt;
&lt;li&gt;NOT for preprocessing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.3 &lt;strong&gt;Model Evaluation and Selection&lt;/strong&gt;&lt;br&gt;
Critical Concept:&lt;br&gt;
Building models is only half the battle. Evaluating them correctly is equally important.&lt;br&gt;
4.3.1 Train-Test Split&lt;br&gt;
Concept:&lt;br&gt;
Split data into separate training and testing sets.&lt;br&gt;
&lt;strong&gt;Typical Split&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;70-80% training&lt;/li&gt;
&lt;li&gt;20-30% testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why it Matters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Evaluate generalization&lt;/li&gt;
&lt;li&gt;Detect overfitting&lt;/li&gt;
&lt;li&gt;Estimate real-world performance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Best Practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Random splitting for i.i.d. data&lt;/li&gt;
&lt;li&gt;Stratified split for imbalanced classes&lt;/li&gt;
&lt;li&gt;Time-based split for time series&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.3.2 Cross-Validation&lt;br&gt;
Concept:&lt;br&gt;
Multiple train-test splits to get robust performance estimate.&lt;br&gt;
K-Fold Cross-Validation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Split data into k folds&lt;/li&gt;
&lt;li&gt;Train on k-1 folds, test on remaining&lt;/li&gt;
&lt;li&gt;Repeat k times (each fold used as test once)&lt;/li&gt;
&lt;li&gt;Average results&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better use of limited data&lt;/li&gt;
&lt;li&gt;More reliable performance estimate&lt;/li&gt;
&lt;li&gt;Reduces variance in evaluation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Variants:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stratified K-Fold: Maintains class distribution&lt;/li&gt;
&lt;li&gt;Leave-One-Out: K = number of samples&lt;/li&gt;
&lt;li&gt;Time Series Split: Respects temporal order&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Small to medium datasets&lt;/li&gt;
&lt;li&gt;Hyperparameter tuning&lt;/li&gt;
&lt;li&gt;Model selection&lt;/li&gt;
&lt;li&gt;Not practical for very large datasets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.3.3 Classification Metrics&lt;br&gt;
Confusion Matrix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                Predicted
              Pos    Neg
Actual Pos    TP     FN
       Neg    FP     TN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key Metrics:&lt;/p&gt;

&lt;p&gt;1.Accuracy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;(TP + TN) / Total&lt;/li&gt;
&lt;li&gt;Overall correctness&lt;/li&gt;
&lt;li&gt;Misleading for imbalanced data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Precision:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TP / (TP + FP)&lt;/li&gt;
&lt;li&gt;Of predicted positives, how many are correct?&lt;/li&gt;
&lt;li&gt;Important when false positives are costly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Recall (Sensitivity):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TP / (TP + FN)&lt;/li&gt;
&lt;li&gt;Of actual positives, how many did we find?&lt;/li&gt;
&lt;li&gt;Important when false negatives are costly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.F1 Score:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2 * (Precision * Recall) / (Precision + Recall)&lt;/li&gt;
&lt;li&gt;Harmonic mean of precision and recall&lt;/li&gt;
&lt;li&gt;Good for imbalanced datasets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.ROC-AUC:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Area under ROC curve&lt;/li&gt;
&lt;li&gt;Plots True Positive Rate vs False Positive Rate&lt;/li&gt;
&lt;li&gt;Threshold-independent&lt;/li&gt;
&lt;li&gt;Higher is better (1.0 is perfect)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.Precision-Recall AUC:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Better for imbalanced datasets than ROC-AUC&lt;/li&gt;
&lt;li&gt;Focuses on positive class&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Which Metric to Use?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Balanced data: Accuracy&lt;/li&gt;
&lt;li&gt;Imbalanced data: F1, Precision-Recall AUC&lt;/li&gt;
&lt;li&gt;Cost-sensitive: Precision or Recall depending on cost&lt;/li&gt;
&lt;li&gt;Ranking problems: ROC-AUC&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.3.4 Regression Metrics&lt;br&gt;
&lt;strong&gt;Common Metrics&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Mean Absolute Error (MAE):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Average absolute difference&lt;/li&gt;
&lt;li&gt;Same units as target&lt;/li&gt;
&lt;li&gt;Robust to outliers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Mean Squared Error (MSE):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Average squared difference&lt;/li&gt;
&lt;li&gt;Penalizes large errors more&lt;/li&gt;
&lt;li&gt;Not in same units as target&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Root Mean Squared Error (RMSE):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Square root of MSE&lt;/li&gt;
&lt;li&gt;Same units as target&lt;/li&gt;
&lt;li&gt;Popular choice&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.R-Squared (R²):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Proportion of variance explained&lt;/li&gt;
&lt;li&gt;Range: 0 to 1 (can be negative)&lt;/li&gt;
&lt;li&gt;1.0 is perfect fit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Mean Absolute Percentage Error (MAPE):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Percentage error&lt;/li&gt;
&lt;li&gt;Scale-independent&lt;/li&gt;
&lt;li&gt;Undefined when actual is zero&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Which Metric to Use?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Outliers not critical: RMSE&lt;/li&gt;
&lt;li&gt;Outliers are noise: MAE&lt;/li&gt;
&lt;li&gt;Need percentage: MAPE&lt;/li&gt;
&lt;li&gt;Comparing models: R²&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.3.5 Overfitting and Underfitting&lt;br&gt;
&lt;strong&gt;Underfitting&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model too simple&lt;/li&gt;
&lt;li&gt;High training error&lt;/li&gt;
&lt;li&gt;High test error&lt;/li&gt;
&lt;li&gt;Solution: More complex model, more features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Overfitting&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model too complex&lt;/li&gt;
&lt;li&gt;Low training error&lt;/li&gt;
&lt;li&gt;High test error&lt;/li&gt;
&lt;li&gt;Model memorizes training data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Solutions to Overfitting&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;More training data&lt;/li&gt;
&lt;li&gt;Regularization (L1, L2)&lt;/li&gt;
&lt;li&gt;Simpler model&lt;/li&gt;
&lt;li&gt;Cross-validation&lt;/li&gt;
&lt;li&gt;Early stopping&lt;/li&gt;
&lt;li&gt;Dropout (neural networks)&lt;/li&gt;
&lt;li&gt;Data augmentation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Bias-Variance Tradeoff&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High Bias: Underfitting&lt;/li&gt;
&lt;li&gt;High Variance: Overfitting&lt;/li&gt;
&lt;li&gt;Goal: Balance both&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.3.6 Hyperparameter Tuning&lt;br&gt;
&lt;strong&gt;What are Hyperparameters?&lt;/strong&gt;&lt;br&gt;
Parameters set before training (not learned from data).&lt;br&gt;
&lt;strong&gt;Tuning Methods&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Grid Search:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Try all combinations&lt;/li&gt;
&lt;li&gt;Exhaustive but slow&lt;/li&gt;
&lt;li&gt;Good for small search spaces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Random Search:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Random combinations&lt;/li&gt;
&lt;li&gt;Often finds good solutions faster&lt;/li&gt;
&lt;li&gt;Better for large search spaces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Bayesian Optimization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses previous results to guide search&lt;/li&gt;
&lt;li&gt;More efficient&lt;/li&gt;
&lt;li&gt;Libraries: Optuna, Hyperopt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Automated Methods (2026):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AutoML tools&lt;/li&gt;
&lt;li&gt;Neural Architecture Search&lt;/li&gt;
&lt;li&gt;Ray Tune for distributed tuning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Best Practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use cross-validation during tuning&lt;/li&gt;
&lt;li&gt;Start with wide range, then narrow&lt;/li&gt;
&lt;li&gt;Consider computational budget&lt;/li&gt;
&lt;li&gt;Document parameter choices&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  5. Deep Learning Fundamentals
&lt;/h2&gt;

&lt;p&gt;Deep learning has revolutionized AI since 2012. Understanding neural networks is essential for modern AI/ML engineers.&lt;/p&gt;

&lt;p&gt;5.1 &lt;strong&gt;Neural Network Basics&lt;/strong&gt;&lt;br&gt;
What is a Neural Network?&lt;br&gt;
A computational model inspired by biological neurons that learns to map inputs to outputs through layers of interconnected nodes.&lt;br&gt;
Basic Components:&lt;/p&gt;

&lt;p&gt;1.Neurons (Nodes):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Receive inputs&lt;/li&gt;
&lt;li&gt;Apply weights and bias&lt;/li&gt;
&lt;li&gt;Apply activation function&lt;/li&gt;
&lt;li&gt;Output result&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Input layer: Receives data&lt;/li&gt;
&lt;li&gt;Hidden layers: Process information&lt;/li&gt;
&lt;li&gt;Output layer: Produces predictions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Weights and Biases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learned parameters&lt;/li&gt;
&lt;li&gt;Adjusted during training&lt;/li&gt;
&lt;li&gt;Determine network behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Forward Propagation&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Input data enters network&lt;/li&gt;
&lt;li&gt;Each layer performs: output = activation(weights * input + bias)&lt;/li&gt;
&lt;li&gt;Pass output to next layer&lt;/li&gt;
&lt;li&gt;Final layer produces prediction&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Mathematical Representation&lt;/strong&gt;:&lt;br&gt;
For a single neuron:&lt;br&gt;
y = activation(w1x1 + w2x2 + ... + wn*xn + b)&lt;/p&gt;

&lt;p&gt;5.2 &lt;strong&gt;Activation Functions&lt;/strong&gt;&lt;br&gt;
Why Needed?&lt;br&gt;
Without activation functions, neural networks would just be linear transformations (no matter how many layers).&lt;/p&gt;

&lt;p&gt;Common Activation Functions:&lt;/p&gt;

&lt;p&gt;1.Sigmoid:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Formula: 1 / (1 + e^-x)&lt;/li&gt;
&lt;li&gt;Output: (0, 1)&lt;/li&gt;
&lt;li&gt;Use: Binary classification output&lt;/li&gt;
&lt;li&gt;Problems: Vanishing gradients, not zero-centered&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Tanh:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Formula: (e^x - e^-x) / (e^x + e^-x)&lt;/li&gt;
&lt;li&gt;Output: (-1, 1)&lt;/li&gt;
&lt;li&gt;Use: Hidden layers (better than sigmoid)&lt;/li&gt;
&lt;li&gt;Problems: Still vanishing gradients&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.ReLU (Rectified Linear Unit):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Formula: max(0, x)&lt;/li&gt;
&lt;li&gt;Output: [0, infinity)&lt;/li&gt;
&lt;li&gt;Use: Most common for hidden layers&lt;/li&gt;
&lt;li&gt;Advantages: Fast, no vanishing gradients&lt;/li&gt;
&lt;li&gt;Problems: Dead neurons (negative inputs always 0)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Leaky ReLU:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Formula: max(0.01x, x)&lt;/li&gt;
&lt;li&gt;Fixes dead neuron problem&lt;/li&gt;
&lt;li&gt;Small gradient for negative values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.GELU (Gaussian Error Linear Unit):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used in transformers (BERT, GPT)&lt;/li&gt;
&lt;li&gt;Smoother than ReLU&lt;/li&gt;
&lt;li&gt;Better performance in many cases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.Swish/SiLU:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Formula: x * sigmoid(x)&lt;/li&gt;
&lt;li&gt;Self-gated activation&lt;/li&gt;
&lt;li&gt;Used in modern architectures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7.Softmax:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used in output layer for multi-class&lt;/li&gt;
&lt;li&gt;Converts scores to probabilities&lt;/li&gt;
&lt;li&gt;Sum of outputs = 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Choosing Activation&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hidden layers: ReLU or variants&lt;/li&gt;
&lt;li&gt;Binary classification output: Sigmoid&lt;/li&gt;
&lt;li&gt;Multi-class output: Softmax&lt;/li&gt;
&lt;li&gt;Regression output: Linear (no activation)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.3 &lt;strong&gt;Loss Functions&lt;/strong&gt;&lt;br&gt;
Purpose:&lt;br&gt;
Measure how wrong the model's predictions are. Training minimizes this.&lt;br&gt;
&lt;strong&gt;Classification Loss Functions&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Binary Cross-Entropy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For binary classification&lt;/li&gt;
&lt;li&gt;Formula: -[y*log(p) + (1-y)*log(1-p)]&lt;/li&gt;
&lt;li&gt;Used with sigmoid output&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Categorical Cross-Entropy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For multi-class classification&lt;/li&gt;
&lt;li&gt;Each sample belongs to one class&lt;/li&gt;
&lt;li&gt;Used with softmax output&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Sparse Categorical Cross-Entropy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Same as categorical but with integer labels&lt;/li&gt;
&lt;li&gt;More memory efficient&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Regression Loss Functions&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Mean Squared Error (MSE):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Most common for regression&lt;/li&gt;
&lt;li&gt;Sensitive to outliers&lt;/li&gt;
&lt;li&gt;Formula: mean((y_true - y_pred)^2)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Mean Absolute Error (MAE):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;More robust to outliers&lt;/li&gt;
&lt;li&gt;Formula: mean(|y_true - y_pred|)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Huber Loss:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Combination of MSE and MAE&lt;/li&gt;
&lt;li&gt;Less sensitive to outliers than MSE&lt;/li&gt;
&lt;li&gt;Quadratic for small errors, linear for large&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advanced Loss Functions (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Focal Loss:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Addresses class imbalance&lt;/li&gt;
&lt;li&gt;Focuses on hard examples&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Contrastive Loss:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For similarity learning&lt;/li&gt;
&lt;li&gt;Used in embedding models&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Triplet Loss:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For metric learning&lt;/li&gt;
&lt;li&gt;Anchor, positive, negative examples&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.4 Backpropagation&lt;br&gt;
&lt;strong&gt;What is Backpropagation?&lt;/strong&gt;&lt;br&gt;
Algorithm for computing gradients of loss with respect to all network weights.&lt;br&gt;
How it Works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Forward pass: Compute predictions and loss&lt;/li&gt;
&lt;li&gt;Backward pass: Compute gradients using chain rule&lt;/li&gt;
&lt;li&gt;Update weights using gradients and optimizer&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Chain Rule Application&lt;/strong&gt;:&lt;br&gt;
For nested functions f(g(x)), derivative is:&lt;br&gt;
df/dx = (df/dg) * (dg/dx)&lt;br&gt;
Neural networks are composition of many functions, so chain rule applies throughout.&lt;br&gt;
&lt;strong&gt;Computational Graph&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nodes: Operations&lt;/li&gt;
&lt;li&gt;Edges: Data flow&lt;/li&gt;
&lt;li&gt;Forward pass: Compute values&lt;/li&gt;
&lt;li&gt;Backward pass: Compute gradients&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why it Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Efficiently computes all gradients in one backward pass&lt;/li&gt;
&lt;li&gt;Reuses intermediate computations&lt;/li&gt;
&lt;li&gt;Foundation of deep learning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Vanishing Gradients:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gradients become very small in deep networks&lt;/li&gt;
&lt;li&gt;Early layers learn slowly&lt;/li&gt;
&lt;li&gt;Solutions: ReLU, skip connections, batch normalization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Exploding Gradients:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gradients become very large&lt;/li&gt;
&lt;li&gt;Training becomes unstable&lt;/li&gt;
&lt;li&gt;Solutions: Gradient clipping, proper initialization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.5 Optimization Algorithms&lt;br&gt;
Beyond Basic Gradient Descent:&lt;br&gt;
&lt;strong&gt;Momentum&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adds fraction of previous update&lt;/li&gt;
&lt;li&gt;Helps escape local minima&lt;/li&gt;
&lt;li&gt;Smooths optimization path&lt;/li&gt;
&lt;li&gt;Formula: v = momentum * v - learning_rate * gradient&lt;/li&gt;
&lt;li&gt;weights = weights + v&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;RMSprop&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adaptive learning rates per parameter&lt;/li&gt;
&lt;li&gt;Divides by running average of squared gradients&lt;/li&gt;
&lt;li&gt;Works well for non-stationary objectives&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Adam (Adaptive Moment Estimation)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Combines momentum and RMSprop&lt;/li&gt;
&lt;li&gt;Most popular optimizer&lt;/li&gt;
&lt;li&gt;Maintains per-parameter learning rates&lt;/li&gt;
&lt;li&gt;Works well with minimal tuning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;AdamW&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adam with decoupled weight decay&lt;/li&gt;
&lt;li&gt;Better regularization&lt;/li&gt;
&lt;li&gt;Preferred in many modern applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modern Optimizers (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Lion:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;More memory efficient than Adam&lt;/li&gt;
&lt;li&gt;Better performance on large models&lt;/li&gt;
&lt;li&gt;Growing adoption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Sophia:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Second-order optimizer&lt;/li&gt;
&lt;li&gt;Faster convergence for LLMs&lt;/li&gt;
&lt;li&gt;Used in large-scale training&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Muon:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Coordinate-wise momentum&lt;/li&gt;
&lt;li&gt;Better for certain architectures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Learning Rate Schedules&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Step Decay:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce by factor every N epochs&lt;/li&gt;
&lt;li&gt;Simple and effective&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Exponential Decay:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gradually decrease&lt;/li&gt;
&lt;li&gt;Smooth reduction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Cosine Annealing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Follows cosine curve&lt;/li&gt;
&lt;li&gt;Popular in modern training&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Warmup:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with small learning rate&lt;/li&gt;
&lt;li&gt;Gradually increase&lt;/li&gt;
&lt;li&gt;Helps stability in early training&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.One Cycle Policy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Increases then decreases&lt;/li&gt;
&lt;li&gt;Faster training&lt;/li&gt;
&lt;li&gt;Popular for CNNs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.6 Regularization Techniques&lt;br&gt;
Why Regularization?&lt;br&gt;
Prevent overfitting and improve generalization.&lt;br&gt;
&lt;strong&gt;Common Techniques&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.L2 Regularization (Weight Decay):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add penalty for large weights&lt;/li&gt;
&lt;li&gt;Loss = original_loss + lambda * sum(weights^2)&lt;/li&gt;
&lt;li&gt;Encourages smaller weights&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.L1 Regularization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loss = original_loss + lambda * sum(|weights|)&lt;/li&gt;
&lt;li&gt;Encourages sparsity&lt;/li&gt;
&lt;li&gt;Feature selection&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Dropout:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Randomly drop neurons during training&lt;/li&gt;
&lt;li&gt;Prevents co-adaptation&lt;/li&gt;
&lt;li&gt;Typical rate: 0.2-0.5&lt;/li&gt;
&lt;li&gt;Not used during inference&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Batch Normalization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normalize layer inputs&lt;/li&gt;
&lt;li&gt;Reduces internal covariate shift&lt;/li&gt;
&lt;li&gt;Acts as regularizer&lt;/li&gt;
&lt;li&gt;Speeds up training&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Layer Normalization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normalizes across features&lt;/li&gt;
&lt;li&gt;Better for sequential models&lt;/li&gt;
&lt;li&gt;Used in transformers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.Data Augmentation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Artificially increase training data&lt;/li&gt;
&lt;li&gt;Images: rotation, flipping, cropping&lt;/li&gt;
&lt;li&gt;Text: back-translation, synonym replacement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7.Early Stopping:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stop training when validation loss stops improving&lt;/li&gt;
&lt;li&gt;Simple and effective&lt;/li&gt;
&lt;li&gt;Monitor patience parameter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;8.Label Smoothing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't use hard 0/1 labels&lt;/li&gt;
&lt;li&gt;Prevents overconfidence&lt;/li&gt;
&lt;li&gt;Typical: 0.1 smoothing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.7 Batch Normalization and Variants&lt;br&gt;
&lt;strong&gt;Batch Normalization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normalizes mini-batch to have mean 0, variance 1&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Learnable scale and shift parameters&lt;br&gt;
Benefits:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Faster training&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Higher learning rates possible&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Less sensitive to initialization&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Acts as regularization&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Layer Normalization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normalizes across features instead of batch&lt;/li&gt;
&lt;li&gt;Better for RNNs and transformers&lt;/li&gt;
&lt;li&gt;Not dependent on batch size&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Instance Normalization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normalizes each sample independently&lt;/li&gt;
&lt;li&gt;Used in style transfer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Group Normalization&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Middle ground between layer and instance&lt;/li&gt;
&lt;li&gt;Divides channels into groups&lt;/li&gt;
&lt;li&gt;Good when batch size is small&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to Use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CNNs: Batch Normalization&lt;/li&gt;
&lt;li&gt;Transformers/RNNs: Layer Normalization&lt;/li&gt;
&lt;li&gt;Style transfer: Instance Normalization&lt;/li&gt;
&lt;li&gt;Small batches: Group Normalization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.8 Weight Initialization&lt;br&gt;
Why it Matters:&lt;br&gt;
Poor initialization can cause vanishing/exploding gradients or slow training.&lt;br&gt;
Common Strategies:&lt;/p&gt;

&lt;p&gt;1.Xavier/Glorot Initialization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For sigmoid/tanh activations&lt;/li&gt;
&lt;li&gt;Variance based on input/output dimensions&lt;/li&gt;
&lt;li&gt;Keeps variance consistent across layers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.He Initialization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For ReLU activations&lt;/li&gt;
&lt;li&gt;Accounts for ReLU's non-linearity&lt;/li&gt;
&lt;li&gt;Most common choice&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Zero Initialization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bad idea (symmetry problem)&lt;/li&gt;
&lt;li&gt;All neurons learn same thing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rule of Thumb&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ReLU networks: He initialization&lt;/li&gt;
&lt;li&gt;Tanh networks: Xavier initialization&lt;/li&gt;
&lt;li&gt;Pre-trained models: Transfer learning weights&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  6. Natural Language Processing (NLP)
&lt;/h2&gt;

&lt;p&gt;NLP has been revolutionized by transformers and large language models. Understanding the evolution from traditional to modern methods is crucial.&lt;br&gt;
6.1 &lt;strong&gt;Text Preprocessing&lt;/strong&gt;&lt;br&gt;
Basic Preprocessing Steps:&lt;/p&gt;

&lt;p&gt;1.Lowercasing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Convert all text to lowercase&lt;/li&gt;
&lt;li&gt;Reduces vocabulary size&lt;/li&gt;
&lt;li&gt;May lose information (proper nouns)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Tokenization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Split text into words or subwords&lt;/li&gt;
&lt;li&gt;Word tokenization: Split by spaces/punctuation&lt;/li&gt;
&lt;li&gt;Sentence tokenization: Split into sentences&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Removing Punctuation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sometimes helpful, sometimes not&lt;/li&gt;
&lt;li&gt;Depends on task&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Removing Stop Words:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Common words (the, is, at)&lt;/li&gt;
&lt;li&gt;May or may not help&lt;/li&gt;
&lt;li&gt;Modern models often keep them&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Stemming:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce words to root form&lt;/li&gt;
&lt;li&gt;Crude: running → run, runs → run&lt;/li&gt;
&lt;li&gt;Fast but imprecise&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.Lemmatization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce to dictionary form&lt;/li&gt;
&lt;li&gt;More accurate than stemming&lt;/li&gt;
&lt;li&gt;Slower&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modern Preprocessing (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Less preprocessing needed for transformers&lt;/li&gt;
&lt;li&gt;Often just basic cleaning&lt;/li&gt;
&lt;li&gt;Models learn from raw text&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.2 &lt;strong&gt;Text Representation&lt;/strong&gt;&lt;br&gt;
Traditional Methods:&lt;/p&gt;

&lt;p&gt;1.Bag of Words (BoW):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Count word occurrences&lt;/li&gt;
&lt;li&gt;Ignores order and context&lt;/li&gt;
&lt;li&gt;Simple baseline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.TF-IDF:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Term Frequency - Inverse Document Frequency&lt;/li&gt;
&lt;li&gt;Weights words by importance&lt;/li&gt;
&lt;li&gt;Rare words get higher weight&lt;/li&gt;
&lt;li&gt;Common across documents get lower weight&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.N-grams:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sequences of n words&lt;/li&gt;
&lt;li&gt;Bigrams: 2 words&lt;/li&gt;
&lt;li&gt;Trigrams: 3 words&lt;/li&gt;
&lt;li&gt;Captures some context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Embedding Methods:&lt;/p&gt;

&lt;p&gt;1.Word2Vec:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dense vector representations&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Two architectures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CBOW: Predict word from context&lt;/li&gt;
&lt;li&gt;Skip-gram: Predict context from word&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Semantic similarity in vector space&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;king - man + woman ≈ queen&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.GloVe:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global Vectors&lt;/li&gt;
&lt;li&gt;Matrix factorization on co-occurrence&lt;/li&gt;
&lt;li&gt;Pre-trained embeddings available&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.FastText:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extension of Word2Vec&lt;/li&gt;
&lt;li&gt;Uses character n-grams&lt;/li&gt;
&lt;li&gt;Handles out-of-vocabulary words&lt;/li&gt;
&lt;li&gt;Good for morphologically rich languages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modern Embeddings (2026):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1.Contextual Embeddings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Same word, different contexts, different embeddings&lt;/li&gt;
&lt;li&gt;From BERT, GPT, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Sentence Embeddings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sentence-BERT&lt;/li&gt;
&lt;li&gt;Universal Sentence Encoder&lt;/li&gt;
&lt;li&gt;Whole sentence to vector&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Specialized Embeddings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code embeddings (CodeBERT)&lt;/li&gt;
&lt;li&gt;Multimodal (CLIP)&lt;/li&gt;
&lt;li&gt;Domain-specific (BioBERT, FinBERT)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.3 &lt;strong&gt;Classical NLP Tasks&lt;/strong&gt;&lt;br&gt;
Text Classification:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spam detection&lt;/li&gt;
&lt;li&gt;Sentiment analysis&lt;/li&gt;
&lt;li&gt;Topic classification&lt;/li&gt;
&lt;li&gt;Intent recognition&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Named Entity Recognition (NER):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Identify entities (person, location, organization)&lt;/li&gt;
&lt;li&gt;Sequence labeling task&lt;/li&gt;
&lt;li&gt;CRF, BiLSTM-CRF, transformer-based&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Part-of-Speech Tagging:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Label grammatical categories&lt;/li&gt;
&lt;li&gt;Noun, verb, adjective, etc.&lt;/li&gt;
&lt;li&gt;Foundation for parsing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sentiment Analysis:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Determine emotional tone&lt;/li&gt;
&lt;li&gt;Positive, negative, neutral&lt;/li&gt;
&lt;li&gt;Aspect-based sentiment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Machine Translation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Translate text between languages&lt;/li&gt;
&lt;li&gt;Sequence-to-sequence task&lt;/li&gt;
&lt;li&gt;Dominated by transformers now&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.4 &lt;strong&gt;Sequence Models (RNN, LSTM, GRU)&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Recurrent Neural Networks (RNN)&lt;/strong&gt;:&lt;br&gt;
Concept:&lt;br&gt;
Process sequential data by maintaining hidden state.&lt;br&gt;
How it Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hidden state updated at each time step&lt;/li&gt;
&lt;li&gt;Same weights used at each step&lt;/li&gt;
&lt;li&gt;Can handle variable-length sequences&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vanishing/exploding gradients&lt;/li&gt;
&lt;li&gt;Difficulty learning long-term dependencies&lt;/li&gt;
&lt;li&gt;Sequential processing (slow)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Long Short-Term Memory (LSTM)&lt;/strong&gt;:&lt;br&gt;
Concept:&lt;br&gt;
RNN variant with gating mechanisms to control information flow.&lt;br&gt;
Components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Forget Gate: Decides what to forget from cell state&lt;/li&gt;
&lt;li&gt;Input Gate: Decides what new information to add&lt;/li&gt;
&lt;li&gt;Output Gate: Decides what to output&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Advantages over RNN:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Captures long-term dependencies&lt;/li&gt;
&lt;li&gt;Mitigates vanishing gradient&lt;/li&gt;
&lt;li&gt;More stable training&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Gated Recurrent Unit (GRU)&lt;/strong&gt;:&lt;br&gt;
Concept:&lt;br&gt;
Simplified LSTM with fewer parameters.&lt;br&gt;
Components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reset Gate: Controls past information&lt;/li&gt;
&lt;li&gt;Update Gate: Controls new information&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster than LSTM&lt;/li&gt;
&lt;li&gt;Fewer parameters&lt;/li&gt;
&lt;li&gt;Often similar performance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Modern Status (2026):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Largely replaced by transformers&lt;/li&gt;
&lt;li&gt;Still used for some time series&lt;/li&gt;
&lt;li&gt;Understanding them helps with attention mechanisms&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.5 Attention Mechanism&lt;br&gt;
Why Attention?&lt;br&gt;
Allows model to focus on relevant parts of input.&lt;br&gt;
How it Works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Compute attention scores for each input position&lt;/li&gt;
&lt;li&gt;Apply softmax to get attention weights&lt;/li&gt;
&lt;li&gt;Weighted sum of values&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Types of Attention:&lt;/p&gt;

&lt;p&gt;1.Additive Attention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses neural network to compute scores&lt;/li&gt;
&lt;li&gt;Original attention mechanism&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Multiplicative (Dot-Product) Attention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster than additive&lt;/li&gt;
&lt;li&gt;Used in transformers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Self-Attention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Attention within same sequence&lt;/li&gt;
&lt;li&gt;Foundation of transformers&lt;/li&gt;
&lt;li&gt;Each position attends to all positions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Scaled Dot-Product Attention:&lt;br&gt;
Attention(Q, K, V) = softmax(QK^T / sqrt(d_k)) * V&lt;br&gt;
Where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Q: Queries&lt;/li&gt;
&lt;li&gt;K: Keys&lt;/li&gt;
&lt;li&gt;V: Values&lt;/li&gt;
&lt;li&gt;d_k: Dimension of keys (scaling factor)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Captures long-range dependencies&lt;/li&gt;
&lt;li&gt;Parallelizable (unlike RNNs)&lt;/li&gt;
&lt;li&gt;Interpretable (can visualize attention)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.6 Transformer Architecture&lt;br&gt;
Revolutionary Impact:&lt;br&gt;
Transformers fundamentally changed NLP and now dominate many AI tasks.&lt;br&gt;
Core Components:&lt;/p&gt;

&lt;p&gt;1.Multi-Head Attention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple attention mechanisms in parallel&lt;/li&gt;
&lt;li&gt;Learn different aspects of relationships&lt;/li&gt;
&lt;li&gt;Concatenate and project results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Position Encoding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add positional information (no recurrence)&lt;/li&gt;
&lt;li&gt;Sinusoidal or learned embeddings&lt;/li&gt;
&lt;li&gt;Tells model about sequence order&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Feed-Forward Networks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Applied to each position separately&lt;/li&gt;
&lt;li&gt;Two linear layers with activation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Layer Normalization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normalizes across features&lt;/li&gt;
&lt;li&gt;Stabilizes training&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Residual Connections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add input to output of sublayer&lt;/li&gt;
&lt;li&gt;Helps gradient flow&lt;/li&gt;
&lt;li&gt;Enables deeper networks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Encoder-Decoder Architecture&lt;/strong&gt;:&lt;br&gt;
Encoder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Self-attention layers&lt;/li&gt;
&lt;li&gt;Processes input sequence&lt;/li&gt;
&lt;li&gt;Creates contextualized representations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Decoder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Self-attention on output&lt;/li&gt;
&lt;li&gt;Cross-attention to encoder&lt;/li&gt;
&lt;li&gt;Generates output sequence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Original Transformer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;6 encoder layers&lt;/li&gt;
&lt;li&gt;6 decoder layers&lt;/li&gt;
&lt;li&gt;Multi-head attention (8 heads)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why Transformers Win:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parallelizable training&lt;/li&gt;
&lt;li&gt;Captures long-range dependencies&lt;/li&gt;
&lt;li&gt;Scales to massive datasets&lt;/li&gt;
&lt;li&gt;Transfer learning capability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Variants (2026):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encoder-only: BERT&lt;/li&gt;
&lt;li&gt;Decoder-only: GPT&lt;/li&gt;
&lt;li&gt;Encoder-decoder: T5, BART&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Large Language Models (LLMs) and Modern NLP&lt;/strong&gt;
This is the most critical section for 2026. LLMs have transformed AI/ML engineering.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;7.1 &lt;strong&gt;Pre-training and Fine-tuning Paradigm&lt;/strong&gt;&lt;br&gt;
Pre-training:&lt;br&gt;
Train on massive unlabeled text data to learn language understanding.&lt;br&gt;
Objectives:&lt;/p&gt;

&lt;p&gt;1.Masked Language Modeling (MLM):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used by BERT&lt;/li&gt;
&lt;li&gt;Randomly mask words&lt;/li&gt;
&lt;li&gt;Predict masked words&lt;/li&gt;
&lt;li&gt;Bidirectional context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Causal Language Modeling (CLM):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used by GPT&lt;/li&gt;
&lt;li&gt;Predict next word&lt;/li&gt;
&lt;li&gt;Left-to-right context&lt;/li&gt;
&lt;li&gt;Autoregressive generation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Denoising:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Used by T5, BART&lt;/li&gt;
&lt;li&gt;Corrupt text in various ways&lt;/li&gt;
&lt;li&gt;Reconstruct original&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Fine-tuning&lt;/strong&gt;:&lt;br&gt;
Adapt pre-trained model to specific task with task-specific data.&lt;br&gt;
&lt;strong&gt;Benefits&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Leverages general knowledge&lt;/li&gt;
&lt;li&gt;Requires less task-specific data&lt;/li&gt;
&lt;li&gt;Better performance&lt;/li&gt;
&lt;li&gt;Faster convergence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Modern Paradigm (2026):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pre-training is expensive (done by few companies)&lt;/li&gt;
&lt;li&gt;Most engineers use pre-trained models&lt;/li&gt;
&lt;li&gt;Fine-tuning or prompting for specific tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7.2 &lt;strong&gt;Major LLM Architectures&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;BERT (Bidirectional Encoder Representations from Transformers)&lt;/strong&gt;:&lt;br&gt;
Architecture:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Encoder-only transformer&lt;br&gt;
Bidirectional context&lt;br&gt;
Pre-trained with MLM and Next Sentence Prediction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use Cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Text classification&lt;/li&gt;
&lt;li&gt;Named Entity Recognition&lt;/li&gt;
&lt;li&gt;Question answering&lt;/li&gt;
&lt;li&gt;Sentence similarity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Variants:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RoBERTa: Improved training&lt;/li&gt;
&lt;li&gt;ALBERT: Parameter sharing&lt;/li&gt;
&lt;li&gt;DistilBERT: Smaller, faster&lt;/li&gt;
&lt;li&gt;DeBERTa: Enhanced attention&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;GPT (Generative Pre-trained Transformer)&lt;/strong&gt;:&lt;br&gt;
Architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decoder-only transformer&lt;/li&gt;
&lt;li&gt;Unidirectional (left-to-right)&lt;/li&gt;
&lt;li&gt;Pre-trained with causal language modeling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Evolution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GPT-1: 117M parameters&lt;/li&gt;
&lt;li&gt;GPT-2: 1.5B parameters&lt;/li&gt;
&lt;li&gt;GPT-3: 175B parameters&lt;/li&gt;
&lt;li&gt;GPT-4: Architecture details not public (likely mixture of experts)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capabilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Text generation&lt;/li&gt;
&lt;li&gt;Few-shot learning&lt;/li&gt;
&lt;li&gt;In-context learning&lt;/li&gt;
&lt;li&gt;Reasoning and problem-solving&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;T5 (Text-to-Text Transfer Transformer)&lt;/strong&gt;:&lt;br&gt;
Architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encoder-decoder transformer&lt;/li&gt;
&lt;li&gt;Frames all tasks as text-to-text&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Input: "translate English to French: Hello"&lt;/li&gt;
&lt;li&gt;Output: "Bonjour"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Flexibility:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unified framework for all NLP tasks&lt;/li&gt;
&lt;li&gt;Easy to adapt to new tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Modern LLMs (2026):&lt;/p&gt;

&lt;p&gt;1.Claude (Anthropic):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Constitutional AI training&lt;/li&gt;
&lt;li&gt;Strong reasoning&lt;/li&gt;
&lt;li&gt;Long context windows (200k+ tokens)&lt;/li&gt;
&lt;li&gt;Multimodal capabilities&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.GPT-4 and GPT-4.5:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multimodal (text, images, code)&lt;/li&gt;
&lt;li&gt;Advanced reasoning&lt;/li&gt;
&lt;li&gt;Function calling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Gemini (Google):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multimodal from ground up&lt;/li&gt;
&lt;li&gt;Strong reasoning&lt;/li&gt;
&lt;li&gt;Multiple model sizes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Llama 3 and 4 (Meta):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open weights&lt;/li&gt;
&lt;li&gt;Strong performance&lt;/li&gt;
&lt;li&gt;Good for fine-tuning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Mixtral (Mistral AI):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mixture of Experts&lt;/li&gt;
&lt;li&gt;Efficient inference&lt;/li&gt;
&lt;li&gt;Open source&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7.3 &lt;strong&gt;Prompting Techniques&lt;/strong&gt;&lt;br&gt;
What is Prompting?&lt;br&gt;
Crafting input text to get desired output from LLM without fine-tuning.&lt;/p&gt;

&lt;p&gt;Basic Prompting:&lt;br&gt;
Simply describe the task in natural language.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
"Translate the following to Spanish: Hello, how are you?"&lt;br&gt;
Few-Shot Prompting:&lt;br&gt;
Provide examples before the actual query.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;English: I love coding
Spanish: Me encanta programar

English: The weather is nice
Spanish: El clima es agradable

English: Hello, how are you?
Spanish:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Chain-of-Thought (CoT)&lt;/strong&gt;:&lt;br&gt;
Encourage step-by-step reasoning.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
"Let's solve this step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, identify what we know&lt;/li&gt;
&lt;li&gt;Then, determine what we need to find&lt;/li&gt;
&lt;li&gt;Finally, calculate the answer"&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Zero-Shot CoT&lt;/strong&gt;:&lt;br&gt;
Simply add "Let's think step by step" to prompt.&lt;br&gt;
Self-Consistency:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generate multiple reasoning paths&lt;/li&gt;
&lt;li&gt;Choose most consistent answer&lt;/li&gt;
&lt;li&gt;Improves accuracy on complex tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;ReAct (Reasoning + Acting)&lt;/strong&gt;:&lt;br&gt;
Interleave reasoning and actions (tool use).&lt;br&gt;
Pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Thought: [reasoning]
Action: [tool/action to take]
Observation: [result]
Thought: [next reasoning]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tree of Thoughts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explore multiple reasoning paths&lt;/li&gt;
&lt;li&gt;Backtrack if needed&lt;/li&gt;
&lt;li&gt;More thorough exploration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Advanced Prompting (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Meta-Prompting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have LLM improve its own prompt&lt;/li&gt;
&lt;li&gt;Iterative refinement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Retrieval-Augmented Prompting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Retrieve relevant context&lt;/li&gt;
&lt;li&gt;Include in prompt&lt;/li&gt;
&lt;li&gt;Reduce hallucinations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Multi-Agent Prompting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple specialized prompts&lt;/li&gt;
&lt;li&gt;Debate or collaborate&lt;/li&gt;
&lt;li&gt;Improved reasoning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prompt Engineering Best Practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Be specific and clear&lt;/li&gt;
&lt;li&gt;Provide context&lt;/li&gt;
&lt;li&gt;Use examples when helpful&lt;/li&gt;
&lt;li&gt;Specify output format&lt;/li&gt;
&lt;li&gt;Iterate and refine&lt;/li&gt;
&lt;li&gt;Test edge cases&lt;/li&gt;
&lt;li&gt;Consider token limits&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7.4 Fine-Tuning LLMs&lt;br&gt;
When to Fine-Tune:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Specific domain knowledge needed&lt;/li&gt;
&lt;li&gt;Consistent output format required&lt;/li&gt;
&lt;li&gt;Specific tone or style needed&lt;/li&gt;
&lt;li&gt;Privacy concerns (keep data in-house)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When NOT to Fine-Tune:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prompting works well&lt;/li&gt;
&lt;li&gt;Limited training data&lt;/li&gt;
&lt;li&gt;Task changes frequently&lt;/li&gt;
&lt;li&gt;Budget constraints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Full Fine-Tuning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Update all parameters&lt;/li&gt;
&lt;li&gt;Requires significant compute&lt;/li&gt;
&lt;li&gt;Best performance&lt;/li&gt;
&lt;li&gt;Expensive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Parameter-Efficient Fine-Tuning (PEFT)&lt;/strong&gt;:&lt;br&gt;
&lt;strong&gt;LoRA (Low-Rank Adaptation)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add small trainable matrices&lt;/li&gt;
&lt;li&gt;Freeze original weights&lt;/li&gt;
&lt;li&gt;Much cheaper than full fine-tuning&lt;/li&gt;
&lt;li&gt;90% less memory&lt;/li&gt;
&lt;li&gt;Nearly same performance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How LoRA Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Original weight: W&lt;/li&gt;
&lt;li&gt;Update: W + A*B&lt;/li&gt;
&lt;li&gt;A and B are small matrices (rank r &amp;lt;&amp;lt; d)&lt;/li&gt;
&lt;li&gt;Only train A and B&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;QLoRA:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LoRA with quantization&lt;/li&gt;
&lt;li&gt;Quantize base model to 4-bit&lt;/li&gt;
&lt;li&gt;Train LoRA adapters in higher precision&lt;/li&gt;
&lt;li&gt;Even more memory efficient&lt;/li&gt;
&lt;li&gt;Can fine-tune 65B models on single GPU&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Adapter Modules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insert small trainable layers&lt;/li&gt;
&lt;li&gt;Freeze base model&lt;/li&gt;
&lt;li&gt;Switch adapters for different tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prefix Tuning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add trainable prefix vectors&lt;/li&gt;
&lt;li&gt;Freeze transformer parameters&lt;/li&gt;
&lt;li&gt;Lightweight adaptation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;P-Tuning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optimize continuous prompts&lt;/li&gt;
&lt;li&gt;More flexible than discrete prompts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fine-Tuning Process:&lt;/p&gt;

&lt;p&gt;1.Data Preparation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clean and format data&lt;/li&gt;
&lt;li&gt;Create instruction-response pairs&lt;/li&gt;
&lt;li&gt;Split train/validation/test&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Model Selection:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose base model&lt;/li&gt;
&lt;li&gt;Consider size vs performance&lt;/li&gt;
&lt;li&gt;Check license&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Training:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Choose fine-tuning method&lt;/li&gt;
&lt;li&gt;Set hyperparameters&lt;/li&gt;
&lt;li&gt;Monitor validation loss&lt;/li&gt;
&lt;li&gt;Use gradient checkpointing for memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Evaluation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test on held-out data&lt;/li&gt;
&lt;li&gt;Human evaluation&lt;/li&gt;
&lt;li&gt;Compare to base model&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Deployment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optimize for inference&lt;/li&gt;
&lt;li&gt;Quantization&lt;/li&gt;
&lt;li&gt;Serve with appropriate framework&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modern Tools (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hugging Face PEFT library&lt;/li&gt;
&lt;li&gt;Axolotl for training&lt;/li&gt;
&lt;li&gt;LitGPT for LLM training&lt;/li&gt;
&lt;li&gt;Modal for serverless training&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7.5 &lt;strong&gt;Retrieval-Augmented Generation (RAG)&lt;/strong&gt;&lt;br&gt;
What is RAG?&lt;br&gt;
Combine retrieval of relevant documents with LLM generation to provide accurate, grounded responses.&lt;br&gt;
&lt;strong&gt;Why RAG?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduces hallucinations&lt;/li&gt;
&lt;li&gt;Provides source citations&lt;/li&gt;
&lt;li&gt;Updates knowledge without retraining&lt;/li&gt;
&lt;li&gt;Cost-effective vs fine-tuning&lt;/li&gt;
&lt;li&gt;Handles private/proprietary data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Basic RAG Architecture:&lt;/p&gt;

&lt;p&gt;1.Indexing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Split documents into chunks&lt;/li&gt;
&lt;li&gt;Generate embeddings&lt;/li&gt;
&lt;li&gt;Store in vector database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Retrieval:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Convert query to embedding&lt;/li&gt;
&lt;li&gt;Find similar chunks&lt;/li&gt;
&lt;li&gt;Retrieve top-k results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Generation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Combine query and retrieved context&lt;/li&gt;
&lt;li&gt;Send to LLM&lt;/li&gt;
&lt;li&gt;Generate response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chunking Strategies:&lt;/p&gt;

&lt;p&gt;1.Fixed-Size Chunks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple: 512 tokens per chunk&lt;/li&gt;
&lt;li&gt;May split mid-sentence&lt;/li&gt;
&lt;li&gt;Fast and simple&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Sentence-Based:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Split on sentences&lt;/li&gt;
&lt;li&gt;More coherent chunks&lt;/li&gt;
&lt;li&gt;Variable size&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Semantic Chunking:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Group by topic/meaning&lt;/li&gt;
&lt;li&gt;Better context preservation&lt;/li&gt;
&lt;li&gt;More complex&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Recursive Splitting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Try paragraph, then sentence, then words&lt;/li&gt;
&lt;li&gt;Maintains structure&lt;/li&gt;
&lt;li&gt;Flexible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chunk Size Considerations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Too small: Lose context&lt;/li&gt;
&lt;li&gt;Too large: Irrelevant info, exceed token limits&lt;/li&gt;
&lt;li&gt;Typical: 256-512 tokens&lt;/li&gt;
&lt;li&gt;Overlap: 50-100 tokens between chunks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Embedding Models (2026):&lt;/p&gt;

&lt;p&gt;1.OpenAI Embeddings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;text-embedding-3-large&lt;/li&gt;
&lt;li&gt;text-embedding-3-small&lt;/li&gt;
&lt;li&gt;High quality, paid API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Open Source:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;bge-large (BAAI)&lt;/li&gt;
&lt;li&gt;e5-mistral (Microsoft)&lt;/li&gt;
&lt;li&gt;gte-large (Alibaba)&lt;/li&gt;
&lt;li&gt;sentence-transformers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Specialized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cohere for semantic search&lt;/li&gt;
&lt;li&gt;Voyage AI for domain-specific&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Vector Databases:&lt;/p&gt;

&lt;p&gt;1.Pinecone:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Managed service&lt;/li&gt;
&lt;li&gt;Easy to use&lt;/li&gt;
&lt;li&gt;Paid&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Weaviate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open source&lt;/li&gt;
&lt;li&gt;Hybrid search&lt;/li&gt;
&lt;li&gt;Self-hosted or cloud&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Chroma:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lightweight&lt;/li&gt;
&lt;li&gt;Easy local development&lt;/li&gt;
&lt;li&gt;Good for prototyping&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Qdrant:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High performance&lt;/li&gt;
&lt;li&gt;Open source&lt;/li&gt;
&lt;li&gt;Production-ready&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Milvus:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Highly scalable&lt;/li&gt;
&lt;li&gt;Open source&lt;/li&gt;
&lt;li&gt;Enterprise features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Retrieval Strategies&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Dense Retrieval:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Embedding similarity&lt;/li&gt;
&lt;li&gt;Semantic search&lt;/li&gt;
&lt;li&gt;Most common&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Sparse Retrieval:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;BM25, TF-IDF&lt;/li&gt;
&lt;li&gt;Keyword matching&lt;/li&gt;
&lt;li&gt;Good for exact matches&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Hybrid Search:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Combine dense and sparse&lt;/li&gt;
&lt;li&gt;Best of both worlds&lt;/li&gt;
&lt;li&gt;Reranking results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Hypothetical Document Embeddings (HyDE):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generate hypothetical answer&lt;/li&gt;
&lt;li&gt;Embed that instead of query&lt;/li&gt;
&lt;li&gt;Better retrieval quality&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advanced RAG Techniques (2026):&lt;/p&gt;

&lt;p&gt;1.Query Rewriting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rephrase user query&lt;/li&gt;
&lt;li&gt;Multiple query variations&lt;/li&gt;
&lt;li&gt;Better retrieval coverage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Multi-Query Retrieval:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generate multiple queries&lt;/li&gt;
&lt;li&gt;Retrieve for each&lt;/li&gt;
&lt;li&gt;Combine results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Re-ranking:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initial retrieval (fast, lower quality)&lt;/li&gt;
&lt;li&gt;Re-rank top results (slower, higher quality)&lt;/li&gt;
&lt;li&gt;Cross-encoder models&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Contextual Compression:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Filter irrelevant parts of retrieved docs&lt;/li&gt;
&lt;li&gt;Keep only relevant sentences&lt;/li&gt;
&lt;li&gt;Reduces token usage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Parent Document Retrieval:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Retrieve small chunks&lt;/li&gt;
&lt;li&gt;Return larger parent documents&lt;/li&gt;
&lt;li&gt;Better context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.Multi-hop Reasoning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iteratively retrieve&lt;/li&gt;
&lt;li&gt;Use previous answers to refine&lt;/li&gt;
&lt;li&gt;Complex questions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7.Self-RAG:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model decides when to retrieve&lt;/li&gt;
&lt;li&gt;Critique and refine responses&lt;/li&gt;
&lt;li&gt;More autonomous&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;RAG Evaluation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Retrieval accuracy (recall, precision)&lt;/li&gt;
&lt;li&gt;Generation quality&lt;/li&gt;
&lt;li&gt;Factual accuracy&lt;/li&gt;
&lt;li&gt;Response relevance&lt;/li&gt;
&lt;li&gt;Latency&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common RAG Frameworks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LangChain: Most popular, comprehensive&lt;/li&gt;
&lt;li&gt;LlamaIndex: Data framework focus&lt;/li&gt;
&lt;li&gt;Haystack: Production-oriented&lt;/li&gt;
&lt;li&gt;txtai: Lightweight alternative&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7.6 &lt;strong&gt;LLM Agents and Orchestration&lt;/strong&gt;&lt;br&gt;
What are LLM Agents?&lt;br&gt;
Systems that use LLMs to reason, plan, and take actions to accomplish goals.&lt;br&gt;
Key Components:&lt;/p&gt;

&lt;p&gt;1.Reasoning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understand task&lt;/li&gt;
&lt;li&gt;Break down into steps&lt;/li&gt;
&lt;li&gt;Adapt based on results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Planning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create action sequence&lt;/li&gt;
&lt;li&gt;Consider dependencies&lt;/li&gt;
&lt;li&gt;Handle failures&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Memory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Short-term: Current conversation&lt;/li&gt;
&lt;li&gt;Long-term: Past interactions&lt;/li&gt;
&lt;li&gt;Semantic: General knowledge&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;External APIs&lt;/li&gt;
&lt;li&gt;Databases&lt;/li&gt;
&lt;li&gt;Code execution&lt;/li&gt;
&lt;li&gt;Web search&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Agent Architectures:&lt;/p&gt;

&lt;p&gt;1.ReAct Agent:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reasoning + Acting loop&lt;/li&gt;
&lt;li&gt;Interleave thought and action&lt;/li&gt;
&lt;li&gt;Popular baseline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Plan-and-Execute:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create plan first&lt;/li&gt;
&lt;li&gt;Execute steps&lt;/li&gt;
&lt;li&gt;More structured&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Reflexion:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Agent reflects on failures&lt;/li&gt;
&lt;li&gt;Learns from mistakes&lt;/li&gt;
&lt;li&gt;Iterative improvement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;LangGraph (2026)&lt;/strong&gt;:&lt;br&gt;
What is LangGraph?&lt;br&gt;
Framework for building stateful, multi-agent applications with cycles and state management.&lt;br&gt;
Key Concepts:&lt;/p&gt;

&lt;p&gt;1.State:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shared data structure&lt;/li&gt;
&lt;li&gt;Updated by nodes&lt;/li&gt;
&lt;li&gt;Persisted across steps&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Nodes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Functions that process state&lt;/li&gt;
&lt;li&gt;Can be LLM calls, tools, logic&lt;/li&gt;
&lt;li&gt;Return state updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Edges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define flow between nodes&lt;/li&gt;
&lt;li&gt;Conditional routing&lt;/li&gt;
&lt;li&gt;Enable cycles&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Cycles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iterate until condition met&lt;/li&gt;
&lt;li&gt;Enable complex workflows&lt;/li&gt;
&lt;li&gt;Self-correction loops&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example Use Cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Research assistant that iteratively refines&lt;/li&gt;
&lt;li&gt;Customer support with escalation paths&lt;/li&gt;
&lt;li&gt;Code generation with testing and refinement&lt;/li&gt;
&lt;li&gt;Multi-agent debates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Multi-Agent Systems&lt;/strong&gt;:&lt;br&gt;
Why Multiple Agents?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Specialization (each agent expert in domain)&lt;/li&gt;
&lt;li&gt;Parallel processing&lt;/li&gt;
&lt;li&gt;Debate and consensus&lt;/li&gt;
&lt;li&gt;Complex task decomposition&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Communication Patterns&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.Sequential:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Agent A → Agent B → Agent C&lt;/li&gt;
&lt;li&gt;Linear pipeline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Hierarchical:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manager agent coordinates workers&lt;/li&gt;
&lt;li&gt;Task delegation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Collaborative:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Agents work together&lt;/li&gt;
&lt;li&gt;Share information&lt;/li&gt;
&lt;li&gt;Consensus building&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example: Research Paper Analysis System:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Manager Agent
├─ Summarizer Agent (condense paper)
├─ Critique Agent (find weaknesses)
├─ Code Reviewer Agent (check implementations)
└─ Citation Agent (find related work)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tool Use / Function Calling:&lt;br&gt;
&lt;strong&gt;Concept&lt;/strong&gt;:&lt;br&gt;
LLM can call external functions to accomplish tasks.&lt;br&gt;
&lt;strong&gt;Process&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define function schema&lt;/li&gt;
&lt;li&gt;LLM decides which function to call&lt;/li&gt;
&lt;li&gt;Extract parameters from LLM output&lt;/li&gt;
&lt;li&gt;Execute function&lt;/li&gt;
&lt;li&gt;Return results to LLM&lt;/li&gt;
&lt;li&gt;LLM generates final response&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Common Tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Web search&lt;/li&gt;
&lt;li&gt;Calculator&lt;/li&gt;
&lt;li&gt;Database query&lt;/li&gt;
&lt;li&gt;API calls&lt;/li&gt;
&lt;li&gt;Code execution&lt;/li&gt;
&lt;li&gt;File operations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;OpenAI Function Calling:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Structured output&lt;/li&gt;
&lt;li&gt;Parallel function calls&lt;/li&gt;
&lt;li&gt;JSON mode&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Challenges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hallucinated parameters&lt;/li&gt;
&lt;li&gt;Function selection errors&lt;/li&gt;
&lt;li&gt;Token limits with many tools&lt;/li&gt;
&lt;li&gt;Latency with multiple calls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Agent Memory:&lt;/p&gt;

&lt;p&gt;1.Short-Term Memory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Current conversation&lt;/li&gt;
&lt;li&gt;Working context&lt;/li&gt;
&lt;li&gt;Managed by context window&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Long-Term Memory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vector database of past interactions&lt;/li&gt;
&lt;li&gt;Retrieve relevant memories&lt;/li&gt;
&lt;li&gt;Personalization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Entity Memory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remember facts about entities&lt;/li&gt;
&lt;li&gt;Knowledge graph&lt;/li&gt;
&lt;li&gt;Consistent information&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Agent Frameworks (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.LangGraph:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;State machines&lt;/li&gt;
&lt;li&gt;Complex workflows&lt;/li&gt;
&lt;li&gt;Production-ready&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.AutoGPT:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Autonomous task execution&lt;/li&gt;
&lt;li&gt;Self-prompting&lt;/li&gt;
&lt;li&gt;Web interaction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.BabyAGI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Task creation and prioritization&lt;/li&gt;
&lt;li&gt;Simple but effective&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.CrewAI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Role-based agents&lt;/li&gt;
&lt;li&gt;Collaborative workflows&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.AgentGPT:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Browser-based&lt;/li&gt;
&lt;li&gt;Visual task planning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Production-Preferred Frameworks (2026):&lt;br&gt;
| Framework | Best For | Key Feature | Maturity |&lt;br&gt;
|----------|----------|-------------|----------|&lt;br&gt;
| &lt;strong&gt;LangGraph&lt;/strong&gt; | Complex workflows, state machines | Graph-based orchestration, cycles | Production |&lt;br&gt;
| &lt;strong&gt;CrewAI&lt;/strong&gt; | Role-based multi-agent teams | Agent roles, collaboration patterns | Production |&lt;br&gt;
| &lt;strong&gt;AutoGen (Microsoft)&lt;/strong&gt; | Conversational agents, coding | Multi-agent conversation, code execution | Production |&lt;br&gt;
| &lt;strong&gt;OpenAI Swarm&lt;/strong&gt; | Lightweight orchestration | Simple, OpenAI-native | Experimental |&lt;br&gt;
| &lt;strong&gt;Dapr Agents&lt;/strong&gt; | Cloud-native, distributed | Kubernetes integration, resilience | Emerging |&lt;/p&gt;



&lt;p&gt;Framework Comparison:&lt;/p&gt;

&lt;p&gt;┌─────────────────────────────────────────────────────────────┐&lt;br&gt;
│  LangGraph: State Machine for Agents                        │&lt;br&gt;
│  ┌─────────┐    ┌─────────┐    ┌─────────┐                  │&lt;br&gt;
│  │ Research│───►│ Synthesize│───►│ Generate│                │&lt;br&gt;
│  │  Agent  │◄───│   Agent   │◄───│ Report  │                │&lt;br&gt;
│  └─────────┘    └─────────┘    └─────────┘                  │&lt;br&gt;
│       ▲              │              │                       │&lt;br&gt;
│       └──────────────┴──────────────┘                       │&lt;br&gt;
│                    (Cycles allowed)                         │&lt;br&gt;
└─────────────────────────────────────────────────────────────┘&lt;/p&gt;

&lt;p&gt;┌─────────────────────────────────────────────────────────────┐&lt;br&gt;
│  CrewAI: Role-Based Collaboration                           │&lt;br&gt;
│  ┌──────────┐  ┌──────────┐  ┌──────────┐                   │&lt;br&gt;
│  │  Manager │  │ Researcher│  │  Writer  │                  │&lt;br&gt;
│  │  (Boss)  │  │(Employee)│  │(Employee)│                   │&lt;br&gt;
│  └────┬─────┘  └────┬─────┘  └────┬─────┘                   │&lt;br&gt;
│       │             │             │                         │&lt;br&gt;
│       └─────────────┴─────────────┘                         │&lt;br&gt;
│              (Hierarchical delegation)                      │&lt;br&gt;
└─────────────────────────────────────────────────────────────┘&lt;/p&gt;


&lt;h2&gt;
  
  
  Decision Matrix
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;If You Need...&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;th&gt;Avoid&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Complex state machines, cycles&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;LangGraph&lt;/td&gt;
&lt;td&gt;LangChain (too linear)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Role-based teams, collaboration&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;CrewAI&lt;/td&gt;
&lt;td&gt;AutoGen (less structured)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Code execution, math, data analysis&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;AutoGen&lt;/td&gt;
&lt;td&gt;Pure LLM chains&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Simple 2-3 step workflows&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;OpenAI Swarm&lt;/td&gt;
&lt;td&gt;Over-engineering with LangGraph&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Enterprise Kubernetes deployment&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Dapr Agents&lt;/td&gt;
&lt;td&gt;Self-managed solutions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;MCP ecosystem integration&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;LangGraph + MCP&lt;/td&gt;
&lt;td&gt;Closed frameworks&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;7.7 &lt;strong&gt;Prompt Optimization and DSPy&lt;/strong&gt;&lt;br&gt;
DSPy (Declarative Self-improving Language Programs):&lt;br&gt;
What is DSPy?&lt;br&gt;
Framework for programming with LLMs using optimizable prompts and modules.&lt;br&gt;
Key Ideas:&lt;/p&gt;

&lt;p&gt;1.Signatures:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Define input-output behavior&lt;/li&gt;
&lt;li&gt;Abstract away prompt details&lt;/li&gt;
&lt;li&gt;Example: "question -&amp;gt; answer"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Modules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Composable LLM calls&lt;/li&gt;
&lt;li&gt;Chain of Thought&lt;/li&gt;
&lt;li&gt;ReAct&lt;/li&gt;
&lt;li&gt;Multi-hop reasoning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Optimizers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automatically improve prompts&lt;/li&gt;
&lt;li&gt;Learn from examples&lt;/li&gt;
&lt;li&gt;Bootstrap few-shot examples&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why DSPy?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Systematic prompt engineering&lt;/li&gt;
&lt;li&gt;Reproducible results&lt;/li&gt;
&lt;li&gt;Transferable across models&lt;/li&gt;
&lt;li&gt;Automatic optimization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
Instead of manually writing prompts, define what you want:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class QA(dspy.Signature):
    question = dspy.InputField()
    answer = dspy.OutputField()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;DSPy optimizes the actual prompt automatically.&lt;br&gt;
&lt;strong&gt;Optimizers&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.BootstrapFewShot:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generate examples from training data&lt;/li&gt;
&lt;li&gt;Select best demonstrations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.MIPRO:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multi-prompt optimization&lt;/li&gt;
&lt;li&gt;Instruction and demonstration tuning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Ensemble:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Combine multiple strategies&lt;/li&gt;
&lt;li&gt;Vote on outputs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use Cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Complex reasoning chains&lt;/li&gt;
&lt;li&gt;Multi-step workflows&lt;/li&gt;
&lt;li&gt;When few-shot examples matter&lt;/li&gt;
&lt;li&gt;Cross-model portability&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  7.8 Model Context Protocol (MCP) and Agent Standards
&lt;/h2&gt;
&lt;h3&gt;
  
  
  7.8.1 Introduction to MCP (Model Context Protocol)
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What is MCP?&lt;/strong&gt;&lt;br&gt;
The Model Context Protocol (MCP), introduced by Anthropic in late 2024, has rapidly become the "USB-C port for AI applications" — a universal standard for connecting AI systems to external tools, data sources, and services. By 2026, MCP has emerged as the foundational protocol for agent interoperability, similar to how HTTP enabled the web or REST APIs enabled microservices.&lt;br&gt;
&lt;strong&gt;Why MCP Matters in 2026:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Universal Integration: Connect any LLM to any tool without custom adapters&lt;/li&gt;
&lt;li&gt;Bidirectional Communication: Unlike simple function calling, MCP supports persistent, stateful connections&lt;/li&gt;
&lt;li&gt;Security-First Design: Built-in authentication, access controls, and audit logging&lt;/li&gt;
&lt;li&gt;Ecosystem Explosion: Thousands of pre-built MCP servers for databases, APIs, SaaS tools, and enterprise systems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MCP vs Traditional Function Calling:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;Traditional Function Calling&lt;/th&gt;
&lt;th&gt;MCP&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Connection&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Stateless, per-request&lt;/td&gt;
&lt;td&gt;Stateful, persistent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Discovery&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Hardcoded in prompt&lt;/td&gt;
&lt;td&gt;Dynamic server discovery&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Context&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Limited to single turn&lt;/td&gt;
&lt;td&gt;Full conversation history&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Security&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Per-function implementation&lt;/td&gt;
&lt;td&gt;Standardized auth layer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Tool Updates&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Requires prompt changes&lt;/td&gt;
&lt;td&gt;Server-side updates, client auto-sync&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Ecosystem&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Fragmented, custom integrations&lt;/td&gt;
&lt;td&gt;Standardized, composable marketplace&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  7.8.2 MCP Architecture Components
&lt;/h3&gt;

&lt;p&gt;Core Architecture:&lt;/p&gt;

&lt;p&gt;┌───────────────────────────────────────────┐&lt;br&gt;
│           MCP Host (AI Application)       │&lt;br&gt;
│  ┌─────────────────────────────────────┐  │&lt;br&gt;
│  │      MCP Client Layer               │  │&lt;br&gt;
│  │  ┌─────────┐ ┌─────────┐ ┌────────┐ │  │&lt;br&gt;
│  │  │Client A │ │Client B │ │Client C│ │  │&lt;br&gt;
│  │  │(Slack)  │ │(GitHub) │ │(Postgres)│  │&lt;br&gt;
│  │  └────┬────┘ └────┬────┘ └────┬───┘ │  │&lt;br&gt;
│  └───────┼───────────┼───────────┼─────┘  │&lt;br&gt;
│          │           │           │        │&lt;br&gt;
│  ┌───────┴───────────┴───────────┴─────┐  │&lt;br&gt;
│  │      Transport Layer (StdIO/SSE)     │ │&lt;br&gt;
│  └──────────────────────────────────────┘ │&lt;br&gt;
└───────────────────────────────────────────┘&lt;br&gt;
                    │&lt;br&gt;
┌────────────────────────────────────────────┐&lt;br&gt;
│           MCP Servers (Tools/Data)         │&lt;br&gt;
│  ┌─────────┐ ┌─────────┐ ┌─────────┐       │&lt;br&gt;
│  │  Slack  │ │  GitHub │ │ Postgres│       │&lt;br&gt;
│  │  Server │ │  Server │ │  Server │       │&lt;br&gt;
│  │(Node.js)│ │ (Python)│ │  (Rust) │       │&lt;br&gt;
│  └─────────┘ └─────────┘ └─────────┘       │&lt;br&gt;
└────────────────────────────────────────────┘&lt;br&gt;
Key Components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;MCP Host: The AI application (Claude Desktop, Cursor, custom agents) that initiates connections&lt;/li&gt;
&lt;li&gt;MCP Clients: Protocol clients within the host that manage individual server connections&lt;/li&gt;
&lt;li&gt;MCP Servers: Lightweight programs exposing specific capabilities (tools, resources, prompts)&lt;/li&gt;
&lt;li&gt;Transport Layer: Communication channel (stdio for local, Server-Sent Events for remote)&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  7.8.3 Building an MCP Server
&lt;/h3&gt;

&lt;p&gt;Server Implementation (Python):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from mcp.server import Server
from mcp.types import TextContent, Tool
import httpx

# Initialize MCP server
server = Server("weather-server")

@server.list_tools()
async def list_tools() -&amp;gt; list[Tool]:
    """Declare available tools"""
    return [
        Tool(
            name="get_weather",
            description="Get current weather for a location",
            inputSchema={
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "City name"},
                    "units": {"type": "string", "enum": ["celsius", "fahrenheit"]}
                },
                "required": ["city"]
            }
        )
    ]

@server.call_tool()
async def call_tool(name: str, arguments: dict) -&amp;gt; list[TextContent]:
    """Execute tool logic"""
    if name == "get_weather":
        city = arguments["city"]
        units = arguments.get("units", "celsius")

        # Actual API call
        async with httpx.AsyncClient() as client:
            response = await client.get(
                f"https://api.weather.com/v1/current?city={city}&amp;amp;units={units}"
            )
            data = response.json()

        return [TextContent(type="text", text=f"{data['temp']}°{units[0].upper()}")]

    raise ValueError(f"Unknown tool: {name}")

# Run server
if __name__ == "__main__":
    server.run(transport="stdio")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Server Capabilities Pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Advanced server with resources and prompts
@server.list_resources()
async def list_resources():
    """Expose data resources"""
    return [
        Resource(
            uri="file:///logs/app.log",
            name="Application Logs",
            mimeType="text/plain"
        )
    ]

@server.list_prompts()
async def list_prompts():
    """Provide templated prompts"""
    return [
        Prompt(
            name="code-review",
            description="Review code for bugs",
            arguments=[
                PromptArgument(
                    name="code",
                    description="Code to review",
                    required=True
                )
            ]
        )
    ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7.8.4 MCP Client Integration
&lt;/h3&gt;

&lt;p&gt;Connecting to MCP Servers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

# Configure server connection
server_params = StdioServerParameters(
    command="python",
    args=["weather_server.py"],
    env=None
)

async def use_mcp_server():
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            # Initialize connection
            await session.initialize()

            # Discover available tools
            tools = await session.list_tools()
            print(f"Available tools: {[tool.name for tool in tools.tools]}")

            # Call tool with automatic schema validation
            result = await session.call_tool(
                "get_weather",
                arguments={"city": "San Francisco", "units": "celsius"}
            )

            return result.content[0].text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multi-Server Orchestration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from mcp.client import MultiServerMCPClient

async def multi_server_agent():
    """Agent using multiple MCP servers simultaneously"""

    servers = {
        "slack": {
            "command": "python",
            "args": ["slack_mcp_server.py"],
            "env": {"SLACK_TOKEN": os.environ["SLACK_TOKEN"]}
        },
        "github": {
            "command": "npx",
            "args": ["-y", "@modelcontextprotocol/server-github"],
            "env": {"GITHUB_TOKEN": os.environ["GITHUB_TOKEN"]}
        },
        "postgres": {
            "command": "python",
            "args": ["postgres_mcp_server.py"],
            "env": {"DATABASE_URL": os.environ["DATABASE_URL"]}
        }
    }

    async with MultiServerMCPClient(servers) as client:
        # All tools from all servers available
        all_tools = client.get_tools()

        # LangChain/LangGraph integration
        from langchain_mcp import MCPToolkit
        toolkit = MCPToolkit(client)

        # Create agent with unified tool access
        agent = create_react_agent(llm, toolkit.get_tools())

        # Agent can now seamlessly use Slack, GitHub, and Postgres
        result = await agent.ainvoke({
            "input": "Get the last 5 GitHub issues, post summary to Slack #dev, and store in Postgres"
        })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7.8.5 A2A (Agent-to-Agent) Protocol
&lt;/h3&gt;

&lt;p&gt;What is A2A?&lt;br&gt;
Announced by Google in April 2025, the Agent-to-Agent (A2A) protocol complements MCP by enabling direct communication between autonomous agents. While MCP connects agents to tools, A2A connects agents to each other.&lt;br&gt;
A2A Core Concepts:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Agent Card&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Public metadata describing agent capabilities (skills, endpoints, auth requirements)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Task&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Unit of work with lifecycle: submitted → working → input-required → completed/failed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Message&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Communication container with parts (text, files, structured data)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Part&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Typed content: TextPart, FilePart, DataPart&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A2A Task Lifecycle:&lt;br&gt;
┌──────────┐    ┌──────────┐    ┌──────────────┐    ┌──────────┐&lt;br&gt;
│ Submitted│───→│  Working │───→│Input-Required│───→│ Completed│&lt;br&gt;
│          │    │          │    │  (optional)  │    │          │&lt;br&gt;
└──────────┘    └──────────┘    └──────────────┘    └──────────┘&lt;br&gt;
                              ↓&lt;br&gt;
                         ┌──────────┐&lt;br&gt;
                         │  Failed  │&lt;br&gt;
                         └──────────┘&lt;br&gt;
A2A Implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from a2a import AgentCard, Task, Message, TextPart
from a2a.server import A2AServer

# Define agent capabilities
agent_card = AgentCard(
    name="CodeReviewAgent",
    description="Reviews code for security and style",
    url="https://code-review-agent.example.com/a2a",
    capabilities={
        "streaming": True,
        "pushNotifications": False
    },
    skills=[
        {
            "id": "security-review",
            "name": "Security Review",
            "description": "Scan code for vulnerabilities",
            "tags": ["security", "code-review"]
        }
    ]
)

class CodeReviewA2AServer(A2AServer):
    async def handle_task(self, task: Task) -&amp;gt; Task:
        """Process incoming task from another agent"""

        # Extract code from message parts
        code = None
        for part in task.message.parts:
            if isinstance(part, TextPart):
                code = part.text
            elif isinstance(part, FilePart):
                code = await self.download_file(part.file_url)

        # Perform review
        review_result = await self.review_code(code)

        # Update task status
        task.status = TaskStatus(state=TaskState.COMPLETED)
        task.artifacts = [
            Message(
                role="agent",
                parts=[TextPart(text=review_result)]
            )
        ]

        return task

# Start server
server = CodeReviewA2AServer(agent_card=agent_card)
server.run(port=5000)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A2A Client Calling Another Agent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from a2a.client import A2AClient

async def delegate_to_specialist():
    """Primary agent delegating to specialist agent via A2A"""

    # Discover specialist agent
    client = A2AClient(
        agent_card_url="https://code-review-agent.example.com/agent.json"
    )

    # Create task for specialist
    task = Task(
        message=Message(
            role="user",
            parts=[
                TextPart(text="Review this Python authentication code"),
                FilePart(
                    name="auth.py",
                    mimeType="text/x-python",
                    bytes=code_bytes
                )
            ]
        )
    )

    # Submit task and await completion
    completed_task = await client.submit_task(task)

    # Process result
    review_feedback = completed_task.artifacts[0].parts[0].text
    return review_feedback
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7.8.6 MCP + A2A Combined Architecture&lt;br&gt;
Enterprise Agent Mesh Pattern:&lt;/p&gt;

&lt;p&gt;┌─────────────────────────────────────────────────────────┐&lt;br&gt;
│                    Enterprise Agent Mesh                │&lt;br&gt;
│  ┌─────────────┐    ┌─────────────┐    ┌────────────┐   │&lt;br&gt;
│  │  Customer   │◄──►│ Orchestrator│◄──►│  Billing   │   │&lt;br&gt;
│  │    Agent    │A2A │    Agent    │A2A │   Agent    │   │&lt;br&gt;
│  └──────┬──────┘    └──────┬──────┘    └──────┬─────┘   │&lt;br&gt;
│         │                  │                    │       │&lt;br&gt;
│         └──────────────────┼────────────────────┘       │&lt;br&gt;
│                            │                            │&lt;br&gt;
│                    ┌───────┴───────┐                    │&lt;br&gt;
│                    │   MCP Layer    │                   │&lt;br&gt;
│                    │  (Tool Access)  │                  │&lt;br&gt;
│                    └───────┬───────┘                    │&lt;br&gt;
│         ┌──────────────────┼──────────────────┐         │&lt;br&gt;
│    ┌────┴────┐        ┌────┴────┐        ┌────┴────┐    │&lt;br&gt;
│    │  CRM    │        │Payment  │        │Database │    │&lt;br&gt;
│    │ Server  │        │ Server  │        │ Server  │    │&lt;br&gt;
│    └─────────┘        └─────────┘        └─────────┘    │&lt;br&gt;
└─────────────────────────────────────────────────────────┘&lt;br&gt;
Implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EnterpriseOrchestrator:
    """Agent combining MCP tools and A2A agent delegation"""

    def __init__(self):
        self.mcp_client = MultiServerMCPClient({
            "crm": "crm_mcp_server.py",
            "payment": "payment_mcp_server.py"
        })
        self.a2a_clients = {
            "billing": A2AClient("https://billing-agent.example.com"),
            "support": A2AClient("https://support-agent.example.com")
        }

    async def process_customer_request(self, request: str):
        """Route request to appropriate tools or agents"""

        # Intent classification
        intent = await self.classify_intent(request)

        if intent == "refund":
            # Use MCP for immediate data access
            customer_data = await self.mcp_client.call_tool(
                "crm.get_customer", {"query": request}
            )

            # Delegate to billing specialist via A2A
            task = Task(message=Message(
                role="user",
                parts=[TextPart(text=f"Process refund for: {customer_data}")]
            ))
            result = await self.a2a_clients["billing"].submit_task(task)

            return result.artifacts[0].parts[0].text

        elif intent == "technical_support":
            # Delegate to support agent
            return await self.a2a_clients["support"].submit_task(...)

        else:
            # Handle directly with MCP tools
            return await self.handle_with_tools(request)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7.8.7 Security and Governance&lt;br&gt;
Authentication Patterns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# OAuth 2.0 for MCP servers
from mcp.auth import OAuth2Handler

auth_handler = OAuth2Handler(
    client_id=os.environ["MCP_CLIENT_ID"],
    client_secret=os.environ["MCP_CLIENT_SECRET"],
    token_url="https://auth.example.com/token"
)

# Server-side access control
@server.call_tool()
async def secure_tool_call(name: str, arguments: dict, context: Context):
    # Verify user permissions from JWT
    user_role = context.auth.claims.get("role")

    if name == "admin_delete_user" and user_role != "admin":
        raise PermissionError("Admin access required")

    # Audit logging
    await audit_log.record(
        user=context.auth.user_id,
        tool=name,
        arguments=arguments,
        timestamp=datetime.now()
    )

    return await execute_tool(name, arguments)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Best Practices:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Principle of Least Privilege: Each MCP server only exposes necessary tools&lt;/li&gt;
&lt;li&gt;Input Validation: Strict schema validation on all inputs&lt;/li&gt;
&lt;li&gt;Rate Limiting: Prevent abuse through per-user and per-tool quotas&lt;/li&gt;
&lt;li&gt;Audit Logging: Complete traceability of all agent actions&lt;/li&gt;
&lt;li&gt;Secret Management: Never hardcode credentials in server code&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  8. Computer Vision
&lt;/h2&gt;

&lt;p&gt;Computer vision has been transformed by deep learning, particularly CNNs and now vision transformers.&lt;/p&gt;

&lt;p&gt;8.1 &lt;strong&gt;Convolutional Neural Networks (CNNs)&lt;/strong&gt;&lt;br&gt;
Why CNNs for Images?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Local connectivity (nearby pixels related)&lt;/li&gt;
&lt;li&gt;Parameter sharing (same features everywhere)&lt;/li&gt;
&lt;li&gt;Translation invariance&lt;/li&gt;
&lt;li&gt;Hierarchical feature learning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Core Operations:&lt;br&gt;
&lt;strong&gt;Convolution&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slide filter over image&lt;/li&gt;
&lt;li&gt;Element-wise multiplication and sum&lt;/li&gt;
&lt;li&gt;Create feature map&lt;/li&gt;
&lt;li&gt;Detect patterns (edges, textures, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key Concepts:&lt;/p&gt;

&lt;p&gt;1.Filters/Kernels:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Small matrices (3x3, 5x5, 7x7)&lt;/li&gt;
&lt;li&gt;Learned during training&lt;/li&gt;
&lt;li&gt;Detect specific features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Stride:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step size when sliding filter&lt;/li&gt;
&lt;li&gt;Stride 1: Every position&lt;/li&gt;
&lt;li&gt;Stride 2: Skip positions, reduce size&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Padding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add zeros around image&lt;/li&gt;
&lt;li&gt;Preserve spatial dimensions&lt;/li&gt;
&lt;li&gt;"Same" padding: Output size = input size&lt;/li&gt;
&lt;li&gt;"Valid" padding: No padding&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Pooling:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Downsample feature maps&lt;/li&gt;
&lt;li&gt;Max pooling: Take maximum&lt;/li&gt;
&lt;li&gt;Average pooling: Take average&lt;/li&gt;
&lt;li&gt;Reduces computation&lt;/li&gt;
&lt;li&gt;Provides translation invariance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;CNN Architecture Evolution&lt;/strong&gt;:&lt;br&gt;
&lt;strong&gt;LeNet-5 (1998)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First successful CNN&lt;/li&gt;
&lt;li&gt;Handwritten digit recognition&lt;/li&gt;
&lt;li&gt;Conv → Pool → Conv → Pool → FC&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;AlexNet (2012)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ImageNet breakthrough&lt;/li&gt;
&lt;li&gt;8 layers&lt;/li&gt;
&lt;li&gt;ReLU activation&lt;/li&gt;
&lt;li&gt;Dropout regularization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;VGG (2014)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Very deep (16-19 layers)&lt;/li&gt;
&lt;li&gt;Small 3x3 filters&lt;/li&gt;
&lt;li&gt;Simple architecture&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;ResNet (2015)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Skip connections / Residual connections&lt;/li&gt;
&lt;li&gt;Enables training very deep networks (100+ layers)&lt;/li&gt;
&lt;li&gt;Solves vanishing gradient problem&lt;/li&gt;
&lt;li&gt;Formula: F(x) + x&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Inception/GoogLeNet (2014)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multi-scale features&lt;/li&gt;
&lt;li&gt;Inception modules&lt;/li&gt;
&lt;li&gt;1x1 convolutions for dimensionality reduction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;EfficientNet (2019)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compound scaling (depth, width, resolution)&lt;/li&gt;
&lt;li&gt;Best accuracy-efficiency tradeoff&lt;/li&gt;
&lt;li&gt;Multiple variants (B0-B7)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modern Architectures (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.ConvNeXt:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modernized CNN&lt;/li&gt;
&lt;li&gt;Competitive with transformers&lt;/li&gt;
&lt;li&gt;Better than many ViTs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.NFNet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normalization-free&lt;/li&gt;
&lt;li&gt;Faster training&lt;/li&gt;
&lt;li&gt;Good performance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Transfer Learning in Vision&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pre-train on ImageNet (or larger datasets)&lt;/li&gt;
&lt;li&gt;Fine-tune on specific task&lt;/li&gt;
&lt;li&gt;Much less data needed&lt;/li&gt;
&lt;li&gt;Faster convergence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common Techniques:&lt;/p&gt;

&lt;p&gt;1.Data Augmentation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Random crops&lt;/li&gt;
&lt;li&gt;Horizontal flips&lt;/li&gt;
&lt;li&gt;Rotations&lt;/li&gt;
&lt;li&gt;Color jittering&lt;/li&gt;
&lt;li&gt;Cutout/CutMix&lt;/li&gt;
&lt;li&gt;Increases training data diversity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Normalization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Batch normalization standard&lt;/li&gt;
&lt;li&gt;Group normalization for small batches&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Progressive Resizing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with small images&lt;/li&gt;
&lt;li&gt;Gradually increase size&lt;/li&gt;
&lt;li&gt;Faster training&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;8.2 &lt;strong&gt;Object Detection&lt;/strong&gt;&lt;br&gt;
Task:&lt;br&gt;
Find and classify all objects in an image.&lt;br&gt;
Output:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bounding boxes (x, y, width, height)&lt;/li&gt;
&lt;li&gt;Class labels&lt;/li&gt;
&lt;li&gt;Confidence scores&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two-Stage Detectors:&lt;br&gt;
&lt;strong&gt;R-CNN Family&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.R-CNN:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Region proposals&lt;/li&gt;
&lt;li&gt;CNN features per proposal&lt;/li&gt;
&lt;li&gt;SVM classification&lt;/li&gt;
&lt;li&gt;Slow&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Fast R-CNN:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shared CNN features&lt;/li&gt;
&lt;li&gt;ROI pooling&lt;/li&gt;
&lt;li&gt;Faster&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Faster R-CNN:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Region Proposal Network (RPN)&lt;/li&gt;
&lt;li&gt;End-to-end training&lt;/li&gt;
&lt;li&gt;State-of-art accuracy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Single-Stage Detectors&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.YOLO (You Only Look Once):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single network pass&lt;/li&gt;
&lt;li&gt;Very fast&lt;/li&gt;
&lt;li&gt;Good for real-time&lt;/li&gt;
&lt;li&gt;Latest: YOLOv8, YOLOv9 (2026)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.SSD (Single Shot Detector):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multi-scale feature maps&lt;/li&gt;
&lt;li&gt;Good speed-accuracy balance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.RetinaNet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Focal loss for class imbalance&lt;/li&gt;
&lt;li&gt;Feature Pyramid Network&lt;/li&gt;
&lt;li&gt;High accuracy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modern Detectors (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.DETR (Detection Transformer):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transformer-based&lt;/li&gt;
&lt;li&gt;No anchors needed&lt;/li&gt;
&lt;li&gt;Set prediction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.YOLOX:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Anchor-free&lt;/li&gt;
&lt;li&gt;Strong performance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.RT-DETR:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time transformer detector&lt;/li&gt;
&lt;li&gt;Best of both worlds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Evaluation Metrics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;mAP (mean Average Precision)&lt;/li&gt;
&lt;li&gt;IoU (Intersection over Union)&lt;/li&gt;
&lt;li&gt;FPS (Frames Per Second)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;8.3** Semantic Segmentation**&lt;br&gt;
Task:&lt;br&gt;
Classify every pixel in image.&lt;br&gt;
Architectures:&lt;br&gt;
&lt;strong&gt;FCN (Fully Convolutional Network):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No fully connected layers&lt;/li&gt;
&lt;li&gt;Produces spatial output&lt;/li&gt;
&lt;li&gt;Foundation for segmentation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;U-Net:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encoder-decoder architecture&lt;/li&gt;
&lt;li&gt;Skip connections&lt;/li&gt;
&lt;li&gt;Popular for medical imaging&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;DeepLab:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Atrous convolution&lt;/li&gt;
&lt;li&gt;Spatial Pyramid Pooling&lt;/li&gt;
&lt;li&gt;Good boundary refinement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Mask R-CNN:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extends Faster R-CNN&lt;/li&gt;
&lt;li&gt;Instance segmentation&lt;/li&gt;
&lt;li&gt;Segment each object instance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Modern Approaches (2026):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Segment Anything Model (SAM)&lt;/li&gt;
&lt;li&gt;SegFormer (transformer-based)&lt;/li&gt;
&lt;li&gt;Mask2Former&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;8.4 &lt;strong&gt;Vision Transformers (ViT)&lt;/strong&gt;&lt;br&gt;
Concept:&lt;br&gt;
Apply transformer architecture to images.&lt;br&gt;
How it Works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Split image into patches (16x16 pixels)&lt;/li&gt;
&lt;li&gt;Flatten patches to sequences&lt;/li&gt;
&lt;li&gt;Add positional embeddings&lt;/li&gt;
&lt;li&gt;Feed to transformer encoder&lt;/li&gt;
&lt;li&gt;Classification head on [CLS] token&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Captures long-range dependencies&lt;/li&gt;
&lt;li&gt;Scales well with data&lt;/li&gt;
&lt;li&gt;Pre-training on large datasets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requires more data than CNNs&lt;/li&gt;
&lt;li&gt;Less inductive bias&lt;/li&gt;
&lt;li&gt;Higher compute requirements&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Variants:&lt;/p&gt;

&lt;p&gt;1.DeiT (Data-efficient ViT):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Knowledge distillation&lt;/li&gt;
&lt;li&gt;Less data needed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Swin Transformer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hierarchical structure&lt;/li&gt;
&lt;li&gt;Shifted windows&lt;/li&gt;
&lt;li&gt;Better for dense prediction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.BEiT:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Self-supervised pre-training&lt;/li&gt;
&lt;li&gt;Masked image modeling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.ViT-Adapter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Efficient adaptation&lt;/li&gt;
&lt;li&gt;Better fine-tuning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Modern Vision Models (2026):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;EVA (billion-scale ViT)&lt;/li&gt;
&lt;li&gt;DINOv2 (self-supervised)&lt;/li&gt;
&lt;li&gt;InternViT (strong performance)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;8.5 Multi-Modal Models&lt;br&gt;
Concept:&lt;br&gt;
Models that understand multiple modalities (vision + language).&lt;br&gt;
CLIP (Contrastive Language-Image Pre-training):&lt;br&gt;
How it Works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Train vision and text encoders jointly&lt;/li&gt;
&lt;li&gt;Maximize similarity of matched pairs&lt;/li&gt;
&lt;li&gt;Minimize similarity of unmatched pairs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Capabilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Zero-shot image classification&lt;/li&gt;
&lt;li&gt;Text-to-image retrieval&lt;/li&gt;
&lt;li&gt;Image-to-text retrieval&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Image search&lt;/li&gt;
&lt;li&gt;Zero-shot classification&lt;/li&gt;
&lt;li&gt;Image generation guidance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Modern Multi-Modal Models (2026):&lt;/p&gt;

&lt;p&gt;1.GPT-4V:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vision + language understanding&lt;/li&gt;
&lt;li&gt;Image analysis and reasoning&lt;/li&gt;
&lt;li&gt;Chart and diagram understanding&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Gemini:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Native multi-modal&lt;/li&gt;
&lt;li&gt;Video understanding&lt;/li&gt;
&lt;li&gt;Interleaved image-text&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.LLaVA:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open-source vision-language&lt;/li&gt;
&lt;li&gt;Instruction tuning&lt;/li&gt;
&lt;li&gt;Strong performance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Claude 3 Vision:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Document understanding&lt;/li&gt;
&lt;li&gt;Image analysis&lt;/li&gt;
&lt;li&gt;Multi-image reasoning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Image Generation&lt;/strong&gt;:&lt;br&gt;
Diffusion Models:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stable Diffusion&lt;/li&gt;
&lt;li&gt;DALL-E 3&lt;/li&gt;
&lt;li&gt;Midjourney&lt;/li&gt;
&lt;li&gt;Imagen&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How Diffusion Works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add noise to images (forward process)&lt;/li&gt;
&lt;li&gt;Learn to denoise (reverse process)&lt;/li&gt;
&lt;li&gt;Generate by starting from noise&lt;/li&gt;
&lt;li&gt;Guided by text prompts&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Text-to-image generation&lt;/li&gt;
&lt;li&gt;Image editing&lt;/li&gt;
&lt;li&gt;Inpainting&lt;/li&gt;
&lt;li&gt;Style transfer&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Vision-Language Models Update
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Latest Models (2026)
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Provider&lt;/th&gt;
&lt;th&gt;Capabilities&lt;/th&gt;
&lt;th&gt;Parameters&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;GPT-4o Vision&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;OpenAI&lt;/td&gt;
&lt;td&gt;General vision, OCR, charts&lt;/td&gt;
&lt;td&gt;Unknown&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Claude 3.5 Sonnet Vision&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Anthropic&lt;/td&gt;
&lt;td&gt;Document analysis, diagrams&lt;/td&gt;
&lt;td&gt;Unknown&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Gemini 1.5 Pro&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Google&lt;/td&gt;
&lt;td&gt;Video understanding, 1M context&lt;/td&gt;
&lt;td&gt;Unknown&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Qwen2-VL&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Alibaba&lt;/td&gt;
&lt;td&gt;Multilingual, document, video&lt;/td&gt;
&lt;td&gt;2B–72B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Pixtral&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Mistral&lt;/td&gt;
&lt;td&gt;High-res images, 128k context&lt;/td&gt;
&lt;td&gt;12B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Molmo&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;AllenAI&lt;/td&gt;
&lt;td&gt;Open weights, competitive&lt;/td&gt;
&lt;td&gt;7B–72B&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;
  
  
  9. Advanced AI/ML Concepts (2026)
&lt;/h2&gt;

&lt;p&gt;These are cutting-edge techniques that define modern AI/ML engineering.&lt;br&gt;
9.1 &lt;strong&gt;Mixture of Experts (MoE)&lt;/strong&gt;&lt;br&gt;
Concept:&lt;br&gt;
Use multiple specialized sub-networks (experts) and route inputs dynamically.&lt;br&gt;
Architecture:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple expert networks&lt;/li&gt;
&lt;li&gt;Gating network decides which experts to use&lt;/li&gt;
&lt;li&gt;Typically 2-8 experts activated per input&lt;/li&gt;
&lt;li&gt;Rest remain dormant&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Massive parameter count&lt;/li&gt;
&lt;li&gt;Constant compute cost&lt;/li&gt;
&lt;li&gt;Specialization&lt;/li&gt;
&lt;li&gt;Better scaling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Modern MoE Models:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mixtral 8x7B: 8 experts, 7B each&lt;/li&gt;
&lt;li&gt;GPT-4 (rumored to use MoE)&lt;/li&gt;
&lt;li&gt;Switch Transformers&lt;/li&gt;
&lt;li&gt;GLaM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Challenges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load balancing across experts&lt;/li&gt;
&lt;li&gt;Training instability&lt;/li&gt;
&lt;li&gt;Inference optimization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;9.2 &lt;strong&gt;Constitutional AI and RLHF&lt;/strong&gt;&lt;br&gt;
RLHF (Reinforcement Learning from Human Feedback):&lt;br&gt;
Process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pre-train language model&lt;/li&gt;
&lt;li&gt;Collect human preferences&lt;/li&gt;
&lt;li&gt;Train reward model on preferences&lt;/li&gt;
&lt;li&gt;Fine-tune with RL (PPO typically)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Why it Works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Aligns model with human values&lt;/li&gt;
&lt;li&gt;Reduces harmful outputs&lt;/li&gt;
&lt;li&gt;Improves helpfulness&lt;/li&gt;
&lt;li&gt;Better instruction following&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Constitutional AI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Self-critique and revision&lt;/li&gt;
&lt;li&gt;Principles-based training&lt;/li&gt;
&lt;li&gt;Reduces need for human feedback&lt;/li&gt;
&lt;li&gt;More scalable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DPO (Direct Preference Optimization):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simpler than RLHF&lt;/li&gt;
&lt;li&gt;Direct optimization&lt;/li&gt;
&lt;li&gt;No separate reward model&lt;/li&gt;
&lt;li&gt;Often comparable results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;9.3 Quantization and Model Compression&lt;br&gt;
Why Compress?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce memory requirements&lt;/li&gt;
&lt;li&gt;Faster inference&lt;/li&gt;
&lt;li&gt;Deploy on edge devices&lt;/li&gt;
&lt;li&gt;Lower costs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Quantization:&lt;br&gt;
Concept:&lt;br&gt;
Reduce precision of weights and activations.&lt;br&gt;
Types:&lt;/p&gt;

&lt;p&gt;1.Post-Training Quantization (PTQ):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quantize after training&lt;/li&gt;
&lt;li&gt;No retraining needed&lt;/li&gt;
&lt;li&gt;Some accuracy loss&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Quantization-Aware Training (QAT):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quantization during training&lt;/li&gt;
&lt;li&gt;Better accuracy&lt;/li&gt;
&lt;li&gt;More complex&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Precision Levels:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;FP32: Full precision (4 bytes)&lt;/li&gt;
&lt;li&gt;FP16: Half precision (2 bytes)&lt;/li&gt;
&lt;li&gt;INT8: 8-bit integers (1 byte)&lt;/li&gt;
&lt;li&gt;INT4: 4-bit (0.5 bytes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;GPTQ:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Post-training quantization for LLMs&lt;/li&gt;
&lt;li&gt;Layer-wise quantization&lt;/li&gt;
&lt;li&gt;Minimal accuracy loss&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;GGUF/GGML:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Quantization format&lt;/li&gt;
&lt;li&gt;Used by llama.cpp&lt;/li&gt;
&lt;li&gt;2-bit to 8-bit options&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AWQ (Activation-aware Weight Quantization):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Protects important weights&lt;/li&gt;
&lt;li&gt;Better than naive quantization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other Compression Techniques:&lt;/p&gt;

&lt;p&gt;1.Pruning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove unimportant connections&lt;/li&gt;
&lt;li&gt;Structured or unstructured&lt;/li&gt;
&lt;li&gt;Can achieve high sparsity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Knowledge Distillation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Train small model from large&lt;/li&gt;
&lt;li&gt;Student learns from teacher&lt;/li&gt;
&lt;li&gt;DistilBERT, TinyBERT&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Low-Rank Factorization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decompose weight matrices&lt;/li&gt;
&lt;li&gt;Fewer parameters&lt;/li&gt;
&lt;li&gt;Some accuracy loss&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;9.4 &lt;strong&gt;Flash Attention and Training Optimizations&lt;/strong&gt;&lt;br&gt;
Flash Attention:&lt;br&gt;
Problem:&lt;br&gt;
Standard attention is O(n²) in memory and slow.&lt;br&gt;
Solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tiled computation&lt;/li&gt;
&lt;li&gt;Kernel fusion&lt;/li&gt;
&lt;li&gt;IO-aware algorithm&lt;/li&gt;
&lt;li&gt;2-4x faster training&lt;/li&gt;
&lt;li&gt;Lower memory usage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;FlashAttention-2:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Further optimizations&lt;/li&gt;
&lt;li&gt;Better GPU utilization&lt;/li&gt;
&lt;li&gt;Supports longer sequences&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Other Training Optimizations:&lt;/p&gt;

&lt;p&gt;1.Gradient Checkpointing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Trade compute for memory&lt;/li&gt;
&lt;li&gt;Recompute activations in backward pass&lt;/li&gt;
&lt;li&gt;Enables larger batch sizes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Mixed Precision Training:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;FP16 for most operations&lt;/li&gt;
&lt;li&gt;FP32 for critical parts&lt;/li&gt;
&lt;li&gt;2-3x speedup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Distributed Training:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data parallelism&lt;/li&gt;
&lt;li&gt;Model parallelism&lt;/li&gt;
&lt;li&gt;Pipeline parallelism&lt;/li&gt;
&lt;li&gt;ZeRO (Zero Redundancy Optimizer)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Gradient Accumulation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simulate larger batch sizes&lt;/li&gt;
&lt;li&gt;Multiple forward passes before backward&lt;/li&gt;
&lt;li&gt;Works around memory limits&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;9.5 &lt;strong&gt;Efficient Inference&lt;/strong&gt;&lt;br&gt;
Speculative Decoding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Draft model generates quickly&lt;/li&gt;
&lt;li&gt;Main model verifies&lt;/li&gt;
&lt;li&gt;Accept if correct, else regenerate&lt;/li&gt;
&lt;li&gt;2-3x speedup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;KV Cache Optimization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cache key-value pairs&lt;/li&gt;
&lt;li&gt;Reduces computation in generation&lt;/li&gt;
&lt;li&gt;Manages memory carefully&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Continuous Batching:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dynamic batching of requests&lt;/li&gt;
&lt;li&gt;Better GPU utilization&lt;/li&gt;
&lt;li&gt;Lower latency&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Frameworks (2026):&lt;/p&gt;

&lt;p&gt;1.vLLM:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PagedAttention&lt;/li&gt;
&lt;li&gt;Continuous batching&lt;/li&gt;
&lt;li&gt;State-of-art serving&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.TensorRT-LLM:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NVIDIA optimization&lt;/li&gt;
&lt;li&gt;FP8 support&lt;/li&gt;
&lt;li&gt;Fast inference&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Text Generation Inference (TGI):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hugging Face serving&lt;/li&gt;
&lt;li&gt;Flash Attention&lt;/li&gt;
&lt;li&gt;Continuous batching&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.llama.cpp:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CPU inference&lt;/li&gt;
&lt;li&gt;Quantization support&lt;/li&gt;
&lt;li&gt;Cross-platform&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;9.6 &lt;strong&gt;Long Context and Memory&lt;/strong&gt;&lt;br&gt;
Challenge:&lt;br&gt;
Transformers are O(n²) in sequence length.&lt;br&gt;
Solutions:&lt;/p&gt;

&lt;p&gt;1.Sparse Attention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don't attend to all positions&lt;/li&gt;
&lt;li&gt;Patterns: local, strided, global&lt;/li&gt;
&lt;li&gt;Longformer, BigBird&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Linear Attention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduce to O(n)&lt;/li&gt;
&lt;li&gt;Performers, RWKV&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.State Space Models:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Mamba architecture&lt;/li&gt;
&lt;li&gt;Linear time inference&lt;/li&gt;
&lt;li&gt;Competitive performance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Recurrent Memory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;External memory modules&lt;/li&gt;
&lt;li&gt;Retrieve relevant context&lt;/li&gt;
&lt;li&gt;Unlimited context theoretically&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Long Context Models (2026)&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Claude 3: 200K tokens&lt;/li&gt;
&lt;li&gt;Gemini 1.5: 1M+ tokens&lt;/li&gt;
&lt;li&gt;GPT-4 Turbo: 128K tokens&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Context Window Management&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sliding window&lt;/li&gt;
&lt;li&gt;Hierarchical processing&lt;/li&gt;
&lt;li&gt;Compression techniques&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;9.7 &lt;strong&gt;Multi-Task and Meta-Learning&lt;/strong&gt;&lt;br&gt;
Multi-Task Learning:&lt;br&gt;
Train single model on multiple related tasks simultaneously.&lt;br&gt;
Benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shared representations&lt;/li&gt;
&lt;li&gt;Better generalization&lt;/li&gt;
&lt;li&gt;Efficient parameter use&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Approaches:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hard parameter sharing&lt;/li&gt;
&lt;li&gt;Soft parameter sharing&lt;/li&gt;
&lt;li&gt;Task-specific heads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Meta-Learning:&lt;br&gt;
Learn how to learn quickly from few examples.&lt;br&gt;
Approaches:&lt;/p&gt;

&lt;p&gt;1.MAML (Model-Agnostic Meta-Learning):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn initialization&lt;/li&gt;
&lt;li&gt;Fast adaptation with gradient descent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Prototypical Networks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn metric space&lt;/li&gt;
&lt;li&gt;Classify based on prototypes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Matching Networks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Attention-based similarity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Few-Shot Learning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn from few examples&lt;/li&gt;
&lt;li&gt;k-shot n-way classification&lt;/li&gt;
&lt;li&gt;Important for rare classes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;9.8 Reinforcement Learning Basics&lt;br&gt;
Core Concepts:&lt;br&gt;
Agent and Environment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Agent takes actions&lt;/li&gt;
&lt;li&gt;Environment provides states and rewards&lt;/li&gt;
&lt;li&gt;Goal: Maximize cumulative reward&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key Terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;State: Current situation&lt;/li&gt;
&lt;li&gt;Action: What agent can do&lt;/li&gt;
&lt;li&gt;Reward: Feedback signal&lt;/li&gt;
&lt;li&gt;Policy: Strategy for choosing actions&lt;/li&gt;
&lt;li&gt;Value Function: Expected future reward&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Algorithms:&lt;/p&gt;

&lt;p&gt;1.Q-Learning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn action-value function&lt;/li&gt;
&lt;li&gt;Off-policy&lt;/li&gt;
&lt;li&gt;Works for discrete actions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.DQN (Deep Q-Network):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Neural network for Q-function&lt;/li&gt;
&lt;li&gt;Experience replay&lt;/li&gt;
&lt;li&gt;Target network&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Policy Gradient:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Directly optimize policy&lt;/li&gt;
&lt;li&gt;REINFORCE algorithm&lt;/li&gt;
&lt;li&gt;Can handle continuous actions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Actor-Critic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Combines value and policy&lt;/li&gt;
&lt;li&gt;Actor: Policy network&lt;/li&gt;
&lt;li&gt;Critic: Value network&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.PPO (Proximal Policy Optimization):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stable policy updates&lt;/li&gt;
&lt;li&gt;Used in RLHF&lt;/li&gt;
&lt;li&gt;Popular choice&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Applications in LLMs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RLHF for alignment&lt;/li&gt;
&lt;li&gt;Code generation with execution feedback&lt;/li&gt;
&lt;li&gt;Game playing&lt;/li&gt;
&lt;li&gt;Robotics control&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  10. &lt;strong&gt;MLOps and Production Systems&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Building models is one thing. Deploying and maintaining them in production is another.&lt;/p&gt;

&lt;p&gt;10.1 &lt;strong&gt;ML Pipeline Design&lt;/strong&gt;&lt;br&gt;
Components:&lt;/p&gt;

&lt;p&gt;1.Data Ingestion:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Batch or streaming&lt;/li&gt;
&lt;li&gt;Data validation&lt;/li&gt;
&lt;li&gt;Schema enforcement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Data Preprocessing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cleaning&lt;/li&gt;
&lt;li&gt;Feature engineering&lt;/li&gt;
&lt;li&gt;Transformation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Training:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model selection&lt;/li&gt;
&lt;li&gt;Hyperparameter tuning&lt;/li&gt;
&lt;li&gt;Cross-validation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Evaluation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Metrics computation&lt;/li&gt;
&lt;li&gt;Model comparison&lt;/li&gt;
&lt;li&gt;Error analysis&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Deployment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model serving&lt;/li&gt;
&lt;li&gt;API creation&lt;/li&gt;
&lt;li&gt;Monitoring&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.Monitoring:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance tracking&lt;/li&gt;
&lt;li&gt;Data drift detection&lt;/li&gt;
&lt;li&gt;Retraining triggers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pipeline Orchestration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Airflow: Workflow management&lt;/li&gt;
&lt;li&gt;Kubeflow: Kubernetes-native&lt;/li&gt;
&lt;li&gt;Prefect: Modern orchestration&lt;/li&gt;
&lt;li&gt;Metaflow: Netflix's framework&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;10.2 Model Serving&lt;br&gt;
Deployment Patterns:&lt;/p&gt;

&lt;p&gt;1.Batch Prediction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Process data in batches&lt;/li&gt;
&lt;li&gt;Scheduled jobs&lt;/li&gt;
&lt;li&gt;Good for non-real-time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Online Prediction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time API&lt;/li&gt;
&lt;li&gt;Low latency required&lt;/li&gt;
&lt;li&gt;Synchronous requests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Streaming:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Process continuous stream&lt;/li&gt;
&lt;li&gt;Near real-time&lt;/li&gt;
&lt;li&gt;Event-driven&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Serving Frameworks&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;1.TensorFlow Serving:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Production-grade&lt;/li&gt;
&lt;li&gt;Model versioning&lt;/li&gt;
&lt;li&gt;Batching support&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.TorchServe:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PyTorch models&lt;/li&gt;
&lt;li&gt;Multi-model serving&lt;/li&gt;
&lt;li&gt;Metrics out of box&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.FastAPI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python web framework&lt;/li&gt;
&lt;li&gt;Async support&lt;/li&gt;
&lt;li&gt;Easy to use&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.BentoML:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model packaging&lt;/li&gt;
&lt;li&gt;Multi-framework&lt;/li&gt;
&lt;li&gt;Production features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Ray Serve:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scalable serving&lt;/li&gt;
&lt;li&gt;Model composition&lt;/li&gt;
&lt;li&gt;Distributed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;API Design&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RESTful endpoints&lt;/li&gt;
&lt;li&gt;Input validation&lt;/li&gt;
&lt;li&gt;Error handling&lt;/li&gt;
&lt;li&gt;Rate limiting&lt;/li&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;10.3 &lt;strong&gt;Model Monitoring&lt;/strong&gt;&lt;br&gt;
What to Monitor:&lt;/p&gt;

&lt;p&gt;1.Performance Metrics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accuracy, precision, recall&lt;/li&gt;
&lt;li&gt;Latency&lt;/li&gt;
&lt;li&gt;Throughput&lt;/li&gt;
&lt;li&gt;Error rates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Data Quality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Missing values&lt;/li&gt;
&lt;li&gt;Outliers&lt;/li&gt;
&lt;li&gt;Distribution shifts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Data Drift:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Input distribution changes&lt;/li&gt;
&lt;li&gt;Feature drift&lt;/li&gt;
&lt;li&gt;Covariate shift&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Concept Drift:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Relationship changes&lt;/li&gt;
&lt;li&gt;Model becomes outdated&lt;/li&gt;
&lt;li&gt;Triggers retraining&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Model Drift:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance degradation&lt;/li&gt;
&lt;li&gt;Accuracy decline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Monitoring Tools&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prometheus + Grafana&lt;/li&gt;
&lt;li&gt;DataDog&lt;/li&gt;
&lt;li&gt;Weights &amp;amp; Biases&lt;/li&gt;
&lt;li&gt;MLflow&lt;/li&gt;
&lt;li&gt;Evidently AI&lt;/li&gt;
&lt;li&gt;Whylabs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;10.4 &lt;strong&gt;Model Versioning and Registry&lt;/strong&gt;&lt;br&gt;
Why Version Models?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reproducibility&lt;/li&gt;
&lt;li&gt;Rollback capability&lt;/li&gt;
&lt;li&gt;A/B testing&lt;/li&gt;
&lt;li&gt;Audit trail&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What to Track:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model artifacts&lt;/li&gt;
&lt;li&gt;Training code&lt;/li&gt;
&lt;li&gt;Dependencies&lt;/li&gt;
&lt;li&gt;Hyperparameters&lt;/li&gt;
&lt;li&gt;Training data version&lt;/li&gt;
&lt;li&gt;Metrics&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MLflow Model Registry&lt;/li&gt;
&lt;li&gt;DVC (Data Version Control)&lt;/li&gt;
&lt;li&gt;Weights &amp;amp; Biases&lt;/li&gt;
&lt;li&gt;Neptune.ai&lt;/li&gt;
&lt;li&gt;Comet.ml&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;10.5 &lt;strong&gt;A/B Testing and Experimentation&lt;/strong&gt;&lt;br&gt;
Purpose:&lt;br&gt;
Validate model improvements before full rollout.&lt;br&gt;
Process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define success metrics&lt;/li&gt;
&lt;li&gt;Split traffic (e.g., 90/10)&lt;/li&gt;
&lt;li&gt;Deploy both models&lt;/li&gt;
&lt;li&gt;Collect metrics&lt;/li&gt;
&lt;li&gt;Statistical significance testing&lt;/li&gt;
&lt;li&gt;Make decision&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Considerations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sample size&lt;/li&gt;
&lt;li&gt;Ramp-up strategy&lt;/li&gt;
&lt;li&gt;Monitoring&lt;/li&gt;
&lt;li&gt;Rollback plan&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;10.6 CI/CD for ML&lt;br&gt;
Continuous Integration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automated testing&lt;/li&gt;
&lt;li&gt;Code quality checks&lt;/li&gt;
&lt;li&gt;Model validation&lt;/li&gt;
&lt;li&gt;Data validation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Continuous Deployment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automated deployment&lt;/li&gt;
&lt;li&gt;Gradual rollout&lt;/li&gt;
&lt;li&gt;Blue-green deployment&lt;/li&gt;
&lt;li&gt;Canary releases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Testing Strategies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unit tests for code&lt;/li&gt;
&lt;li&gt;Integration tests&lt;/li&gt;
&lt;li&gt;Model performance tests&lt;/li&gt;
&lt;li&gt;Data validation tests&lt;/li&gt;
&lt;li&gt;Shadow mode testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub Actions&lt;/li&gt;
&lt;li&gt;GitLab CI&lt;/li&gt;
&lt;li&gt;Jenkins&lt;/li&gt;
&lt;li&gt;CircleCI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;10.7 Infrastructure and Scaling&lt;br&gt;
Compute Options:&lt;/p&gt;

&lt;p&gt;1.On-Premise:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Full control&lt;/li&gt;
&lt;li&gt;High upfront cost&lt;/li&gt;
&lt;li&gt;Maintenance overhead&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Cloud Providers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS SageMaker&lt;/li&gt;
&lt;li&gt;Google Cloud AI Platform&lt;/li&gt;
&lt;li&gt;Azure ML&lt;/li&gt;
&lt;li&gt;Elastic scaling&lt;/li&gt;
&lt;li&gt;Pay-as-you-go&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Managed Services:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hugging Face Inference&lt;/li&gt;
&lt;li&gt;Replicate&lt;/li&gt;
&lt;li&gt;Modal&lt;/li&gt;
&lt;li&gt;Together AI&lt;/li&gt;
&lt;li&gt;Easier deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;GPU Considerations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Training: A100, H100&lt;/li&gt;
&lt;li&gt;Inference: T4, L4&lt;/li&gt;
&lt;li&gt;Cost vs performance&lt;/li&gt;
&lt;li&gt;Spot instances for savings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Scaling Strategies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Horizontal scaling (more instances)&lt;/li&gt;
&lt;li&gt;Vertical scaling (bigger instances)&lt;/li&gt;
&lt;li&gt;Auto-scaling policies&lt;/li&gt;
&lt;li&gt;Load balancing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;10.8 Security and Privacy&lt;br&gt;
Model Security:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Input validation&lt;/li&gt;
&lt;li&gt;Rate limiting&lt;/li&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;li&gt;Encryption in transit&lt;/li&gt;
&lt;li&gt;Secure model storage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Privacy Concerns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Personal data in training&lt;/li&gt;
&lt;li&gt;Model inversion attacks&lt;/li&gt;
&lt;li&gt;Membership inference&lt;/li&gt;
&lt;li&gt;Data anonymization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Techniques:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Differential privacy&lt;/li&gt;
&lt;li&gt;Federated learning&lt;/li&gt;
&lt;li&gt;Secure multi-party computation&lt;/li&gt;
&lt;li&gt;Homomorphic encryption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Compliance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GDPR&lt;/li&gt;
&lt;li&gt;CCPA&lt;/li&gt;
&lt;li&gt;HIPAA (healthcare)&lt;/li&gt;
&lt;li&gt;Model explainability requirements&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  11.&lt;strong&gt;Tools and Frameworks&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;11.1 &lt;strong&gt;Deep Learning Frameworks&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;PyTorch&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Research-friendly&lt;/li&gt;
&lt;li&gt;Dynamic computation graphs&lt;/li&gt;
&lt;li&gt;Pythonic API&lt;/li&gt;
&lt;li&gt;Strong community&lt;/li&gt;
&lt;li&gt;TorchScript for production&lt;/li&gt;
&lt;li&gt;Growing industry adoption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Research projects&lt;/li&gt;
&lt;li&gt;Experimentation&lt;/li&gt;
&lt;li&gt;Custom architectures&lt;/li&gt;
&lt;li&gt;When flexibility matters&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Production-focused&lt;/li&gt;
&lt;li&gt;Static graphs (TF 2.x more dynamic)&lt;/li&gt;
&lt;li&gt;TensorFlow Serving&lt;/li&gt;
&lt;li&gt;TensorFlow Lite for mobile&lt;/li&gt;
&lt;li&gt;Enterprise adoption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Production deployment&lt;/li&gt;
&lt;li&gt;Mobile/edge deployment&lt;/li&gt;
&lt;li&gt;Large-scale distributed training&lt;/li&gt;
&lt;li&gt;When ecosystem integration matters&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;High-performance numerical computing&lt;/li&gt;
&lt;li&gt;Automatic differentiation&lt;/li&gt;
&lt;li&gt;JIT compilation&lt;/li&gt;
&lt;li&gt;GPU/TPU support&lt;/li&gt;
&lt;li&gt;Functional programming style&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When to use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Research requiring performance&lt;/li&gt;
&lt;li&gt;Custom numerical algorithms&lt;/li&gt;
&lt;li&gt;When composability matte
rs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;11.2 &lt;strong&gt;Classical ML Libraries&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Scikit-learn&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Comprehensive classical ML&lt;/li&gt;
&lt;li&gt;Consistent API&lt;/li&gt;
&lt;li&gt;Excellent documentation&lt;/li&gt;
&lt;li&gt;Preprocessing utilities&lt;/li&gt;
&lt;li&gt;Model selection tools&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key Modules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Classification&lt;/li&gt;
&lt;li&gt;Regression&lt;/li&gt;
&lt;li&gt;Clustering&lt;/li&gt;
&lt;li&gt;Dimensionality reduction&lt;/li&gt;
&lt;li&gt;Model selection&lt;/li&gt;
&lt;li&gt;Preprocessing&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Gradient boosting&lt;/li&gt;
&lt;li&gt;Fast and efficient&lt;/li&gt;
&lt;li&gt;Handles missing values&lt;/li&gt;
&lt;li&gt;Built-in regularization&lt;/li&gt;
&lt;li&gt;Parallel processing&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Faster than XGBoost&lt;/li&gt;
&lt;li&gt;Lower memory usage&lt;/li&gt;
&lt;li&gt;Histogram-based&lt;/li&gt;
&lt;li&gt;Good for large datasets&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Handles categorical features natively&lt;/li&gt;
&lt;li&gt;Ordered boosting&lt;/li&gt;
&lt;li&gt;Robust to overfitting&lt;/li&gt;
&lt;li&gt;Less hyperparameter tuning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;11.3 &lt;strong&gt;NLP and LLM Frameworks&lt;/strong&gt;&lt;br&gt;
Hugging Face Transformers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pre-trained models&lt;/li&gt;
&lt;li&gt;Consistent API&lt;/li&gt;
&lt;li&gt;Active community&lt;/li&gt;
&lt;li&gt;Model hub&lt;/li&gt;
&lt;li&gt;Easy fine-tuning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Models Available:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;BERT variants&lt;/li&gt;
&lt;li&gt;GPT models&lt;/li&gt;
&lt;li&gt;T5, BART&lt;/li&gt;
&lt;li&gt;Vision transformers&lt;/li&gt;
&lt;li&gt;Multi-modal models&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;LangChain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LLM application framework&lt;/li&gt;
&lt;li&gt;Chains for workflows&lt;/li&gt;
&lt;li&gt;Agents and tools&lt;/li&gt;
&lt;li&gt;Memory management&lt;/li&gt;
&lt;li&gt;Retrieval components&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prompts&lt;/li&gt;
&lt;li&gt;Models&lt;/li&gt;
&lt;li&gt;Chains&lt;/li&gt;
&lt;li&gt;Agents&lt;/li&gt;
&lt;li&gt;Memory&lt;/li&gt;
&lt;li&gt;Callbacks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;LlamaIndex (formerly GPT Index):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data framework for LLMs&lt;/li&gt;
&lt;li&gt;Document loaders&lt;/li&gt;
&lt;li&gt;Index structures&lt;/li&gt;
&lt;li&gt;Query engines&lt;/li&gt;
&lt;li&gt;Agent tools&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use Cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RAG applications&lt;/li&gt;
&lt;li&gt;Document Q&amp;amp;A&lt;/li&gt;
&lt;li&gt;Knowledge bases&lt;/li&gt;
&lt;li&gt;Semantic search&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;LangGraph:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Agent orchestration&lt;/li&gt;
&lt;li&gt;Stateful applications&lt;/li&gt;
&lt;li&gt;Cyclic workflows&lt;/li&gt;
&lt;li&gt;Multi-agent systems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Haystack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;NLP framework&lt;/li&gt;
&lt;li&gt;Pipeline-based&lt;/li&gt;
&lt;li&gt;Production-ready&lt;/li&gt;
&lt;li&gt;Search and QA focus&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;11.4 Vector Databases&lt;br&gt;
Pinecone:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Managed vector database&lt;/li&gt;
&lt;li&gt;Serverless&lt;/li&gt;
&lt;li&gt;Easy to use&lt;/li&gt;
&lt;li&gt;Good performance&lt;/li&gt;
&lt;li&gt;Paid service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similarity search&lt;/li&gt;
&lt;li&gt;Filtering&lt;/li&gt;
&lt;li&gt;Metadata storage&lt;/li&gt;
&lt;li&gt;Namespaces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Weaviate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open source&lt;/li&gt;
&lt;li&gt;Hybrid search&lt;/li&gt;
&lt;li&gt;GraphQL API&lt;/li&gt;
&lt;li&gt;Modules for ML&lt;/li&gt;
&lt;li&gt;Self-hosted or cloud&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Chroma:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lightweight&lt;/li&gt;
&lt;li&gt;Easy local development&lt;/li&gt;
&lt;li&gt;Good for prototyping&lt;/li&gt;
&lt;li&gt;Simple API&lt;/li&gt;
&lt;li&gt;Embeddings built-in&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Qdrant:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;High performance&lt;/li&gt;
&lt;li&gt;Open source&lt;/li&gt;
&lt;li&gt;Rich filtering&lt;/li&gt;
&lt;li&gt;Production-ready&lt;/li&gt;
&lt;li&gt;Rust-based (fast)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Milvus:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Highly scalable&lt;/li&gt;
&lt;li&gt;Multiple index types&lt;/li&gt;
&lt;li&gt;Enterprise features&lt;/li&gt;
&lt;li&gt;Active development&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Comparison Factors:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance&lt;/li&gt;
&lt;li&gt;Scalability&lt;/li&gt;
&lt;li&gt;Cost&lt;/li&gt;
&lt;li&gt;Ease of use&lt;/li&gt;
&lt;li&gt;Features needed&lt;/li&gt;
&lt;li&gt;Deployment model&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;11.5 Experiment Tracking&lt;br&gt;
Weights &amp;amp; Biases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Experiment tracking&lt;/li&gt;
&lt;li&gt;Hyperparameter tuning&lt;/li&gt;
&lt;li&gt;Model versioning&lt;/li&gt;
&lt;li&gt;Collaboration features&lt;/li&gt;
&lt;li&gt;Visualization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MLflow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open source&lt;/li&gt;
&lt;li&gt;Experiment tracking&lt;/li&gt;
&lt;li&gt;Model registry&lt;/li&gt;
&lt;li&gt;Model deployment&lt;/li&gt;
&lt;li&gt;Multiple framework support&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Neptune.ai:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Metadata store&lt;/li&gt;
&lt;li&gt;Experiment organization&lt;/li&gt;
&lt;li&gt;Team collaboration&lt;/li&gt;
&lt;li&gt;Version control&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TensorBoard:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TensorFlow integration&lt;/li&gt;
&lt;li&gt;Visualization&lt;/li&gt;
&lt;li&gt;Scalar/image/graph logging&lt;/li&gt;
&lt;li&gt;Hyperparameter tuning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Comet.ml:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Experiment management&lt;/li&gt;
&lt;li&gt;Model production&lt;/li&gt;
&lt;li&gt;Team features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What to Track:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hyperparameters&lt;/li&gt;
&lt;li&gt;Metrics&lt;/li&gt;
&lt;li&gt;Code version&lt;/li&gt;
&lt;li&gt;Dependencies&lt;/li&gt;
&lt;li&gt;System metrics&lt;/li&gt;
&lt;li&gt;Artifacts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;11.6 &lt;strong&gt;Data Tools&lt;/strong&gt;&lt;br&gt;
Pandas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data manipulation&lt;/li&gt;
&lt;li&gt;DataFrame operations&lt;/li&gt;
&lt;li&gt;Time series&lt;/li&gt;
&lt;li&gt;Statistical functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Polars:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster than Pandas&lt;/li&gt;
&lt;li&gt;Better memory efficiency&lt;/li&gt;
&lt;li&gt;Lazy evaluation&lt;/li&gt;
&lt;li&gt;Growing adoption&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parallel computing&lt;/li&gt;
&lt;li&gt;Scales Pandas&lt;/li&gt;
&lt;li&gt;Out-of-core computation&lt;/li&gt;
&lt;li&gt;Distributed arrays&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apache Spark:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Big data processing&lt;/li&gt;
&lt;li&gt;Distributed computing&lt;/li&gt;
&lt;li&gt;MLlib for ML&lt;/li&gt;
&lt;li&gt;Scala/Python APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DuckDB:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Analytical database&lt;/li&gt;
&lt;li&gt;SQL interface&lt;/li&gt;
&lt;li&gt;Fast for analytics&lt;/li&gt;
&lt;li&gt;In-process&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;11.7 Visualization&lt;br&gt;
Matplotlib:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Foundational plotting&lt;/li&gt;
&lt;li&gt;Fine-grained control&lt;/li&gt;
&lt;li&gt;Publication quality&lt;/li&gt;
&lt;li&gt;Steep learning curve&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Seaborn:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Statistical visualization&lt;/li&gt;
&lt;li&gt;Built on Matplotlib&lt;/li&gt;
&lt;li&gt;Beautiful defaults&lt;/li&gt;
&lt;li&gt;Less verbose&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Plotly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Interactive plots&lt;/li&gt;
&lt;li&gt;Web-based&lt;/li&gt;
&lt;li&gt;Dashboards&lt;/li&gt;
&lt;li&gt;Multiple languages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Altair:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Declarative visualization&lt;/li&gt;
&lt;li&gt;Grammar of graphics&lt;/li&gt;
&lt;li&gt;Concise syntax&lt;/li&gt;
&lt;li&gt;Interactive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Streamlit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data apps&lt;/li&gt;
&lt;li&gt;Interactive dashboards&lt;/li&gt;
&lt;li&gt;Pure Python&lt;/li&gt;
&lt;li&gt;Fast prototyping&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Gradio:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ML demos&lt;/li&gt;
&lt;li&gt;Share models&lt;/li&gt;
&lt;li&gt;Simple interface creation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;11.8 Cloud Platforms&lt;br&gt;
AWS:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SageMaker: ML platform&lt;/li&gt;
&lt;li&gt;EC2: Compute&lt;/li&gt;
&lt;li&gt;S3: Storage&lt;/li&gt;
&lt;li&gt;Lambda: Serverless&lt;/li&gt;
&lt;li&gt;Bedrock: LLM access&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Google Cloud:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vertex AI: ML platform&lt;/li&gt;
&lt;li&gt;Compute Engine&lt;/li&gt;
&lt;li&gt;Cloud Storage&lt;/li&gt;
&lt;li&gt;Cloud Functions&lt;/li&gt;
&lt;li&gt;Gemini API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Azure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Azure ML&lt;/li&gt;
&lt;li&gt;Virtual Machines&lt;/li&gt;
&lt;li&gt;Blob Storage&lt;/li&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;li&gt;OpenAI Service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Specialized Platforms:&lt;br&gt;
Modal:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Serverless containers&lt;/li&gt;
&lt;li&gt;GPU access&lt;/li&gt;
&lt;li&gt;Easy deployment&lt;/li&gt;
&lt;li&gt;Python-first&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Replicate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model hosting&lt;/li&gt;
&lt;li&gt;API for models&lt;/li&gt;
&lt;li&gt;Pay per use&lt;/li&gt;
&lt;li&gt;No infrastructure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hugging Face Inference:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hosted models&lt;/li&gt;
&lt;li&gt;Serverless&lt;/li&gt;
&lt;li&gt;Easy integration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Together AI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open model hosting&lt;/li&gt;
&lt;li&gt;Competitive pricing&lt;/li&gt;
&lt;li&gt;API access&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  12. Building Your First AI/ML Project
&lt;/h2&gt;

&lt;p&gt;Now that you know the concepts, let's discuss how to actually build projects.&lt;/p&gt;

&lt;p&gt;12.1 &lt;strong&gt;Project Selection&lt;/strong&gt;&lt;br&gt;
Good Project Characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Solves a real problem&lt;/li&gt;
&lt;li&gt;Showcases multiple skills&lt;/li&gt;
&lt;li&gt;Has clear success metrics&lt;/li&gt;
&lt;li&gt;Manageable scope&lt;/li&gt;
&lt;li&gt;Interesting to you&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Project Difficulty Levels:&lt;br&gt;
Beginner:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Image classification (MNIST, CIFAR-10)&lt;/li&gt;
&lt;li&gt;Sentiment analysis&lt;/li&gt;
&lt;li&gt;House price prediction&lt;/li&gt;
&lt;li&gt;Customer churn prediction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Intermediate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Object detection&lt;/li&gt;
&lt;li&gt;Text generation&lt;/li&gt;
&lt;li&gt;Recommendation system&lt;/li&gt;
&lt;li&gt;Time series forecasting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advanced:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RAG application&lt;/li&gt;
&lt;li&gt;Multi-agent system&lt;/li&gt;
&lt;li&gt;Fine-tuned LLM&lt;/li&gt;
&lt;li&gt;End-to-end ML pipeline&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Portfolio Projects Should Show:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data processing skills&lt;/li&gt;
&lt;li&gt;Model building&lt;/li&gt;
&lt;li&gt;Evaluation methodology&lt;/li&gt;
&lt;li&gt;Deployment capability&lt;/li&gt;
&lt;li&gt;Code quality&lt;/li&gt;
&lt;li&gt;Documentation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;12.2 Project Structure&lt;br&gt;
Recommended Structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project_name/
├── data/
│   ├── raw/
│   ├── processed/
│   └── external/
├── notebooks/
│   ├── exploration.ipynb
│   └── experiments.ipynb
├── src/
│   ├── __init__.py
│   ├── data/
│   │   ├── load.py
│   │   └── preprocess.py
│   ├── features/
│   │   └── build_features.py
│   ├── models/
│   │   ├── train.py
│   │   └── predict.py
│   └── visualization/
│       └── visualize.py
├── tests/
│   └── test_models.py
├── configs/
│   └── config.yaml
├── models/
│   └── model.pkl
├── requirements.txt
├── setup.py
├── README.md
└── .gitignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Best Practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Separate code from notebooks&lt;/li&gt;
&lt;li&gt;Version control everything&lt;/li&gt;
&lt;li&gt;Clear naming conventions&lt;/li&gt;
&lt;li&gt;Modular code&lt;/li&gt;
&lt;li&gt;Configuration files&lt;/li&gt;
&lt;li&gt;Comprehensive README&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;12.3 README Best Practices&lt;br&gt;
Essential Sections:&lt;/p&gt;

&lt;p&gt;1.Project Title and Description&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What problem does it solve?&lt;/li&gt;
&lt;li&gt;High-level approach&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2.Demo/Results&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Screenshots&lt;/li&gt;
&lt;li&gt;Sample outputs&lt;/li&gt;
&lt;li&gt;Performance metrics&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.Installation&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dependencies&lt;/li&gt;
&lt;li&gt;Setup instructions&lt;/li&gt;
&lt;li&gt;Virtual environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.Usage&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to run&lt;/li&gt;
&lt;li&gt;Example commands&lt;/li&gt;
&lt;li&gt;API documentation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5.Project Structure&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Brief explanation of folders&lt;/li&gt;
&lt;li&gt;Key files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.Methodology&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data source&lt;/li&gt;
&lt;li&gt;Preprocessing steps&lt;/li&gt;
&lt;li&gt;Model architecture&lt;/li&gt;
&lt;li&gt;Training process&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7.Results&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Metrics&lt;/li&gt;
&lt;li&gt;Visualizations&lt;/li&gt;
&lt;li&gt;Comparisons&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;8.Future Work&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Improvements&lt;/li&gt;
&lt;li&gt;Extensions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;9.Acknowledgments&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data sources&lt;/li&gt;
&lt;li&gt;References&lt;/li&gt;
&lt;li&gt;Inspiration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;12.4 &lt;strong&gt;Development Workflow&lt;/strong&gt;&lt;br&gt;
Step 1: Problem Definition&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clearly define the problem&lt;/li&gt;
&lt;li&gt;Understand success criteria&lt;/li&gt;
&lt;li&gt;Identify constraints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 2: Data Collection&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find relevant datasets&lt;/li&gt;
&lt;li&gt;Understand data structure&lt;/li&gt;
&lt;li&gt;Check data quality&lt;/li&gt;
&lt;li&gt;Handle licensing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 3: Exploratory Data Analysis&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visualize distributions&lt;/li&gt;
&lt;li&gt;Find patterns&lt;/li&gt;
&lt;li&gt;Identify anomalies&lt;/li&gt;
&lt;li&gt;Check correlations&lt;/li&gt;
&lt;li&gt;Generate hypotheses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 4: Data Preprocessing&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Handle missing values&lt;/li&gt;
&lt;li&gt;Remove duplicates&lt;/li&gt;
&lt;li&gt;Feature engineering&lt;/li&gt;
&lt;li&gt;Normalization/scaling&lt;/li&gt;
&lt;li&gt;Train-test split&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 5: Baseline Model&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start simple&lt;/li&gt;
&lt;li&gt;Establish baseline&lt;/li&gt;
&lt;li&gt;Understand data better&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 6: Experimentation&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Try different models&lt;/li&gt;
&lt;li&gt;Hyperparameter tuning&lt;/li&gt;
&lt;li&gt;Feature selection&lt;/li&gt;
&lt;li&gt;Ensemble methods&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 7: Evaluation&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple metrics&lt;/li&gt;
&lt;li&gt;Cross-validation&lt;/li&gt;
&lt;li&gt;Error analysis&lt;/li&gt;
&lt;li&gt;Visualize results&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 8: Optimization&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Address weaknesses&lt;/li&gt;
&lt;li&gt;Improve performance&lt;/li&gt;
&lt;li&gt;Consider trade-offs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 9: Deployment&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create API&lt;/li&gt;
&lt;li&gt;Containerize&lt;/li&gt;
&lt;li&gt;Deploy to cloud&lt;/li&gt;
&lt;li&gt;Monitor performance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Step 10: Documentation&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code comments&lt;/li&gt;
&lt;li&gt;README&lt;/li&gt;
&lt;li&gt;API documentation&lt;/li&gt;
&lt;li&gt;Blog post/report&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;12.5 &lt;strong&gt;Common Pitfalls to Avoid&lt;/strong&gt;&lt;br&gt;
Data Leakage:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test data in training&lt;/li&gt;
&lt;li&gt;Future information in features&lt;/li&gt;
&lt;li&gt;Target information in features&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overfitting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Too complex model&lt;/li&gt;
&lt;li&gt;Insufficient regularization&lt;/li&gt;
&lt;li&gt;Not enough data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Poor Evaluation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Wrong metrics&lt;/li&gt;
&lt;li&gt;No cross-validation&lt;/li&gt;
&lt;li&gt;Ignoring class imbalance&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reproducibility Issues:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No random seeds&lt;/li&gt;
&lt;li&gt;Missing dependencies&lt;/li&gt;
&lt;li&gt;Undocumented steps&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Scalability Problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inefficient code&lt;/li&gt;
&lt;li&gt;Memory issues&lt;/li&gt;
&lt;li&gt;No batch processing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Production Neglect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No monitoring&lt;/li&gt;
&lt;li&gt;No error handling&lt;/li&gt;
&lt;li&gt;Hardcoded values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;12.6 Making Your Project Stand Out&lt;br&gt;
Code Quality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clean, readable code&lt;/li&gt;
&lt;li&gt;Consistent style (PEP 8)&lt;/li&gt;
&lt;li&gt;Type hints&lt;/li&gt;
&lt;li&gt;Documentation strings&lt;/li&gt;
&lt;li&gt;Unit tests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Visualization:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Professional plots&lt;/li&gt;
&lt;li&gt;Interactive dashboards&lt;/li&gt;
&lt;li&gt;Clear labels and titles&lt;/li&gt;
&lt;li&gt;Appropriate colors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Deployment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Live demo (Streamlit, Gradio)&lt;/li&gt;
&lt;li&gt;API with documentation&lt;/li&gt;
&lt;li&gt;Docker container&lt;/li&gt;
&lt;li&gt;Cloud deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Documentation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Comprehensive README&lt;/li&gt;
&lt;li&gt;Code comments&lt;/li&gt;
&lt;li&gt;Blog post explaining approach&lt;/li&gt;
&lt;li&gt;Video walkthrough&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Innovation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Novel approach&lt;/li&gt;
&lt;li&gt;Creative application&lt;/li&gt;
&lt;li&gt;Unique dataset&lt;/li&gt;
&lt;li&gt;Interesting insights&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  13. Specific Project Ideas with Implementation Guides
&lt;/h2&gt;

&lt;p&gt;13.1 &lt;strong&gt;AI Second Brain (Recommended)&lt;/strong&gt;&lt;br&gt;
Overview:&lt;br&gt;
Personal knowledge management system using RAG and agents.&lt;br&gt;
Tech Stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LangGraph for orchestration&lt;/li&gt;
&lt;li&gt;Qdrant for vector storage&lt;/li&gt;
&lt;li&gt;OpenAI/Claude for LLM&lt;/li&gt;
&lt;li&gt;Streamlit for UI&lt;/li&gt;
&lt;li&gt;Python-docx, PyPDF2 for parsing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Implementation Phases:&lt;br&gt;
Phase 1: Basic RAG (Week 1-2)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Document ingestion (PDF, TXT, DOCX)&lt;/li&gt;
&lt;li&gt;Text chunking&lt;/li&gt;
&lt;li&gt;Embedding generation&lt;/li&gt;
&lt;li&gt;Vector storage&lt;/li&gt;
&lt;li&gt;Simple Q&amp;amp;A interface&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Phase 2: Agent System (Week 3-4)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Query planning agent&lt;/li&gt;
&lt;li&gt;Retrieval agent&lt;/li&gt;
&lt;li&gt;Synthesis agent&lt;/li&gt;
&lt;li&gt;Memory management&lt;/li&gt;
&lt;li&gt;Source attribution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Phase 3: Advanced Features (Week 5-6)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multi-modal support (images, audio)&lt;/li&gt;
&lt;li&gt;Graph-based connections&lt;/li&gt;
&lt;li&gt;Proactive insights&lt;/li&gt;
&lt;li&gt;Export capabilities&lt;/li&gt;
&lt;li&gt;Voice interface&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Key Challenges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chunking strategy&lt;/li&gt;
&lt;li&gt;Context window management&lt;/li&gt;
&lt;li&gt;Relevance scoring&lt;/li&gt;
&lt;li&gt;Performance optimization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Learning Outcomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;RAG implementation&lt;/li&gt;
&lt;li&gt;Agent orchestration&lt;/li&gt;
&lt;li&gt;Vector databases&lt;/li&gt;
&lt;li&gt;LLM integration&lt;/li&gt;
&lt;li&gt;Production deployment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;13.2 &lt;strong&gt;Code Review Agent&lt;/strong&gt;&lt;br&gt;
Overview:&lt;br&gt;
Autonomous agent that reviews code and suggests improvements.&lt;br&gt;
Tech Stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LangChain for agent framework&lt;/li&gt;
&lt;li&gt;GitHub API&lt;/li&gt;
&lt;li&gt;Tree-sitter for parsing&lt;/li&gt;
&lt;li&gt;GPT-4 for analysis&lt;/li&gt;
&lt;li&gt;FastAPI for backend&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Architectural analysis&lt;/li&gt;
&lt;li&gt;Code smell detection&lt;/li&gt;
&lt;li&gt;Security vulnerability scanning&lt;/li&gt;
&lt;li&gt;Test coverage suggestions&lt;/li&gt;
&lt;li&gt;Documentation generation&lt;/li&gt;
&lt;li&gt;Refactoring recommendations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Implementation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parse code with tree-sitter&lt;/li&gt;
&lt;li&gt;Extract context (imports, classes, functions)&lt;/li&gt;
&lt;li&gt;LLM analysis with structured output&lt;/li&gt;
&lt;li&gt;Generate actionable suggestions&lt;/li&gt;
&lt;li&gt;Create pull request comments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Differentiation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multi-file context awareness&lt;/li&gt;
&lt;li&gt;Learns project conventions&lt;/li&gt;
&lt;li&gt;Explains reasoning&lt;/li&gt;
&lt;li&gt;Interactive refinement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;13.3 &lt;strong&gt;Financial Analysis Agent System&lt;/strong&gt;&lt;br&gt;
Overview:&lt;br&gt;
Multi-agent system for investment research.&lt;br&gt;
Agents:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;News Sentiment Agent&lt;/li&gt;
&lt;li&gt;Technical Analysis Agent&lt;/li&gt;
&lt;li&gt;Fundamental Analysis Agent&lt;/li&gt;
&lt;li&gt;Risk Assessment Agent&lt;/li&gt;
&lt;li&gt;Report Generation Agent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tech Stack:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LangGraph for orchestration&lt;/li&gt;
&lt;li&gt;Alpha Vantage API&lt;/li&gt;
&lt;li&gt;News API&lt;/li&gt;
&lt;li&gt;RAG for historical analysis&lt;/li&gt;
&lt;li&gt;Plotly for visualization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Workflow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User asks about stock/sector&lt;/li&gt;
&lt;li&gt;Agents work in parallel&lt;/li&gt;
&lt;li&gt;Collect and synthesize findings&lt;/li&gt;
&lt;li&gt;Generate comprehensive report&lt;/li&gt;
&lt;li&gt;Provide actionable insights&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advanced Features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time data streaming&lt;/li&gt;
&lt;li&gt;Portfolio optimization&lt;/li&gt;
&lt;li&gt;Backtesting strategies&lt;/li&gt;
&lt;li&gt;Alert system&lt;/li&gt;
&lt;li&gt;Explainable recommendations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;13.4 &lt;strong&gt;Custom ChatBot with Domain Expertise&lt;/strong&gt;&lt;br&gt;
Overview:&lt;br&gt;
Specialized chatbot for specific domain (legal, medical, technical).&lt;br&gt;
Approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fine-tune on domain data&lt;/li&gt;
&lt;li&gt;RAG for current information&lt;/li&gt;
&lt;li&gt;Custom evaluation metrics&lt;/li&gt;
&lt;li&gt;Safety guardrails&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Implementation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Collect domain-specific data&lt;/li&gt;
&lt;li&gt;Fine-tune base model (LoRA)&lt;/li&gt;
&lt;li&gt;Build RAG system for documentation&lt;/li&gt;
&lt;li&gt;Create evaluation dataset&lt;/li&gt;
&lt;li&gt;Implement safety checks&lt;/li&gt;
&lt;li&gt;Deploy with monitoring&lt;/li&gt;
&lt;/ol&gt;

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

&lt;ul&gt;
&lt;li&gt;Legal document assistant&lt;/li&gt;
&lt;li&gt;Medical information chatbot&lt;/li&gt;
&lt;li&gt;Technical support agent&lt;/li&gt;
&lt;li&gt;Educational tutor&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  14. Interview Preparation
&lt;/h2&gt;

&lt;p&gt;14.1 &lt;strong&gt;Technical Interview Topics&lt;/strong&gt;&lt;br&gt;
Machine Learning Fundamentals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explain bias-variance tradeoff&lt;/li&gt;
&lt;li&gt;Overfitting and solutions&lt;/li&gt;
&lt;li&gt;Different types of ML&lt;/li&gt;
&lt;li&gt;Evaluation metrics&lt;/li&gt;
&lt;li&gt;Cross-validation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Deep Learning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Backpropagation&lt;/li&gt;
&lt;li&gt;Activation functions&lt;/li&gt;
&lt;li&gt;Regularization techniques&lt;/li&gt;
&lt;li&gt;CNN architectures&lt;/li&gt;
&lt;li&gt;Transformer architecture&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;LLMs and NLP:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Attention mechanism&lt;/li&gt;
&lt;li&gt;Pre-training objectives&lt;/li&gt;
&lt;li&gt;Fine-tuning vs prompting&lt;/li&gt;
&lt;li&gt;RAG architecture&lt;/li&gt;
&lt;li&gt;Prompt engineering&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MLOps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model deployment strategies&lt;/li&gt;
&lt;li&gt;Monitoring approaches&lt;/li&gt;
&lt;li&gt;Handling data drift&lt;/li&gt;
&lt;li&gt;A/B testing&lt;/li&gt;
&lt;li&gt;CI/CD for ML&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;System Design:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ML system architecture&lt;/li&gt;
&lt;li&gt;Scalability considerations&lt;/li&gt;
&lt;li&gt;Trade-offs (latency vs accuracy)&lt;/li&gt;
&lt;li&gt;Data pipeline design&lt;/li&gt;
&lt;li&gt;Model serving&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;14.2 &lt;strong&gt;Common Interview Questions&lt;/strong&gt;&lt;br&gt;
Conceptual Questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Explain how gradient descent works&lt;/li&gt;
&lt;li&gt;What is the vanishing gradient problem?&lt;/li&gt;
&lt;li&gt;When would you use CNN vs RNN vs Transformer?&lt;/li&gt;
&lt;li&gt;Explain attention mechanism&lt;/li&gt;
&lt;li&gt;What is transfer learning?&lt;/li&gt;
&lt;li&gt;How do you handle imbalanced datasets?&lt;/li&gt;
&lt;li&gt;Explain regularization and types&lt;/li&gt;
&lt;li&gt;What is the difference between L1 and L2 regularization?&lt;/li&gt;
&lt;li&gt;How do transformers work?&lt;/li&gt;
&lt;li&gt;What is RAG and when to use it?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Practical Questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How would you build a recommendation system?&lt;/li&gt;
&lt;li&gt;Design a spam detection system&lt;/li&gt;
&lt;li&gt;How to detect data drift in production?&lt;/li&gt;
&lt;li&gt;Approach to reduce model latency&lt;/li&gt;
&lt;li&gt;How to improve model accuracy?&lt;/li&gt;
&lt;li&gt;Debug a model that's not learning&lt;/li&gt;
&lt;li&gt;Choose between multiple models&lt;/li&gt;
&lt;li&gt;Handle missing data&lt;/li&gt;
&lt;li&gt;Feature engineering approach&lt;/li&gt;
&lt;li&gt;Evaluate model fairness&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Coding Questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Implement linear regression from scratch&lt;/li&gt;
&lt;li&gt;Code softmax function&lt;/li&gt;
&lt;li&gt;Calculate accuracy, precision, recall&lt;/li&gt;
&lt;li&gt;Implement K-means clustering&lt;/li&gt;
&lt;li&gt;Write data preprocessing pipeline&lt;/li&gt;
&lt;li&gt;Implement attention mechanism&lt;/li&gt;
&lt;li&gt;Code cross-validation&lt;/li&gt;
&lt;li&gt;Build simple neural network&lt;/li&gt;
&lt;li&gt;Implement gradient descent&lt;/li&gt;
&lt;li&gt;Parse and process text data&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;14.3 Behavioral Questions&lt;br&gt;
Common Questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Tell me about a challenging ML project&lt;/li&gt;
&lt;li&gt;How do you stay updated with AI/ML?&lt;/li&gt;
&lt;li&gt;Describe a time you debugged a model&lt;/li&gt;
&lt;li&gt;Experience with production deployment&lt;/li&gt;
&lt;li&gt;How do you prioritize tasks?&lt;/li&gt;
&lt;li&gt;Working with cross-functional teams&lt;/li&gt;
&lt;li&gt;Handling disagreements&lt;/li&gt;
&lt;li&gt;Learning from failure&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;STAR Method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Situation: Context&lt;/li&gt;
&lt;li&gt;Task: What needed to be done&lt;/li&gt;
&lt;li&gt;Action: What you did&lt;/li&gt;
&lt;li&gt;Result: Outcome and learning&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  15. Learning Resources and Roadmap
&lt;/h2&gt;

&lt;p&gt;15.1** Online Courses**&lt;br&gt;
Fundamentals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Andrew Ng's Machine Learning (Coursera)&lt;/li&gt;
&lt;li&gt;Fast.ai Practical Deep Learning&lt;/li&gt;
&lt;li&gt;MIT Introduction to Deep Learning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Advanced:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stanford CS224N (NLP)&lt;/li&gt;
&lt;li&gt;Stanford CS231N (Computer Vision)&lt;/li&gt;
&lt;li&gt;Berkeley CS 285 (Deep RL)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Specialized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hugging Face NLP Course&lt;/li&gt;
&lt;li&gt;DeepLearning.AI LLM Courses&lt;/li&gt;
&lt;li&gt;Full Stack Deep Learning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;15.2 Books&lt;br&gt;
Mathematics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Mathematics for Machine Learning"&lt;/li&gt;
&lt;li&gt;"Deep Learning" by Goodfellow&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Machine Learning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Hands-On Machine Learning" by Géron&lt;/li&gt;
&lt;li&gt;"Pattern Recognition and Machine Learning" by Bishop&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Deep Learning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Deep Learning with Python" by Chollet&lt;/li&gt;
&lt;li&gt;"Dive into Deep Learning"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;LLMs and Modern AI:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Build a Large Language Model" by Raschka&lt;/li&gt;
&lt;li&gt;"Natural Language Processing with Transformers"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;15.3 Research Papers&lt;br&gt;
Must-Read Classics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Attention Is All You Need (Transformers)&lt;/li&gt;
&lt;li&gt;BERT: Pre-training of Deep Bidirectional Transformers&lt;/li&gt;
&lt;li&gt;GPT-3: Language Models are Few-Shot Learners&lt;/li&gt;
&lt;li&gt;ResNet: Deep Residual Learning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Recent Important Papers (2024-2026):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Constitutional AI papers&lt;/li&gt;
&lt;li&gt;Retrieval-Augmented Generation techniques&lt;/li&gt;
&lt;li&gt;Mixture of Experts architectures&lt;/li&gt;
&lt;li&gt;Long context methods&lt;/li&gt;
&lt;li&gt;Agent orchestration frameworks&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Quick Reference
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Common ML Algorithms Cheat Sheet&lt;/strong&gt;&lt;br&gt;
Classification:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logistic Regression: Simple, interpretable&lt;/li&gt;
&lt;li&gt;Decision Trees: Non-linear, interpretable&lt;/li&gt;
&lt;li&gt;Random Forest: Robust, high performance&lt;/li&gt;
&lt;li&gt;Gradient Boosting: Best performance on tabular&lt;/li&gt;
&lt;li&gt;SVM: Good for high dimensions&lt;/li&gt;
&lt;li&gt;Neural Networks: Complex patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Regression:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linear Regression: Simple baseline&lt;/li&gt;
&lt;li&gt;Ridge/Lasso: With regularization&lt;/li&gt;
&lt;li&gt;Decision Trees: Non-linear&lt;/li&gt;
&lt;li&gt;Random Forest: Robust&lt;/li&gt;
&lt;li&gt;Gradient Boosting: Best performance&lt;/li&gt;
&lt;li&gt;Neural Networks: Complex patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Clustering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;K-Means: Simple, fast&lt;/li&gt;
&lt;li&gt;DBSCAN: Arbitrary shapes, handles noise&lt;/li&gt;
&lt;li&gt;Hierarchical: Dendrogram, no k needed&lt;/li&gt;
&lt;li&gt;Gaussian Mixture: Probabilistic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Dimensionality Reduction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PCA: Linear, preserves variance&lt;/li&gt;
&lt;li&gt;t-SNE: Non-linear, visualization&lt;/li&gt;
&lt;li&gt;UMAP: Faster than t-SNE&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2026 Recommended Resources
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Resource&lt;/th&gt;
&lt;th&gt;Provider&lt;/th&gt;
&lt;th&gt;Focus&lt;/th&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;MCP Specification Deep Dive&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Anthropic&lt;/td&gt;
&lt;td&gt;Protocol architecture&lt;/td&gt;
&lt;td&gt;Intermediate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;A2A Protocol Workshop&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Google&lt;/td&gt;
&lt;td&gt;Agent-to-agent communication&lt;/td&gt;
&lt;td&gt;Advanced&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;FinOps for ML Engineering&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;FinOps Foundation&lt;/td&gt;
&lt;td&gt;Cost architecture&lt;/td&gt;
&lt;td&gt;Intermediate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;SLM Deployment Mastery&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Microsoft/Google&lt;/td&gt;
&lt;td&gt;Edge AI, quantization&lt;/td&gt;
&lt;td&gt;Intermediate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Human-in-the-Loop AI Design&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Stanford HAI&lt;/td&gt;
&lt;td&gt;Responsible AI&lt;/td&gt;
&lt;td&gt;All levels&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Multi-Agent Systems 2026&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;LangChain Academy&lt;/td&gt;
&lt;td&gt;CrewAI, LangGraph&lt;/td&gt;
&lt;td&gt;Intermediate&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Python Libraries Quick Reference&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Data Manipulation
import numpy as np
import pandas as pd

# Visualization
import matplotlib.pyplot as plt
import seaborn as sns

# Classical ML
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report

# Deep Learning
import torch
import torch.nn as nn
from transformers import AutoModel, AutoTokenizer

# LLM Applications
from langchain import OpenAI
from langchain.chains import LLMChain
import chromadb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Essential Terminal Commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Virtual Environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
venv\Scripts\activate  # Windows

# Package Management
pip install package_name
pip freeze &amp;gt; requirements.txt
pip install -r requirements.txt

# Git
git init
git add .
git commit -m "message"
git push origin main

# Jupyter
jupyter notebook
jupyter lab
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Evaluation Metrics Quick Reference&lt;br&gt;
Classification:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sklearn.metrics import (
    accuracy_score,
    precision_score,
    recall_score,
    f1_score,
    roc_auc_score,
    confusion_matrix
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from sklearn.metrics import (
    mean_absolute_error,
    mean_squared_error,
    r2_score
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Glossary
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Activation Function: Function that introduces non-linearity in neural networks&lt;/li&gt;
&lt;li&gt;Attention: Mechanism allowing models to focus on relevant parts of input&lt;/li&gt;
&lt;li&gt;Backpropagation: Algorithm for computing gradients in neural networks&lt;/li&gt;
&lt;li&gt;Batch Size: Number of samples processed before updating weights&lt;/li&gt;
&lt;li&gt;Bias: Learnable parameter added to weighted sum in neurons&lt;/li&gt;
&lt;li&gt;Cross-Entropy: Loss function for classification tasks&lt;/li&gt;
&lt;li&gt;Embedding: Dense vector representation of discrete data&lt;/li&gt;
&lt;li&gt;Epoch: One complete pass through training dataset&lt;/li&gt;
&lt;li&gt;Fine-tuning: Adapting pre-trained model to specific task&lt;/li&gt;
&lt;li&gt;Gradient Descent: Optimization algorithm using gradients&lt;/li&gt;
&lt;li&gt;Hyperparameter: Parameter set before training (not learned)&lt;/li&gt;
&lt;li&gt;Learning Rate: Step size in gradient descent&lt;/li&gt;
&lt;li&gt;Loss Function: Measures difference between predictions and actual values&lt;/li&gt;
&lt;li&gt;Overfitting: Model memorizes training data, poor generalization&lt;/li&gt;
&lt;li&gt;Pre-training: Training on large general dataset before task-specific training&lt;/li&gt;
&lt;li&gt;RAG: Retrieval-Augmented Generation, combining retrieval and generation&lt;/li&gt;
&lt;li&gt;Regularization: Techniques to prevent overfitting&lt;/li&gt;
&lt;li&gt;Tokenization: Splitting text into tokens (words/subwords)&lt;/li&gt;
&lt;li&gt;Transfer Learning: Using knowledge from one task for another&lt;/li&gt;
&lt;li&gt;Transformer: Neural network architecture based on attention&lt;/li&gt;
&lt;li&gt;Underfitting: Model too simple to capture patterns&lt;/li&gt;
&lt;li&gt;Validation Set: Data used to tune hyperparameters&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Weight: Learnable parameter in neural networks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test-Time Compute and Reasoning Models&lt;br&gt;
16.1 The Shift from Training to Inference Compute&lt;br&gt;
Traditional Paradigm:&lt;br&gt;
Spend massive compute during training, fast inference.&lt;br&gt;
New Paradigm (2026):&lt;br&gt;
Spend more compute at inference time for better reasoning.&lt;br&gt;
Why This Matters:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Better reasoning on complex problems&lt;/li&gt;
&lt;li&gt;Can solve problems not seen in training&lt;/li&gt;
&lt;li&gt;More accurate responses&lt;/li&gt;
&lt;li&gt;Closer to human-like thinking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OpenAI o1 model (reasoning model)&lt;/li&gt;
&lt;li&gt;Chain-of-thought at inference&lt;/li&gt;
&lt;li&gt;Self-consistency with multiple samples&lt;/li&gt;
&lt;li&gt;Iterative refinement&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;16.2 Chain-of-Thought Reasoning at Inference&lt;br&gt;
Basic Chain-of-Thought:Model explains step-by-step before answering.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from openai import OpenAI

client = OpenAI()

def chain_of_thought_reasoning(question):
    prompt = f"""Let's solve this step by step:

Question: {question}

Please think through this carefully:
1. First, identify what we know
2. Then, determine what we need to find
3. Finally, work through the solution step by step

Your reasoning:"""

    response = client.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "user", "content": prompt}
        ],
        temperature=0.7
    )

    return response.choices[0].message.content

# Example
question = "If a train travels 120 km in 2 hours, then speeds up and travels 200 km in the next 2.5 hours, what is the average speed for the entire journey?"
reasoning = chain_of_thought_reasoning(question)
print(reasoning)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advanced: Self-Consistency&lt;br&gt;
Generate multiple reasoning paths and pick most consistent answer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def self_consistency_reasoning(question, num_samples=5):
    answers = []

    for i in range(num_samples):
        response = client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "user", "content": f"Solve step by step: {question}"}
            ],
            temperature=0.8  # Higher temperature for diversity
        )

        answer_text = response.choices[0].message.content
        # Extract final answer
        final_answer = extract_final_answer(answer_text)
        answers.append(final_answer)

    # Find most common answer
    from collections import Counter
    most_common = Counter(answers).most_common(1)[0][0]

    return most_common

def extract_final_answer(text):
    # Extract number or answer from reasoning
    import re
    matches = re.findall(r'(?:answer is|equals?|=)\s*([0-9.]+)', text.lower())
    if matches:
        return float(matches[-1])
    return text.split('\n')[-1].strip()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.3 Tree of Thoughts&lt;br&gt;
Concept:&lt;br&gt;
Explore multiple reasoning branches like a tree search.&lt;br&gt;
Implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class TreeOfThoughts:
    def __init__(self, model):
        self.model = model
        self.thoughts_history = []

    def generate_thoughts(self, problem, num_thoughts=3):
        """Generate multiple initial approaches"""
        prompt = f"""Problem: {problem}

Generate {num_thoughts} different ways to approach this problem.
Each approach should be distinct.

Approaches:"""

        response = self.model.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": prompt}],
            n=num_thoughts,
            temperature=0.9
        )

        thoughts = [choice.message.content for choice in response.choices]
        return thoughts

    def evaluate_thought(self, thought, problem):
        """Evaluate how promising a thought is"""
        prompt = f"""Problem: {problem}

Approach being considered: {thought}

Rate this approach from 1-10 based on:
- Likelihood of success
- Logical soundness
- Efficiency

Rating (just the number):"""

        response = self.model.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.3
        )

        try:
            rating = float(response.choices[0].message.content.strip())
        except:
            rating = 5.0

        return rating

    def expand_thought(self, thought, problem):
        """Develop a thought further"""
        prompt = f"""Problem: {problem}

Current approach: {thought}

Continue developing this approach. What's the next step?"""

        response = self.model.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.7
        )

        return response.choices[0].message.content

    def solve(self, problem, max_depth=3, breadth=3):
        """Solve problem using tree search"""
        # Generate initial thoughts
        thoughts = self.generate_thoughts(problem, breadth)

        # Evaluate and select best
        best_thoughts = []
        for thought in thoughts:
            rating = self.evaluate_thought(thought, problem)
            best_thoughts.append((rating, thought))

        best_thoughts.sort(reverse=True)

        # Expand most promising thoughts
        for depth in range(max_depth):
            new_thoughts = []

            for rating, thought in best_thoughts[:breadth]:
                expanded = self.expand_thought(thought, problem)
                new_rating = self.evaluate_thought(expanded, problem)
                new_thoughts.append((new_rating, expanded))

            best_thoughts = sorted(new_thoughts, reverse=True)

        # Return best solution
        return best_thoughts[0][1]

# Usage
tot = TreeOfThoughts(client)
solution = tot.solve("How can we reduce traffic congestion in a city?")
print(solution)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.4 Iterative Refinement&lt;br&gt;
Concept:&lt;br&gt;
Generate answer, critique it, improve it, repeat.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def iterative_refinement(question, iterations=3):
    current_answer = ""

    for i in range(iterations):
        if i == 0:
            # Initial answer
            prompt = f"Question: {question}\n\nAnswer:"
        else:
            # Refinement
            prompt = f"""Question: {question}

Previous answer: {current_answer}

Please critique the previous answer and provide an improved version.
What's missing? What could be better?

Improved answer:"""

        response = client.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.7
        )

        current_answer = response.choices[0].message.content
        print(f"\n--- Iteration {i+1} ---")
        print(current_answer)

    return current_answer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.5 Debate and Multi-Agent Reasoning&lt;br&gt;
Concept:&lt;br&gt;
Multiple agents debate to reach better answer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class DebateSystem:
    def __init__(self, model, num_agents=3):
        self.model = model
        self.num_agents = num_agents

    def generate_initial_answers(self, question):
        """Each agent generates initial answer"""
        answers = []

        for i in range(self.num_agents):
            response = self.model.chat.completions.create(
                model="gpt-4",
                messages=[
                    {"role": "system", "content": f"You are expert debater {i+1}."},
                    {"role": "user", "content": question}
                ],
                temperature=0.8
            )
            answers.append(response.choices[0].message.content)

        return answers

    def debate_round(self, question, previous_answers):
        """One round of debate"""
        new_answers = []

        for i in range(self.num_agents):
            # Show other agents' answers
            other_answers = [ans for j, ans in enumerate(previous_answers) if j != i]

            prompt = f"""Question: {question}

Your previous answer: {previous_answers[i]}

Other experts said:
{chr(10).join(f"Expert {j+1}: {ans}" for j, ans in enumerate(other_answers))}

Considering the other perspectives, refine your answer:"""

            response = self.model.chat.completions.create(
                model="gpt-4",
                messages=[{"role": "user", "content": prompt}],
                temperature=0.7
            )

            new_answers.append(response.choices[0].message.content)

        return new_answers

    def synthesize(self, question, final_answers):
        """Synthesize final answer from debate"""
        prompt = f"""Question: {question}

After debate, the experts concluded:
{chr(10).join(f"Expert {i+1}: {ans}" for i, ans in enumerate(final_answers))}

Synthesize these perspectives into one coherent final answer:"""

        response = self.model.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.5
        )

        return response.choices[0].message.content

    def solve(self, question, rounds=2):
        """Run full debate"""
        answers = self.generate_initial_answers(question)

        for round_num in range(rounds):
            print(f"\n=== Debate Round {round_num + 1} ===")
            answers = self.debate_round(question, answers)

        final = self.synthesize(question, answers)
        return final

# Usage
debate = DebateSystem(client, num_agents=3)
answer = debate.solve("What is the most effective way to address climate change?", rounds=2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.6 Process Supervision&lt;br&gt;
Concept:&lt;br&gt;
Reward model evaluates reasoning steps, not just final answer.&lt;br&gt;
Training Process Reward Model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import torch
import torch.nn as nn

class ProcessRewardModel(nn.Module):
    def __init__(self, hidden_size=768):
        super().__init__()

        self.encoder = AutoModel.from_pretrained("bert-base-uncased")
        self.reward_head = nn.Sequential(
            nn.Linear(hidden_size, 256),
            nn.ReLU(),
            nn.Dropout(0.1),
            nn.Linear(256, 1)
        )

    def forward(self, step_text):
        # Encode reasoning step
        outputs = self.encoder(**step_text)
        pooled = outputs.pooler_output

        # Predict reward
        reward = self.reward_head(pooled)
        return reward

# Training data format
training_data = [
    {
        "step": "First, let's identify the known variables: distance = 120 km, time = 2 hours",
        "reward": 1.0  # Good step
    },
    {
        "step": "The speed is 120",
        "reward": 0.3  # Incomplete reasoning
    },
    {
        "step": "Therefore, speed = distance / time = 120 / 2 = 60 km/h",
        "reward": 1.0  # Correct step
    }
]

# Use reward model during inference
def guided_reasoning(question, reward_model, num_steps=5):
    """Generate reasoning guided by process rewards"""
    reasoning_steps = []
    current_context = question

    for step_num in range(num_steps):
        # Generate multiple possible next steps
        candidates = []
        for i in range(5):
            prompt = f"""{current_context}

Next reasoning step:"""

            response = client.chat.completions.create(
                model="gpt-4",
                messages=[{"role": "user", "content": prompt}],
                temperature=0.8,
                max_tokens=100
            )

            step = response.choices[0].message.content
            candidates.append(step)

        # Evaluate each candidate with reward model
        best_step = None
        best_reward = -float('inf')

        for step in candidates:
            reward = reward_model.evaluate(step)
            if reward &amp;gt; best_reward:
                best_reward = reward
                best_step = step

        reasoning_steps.append(best_step)
        current_context += f"\n{best_step}"

    return reasoning_steps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;25.7 Verification and Self-Correction&lt;br&gt;
Concept:&lt;br&gt;
Model verifies its own answer and corrects if needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def verify_and_correct(question, initial_answer):
    """Self-verification loop"""
    max_corrections = 3
    current_answer = initial_answer

    for attempt in range(max_corrections):
        # Verify answer
        verify_prompt = f"""Question: {question}

Proposed answer: {current_answer}

Please carefully verify this answer:
1. Check the logic
2. Check calculations
3. Check if it fully answers the question

Is this answer correct? If not, what's wrong?"""

        verification = client.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": verify_prompt}],
            temperature=0.3
        )

        verification_result = verification.choices[0].message.content

        # Check if answer is deemed correct
        if "correct" in verification_result.lower() and "not correct" not in verification_result.lower():
            print(f"Answer verified after {attempt + 1} attempt(s)")
            return current_answer

        # Generate correction
        correct_prompt = f"""Question: {question}

Current answer: {current_answer}

Verification found issues: {verification_result}

Please provide a corrected answer:"""

        correction = client.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": correct_prompt}],
            temperature=0.5
        )

        current_answer = correction.choices[0].message.content
        print(f"Correction attempt {attempt + 1}")

    return current_answer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.8 Compute-Optimal Inference&lt;br&gt;
Trading Inference Compute for Quality:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ComputeOptimalInference:
    def __init__(self, model, compute_budget):
        self.model = model
        self.compute_budget = compute_budget

    def allocate_compute(self, question_difficulty):
        """Allocate more compute to harder questions"""
        if question_difficulty &amp;lt; 0.3:
            # Easy question
            return {
                'samples': 1,
                'temperature': 0.3,
                'max_tokens': 200
            }
        elif question_difficulty &amp;lt; 0.7:
            # Medium question
            return {
                'samples': 3,
                'temperature': 0.7,
                'max_tokens': 500
            }
        else:
            # Hard question
            return {
                'samples': 5,
                'temperature': 0.9,
                'max_tokens': 1000
            }

    def estimate_difficulty(self, question):
        """Estimate question difficulty"""
        difficulty_prompt = f"""Rate the difficulty of this question from 0-1:

Question: {question}

Consider:
- Complexity of reasoning required
- Number of steps needed
- Ambiguity

Difficulty score (just the number):"""

        response = client.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": difficulty_prompt}],
            temperature=0.3,
            max_tokens=10
        )

        try:
            difficulty = float(response.choices[0].message.content.strip())
        except:
            difficulty = 0.5

        return difficulty

    def solve(self, question):
        """Solve with compute allocation based on difficulty"""
        difficulty = self.estimate_difficulty(question)
        config = self.allocate_compute(difficulty)

        print(f"Question difficulty: {difficulty:.2f}")
        print(f"Allocated compute: {config}")

        # Generate multiple samples
        answers = []
        for i in range(config['samples']):
            response = client.chat.completions.create(
                model="gpt-4",
                messages=[{"role": "user", "content": question}],
                temperature=config['temperature'],
                max_tokens=config['max_tokens']
            )
            answers.append(response.choices[0].message.content)

        # If multiple samples, use self-consistency
        if len(answers) &amp;gt; 1:
            return self.select_best_answer(answers, question)

        return answers[0]

    def select_best_answer(self, answers, question):
        """Select best from multiple answers"""
        # Could use reward model, voting, or LLM judge
        judge_prompt = f"""Question: {question}

Multiple answers were generated:
{chr(10).join(f"{i+1}. {ans}" for i, ans in enumerate(answers))}

Which answer is best? Respond with just the number:"""

        response = client.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": judge_prompt}],
            temperature=0.3
        )

        try:
            best_idx = int(response.choices[0].message.content.strip()) - 1
            return answers[best_idx]
        except:
            return answers[0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key Takeaways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test-time compute improves reasoning quality&lt;/li&gt;
&lt;li&gt;Chain-of-thought is foundation&lt;/li&gt;
&lt;li&gt;Tree of Thoughts explores multiple paths&lt;/li&gt;
&lt;li&gt;Self-consistency through multiple samples&lt;/li&gt;
&lt;li&gt;Verification and self-correction catch errors&lt;/li&gt;
&lt;li&gt;Allocate compute based on problem difficulty&lt;/li&gt;
&lt;li&gt;Future: spending 100x more inference compute for 10x better answers&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  16. Adversarial Machine Learning and Model Security
&lt;/h2&gt;

&lt;p&gt;16.1 Introduction to Adversarial Attacks&lt;br&gt;
What are Adversarial Examples:&lt;br&gt;
Inputs specifically crafted to fool ML models.&lt;br&gt;
Why This Matters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Security applications (face recognition bypass)&lt;/li&gt;
&lt;li&gt;Autonomous vehicles (stop sign manipulation)&lt;/li&gt;
&lt;li&gt;Spam filters (adversarial emails)&lt;/li&gt;
&lt;li&gt;Financial fraud detection evasion&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Types of Attacks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Evasion Attacks:Modify input at test time to avoid detection.&lt;/li&gt;
&lt;li&gt;Poisoning Attacks:Corrupt training data to degrade model.&lt;/li&gt;
&lt;li&gt;Model Extraction:Steal model by querying it.&lt;/li&gt;
&lt;li&gt;Model Inversion:Reconstruct training data from model.
16.2 Image Adversarial AttacksFast Gradient Sign Method (FGSM):
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import torch
import torch.nn.functional as F

def fgsm_attack(image, epsilon, data_grad):
    """
    Generate adversarial example using FGSM

    Args:
        image: Original image
        epsilon: Perturbation magnitude
        data_grad: Gradient of loss w.r.t. image
    """
    # Get sign of gradient
    sign_data_grad = data_grad.sign()

    # Create perturbed image
    perturbed_image = image + epsilon * sign_data_grad

    # Clip to valid image range [0, 1]
    perturbed_image = torch.clamp(perturbed_image, 0, 1)

    return perturbed_image

# Example usage
def generate_adversarial_example(model, image, label, epsilon=0.3):
    # Enable gradient tracking for image
    image.requires_grad = True

    # Forward pass
    output = model(image)
    loss = F.nll_loss(output, label)

    # Backward pass
    model.zero_grad()
    loss.backward()

    # Get gradient
    data_grad = image.grad.data

    # Generate adversarial example
    perturbed_image = fgsm_attack(image, epsilon, data_grad)

    # Test on adversarial example
    output = model(perturbed_image)
    pred = output.max(1, keepdim=True)[1]

    return perturbed_image, pred
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Projected Gradient Descent (PGD):&lt;br&gt;
More powerful iterative attack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def pgd_attack(model, images, labels, epsilon=0.3, alpha=0.01, num_iter=40):
    """
    PGD attack - iterative FGSM

    Args:
        model: Target model
        images: Clean images
        labels: True labels
        epsilon: Maximum perturbation
        alpha: Step size
        num_iter: Number of iterations
    """
    # Start with random perturbation
    perturbed_images = images.clone().detach()
    perturbed_images = perturbed_images + torch.empty_like(perturbed_images).uniform_(-epsilon, epsilon)
    perturbed_images = torch.clamp(perturbed_images, 0, 1)

    for i in range(num_iter):
        perturbed_images.requires_grad = True

        outputs = model(perturbed_images)
        loss = F.cross_entropy(outputs, labels)

        # Gradient
        loss.backward()
        data_grad = perturbed_images.grad.data

        # Update perturbation
        perturbed_images = perturbed_images.detach() + alpha * data_grad.sign()

        # Project back to epsilon ball
        perturbation = torch.clamp(perturbed_images - images, -epsilon, epsilon)
        perturbed_images = torch.clamp(images + perturbation, 0, 1)

    return perturbed_images
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Carlini-Wagner (C&amp;amp;W) Attack:&lt;br&gt;
Optimization-based attack that minimizes perturbation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def cw_attack(model, images, labels, c=1, kappa=0, max_iter=1000, learning_rate=0.01):
    """
    C&amp;amp;W L2 attack

    Args:
        model: Target model
        images: Original images
        labels: True labels
        c: Trade-off constant
        kappa: Confidence parameter
    """
    # Use tanh space for box constraints
    w = torch.zeros_like(images, requires_grad=True)
    optimizer = torch.optim.Adam([w], lr=learning_rate)

    for step in range(max_iter):
        # Convert from tanh space to image space
        perturbed = 0.5 * (torch.tanh(w) + 1)

        # Get logits
        logits = model(perturbed)

        # Get correct and max other class scores
        real = logits.gather(1, labels.unsqueeze(1)).squeeze()
        other = (logits - 1e4 * F.one_hot(labels, logits.size(1))).max(1)[0]

        # Loss: maximize other class score while minimizing perturbation
        loss1 = torch.clamp(real - other + kappa, min=0)  # Classification loss
        loss2 = torch.sum((perturbed - images) ** 2)  # L2 distance

        loss = loss2 + c * loss1

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    return 0.5 * (torch.tanh(w) + 1).detach()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.3 Text Adversarial Attacks&lt;br&gt;
Character-Level Perturbations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def text_adversarial_attack(text, model, num_perturbations=3):
    """
    Simple character-level attack on text
    """
    import random

    words = text.split()
    perturbed_text = text

    for _ in range(num_perturbations):
        # Pick random word
        word_idx = random.randint(0, len(words) - 1)
        word = words[word_idx]

        if len(word) &amp;gt; 1:
            # Character swap
            char_idx = random.randint(0, len(word) - 2)
            chars = list(word)
            chars[char_idx], chars[char_idx + 1] = chars[char_idx + 1], chars[char_idx]
            words[word_idx] = ''.join(chars)

        perturbed_text = ' '.join(words)

        # Test if attack successful
        if model_prediction_changes(model, text, perturbed_text):
            return perturbed_text

    return perturbed_text
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Semantic-Preserving Attacks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from transformers import pipeline

class SemanticTextAttack:
    def __init__(self):
        self.paraphraser = pipeline("text2text-generation", model="Vamsi/T5_Paraphrase_Paws")
        self.synonym_dict = {
            'good': ['great', 'excellent', 'nice'],
            'bad': ['terrible', 'awful', 'poor']
        }

    def word_substitution_attack(self, text, target_model):
        """Replace words with synonyms until model prediction changes"""
        words = text.split()

        for i, word in enumerate(words):
            if word.lower() in self.synonym_dict:
                for synonym in self.synonym_dict[word.lower()]:
                    words[i] = synonym
                    perturbed = ' '.join(words)

                    if self.check_prediction_change(target_model, text, perturbed):
                        return perturbed

                words[i] = word  # Reset if not successful

        return text

    def paraphrase_attack(self, text, target_model):
        """Generate paraphrases until model prediction changes"""
        paraphrases = self.paraphraser(text, num_return_sequences=5, max_length=100)

        for para in paraphrases:
            perturbed = para['generated_text']
            if self.check_prediction_change(target_model, text, perturbed):
                return perturbed

        return text

    def check_prediction_change(self, model, original, perturbed):
        """Check if perturbation changed prediction"""
        orig_pred = model(original)
        pert_pred = model(perturbed)
        return orig_pred != pert_pred
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.4 Defense Mechanisms&lt;br&gt;
Adversarial Training:&lt;br&gt;
Train on both clean and adversarial examples.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def adversarial_training(model, train_loader, num_epochs, epsilon=0.3):
    """
    Train model with adversarial examples
    """
    optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

    for epoch in range(num_epochs):
        for images, labels in train_loader:
            # Generate adversarial examples
            adversarial_images = fgsm_attack(model, images, labels, epsilon)

            # Combine clean and adversarial
            combined_images = torch.cat([images, adversarial_images])
            combined_labels = torch.cat([labels, labels])

            # Train on both
            optimizer.zero_grad()
            outputs = model(combined_images)
            loss = F.cross_entropy(outputs, combined_labels)
            loss.backward()
            optimizer.step()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Defensive Distillation:&lt;br&gt;
Train model at high temperature, then distill at lower temperature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def defensive_distillation(teacher_model, student_model, train_loader, temperature=100):
    """
    Defensive distillation to make model more robust
    """
    # Step 1: Train teacher at high temperature
    teacher_optimizer = torch.optim.Adam(teacher_model.parameters())

    for images, labels in train_loader:
        outputs = teacher_model(images) / temperature
        loss = F.cross_entropy(outputs, labels)

        teacher_optimizer.zero_grad()
        loss.backward()
        teacher_optimizer.step()

    # Step 2: Distill to student
    student_optimizer = torch.optim.Adam(student_model.parameters())

    for images, labels in train_loader:
        # Get teacher's soft labels
        with torch.no_grad():
            teacher_outputs = F.softmax(teacher_model(images) / temperature, dim=1)

        # Train student to match
        student_outputs = F.log_softmax(student_model(images) / temperature, dim=1)
        loss = F.kl_div(student_outputs, teacher_outputs, reduction='batchmean')

        student_optimizer.zero_grad()
        loss.backward()
        student_optimizer.step()

    return student_model
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Input Transformation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def input_transformation_defense(image, model):
    """
    Apply transformations to remove adversarial perturbations
    """
    import torchvision.transforms as transforms

    # Possible transformations
    transforms_list = [
        transforms.GaussianBlur(kernel_size=3),
        transforms.RandomCrop(size=image.shape[-2:], padding=4),
        transforms.ColorJitter(brightness=0.1, contrast=0.1)
    ]

    # Apply random transformation
    transform = random.choice(transforms_list)
    cleaned_image = transform(image)

    # Get prediction
    output = model(cleaned_image)

    return output
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Certified Robustness:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from torch import nn

class CertifiedModel(nn.Module):
    """
    Model with certified robustness using randomized smoothing
    """
    def __init__(self, base_model, noise_std=0.25):
        super().__init__()
        self.base_model = base_model
        self.noise_std = noise_std

    def forward(self, x, num_samples=100):
        """
        Predict using randomized smoothing
        """
        # Generate noisy copies
        batch_size = x.size(0)
        predictions = []

        for _ in range(num_samples):
            noise = torch.randn_like(x) * self.noise_std
            noisy_x = x + noise

            with torch.no_grad():
                pred = self.base_model(noisy_x)
            predictions.append(pred)

        # Average predictions
        avg_pred = torch.stack(predictions).mean(dim=0)

        return avg_pred
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.5 Model Extraction Attacks&lt;br&gt;
Query-Based Extraction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ModelExtraction:
    def __init__(self, target_model):
        self.target_model = target_model
        self.queries = []
        self.responses = []

    def query(self, input_data):
        """Query target model"""
        output = self.target_model(input_data)

        self.queries.append(input_data)
        self.responses.append(output)

        return output

    def train_substitute_model(self, substitute_model, num_queries=10000):
        """
        Train substitute model to mimic target
        """
        optimizer = torch.optim.Adam(substitute_model.parameters())

        # Generate synthetic queries
        for i in range(num_queries):
            # Sample random input
            synthetic_input = torch.randn(1, *input_shape)

            # Get target prediction
            with torch.no_grad():
                target_output = self.query(synthetic_input)

            # Train substitute to match
            substitute_output = substitute_model(synthetic_input)
            loss = F.mse_loss(substitute_output, target_output)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        return substitute_model
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Defense Against Model Extraction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def query_detection(query_history, window_size=100, threshold=0.8):
    """
    Detect suspicious query patterns
    """
    if len(query_history) &amp;lt; window_size:
        return False

    recent_queries = query_history[-window_size:]

    # Check for similar queries (potential extraction)
    similarities = []
    for i in range(len(recent_queries)-1):
        sim = cosine_similarity(recent_queries[i], recent_queries[i+1])
        similarities.append(sim)

    avg_similarity = np.mean(similarities)

    if avg_similarity &amp;gt; threshold:
        # Suspicious pattern detected
        return True

    return False

def add_noise_to_output(output, noise_level=0.01):
    """
    Add noise to outputs to prevent exact extraction
    """
    noise = torch.randn_like(output) * noise_level
    return output + noise
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.6 Privacy Attacks&lt;br&gt;
Membership Inference:&lt;br&gt;
Determine if specific data point was in training set.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MembershipInferenceAttack:
    def __init__(self, target_model):
        self.target_model = target_model
        self.attack_model = self.build_attack_model()

    def build_attack_model(self):
        """
        Binary classifier: member vs non-member
        """
        return nn.Sequential(
            nn.Linear(10, 64),  # Assuming 10-class classification
            nn.ReLU(),
            nn.Linear(64, 32),
            nn.ReLU(),
            nn.Linear(32, 1),
            nn.Sigmoid()
        )

    def train_attack_model(self, member_data, non_member_data):
        """
        Train attack model to distinguish members
        """
        optimizer = torch.optim.Adam(self.attack_model.parameters())

        for data, label in member_data:
            # Get target model's prediction
            with torch.no_grad():
                prediction = self.target_model(data)

            # Train attack model (label=1 for member)
            attack_output = self.attack_model(prediction)
            loss = F.binary_cross_entropy(attack_output, torch.ones_like(attack_output))

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

        for data, label in non_member_data:
            with torch.no_grad():
                prediction = self.target_model(data)

            attack_output = self.attack_model(prediction)
            loss = F.binary_cross_entropy(attack_output, torch.zeros_like(attack_output))

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

    def infer_membership(self, data):
        """
        Infer if data was in training set
        """
        with torch.no_grad():
            prediction = self.target_model(data)
            membership_prob = self.attack_model(prediction)

        return membership_prob &amp;gt; 0.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Defense: Differential Privacy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from opacus import PrivacyEngine

def train_with_differential_privacy(model, train_loader, epsilon=1.0, delta=1e-5):
    """
    Train model with differential privacy guarantees
    """
    optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

    # Attach privacy engine
    privacy_engine = PrivacyEngine()

    model, optimizer, train_loader = privacy_engine.make_private(
        module=model,
        optimizer=optimizer,
        data_loader=train_loader,
        noise_multiplier=1.1,
        max_grad_norm=1.0,
    )

    # Train as normal
    for epoch in range(num_epochs):
        for data, labels in train_loader:
            optimizer.zero_grad()
            outputs = model(data)
            loss = F.cross_entropy(outputs, labels)
            loss.backward()
            optimizer.step()

        # Check privacy budget
        epsilon_spent = privacy_engine.get_epsilon(delta)
        print(f"Epoch {epoch}, ε = {epsilon_spent:.2f}")

        if epsilon_spent &amp;gt; epsilon:
            print("Privacy budget exceeded, stopping training")
            break

    return model
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.7 Backdoor Attacks&lt;br&gt;
Inserting Backdoor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def poison_training_data(clean_data, trigger_pattern, target_label, poison_rate=0.1):
    """
    Insert backdoor trigger into training data
    """
    poisoned_data = []

    for image, label in clean_data:
        if random.random() &amp;lt; poison_rate:
            # Add trigger
            poisoned_image = image.clone()
            poisoned_image[:, -5:, -5:] = trigger_pattern  # Add pattern to corner

            # Change label to target
            poisoned_data.append((poisoned_image, target_label))
        else:
            poisoned_data.append((image, label))

    return poisoned_data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Defense: Activation Clustering:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def detect_backdoor(model, clean_data, suspicious_data):
    """
    Detect backdoored samples using activation clustering
    """
    from sklearn.cluster import KMeans

    # Get activations for clean data
    clean_activations = []
    with torch.no_grad():
        for data, _ in clean_data:
            activation = model.get_intermediate_activation(data)
            clean_activations.append(activation)

    # Get activations for suspicious data
    suspicious_activations = []
    with torch.no_grad():
        for data, _ in suspicious_data:
            activation = model.get_intermediate_activation(data)
            suspicious_activations.append(activation)

    # Cluster activations
    all_activations = clean_activations + suspicious_activations
    kmeans = KMeans(n_clusters=2)
    clusters = kmeans.fit_predict(all_activations)

    # Check if suspicious samples form separate cluster
    suspicious_cluster = clusters[len(clean_activations):]

    if np.mean(suspicious_cluster) &amp;gt; 0.8 or np.mean(suspicious_cluster) &amp;lt; 0.2:
        return True  # Likely backdoor detected

    return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;16.8 Secure Model Deployment&lt;br&gt;
API Rate Limiting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from flask import Flask, request, jsonify
from functools import wraps
import time

app = Flask(__name__)

# Rate limiting
request_counts = {}
RATE_LIMIT = 100  # requests per minute
RATE_WINDOW = 60  # seconds

def rate_limit(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        client_ip = request.remote_addr
        current_time = time.time()

        # Clean old entries
        if client_ip in request_counts:
            request_counts[client_ip] = [
                t for t in request_counts[client_ip]
                if current_time - t &amp;lt; RATE_WINDOW
            ]
        else:
            request_counts[client_ip] = []

        # Check rate limit
        if len(request_counts[client_ip]) &amp;gt;= RATE_LIMIT:
            return jsonify({'error': 'Rate limit exceeded'}), 429

        # Record request
        request_counts[client_ip].append(current_time)

        return f(*args, **kwargs)

    return decorated_function

@app.route('/predict', methods=['POST'])
@rate_limit
def predict():
    data = request.json
    # ... model prediction
    return jsonify({'prediction': result})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Input Validation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def validate_input(input_data, expected_shape, expected_range):
    """
    Validate input before feeding to model
    """
    # Check shape
    if input_data.shape != expected_shape:
        raise ValueError(f"Invalid input shape: {input_data.shape}")

    # Check range
    if input_data.min() &amp;lt; expected_range[0] or input_data.max() &amp;gt; expected_range[1]:
        raise ValueError(f"Input values out of range: [{input_data.min()}, {input_data.max()}]")

    # Check for NaN or Inf
    if torch.isnan(input_data).any() or torch.isinf(input_data).any():
        raise ValueError("Input contains NaN or Inf values")

    return True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Audit Logging:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import logging
import json
from datetime import datetime

class ModelAuditLogger:
    def __init__(self, log_file='model_audit.log'):
        self.logger = logging.getLogger('model_audit')
        self.logger.setLevel(logging.INFO)

        handler = logging.FileHandler(log_file)
        handler.setFormatter(logging.Formatter('%(message)s'))
        self.logger.addHandler(handler)

    def log_prediction(self, user_id, input_hash, prediction, confidence, timestamp=None):
        """
        Log every model prediction for audit trail
        """
        if timestamp is None:
            timestamp = datetime.now().isoformat()

        log_entry = {
            'timestamp': timestamp,
            'user_id': user_id,
            'input_hash': input_hash,  # Don't log raw input for privacy
            'prediction': prediction,
            'confidence': confidence,
            'model_version': 'v1.2.3'
        }

        self.logger.info(json.dumps(log_entry))

    def log_anomaly(self, anomaly_type, details):
        """
        Log suspicious activity
        """
        log_entry = {
            'timestamp': datetime.now().isoformat(),
            'type': 'anomaly',
            'anomaly_type': anomaly_type,
            'details': details
        }

        self.logger.warning(json.dumps(log_entry))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key Takeaways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adversarial attacks are real security threats&lt;/li&gt;
&lt;li&gt;Defense mechanisms exist but none are perfect&lt;/li&gt;
&lt;li&gt;Adversarial training improves robustness&lt;/li&gt;
&lt;li&gt;Privacy attacks can reveal training data&lt;/li&gt;
&lt;li&gt;Differential privacy provides formal guarantees&lt;/li&gt;
&lt;li&gt;Secure deployment requires multiple layers&lt;/li&gt;
&lt;li&gt;Monitor for suspicious queries&lt;/li&gt;
&lt;li&gt;Always validate inputs&lt;/li&gt;
&lt;li&gt;Log all predictions for audit&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  17. Cost Optimization and Resource Management
&lt;/h2&gt;

&lt;p&gt;17.1 Understanding ML Costs&lt;br&gt;
Cost Components:&lt;br&gt;
Training Costs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compute (GPUs/TPUs)&lt;/li&gt;
&lt;li&gt;Storage (datasets)&lt;/li&gt;
&lt;li&gt;Data processing&lt;/li&gt;
&lt;li&gt;Experiment tracking&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Inference Costs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Model serving infrastructure&lt;/li&gt;
&lt;li&gt;API calls&lt;/li&gt;
&lt;li&gt;Bandwidth&lt;/li&gt;
&lt;li&gt;Cold start times (serverless)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Data Costs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data storage&lt;/li&gt;
&lt;li&gt;Data transfer&lt;/li&gt;
&lt;li&gt;Data labeling&lt;/li&gt;
&lt;li&gt;Data pipeline compute&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Typical Cost Breakdown:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Small Startup:
- Training: $500-2K/month
- Inference: $1K-5K/month
- Data: $500-1K/month

Medium Company:
- Training: $10K-50K/month
- Inference: $20K-100K/month
- Data: $5K-20K/month

Large Enterprise:
- Training: $100K-1M+/month
- Inference: $500K-5M+/month
- Data: $50K-500K+/month
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;17.2 Training Cost Optimization&lt;br&gt;
Strategy 1: Use Spot/Preemptible Instances:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# AWS Spot instance example
import boto3

ec2 = boto3.client('ec2')

def request_spot_instance(instance_type='g4dn.xlarge', max_price='0.50'):
    """
    Request spot instance for training
    Can save 60-90% compared to on-demand
    """
    response = ec2.request_spot_instances(
        SpotPrice=max_price,
        InstanceCount=1,
        LaunchSpecification={
            'ImageId': 'ami-xxxxx',  # Deep learning AMI
            'InstanceType': instance_type,
            'KeyName': 'my-key',
            'SecurityGroups': ['ml-training']
        }
    )

    return response['SpotInstanceRequests'][0]['SpotInstanceRequestId']

# Handle interruptions with checkpointing
def train_with_checkpointing(model, dataloader, checkpoint_dir='checkpoints'):
    """
    Save checkpoints to resume if spot instance terminated
    """
    start_epoch = 0

    # Load checkpoint if exists
    if os.path.exists(f'{checkpoint_dir}/latest.pth'):
        checkpoint = torch.load(f'{checkpoint_dir}/latest.pth')
        model.load_state_dict(checkpoint['model_state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
        start_epoch = checkpoint['epoch']

    for epoch in range(start_epoch, num_epochs):
        for batch in dataloader:
            # Training step
            loss = train_step(model, batch)

        # Save checkpoint every epoch
        torch.save({
            'epoch': epoch,
            'model_state_dict': model.state_dict(),
            'optimizer_state_dict': optimizer.state_dict(),
            'loss': loss
        }, f'{checkpoint_dir}/latest.pth')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strategy 2: Mixed Precision Training:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from torch.cuda.amp import autocast, GradScaler

def train_with_mixed_precision(model, dataloader):
    """
    2x faster training, half memory usage
    """
    scaler = GradScaler()
    optimizer = torch.optim.Adam(model.parameters())

    for data, labels in dataloader:
        optimizer.zero_grad()

        # Forward pass in mixed precision
        with autocast():
            outputs = model(data)
            loss = F.cross_entropy(outputs, labels)

        # Backward pass with gradient scaling
        scaler.scale(loss).backward()
        scaler.step(optimizer)
        scaler.update()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strategy 3: Gradient Accumulation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def train_with_gradient_accumulation(model, dataloader, accumulation_steps=4):
    """
    Simulate larger batch size without memory cost
    """
    optimizer = torch.optim.Adam(model.parameters())

    for i, (data, labels) in enumerate(dataloader):
        # Forward pass
        outputs = model(data)
        loss = F.cross_entropy(outputs, labels)

        # Normalize loss by accumulation steps
        loss = loss / accumulation_steps
        loss.backward()

        # Only step every accumulation_steps batches
        if (i + 1) % accumulation_steps == 0:
            optimizer.step()
            optimizer.zero_grad()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strategy 4: Early Stopping:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EarlyStopping:
    def __init__(self, patience=5, min_delta=0.001):
        self.patience = patience
        self.min_delta = min_delta
        self.counter = 0
        self.best_loss = None
        self.early_stop = False

    def __call__(self, val_loss):
        if self.best_loss is None:
            self.best_loss = val_loss
        elif val_loss &amp;gt; self.best_loss - self.min_delta:
            self.counter += 1
            if self.counter &amp;gt;= self.patience:
                self.early_stop = True
        else:
            self.best_loss = val_loss
            self.counter = 0

# Usage
early_stopping = EarlyStopping(patience=5)

for epoch in range(max_epochs):
    train_loss = train_epoch(model, train_loader)
    val_loss = validate(model, val_loader)

    early_stopping(val_loss)

    if early_stopping.early_stop:
        print(f"Early stopping at epoch {epoch}")
        break  # Save training costs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strategy 5: Hyperparameter Optimization Efficiency:&lt;br&gt;
&lt;/p&gt;

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

def objective(trial):
    # Sample hyperparameters
    lr = trial.suggest_loguniform('lr', 1e-5, 1e-1)
    batch_size = trial.suggest_categorical('batch_size', [16, 32, 64])

    # Train for few epochs only
    model = create_model()
    val_acc = quick_train(model, lr, batch_size, epochs=3)

    return val_acc

# Optuna with pruning (stops bad trials early)
study = optuna.create_study(
    direction='maximize',
    pruner=optuna.pruners.MedianPruner()
)

study.optimize(objective, n_trials=50)  # Much cheaper than grid search
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;17.3 Inference Cost Optimization&lt;br&gt;
Strategy 1: Model Quantization:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import torch.quantization

def quantize_model(model, example_inputs):
    """
    Reduce model size by 4x, inference 2-4x faster
    """
    # Prepare for quantization
    model.qconfig = torch.quantization.get_default_qconfig('fbgemm')
    torch.quantization.prepare(model, inplace=True)

    # Calibrate with sample data
    model(example_inputs)

    # Convert to quantized model
    torch.quantization.convert(model, inplace=True)

    return model

# Compare costs
original_size = get_model_size(model)
quantized_size = get_model_size(quantized_model)
print(f"Size reduction: {original_size / quantized_size:.1f}x")

# Latency comparison
original_latency = benchmark_latency(model)
quantized_latency = benchmark_latency(quantized_model)
print(f"Speedup: {original_latency / quantized_latency:.1f}x")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strategy 2: Batch Inference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class BatchPredictor:
    def __init__(self, model, max_batch_size=32, max_wait_time=0.1):
        self.model = model
        self.max_batch_size = max_batch_size
        self.max_wait_time = max_wait_time
        self.queue = []
        self.results = {}

    async def predict(self, request_id, input_data):
        """
        Batch multiple requests for efficient inference
        """
        # Add to queue
        self.queue.append((request_id, input_data))

        # Wait for batch to fill or timeout
        start_time = time.time()
        while len(self.queue) &amp;lt; self.max_batch_size:
            if time.time() - start_time &amp;gt; self.max_wait_time:
                break
            await asyncio.sleep(0.01)

        # Process batch
        if request_id in [r[0] for r in self.queue[:self.max_batch_size]]:
            batch_requests = self.queue[:self.max_batch_size]
            self.queue = self.queue[self.max_batch_size:]

            # Run batch inference
            batch_inputs = torch.stack([r[1] for r in batch_requests])
            with torch.no_grad():
                batch_outputs = self.model(batch_inputs)

            # Store results
            for (rid, _), output in zip(batch_requests, batch_outputs):
                self.results[rid] = output

        # Return result
        return self.results.pop(request_id)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strategy 3: Caching:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from functools import lru_cache
import hashlib
import redis

class ModelCache:
    def __init__(self, redis_client, ttl=3600):
        self.redis = redis_client
        self.ttl = ttl

    def get_cache_key(self, input_data):
        """Generate deterministic cache key"""
        input_hash = hashlib.sha256(
            input_data.cpu().numpy().tobytes()
        ).hexdigest()
        return f"prediction:{input_hash}"

    def get_cached_prediction(self, input_data):
        """Check cache before running model"""
        key = self.get_cache_key(input_data)
        cached = self.redis.get(key)

        if cached:
            return json.loads(cached)

        return None

    def cache_prediction(self, input_data, prediction):
        """Store prediction in cache"""
        key = self.get_cache_key(input_data)
        self.redis.setex(
            key,
            self.ttl,
            json.dumps(prediction.tolist())
        )

    def predict_with_cache(self, model, input_data):
        """Predict with caching"""
        # Check cache
        cached = self.get_cached_prediction(input_data)
        if cached is not None:
            return cached

        # Run model
        with torch.no_grad():
            prediction = model(input_data)

        # Cache result
        self.cache_prediction(input_data, prediction)

        return prediction
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strategy 4: Model Distillation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def distill_model(large_model, small_model, dataloader, temperature=3.0):
    """
    Train small model to mimic large model
    Cheaper inference, similar performance
    """
    optimizer = torch.optim.Adam(small_model.parameters())

    for data, _ in dataloader:
        # Get teacher predictions
        with torch.no_grad():
            teacher_logits = large_model(data)
            soft_targets = F.softmax(teacher_logits / temperature, dim=1)

        # Train student
        student_logits = small_model(data)
        student_log_probs = F.log_softmax(student_logits / temperature, dim=1)

        # Distillation loss
        loss = F.kl_div(student_log_probs, soft_targets, reduction='batchmean')
        loss = loss * (temperature ** 2)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    return small_model

# Cost comparison
large_model_cost_per_request = 0.001  # $0.001
small_model_cost_per_request = 0.0001  # $0.0001

requests_per_month = 1_000_000

large_model_monthly_cost = large_model_cost_per_request * requests_per_month
small_model_monthly_cost = small_model_cost_per_request * requests_per_month

print(f"Large model: ${large_model_monthly_cost:,.2f}/month")
print(f"Small model: ${small_model_monthly_cost:,.2f}/month")
print(f"Savings: ${large_model_monthly_cost - small_model_monthly_cost:,.2f}/month")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;17.4 Data Cost Optimization&lt;br&gt;
Strategy 1: Data Sampling:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def smart_data_sampling(full_dataset, sample_rate=0.1, method='stratified'):
    """
    Train on subset of data with minimal performance loss
    """
    if method == 'stratified':
        # Maintain class distribution
        from sklearn.model_selection import train_test_split
        sample, _ = train_test_split(
            full_dataset,
            train_size=sample_rate,
            stratify=full_dataset.labels
        )
    elif method == 'uncertainty':
        # Sample high-uncertainty examples
        uncertainties = calculate_uncertainties(model, full_dataset)
        top_uncertain_indices = np.argsort(uncertainties)[-int(len(full_dataset) * sample_rate):]
        sample = full_dataset[top_uncertain_indices]

    return sample
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strategy 2: Data Deduplication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def deduplicate_dataset(dataset, similarity_threshold=0.95):
    """
    Remove duplicate/near-duplicate samples
    Reduces storage and training costs
    """
    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.metrics.pairwise import cosine_similarity

    # Convert to feature vectors
    vectorizer = TfidfVectorizer()
    vectors = vectorizer.fit_transform(dataset.texts)

    # Find duplicates
    keep_indices = []
    for i in range(len(dataset)):
        is_duplicate = False
        for j in keep_indices:
            similarity = cosine_similarity(vectors[i], vectors[j])[0][0]
            if similarity &amp;gt; similarity_threshold:
                is_duplicate = True
                break

        if not is_duplicate:
            keep_indices.append(i)

    deduplicated = dataset[keep_indices]

    print(f"Removed {len(dataset) - len(deduplicated)} duplicates")
    print(f"Cost savings: {(1 - len(deduplicated)/len(dataset)) * 100:.1f}%")

    return deduplicated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Strategy 3: Efficient Data Storage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pandas as pd

def optimize_data_storage(df):
    """
    Reduce storage costs through compression and type optimization
    """
    # Convert to optimal dtypes
    for col in df.columns:
        col_type = df[col].dtype

        if col_type == 'object':
            # Try converting to category
            if df[col].nunique() / len(df) &amp;lt; 0.5:
                df[col] = df[col].astype('category')

        elif col_type == 'int64':
            # Downcast integers
            df[col] = pd.to_numeric(df[col], downcast='integer')

        elif col_type == 'float64':
            # Downcast floats
            df[col] = pd.to_numeric(df[col], downcast='float')

    # Save with compression
    df.to_parquet('data.parquet', compression='snappy')

    # Compare sizes
    csv_size = df.to_csv().encode('utf-8').__sizeof__()
    parquet_size = os.path.getsize('data.parquet')

    print(f"CSV size: {csv_size / 1e6:.2f} MB")
    print(f"Parquet size: {parquet_size / 1e6:.2f} MB")
    print(f"Compression: {csv_size / parquet_size:.1f}x")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;17.5 Cloud Cost Management&lt;br&gt;
Cost Monitoring:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import boto3
from datetime import datetime, timedelta

class AWSCostMonitor:
    def __init__(self):
        self.ce_client = boto3.client('ce')

    def get_daily_costs(self, days=7):
        """Get costs for last N days"""
        end_date = datetime.now().date()
        start_date = end_date - timedelta(days=days)

        response = self.ce_client.get_cost_and_usage(
            TimePeriod={
                'Start': str(start_date),
                'End': str(end_date)
            },
            Granularity='DAILY',
            Metrics=['UnblendedCost'],
            GroupBy=[
                {'Type': 'SERVICE', 'Key': 'SERVICE'}
            ]
        )

        return response['ResultsByTime']

    def set_budget_alert(self, budget_amount, email):
        """Set alert when costs exceed budget"""
        budgets_client = boto3.client('budgets')

        budgets_client.create_budget(
            AccountId='123456789',
            Budget={
                'BudgetName': 'ML Training Budget',
                'BudgetLimit': {
                    'Amount': str(budget_amount),
                    'Unit': 'USD'
                },
                'TimeUnit': 'MONTHLY',
                'BudgetType': 'COST'
            },
            NotificationsWithSubscribers=[
                {
                    'Notification': {
                        'NotificationType': 'ACTUAL',
                        'ComparisonOperator': 'GREATER_THAN',
                        'Threshold': 80.0,
                        'ThresholdType': 'PERCENTAGE'
                    },
                    'Subscribers': [
                        {
                            'SubscriptionType': 'EMAIL',
                            'Address': email
                        }
                    ]
                }
            ]
        )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Auto-Shutdown for Idle Resources:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def auto_shutdown_idle_instances(idle_threshold_hours=2):
    """
    Shutdown instances that have been idle
    """
    ec2 = boto3.client('ec2')
    cloudwatch = boto3.client('cloudwatch')

    # Get all running instances
    instances = ec2.describe_instances(
        Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]
    )

    for reservation in instances['Reservations']:
        for instance in reservation['Instances']:
            instance_id = instance['InstanceId']

            # Check CPU utilization
            response = cloudwatch.get_metric_statistics(
                Namespace='AWS/EC2',
                MetricName='CPUUtilization',
                Dimensions=[{'Name': 'InstanceId', 'Value': instance_id}],
                StartTime=datetime.now() - timedelta(hours=idle_threshold_hours),
                EndTime=datetime.now(),
                Period=3600,
                Statistics=['Average']
            )

            avg_cpu = np.mean([d['Average'] for d in response['Datapoints']])

            if avg_cpu &amp;lt; 5:  # Less than 5% CPU
                print(f"Stopping idle instance: {instance_id}")
                ec2.stop_instances(InstanceIds=[instance_id])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;AI/ML is a rapidly evolving field. Continuous learning is not optional - it's essential.The field of AI/ML is challenging but incredibly rewarding. You're entering at an exciting time - LLMs and modern AI have opened countless opportunities.&lt;br&gt;
Mindset:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Embrace continuous learning&lt;/li&gt;
&lt;li&gt;Don't fear complexity&lt;/li&gt;
&lt;li&gt;Start small, build up&lt;/li&gt;
&lt;li&gt;Learn by doing&lt;/li&gt;
&lt;li&gt;Share knowledge&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Avoiding Overwhelm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Focus on fundamentals first&lt;/li&gt;
&lt;li&gt;One concept at a time&lt;/li&gt;
&lt;li&gt;Build as you learn&lt;/li&gt;
&lt;li&gt;Don't chase every trend&lt;/li&gt;
&lt;li&gt;Depth over breadth initially&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Everyone starts as a beginner&lt;/li&gt;
&lt;li&gt;Confusion is part of learning&lt;/li&gt;
&lt;li&gt;Projects teach more than theory&lt;/li&gt;
&lt;li&gt;Community helps you grow&lt;/li&gt;
&lt;li&gt;Persistence beats talent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The difference between beginners and experts:&lt;br&gt;
Experts have failed more times and learned from those failures.&lt;br&gt;
Your advantage:&lt;br&gt;
You're starting now, in 2026, with access to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Powerful pre-trained models&lt;/li&gt;
&lt;li&gt;Comprehensive frameworks&lt;/li&gt;
&lt;li&gt;Active communities&lt;/li&gt;
&lt;li&gt;Abundant resources&lt;/li&gt;
&lt;li&gt;Clear career paths&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Start today.&lt;br&gt;
Pick one concept from this guide. Learn it deeply. Build something with it. Share your learning. Repeat.&lt;br&gt;
The journey of a thousand miles begins with a single step. You've taken that step by reading this guide. Now take the next one.&lt;br&gt;
Good luck on your AI/ML journey!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
