<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>Real Python</title>
  <link href="https://realpython.com/atom.xml" rel="self"/>
  <link href="https://realpython.com/"/>
  <updated>2024-05-14T14:00:00+00:00</updated>
  <id>https://realpython.com/</id>
  <author>
    <name>Real Python</name>
  </author>

  
    <entry>
      <title>HTML and CSS Foundations for Python Developers</title>
      <id>https://realpython.com/courses/html-css-foundations/</id>
      <link href="https://realpython.com/courses/html-css-foundations/"/>
      <updated>2024-05-14T14:00:00+00:00</updated>
      <summary>There&#x27;s no way around HTML and CSS when you want to build web apps. Even if you&#x27;re not aiming to become a web developer, knowing the basics of HTML and CSS will help you understand the Web better. In this video course, you&#x27;ll get an introduction to HTML and CSS for Python programmers.</summary>
      <content type="html">
        &lt;p&gt;When you want to build websites as a Python programmer, there&amp;rsquo;s no way around HTML and CSS.
Almost every website on the Internet is built with &lt;strong&gt;HTML markup&lt;/strong&gt; to structure the page.
To make a website look nice, you can style HTML with &lt;strong&gt;CSS&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re interested in web development with Python, then knowing HTML and CSS will help you understand web frameworks like &lt;strong&gt;Django&lt;/strong&gt; and &lt;strong&gt;Flask&lt;/strong&gt; better.
But even if you&amp;rsquo;re just getting started with Python, HTML and CSS can enable you to create small websites to impress your friends.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course, you&amp;rsquo;ll learn how to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Structure a basic &lt;strong&gt;HTML&lt;/strong&gt; file&lt;/li&gt;
&lt;li&gt;View and inspect HTML in your &lt;strong&gt;browser&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Insert &lt;strong&gt;images&lt;/strong&gt; and page &lt;strong&gt;links&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Style a website with &lt;strong&gt;CSS&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Format HTML with &lt;strong&gt;accessibility&lt;/strong&gt; in mind&lt;/li&gt;
&lt;li&gt;Use Python to &lt;strong&gt;write and parse&lt;/strong&gt; HTML code&lt;/li&gt;
&lt;/ul&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>What Is the __pycache__ Folder in Python?</title>
      <id>https://realpython.com/python-pycache/</id>
      <link href="https://realpython.com/python-pycache/"/>
      <updated>2024-05-13T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll explore Python&#x27;s __pycache__ folder. You&#x27;ll learn about when and why the interpreter creates these folders, and you&#x27;ll customize their default behavior. Finally, you&#x27;ll take a look under the hood of the cached .pyc files.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;When you develop a self-contained Python script, you might not notice anything unusual about your directory structure. However, as soon as your project becomes more complex, you’ll often decide to extract parts of the functionality into additional &lt;a href=&quot;https://realpython.com/python-modules-packages/&quot;&gt;modules or packages&lt;/a&gt;. That’s when you may start to see a &lt;code&gt;__pycache__&lt;/code&gt; folder appearing out of nowhere next to your source files in seemingly random places:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;project/
│
├── mathematics/
│   │
&lt;span class=&quot;hll&quot;&gt;│   ├── __pycache__/
&lt;/span&gt;│   │
│   ├── arithmetic/
│   │   ├── __init__.py
│   │   ├── add.py
│   │   └── sub.py
│   │
│   ├── geometry/
│   │   │
&lt;span class=&quot;hll&quot;&gt;│   │   ├── __pycache__/
&lt;/span&gt;│   │   │
│   │   ├── __init__.py
│   │   └── shapes.py
│   │
│   └── __init__.py
│
└── calculator.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Notice that the &lt;code&gt;__pycache__&lt;/code&gt; folder can be present at different levels in your project’s directory tree when you have multiple subpackages nested in one another. At the same time, other packages or folders with your Python source files may not contain this mysterious cache directory.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To maintain a cleaner workspace, many &lt;a href=&quot;https://realpython.com/python-ides-code-editors-guide/&quot;&gt;Python IDEs and code editors&lt;/a&gt; are configured out-of-the-box to hide the &lt;code&gt;__pycache__&lt;/code&gt; folders from you, even if those folders exist on your file system.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You may encounter a similar situation after you clone a remote &lt;a href=&quot;https://realpython.com/python-git-github-intro/&quot;&gt;Git repository&lt;/a&gt; with a Python project and run the underlying code. &lt;strong&gt;So, what causes the &lt;code&gt;__pycache__&lt;/code&gt; folder to appear, and for what purpose?&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/python-pycache-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-pycache-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that shows you how to work with the &lt;strong&gt;pycache&lt;/strong&gt; folder in Python.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;container border rounded text-wrap-pretty my-3&quot;&gt;

  &lt;p class=&quot;my-3&quot;&gt;&lt;mark class=&quot;marker-highlight&quot;&gt;&lt;strong&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@quiz&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt; Take the Quiz:&lt;/strong&gt;&lt;/mark&gt; Test your knowledge with our interactive “What Is the __pycache__ Folder in Python?” quiz. You’ll receive a score upon completion to help you track your learning progress:&lt;/p&gt;

  &lt;hr&gt;

  &lt;div class=&quot;row my-3&quot;&gt;
    &lt;div class=&quot;col-xs-12 col-sm-4 col-md-3 align-self-center&quot;&gt;

      &lt;a href=&quot;/quizzes/python-pycache/&quot; tabindex=&quot;-1&quot;&gt;
        &lt;div class=&quot;embed-responsive embed-responsive-16by9&quot;&gt;

            &lt;img class=&quot;card-img-top m-0 p-0 embed-responsive-item rounded&quot; style=&quot;object-fit: contain; background: #aae4b1;&quot; alt=&quot;What Is the __pycache__ Folder in Python?&quot; src=&quot;https://files.realpython.com/media/What-is-__pycache___Watermarked.0e2367687d70.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/What-is-__pycache___Watermarked.0e2367687d70.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/What-is-__pycache___Watermarked.0e2367687d70.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/What-is-__pycache___Watermarked.0e2367687d70.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/What-is-__pycache___Watermarked.0e2367687d70.jpg 1920w&quot; sizes=&quot;(min-width: 1200px) 142px, (min-width: 1000px) 122px, (min-width: 780px) 112px, (min-width: 580px) 139px, calc(100vw - 62px)&quot;&gt;


          &lt;div class=&quot;card-img-overlay d-flex align-items-center&quot;&gt;
            &lt;div class=&quot;mx-auto&quot;&gt;
              &lt;span class=&quot;text-light&quot; style=&quot;opacity: 0.90;&quot;&gt;&lt;span class=&quot;icon baseline scale2x&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@quiz&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/a&gt;

    &lt;/div&gt;
    &lt;div class=&quot;col&quot;&gt;
      &lt;div class=&quot;mt-3 d-md-none&quot;&gt;&lt;/div&gt; 
      &lt;p class=&quot;small text-muted mb-0&quot;&gt;&lt;strong&gt;Interactive Quiz&lt;/strong&gt;&lt;/p&gt;
      &lt;a href=&quot;/quizzes/python-pycache/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;What Is the __pycache__ Folder in Python?&lt;/span&gt;&lt;/a&gt; 
      &lt;p class=&quot;text-muted mb-0 small&quot;&gt;In this quiz, you&#x27;ll have the opportunity to test your knowledge of the __pycache__ folder, including when, where, and why Python creates these folders.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;in-short-it-makes-importing-python-modules-faster&quot;&gt;In Short: It Makes Importing Python Modules Faster&lt;a class=&quot;headerlink&quot; href=&quot;#in-short-it-makes-importing-python-modules-faster&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Even though Python is an &lt;a href=&quot;https://en.wikipedia.org/wiki/Interpreter_(computing)&quot;&gt;interpreted programming language&lt;/a&gt;, its interpreter doesn’t operate directly on your Python code, which would be very slow. Instead, when you &lt;a href=&quot;https://realpython.com/run-python-scripts/&quot;&gt;run a Python script&lt;/a&gt; or &lt;a href=&quot;https://realpython.com/python-import/&quot;&gt;import&lt;/a&gt; a Python module, the interpreter compiles your high-level Python source code into &lt;a href=&quot;https://en.wikipedia.org/wiki/Bytecode&quot;&gt;bytecode&lt;/a&gt;, which is an intermediate binary representation of the code.&lt;/p&gt;
&lt;p&gt;This bytecode enables the interpreter to skip recurring steps, such as &lt;a href=&quot;https://realpython.com/cpython-source-code-guide/#lexing-and-parsing&quot;&gt;lexing and parsing&lt;/a&gt; the code into an &lt;a href=&quot;https://en.wikipedia.org/wiki/Abstract_syntax_tree&quot;&gt;abstract syntax tree&lt;/a&gt; and validating its correctness every time you run the same program. As long as the underlying source code hasn’t changed, Python can reuse the &lt;a href=&quot;https://en.wikipedia.org/wiki/Intermediate_representation&quot;&gt;intermediate representation&lt;/a&gt;, which is immediately ready for execution. This saves time, speeding up your script’s startup time.&lt;/p&gt;
&lt;p&gt;Remember that while loading the compiled bytecode from &lt;code&gt;__pycache__&lt;/code&gt; makes Python modules import faster, it doesn’t affect their execution speed!&lt;/p&gt;
&lt;div class=&quot;card mb-3&quot; id=&quot;collapse_cardd97415&quot;&gt;
&lt;div class=&quot;card-header border-0&quot;&gt;
&lt;p class=&quot;m-0&quot;&gt;
    &lt;button class=&quot;btn w-100&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#collapsed97415&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;collapsed97415&quot; markdown=&quot;1&quot;&gt;&lt;span class=&quot;float-left&quot; markdown=&quot;1&quot;&gt;Bytecode vs Machine Code&lt;/span&gt;&lt;span class=&quot;float-right text-muted&quot;&gt;Show/Hide&lt;/span&gt;&lt;/button&gt;
  &lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;collapse js-collapsible-section&quot; data-parent=&quot;#collapse_cardd97415&quot; id=&quot;collapsed97415&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;p&gt;Why bother with bytecode at all instead of compiling the code straight to the low-level &lt;a href=&quot;https://en.wikipedia.org/wiki/Machine_code&quot;&gt;machine code&lt;/a&gt;? While machine code is what executes on the hardware, providing the ultimate performance, it’s not as &lt;em&gt;portable&lt;/em&gt; or quick to produce as bytecode.&lt;/p&gt;
&lt;p&gt;Machine code is a set of binary instructions understood by your specific CPU architecture, wrapped in a container format like &lt;a href=&quot;https://en.wikipedia.org/wiki/.exe&quot;&gt;EXE&lt;/a&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/Executable_and_Linkable_Format&quot;&gt;ELF&lt;/a&gt;, or &lt;a href=&quot;https://en.wikipedia.org/wiki/Mach-O&quot;&gt;Mach-O&lt;/a&gt;, depending on the operating system. In contrast, bytecode provides a platform-independent abstraction layer and is typically quicker to compile.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Python uses local &lt;code&gt;__pycache__&lt;/code&gt; folders to store the compiled bytecode of &lt;strong&gt;imported modules&lt;/strong&gt; in your project. On subsequent runs, the interpreter will try to load precompiled versions of modules from these folders, provided they’re up-to-date with the corresponding source files. Note that this &lt;a href=&quot;https://realpython.com/lru-cache-python/&quot;&gt;caching&lt;/a&gt; mechanism only gets triggered for modules you &lt;em&gt;import&lt;/em&gt; in your code rather than executing as scripts in the terminal.&lt;/p&gt;
&lt;p&gt;In addition to this &lt;strong&gt;on-disk bytecode caching&lt;/strong&gt;, Python keeps an &lt;strong&gt;in-memory cache of modules&lt;/strong&gt;, which you can access through the &lt;a href=&quot;https://docs.python.org/3/library/sys.html#sys.modules&quot;&gt;&lt;code&gt;sys.modules&lt;/code&gt;&lt;/a&gt; dictionary. It ensures that when you import the same module multiple times from different places within your program, Python will use the already imported module without needing to reload or recompile it. Both mechanisms work together to reduce the overhead of importing Python modules.&lt;/p&gt;
&lt;p&gt;Next, you’re going to find out exactly how much faster Python loads the cached bytecode as opposed to compiling the source code on the fly when you import a module.&lt;/p&gt;
&lt;h2 id=&quot;how-much-faster-is-loading-modules-from-cache&quot;&gt;How Much Faster Is Loading Modules From Cache?&lt;a class=&quot;headerlink&quot; href=&quot;#how-much-faster-is-loading-modules-from-cache&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The caching happens behind the scenes and usually goes unnoticed since Python is quite rapid at compiling the bytecode. Besides, unless you often run short-lived Python scripts, the compilation step remains insignificant when compared to the total execution time. That said, without caching, the overhead associated with bytecode compilation could add up if you had lots of modules and imported them many times over.&lt;/p&gt;
&lt;p&gt;To measure the difference in &lt;strong&gt;import time&lt;/strong&gt; between a cached and uncached module, you can pass the &lt;a href=&quot;https://realpython.com/python37-new-features/#developer-tricks&quot;&gt;&lt;code&gt;-X importtime&lt;/code&gt;&lt;/a&gt; option to the &lt;code&gt;python&lt;/code&gt; command or set the equivalent &lt;a href=&quot;https://docs.python.org/3.7/using/cmdline.html#envvar-PYTHONPROFILEIMPORTTIME&quot;&gt;&lt;code&gt;PYTHONPROFILEIMPORTTIME&lt;/code&gt;&lt;/a&gt; environment variable. When this option is enabled, Python will display a table summarizing how long it took to import each module, including the cumulative time in case a module depends on other modules.&lt;/p&gt;
&lt;p&gt;Suppose you had a &lt;code&gt;calculator.py&lt;/code&gt; script that imports and calls a utility &lt;a href=&quot;https://realpython.com/defining-your-own-python-function/&quot;&gt;function&lt;/a&gt; from a local &lt;code&gt;arithmetic.py&lt;/code&gt; module:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;python&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    &lt;span class=&quot;mr-2&quot; aria-label=&quot;Filename&quot;&gt;&lt;code style=&quot;color: inherit;&quot;&gt;calculator.py&lt;/code&gt;&lt;/span&gt;
    &lt;div class=&quot;noselect&quot;&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;arithmetic&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The imported module defines a single function:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;python&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    &lt;span class=&quot;mr-2&quot; aria-label=&quot;Filename&quot;&gt;&lt;code style=&quot;color: inherit;&quot;&gt;arithmetic.py&lt;/code&gt;&lt;/span&gt;
    &lt;div class=&quot;noselect&quot;&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, the main script delegates the addition of two numbers, three and four, to the &lt;code&gt;add()&lt;/code&gt; function imported from the &lt;code&gt;arithmetic&lt;/code&gt; module.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Even though you use the &lt;code&gt;from ... import&lt;/code&gt; syntax, which only brings the specified symbol into your current &lt;a href=&quot;https://realpython.com/python-namespaces-scope/&quot;&gt;namespace&lt;/a&gt;, Python reads and compiles the entire module anyway. Moreover, &lt;a href=&quot;https://www.flake8rules.com/rules/F401.html&quot;&gt;unused imports&lt;/a&gt; would also trigger the compilation.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The first time you run your script, Python compiles and saves the bytecode of the module you imported into a local &lt;code&gt;__pycache__&lt;/code&gt; folder. If such a folder doesn’t already exist, then Python automatically creates one before moving on. Now, when you execute your script again, Python should find and load the cached bytecode as long as you didn’t alter the associated source code.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-pycache/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-pycache/ »&lt;/a&gt;&lt;/h2&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>The Real Python Podcast – Episode #204: Querying OpenStreetMaps via API &amp; Lazy Evaluation in Python</title>
      <id>https://realpython.com/podcasts/rpp/204/</id>
      <link href="https://realpython.com/podcasts/rpp/204/"/>
      <updated>2024-05-10T12:00:00+00:00</updated>
      <summary>Would you like to get more practice working with APIs in Python? How about exploring the globe using the data from OpenStreetMap? Christopher Trudeau is back on the show this week, bringing another batch of PyCoder&#x27;s Weekly articles and projects.</summary>
      <content type="html">
        &lt;p&gt;Would you like to get more practice working with APIs in Python? How about exploring the globe using the data from OpenStreetMap? Christopher Trudeau is back on the show this week, bringing another batch of PyCoder&#x27;s Weekly articles and projects.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>PyTorch vs TensorFlow for Your Python Deep Learning Project</title>
      <id>https://realpython.com/pytorch-vs-tensorflow/</id>
      <link href="https://realpython.com/pytorch-vs-tensorflow/"/>
      <updated>2024-05-08T14:00:00+00:00</updated>
      <summary>PyTorch vs Tensorflow: Which one should you use? Learn about these two popular deep learning libraries and how to choose the best one for your project.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;PyTorch vs TensorFlow: What’s the difference? Both are open-source Python libraries that use graphs to perform numerical computations on data in deep learning applications. Both are used extensively in academic research and commercial code. Both are extended by a variety of APIs, cloud computing platforms, and model repositories.&lt;/p&gt;
&lt;p&gt;If they’re so similar, then how do you decide which one is best for your project?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this tutorial, you’ll learn:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What the differences are between &lt;strong&gt;PyTorch&lt;/strong&gt; and &lt;strong&gt;TensorFlow&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;What &lt;strong&gt;tools&lt;/strong&gt; and &lt;strong&gt;resources&lt;/strong&gt; are available for each&lt;/li&gt;
&lt;li&gt;How to choose the &lt;strong&gt;best option&lt;/strong&gt; for your specific use case&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You’ll start by taking a close look at both platforms, beginning with the slightly older TensorFlow. Then, you’ll explore PyTorch and some considerations to help you determine which choice is best for your project. Let’s get started!&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Decision Guide:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/pytorch-vs-tensorflow-update-pdf/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-pytorch-vs-tensorflow-update-pdf&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free decision guide&lt;/a&gt; that will help you choose between PyTorch and TensorFlow for your Python deep learning project.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;what-is-tensorflow&quot;&gt;What Is TensorFlow?&lt;a class=&quot;headerlink&quot; href=&quot;#what-is-tensorflow&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;TensorFlow was developed by Google and released as open-source in 2015. It grew out of Google’s homegrown machine learning software, which was refactored and optimized for use in production.&lt;/p&gt;
&lt;p&gt;The name “TensorFlow” describes how you organize and perform operations on data. The basic data structure for both TensorFlow and PyTorch is a &lt;a href=&quot;https://en.wikipedia.org/wiki/Tensor&quot;&gt;tensor&lt;/a&gt;. When you use TensorFlow, you perform operations on the data in these tensors by building a &lt;a href=&quot;https://www.quora.com/What-are-the-differences-between-Data-flow-model-and-State-machine-model?&quot;&gt;stateful dataflow graph&lt;/a&gt;, kind of like a flowchart that remembers past events.&lt;/p&gt;
&lt;h3 id=&quot;who-uses-tensorflow&quot;&gt;Who Uses TensorFlow?&lt;a class=&quot;headerlink&quot; href=&quot;#who-uses-tensorflow&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;TensorFlow has a reputation for being a production-grade deep learning library. It has a large and active user base and a proliferation of official and third-party tools and platforms for training, deploying, and serving models.&lt;/p&gt;
&lt;p&gt;After PyTorch was released in 2016, TensorFlow declined in popularity. But in late 2019, Google released &lt;a href=&quot;https://blog.tensorflow.org/2019/09/tensorflow-20-is-now-available.html&quot;&gt;TensorFlow 2.0&lt;/a&gt;—a major update that simplified the library and made it more user-friendly, leading to renewed interest among the machine learning community. &lt;/p&gt;
&lt;h3 id=&quot;code-style-and-function&quot;&gt;Code Style and Function&lt;a class=&quot;headerlink&quot; href=&quot;#code-style-and-function&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In TensorFlow 2.0, you can use &lt;a href=&quot;https://www.tensorflow.org/guide/eager&quot;&gt;eager execution&lt;/a&gt;, which is how Python normally works. Eager execution evaluates operations immediately, so you can write your code using Python control flow rather than graph control flow. To see this in action, you’ll take a look at how you would multiply two tensors using both Tensorflow 1.0 and 2.0.&lt;/p&gt;
&lt;p&gt;To start, here’s an example of how to multiply tensors using TensorFlow 2.0. With eager execution, all you need is &lt;code&gt;tf.math.multiply()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tf&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;3.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;5.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;tf.Tensor: shape=(3, 3), dtype=float32, numpy=&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;array([[ 2.,  4.,  6.],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [ 6., 12., 18.],&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;       [10., 20., 30.]], dtype=float32)&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this code, you declare your tensors using Python’s list notation, and &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/math/multiply&quot;&gt;&lt;code&gt;tf.math.multiply()&lt;/code&gt;&lt;/a&gt; executes the &lt;strong&gt;element-wise multiplication&lt;/strong&gt; immediately when you call it.&lt;/p&gt;
&lt;p&gt;Before TensorFlow 2.0, you had to manually stitch together an &lt;a href=&quot;https://en.wikipedia.org/wiki/Abstract_syntax_tree&quot;&gt;abstract syntax tree&lt;/a&gt; by making &lt;code&gt;tf.*&lt;/code&gt; API calls. You then had to pass a set of output tensors and input tensors to a &lt;code&gt;session.run()&lt;/code&gt; call and manually compile the model. &lt;/p&gt;
&lt;p&gt;A &lt;code&gt;Session&lt;/code&gt; object is a &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/compat/v1/Session&quot;&gt;class for running TensorFlow operations&lt;/a&gt;. It contains the environment in which &lt;code&gt;Tensor&lt;/code&gt; objects are evaluated and &lt;code&gt;Operation&lt;/code&gt; objects are executed, and it can own resources like &lt;code&gt;tf.Variable&lt;/code&gt; objects. The most common way to use a &lt;code&gt;Session&lt;/code&gt; is as a &lt;a href=&quot;https://realpython.com/python-with-statement/&quot;&gt;context manager&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To see how a &lt;code&gt;Session&lt;/code&gt; is used in this way, here’s an example of how you multiply two tensors using the old TensorFlow 1.0 method:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tensorflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tf&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;disable_eager_execution&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;placeholder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;float32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;y&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;math&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;compat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;session&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;        &lt;span class=&quot;n&quot;&gt;multiply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed_dict&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;3.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;5.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]}&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[[ 2.  4.  6.]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; [ 6. 12. 18.]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt; [10. 20. 30.]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This code uses TensorFlow 2.x’s &lt;code&gt;tf.compat&lt;/code&gt; API to access TensorFlow 1.x methods and disable eager execution. &lt;/p&gt;
&lt;p&gt;You first declare the input tensors &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; using &lt;a href=&quot;https://www.tensorflow.org/api_docs/python/tf/compat/v1/placeholder&quot;&gt;&lt;code&gt;tf.compat.v1.placeholder&lt;/code&gt;&lt;/a&gt; tensor objects. Then you define the operation to perform on them. Note that nothing has been calculated at this point.&lt;/p&gt;
&lt;p&gt;Next, using the &lt;code&gt;tf.Session&lt;/code&gt; object as a context manager, you create a container to encapsulate the runtime environment and do the multiplication by feeding real values into the placeholders with a &lt;code&gt;feed_dict&lt;/code&gt;. Finally, still inside the session, you &lt;a href=&quot;https://realpython.com/python-print/&quot;&gt;&lt;code&gt;print()&lt;/code&gt;&lt;/a&gt; the result.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Keep in mind that because TensorFlow 1.0 has been deprecated, it probably isn’t the best option for your future projects, and you should stick with using TensorFlow 2.0.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/pytorch-vs-tensorflow/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/pytorch-vs-tensorflow/ »&lt;/a&gt;&lt;/h2&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Flattening a List of Lists in Python</title>
      <id>https://realpython.com/courses/flatten-list-of-lists/</id>
      <link href="https://realpython.com/courses/flatten-list-of-lists/"/>
      <updated>2024-05-07T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn how to flatten a list of lists in Python. You&#x27;ll use different tools and techniques to accomplish this task. First, you&#x27;ll use a loop along with the .extend() method of list. Then you&#x27;ll explore other tools, including reduce(), sum(), itertools.chain(), and more.</summary>
      <content type="html">
        &lt;p&gt;Sometimes, when you&amp;rsquo;re working with data, you may have the data as a list of nested lists. A common operation is to &lt;strong&gt;flatten&lt;/strong&gt; this data into a one-dimensional list in Python. Flattening a list involves converting a multidimensional list, such as a matrix, into a one-dimensional list.&lt;/p&gt;
&lt;p&gt;In this video course, you&amp;rsquo;ll learn how to do that in Python.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Python News: What&#x27;s New From April 2024</title>
      <id>https://realpython.com/python-news-april-2024/</id>
      <link href="https://realpython.com/python-news-april-2024/"/>
      <updated>2024-05-06T14:00:00+00:00</updated>
      <summary>In April 2024, the last alpha release of Python 3.13 was published. At the same time, the PSF announced some great news, and PyCon US 2024 opened its call for volunteers. Some fundamental Python projects, such as Django, pandas, and Pillow, also released new versions.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;In April 2024, Python’s core development team released versions &lt;strong&gt;3.13.0a6&lt;/strong&gt; and &lt;strong&gt;3.12.3&lt;/strong&gt; of the language! The former received several exciting features, improvements, and optimizations, while the latter got more than 300 commits for security improvements and bug fixes.&lt;/p&gt;
&lt;p&gt;The 3.13.0a6 release is the last alpha release. In the first half of May, the code will be frozen and won’t accept new features. Note that 3.13.0a6 is a &lt;a href=&quot;https://realpython.com/python-pre-release/&quot;&gt;pre-release&lt;/a&gt;, so you shouldn’t use it for production environments. However, it provides a great way to try out some new and exciting language features.&lt;/p&gt;
&lt;p&gt;There is also great news about PyCon US 2024, which opened its call for volunteers.&lt;/p&gt;
&lt;p&gt;Let’s dive into the most exciting &lt;strong&gt;Python news&lt;/strong&gt; from April 2024!&lt;/p&gt;
&lt;h2 id=&quot;python-3130-alpha-6-and-3123-arrive&quot;&gt;Python 3.13.0 Alpha 6 and 3.12.3 Arrive&lt;a class=&quot;headerlink&quot; href=&quot;#python-3130-alpha-6-and-3123-arrive&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This April, Python &lt;a href=&quot;https://blog.python.org/2024/04/python-3123-and-3130a6-released.html&quot;&gt;released&lt;/a&gt; its sixth &lt;strong&gt;alpha preview release&lt;/strong&gt;, 3.13.0a6. This version is the last alpha release, as Python 3.13 will enter the &lt;strong&gt;beta phase&lt;/strong&gt; on May 7. Once in beta, it won’t accept any new features. &lt;/p&gt;
&lt;p&gt;Python 3.13 brings the following new features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/dev/whatsnew/3.13.html#improved-error-messages&quot;&gt;Colorized exception tracebacks&lt;/a&gt; are now the default behavior in the interactive interpreter or &lt;a href=&quot;https://realpython.com/python-repl/&quot;&gt;REPL&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;An &lt;a href=&quot;https://docs.python.org/dev/whatsnew/3.13.html#experimental-jit-compiler&quot;&gt;experimental JIT compiler&lt;/a&gt; that provides the groundwork for significant performance improvements.&lt;/li&gt;
&lt;li&gt;The garbage collector is now &lt;a href=&quot;https://docs.python.org/dev/whatsnew/3.13.html#incremental-garbage-collection&quot;&gt;incremental&lt;/a&gt;, which should make the garbage collection process more efficient.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/dev/whatsnew/3.13.html#other-language-changes&quot;&gt;Stripped indentation in docstrings&lt;/a&gt; reduces memory usage and the size of &lt;code&gt;.pyc&lt;/code&gt; files.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Meanwhile, the standard library comes with these new features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;a href=&quot;https://docs.python.org/dev/library/dbm.html&quot;&gt;&lt;code&gt;dbm&lt;/code&gt;&lt;/a&gt; module has a new &lt;a href=&quot;https://docs.python.org/dev/whatsnew/3.13.html#id1&quot;&gt;&lt;code&gt;dbm.sqlite3&lt;/code&gt;&lt;/a&gt; backend for creating new files.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://peps.python.org/pep-0594/&quot;&gt;PEP 594&lt;/a&gt; scheduled removals of many deprecated modules: &lt;code&gt;aifc&lt;/code&gt;, &lt;code&gt;audioop&lt;/code&gt;, &lt;code&gt;chunk&lt;/code&gt;, &lt;code&gt;cgi&lt;/code&gt;, &lt;code&gt;cgitb&lt;/code&gt;, &lt;code&gt;crypt&lt;/code&gt;, &lt;code&gt;imghdr&lt;/code&gt;, &lt;code&gt;mailcap&lt;/code&gt;, &lt;code&gt;msilib&lt;/code&gt;, &lt;code&gt;nis&lt;/code&gt;, &lt;code&gt;nntplib&lt;/code&gt;, &lt;code&gt;ossaudiodev&lt;/code&gt;, &lt;code&gt;pipes&lt;/code&gt;, &lt;code&gt;sndhdr&lt;/code&gt;, &lt;code&gt;spwd&lt;/code&gt;, &lt;code&gt;sunau&lt;/code&gt;, &lt;code&gt;telnetlib&lt;/code&gt;, &lt;code&gt;uu&lt;/code&gt;, &lt;code&gt;xdrlib&lt;/code&gt;, &lt;code&gt;lib2to3&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Many deprecated classes, functions, and methods (dead batteries) were &lt;a href=&quot;https://docs.python.org/dev/whatsnew/3.13.html#removed&quot;&gt;removed&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/dev/whatsnew/3.13.html#deprecated&quot;&gt;New deprecations&lt;/a&gt; appeared, and many of them were scheduled for removal in Python 3.15 or 3.16.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For a detailed list of changes, additions, and removals, you can check out the &lt;a href=&quot;https://docs.python.org/3.12/whatsnew/changelog.html#python-3-13-0-alpha-6&quot;&gt;Changelog&lt;/a&gt; document. The next pre-release of Python 3.13 will be 3.13.0b1, which is currently scheduled for May 7.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-news-april-2024/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-news-april-2024/ »&lt;/a&gt;&lt;/h2&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>The Real Python Podcast – Episode #203: Embarking on a Relaxed and Friendly Python Coding Journey</title>
      <id>https://realpython.com/podcasts/rpp/203/</id>
      <link href="https://realpython.com/podcasts/rpp/203/"/>
      <updated>2024-05-03T12:00:00+00:00</updated>
      <summary>Do you get stressed while trying to learn Python? Do you prefer to build small programs or projects as you continue your coding journey? This week on the show, Real Python author Stephen Gruppetta is here to talk about his new book, &quot;The Python Coding Book.&quot;</summary>
      <content type="html">
        &lt;p&gt;Do you get stressed while trying to learn Python? Do you prefer to build small programs or projects as you continue your coding journey? This week on the show, Real Python author Stephen Gruppetta is here to talk about his new book, &quot;The Python Coding Book.&quot;&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Quiz: The Python calendar Module</title>
      <id>https://realpython.com/quizzes/python-calendar-module/</id>
      <link href="https://realpython.com/quizzes/python-calendar-module/"/>
      <updated>2024-05-02T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of the calendar module in Python. It&#x27;ll evaluate your proficiency in manipulating, customizing, and displaying calendars directly within your terminal. By working through this quiz, you&#x27;ll revisit the fundamental functions and methods provided by the calendar module.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of &lt;a href=&quot;https://realpython.com/python-calendar-module/&quot;&gt;creating calendars in Python&lt;/a&gt; using the &lt;code&gt;calendar&lt;/code&gt; module.&lt;/p&gt;
&lt;p&gt;By working through this quiz, you&amp;rsquo;ll revisit the fundamental functions and methods provided by the &lt;code&gt;calendar&lt;/code&gt; module.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Python Sequences: A Comprehensive Guide</title>
      <id>https://realpython.com/python-sequences/</id>
      <link href="https://realpython.com/python-sequences/"/>
      <updated>2024-05-01T14:00:00+00:00</updated>
      <summary>This tutorial dives into Python sequences, which is one of the main categories of data types. You&#x27;ll learn about the properties that make an object a sequence and how to create user-defined sequences.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;A phrase you’ll often hear is that everything in Python is an object, and every object has a type. This points to the importance of data types in Python. However, often what an object can do is more important than what it is. So, it’s useful to discuss &lt;em&gt;categories&lt;/em&gt; of data types and one of the main categories is Python’s &lt;strong&gt;sequence&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this tutorial, you’ll learn about:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Basic &lt;strong&gt;characteristics of a sequence&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Operations&lt;/strong&gt; that are &lt;strong&gt;common to most sequences&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Special methods&lt;/strong&gt; associated with sequences&lt;/li&gt;
&lt;li&gt;Abstract base classes &lt;strong&gt;&lt;code&gt;Sequence&lt;/code&gt; and &lt;code&gt;MutableSequence&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;User-defined mutable and immutable sequences&lt;/strong&gt; and how to create them&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This tutorial assumes that you’re familiar with &lt;a href=&quot;https://realpython.com/python-data-types/&quot;&gt;Python’s built-in data types&lt;/a&gt; and with the &lt;a href=&quot;https://realpython.com/python3-object-oriented-programming/&quot;&gt;basics of object-oriented programming&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/python-sequences-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-sequences-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that you’ll use to learn about Python sequences in this comprehensive guide.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;container border rounded text-wrap-pretty my-3&quot;&gt;

  &lt;p class=&quot;my-3&quot;&gt;&lt;mark class=&quot;marker-highlight&quot;&gt;&lt;strong&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@quiz&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt; Take the Quiz:&lt;/strong&gt;&lt;/mark&gt; Test your knowledge with our interactive “Python Sequences: A Comprehensive Guide” quiz. You’ll receive a score upon completion to help you track your learning progress:&lt;/p&gt;

  &lt;hr&gt;

  &lt;div class=&quot;row my-3&quot;&gt;
    &lt;div class=&quot;col-xs-12 col-sm-4 col-md-3 align-self-center&quot;&gt;

      &lt;a href=&quot;/quizzes/python-sequences/&quot; tabindex=&quot;-1&quot;&gt;
        &lt;div class=&quot;embed-responsive embed-responsive-16by9&quot;&gt;

            &lt;img class=&quot;card-img-top m-0 p-0 embed-responsive-item rounded&quot; style=&quot;object-fit: contain; background: #e5c5ac;&quot; alt=&quot;Python Sequences: A Comprehensive Guide&quot; src=&quot;https://files.realpython.com/media/Python-Sequences_Watermarked.988c75bb60be.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/Python-Sequences_Watermarked.988c75bb60be.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Python-Sequences_Watermarked.988c75bb60be.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Python-Sequences_Watermarked.988c75bb60be.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Python-Sequences_Watermarked.988c75bb60be.jpg 1920w&quot; sizes=&quot;(min-width: 1200px) 142px, (min-width: 1000px) 122px, (min-width: 780px) 112px, (min-width: 580px) 139px, calc(100vw - 62px)&quot;&gt;


          &lt;div class=&quot;card-img-overlay d-flex align-items-center&quot;&gt;
            &lt;div class=&quot;mx-auto&quot;&gt;
              &lt;span class=&quot;text-light&quot; style=&quot;opacity: 0.90;&quot;&gt;&lt;span class=&quot;icon baseline scale2x&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@quiz&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/a&gt;

    &lt;/div&gt;
    &lt;div class=&quot;col&quot;&gt;
      &lt;div class=&quot;mt-3 d-md-none&quot;&gt;&lt;/div&gt; 
      &lt;p class=&quot;small text-muted mb-0&quot;&gt;&lt;strong&gt;Interactive Quiz&lt;/strong&gt;&lt;/p&gt;
      &lt;a href=&quot;/quizzes/python-sequences/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Python Sequences: A Comprehensive Guide&lt;/span&gt;&lt;/a&gt; 
      &lt;p class=&quot;text-muted mb-0 small&quot;&gt;In this quiz, you&#x27;ll test your understanding of sequences in Python. You&#x27;ll revisit the basic characteristics of a sequence, operations common to most sequences, special methods associated with sequences, and how to create user-defined mutable and immutable sequences.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;building-blocks-of-python-sequences&quot;&gt;Building Blocks of Python Sequences&lt;a class=&quot;headerlink&quot; href=&quot;#building-blocks-of-python-sequences&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;It’s likely you used a Python sequence the last time you wrote Python code, even if you don’t know it. The term &lt;em&gt;sequence&lt;/em&gt; doesn’t refer to a specific data type but to a category of data types that share common characteristics.&lt;/p&gt;
&lt;h3 id=&quot;characteristics-of-python-sequences&quot;&gt;Characteristics of Python Sequences&lt;a class=&quot;headerlink&quot; href=&quot;#characteristics-of-python-sequences&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A &lt;a href=&quot;https://docs.python.org/3/glossary.html#term-sequence&quot;&gt;sequence&lt;/a&gt; is a data structure that contains items arranged in order, and you can access each item using an integer index that represents its position in the sequence. You can always find the length of a sequence. Here are some examples of sequences from Python’s basic built-in data types:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# List&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;countries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;USA&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Canada&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;UK&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Norway&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Malta&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;India&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;country&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;countries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;USA&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Canada&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;UK&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Norway&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Malta&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;India&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;countries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;countries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;USA&#x27;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Tuple&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;countries&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;USA&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Canada&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;UK&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Norway&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Malta&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;India&quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;country&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;countries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;USA&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Canada&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;UK&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Norway&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Malta&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;India&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;countries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;countries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;USA&#x27;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Strings&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;country&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;India&quot;&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;letter&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;letter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;I&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;n&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;d&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;i&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;country&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;I&#x27;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Lists, tuples, and strings are among Python’s most basic data types. Even though they’re different types with distinct characteristics, they have some common traits. You can summarize the characteristics that define a Python sequence as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A sequence is an &lt;a href=&quot;https://realpython.com/python-iterators-iterables/&quot;&gt;iterable&lt;/a&gt;, which means you can iterate through it.&lt;/li&gt;
&lt;li&gt;A sequence has a length, which means you can pass it to &lt;a href=&quot;https://realpython.com/len-python-function/&quot;&gt;&lt;code&gt;len()&lt;/code&gt;&lt;/a&gt; to get its number of elements.&lt;/li&gt;
&lt;li&gt;An element of a sequence can be accessed based on its position in the sequence using an integer index. You can use the square bracket notation to index a sequence.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are other built-in data types in Python that also have all of these characteristics. One of these is the &lt;a href=&quot;https://realpython.com/python-range/&quot;&gt;&lt;code&gt;range&lt;/code&gt; object&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;class &#x27;range&#x27;&amp;gt;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;6&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;10&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;6&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;7&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;8&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;9&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You can iterate through a &lt;code&gt;range&lt;/code&gt; object, which makes it iterable. You can also find its length using &lt;code&gt;len()&lt;/code&gt; and fetch items through indexing. Therefore, a &lt;code&gt;range&lt;/code&gt; object is also a sequence.&lt;/p&gt;
&lt;p&gt;You can also verify that &lt;a href=&quot;https://realpython.com/python-mutable-vs-immutable-types/&quot;&gt;&lt;code&gt;bytes&lt;/code&gt; and &lt;code&gt;bytearray&lt;/code&gt; objects&lt;/a&gt;, two of Python’s built-in data structures, are also sequences. Both are sequences of integers. A &lt;code&gt;bytes&lt;/code&gt; sequence is immutable, while a &lt;code&gt;bytearray&lt;/code&gt; is mutable.&lt;/p&gt;
&lt;h3 id=&quot;special-methods-associated-with-python-sequences&quot;&gt;Special Methods Associated With Python Sequences&lt;a class=&quot;headerlink&quot; href=&quot;#special-methods-associated-with-python-sequences&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In Python, the key characteristics of a data type are determined using &lt;a href=&quot;https://realpython.com/python-magic-methods/&quot;&gt;special methods&lt;/a&gt;, which are defined in the class definitions. The special methods associated with the properties of sequences are the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;.__iter__()&lt;/code&gt;&lt;/strong&gt;: This special method makes an object iterable using Python’s preferred iteration protocol. However, it’s possible for a class without an &lt;code&gt;.__iter__()&lt;/code&gt; special method to create iterable objects if the class has a &lt;code&gt;.__getitem__()&lt;/code&gt; special method that supports iteration. Most sequences have an &lt;code&gt;.__iter__()&lt;/code&gt; special method, but it’s possible to have a sequence without this method.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;.__len__()&lt;/code&gt;&lt;/strong&gt;: This special method defines the length of an object, which is normally the number of elements contained within it. The &lt;code&gt;len()&lt;/code&gt; built-in function calls an object’s &lt;code&gt;.__len__()&lt;/code&gt; special method. Every sequence has this special method.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;.__getitem__()&lt;/code&gt;&lt;/strong&gt;: This special method enables you to access an item from a sequence. The square brackets notation can be used to fetch an item. The expression &lt;code&gt;countries[0]&lt;/code&gt; is equivalent to &lt;code&gt;countries.__getitem__(0)&lt;/code&gt;. For sequences, &lt;code&gt;.__getitem__()&lt;/code&gt; should accept integer arguments starting from zero. Every sequence has this special method. This method can also ensure an object is iterable if the &lt;code&gt;.__iter__()&lt;/code&gt; special method is missing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Therefore, all sequences have a &lt;code&gt;.__len__()&lt;/code&gt; and a &lt;code&gt;.__getitem__()&lt;/code&gt; special method and most also have &lt;code&gt;.__iter__()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;However, it’s not sufficient for an object to have these special methods to be a sequence. For example, many mappings also have these three methods but mappings aren’t sequences.&lt;/p&gt;
&lt;p&gt;A dictionary is an example of a mapping. You can find the length of a dictionary and iterate through its keys using a &lt;code&gt;for&lt;/code&gt; loop or other iteration techniques. You can also fetch an item from a dictionary using the square brackets notation.&lt;/p&gt;
&lt;p&gt;This characteristic is defined by &lt;code&gt;.__getitem__()&lt;/code&gt;. However, &lt;code&gt;.__getitem__()&lt;/code&gt; needs arguments that are dictionary keys and returns their matching values. You can’t index a dictionary using integers that refer to an item’s position in the dictionary. Therefore, dictionaries are not sequences.&lt;/p&gt;
&lt;h2 id=&quot;slicing-in-python-sequences&quot;&gt;Slicing in Python Sequences&lt;a class=&quot;headerlink&quot; href=&quot;#slicing-in-python-sequences&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-sequences/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-sequences/ »&lt;/a&gt;&lt;/h2&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Working With Global Variables in Python Functions</title>
      <id>https://realpython.com/courses/global-variables-in-python-functions/</id>
      <link href="https://realpython.com/courses/global-variables-in-python-functions/"/>
      <updated>2024-04-30T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn how to use global variables in Python functions using the global keyword or the built-in globals() function. You&#x27;ll also learn a few strategies to avoid relying on global variables because they can lead to code that&#x27;s difficult to understand, debug, and maintain.</summary>
      <content type="html">
        &lt;p&gt;A &lt;strong&gt;global variable&lt;/strong&gt; is a variable that you can use from any part of a program, including within &lt;strong&gt;functions&lt;/strong&gt;. Using global variables inside your Python functions can be tricky. You&amp;rsquo;ll need to differentiate between accessing and changing the values of the target global variable if you want your code to work correctly.&lt;/p&gt;
&lt;p&gt;Global variables can play a fundamental role in many software projects because they enable data sharing across an entire program. However, you should use them judiciously to avoid issues.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course, you&amp;rsquo;ll:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understand &lt;strong&gt;global variables&lt;/strong&gt; and how they work in Python&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Access&lt;/strong&gt; global variables within your Python functions directly&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Modify&lt;/strong&gt; and &lt;strong&gt;create&lt;/strong&gt; global variables within functions using the &lt;strong&gt;&lt;code&gt;global&lt;/code&gt; keyword&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Access&lt;/strong&gt;, &lt;strong&gt;create&lt;/strong&gt;, and &lt;strong&gt;modify&lt;/strong&gt; global variables within your functions with the &lt;strong&gt;&lt;code&gt;globals()&lt;/code&gt; function&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Explore strategies to &lt;strong&gt;avoid using global variables&lt;/strong&gt; in Python code&lt;/li&gt;
&lt;/ul&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Python&#x27;s unittest: Writing Unit Tests for Your Code</title>
      <id>https://realpython.com/python-unittest/</id>
      <link href="https://realpython.com/python-unittest/"/>
      <updated>2024-04-29T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll learn how to use the unittest framework to create unit tests for your Python code. Along the way, you&#x27;ll also learn how to create test cases, fixtures, test suites, and more.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;The Python &lt;a href=&quot;https://docs.python.org/3/library/index.html&quot;&gt;standard library&lt;/a&gt; ships with a testing framework named &lt;strong&gt;&lt;code&gt;unittest&lt;/code&gt;&lt;/strong&gt;, which you can use to write automated tests for your code. The &lt;code&gt;unittest&lt;/code&gt; package has an object-oriented approach where test cases derive from a base class, which has several useful methods.&lt;/p&gt;
&lt;p&gt;The framework supports many features that will help you write consistent unit tests for your code. These features include test cases, fixtures, test suites, and test discovery capabilities.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this tutorial, you’ll learn how to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Write &lt;code&gt;unittest&lt;/code&gt; tests with the &lt;strong&gt;&lt;code&gt;TestCase&lt;/code&gt;&lt;/strong&gt; class&lt;/li&gt;
&lt;li&gt;Explore the &lt;strong&gt;assert&lt;/strong&gt; methods that &lt;code&gt;TestCase&lt;/code&gt; provides&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;unittest&lt;/code&gt; from the &lt;strong&gt;command line&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Group test cases using the &lt;strong&gt;&lt;code&gt;TestSuite&lt;/code&gt;&lt;/strong&gt; class&lt;/li&gt;
&lt;li&gt;Create &lt;strong&gt;fixtures&lt;/strong&gt; to handle &lt;strong&gt;setup&lt;/strong&gt; and &lt;strong&gt;teardown&lt;/strong&gt; logic&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To get the most out of this tutorial, you should be familiar with some important Python concepts, such as &lt;a href=&quot;https://realpython.com/python3-object-oriented-programming/&quot;&gt;object-oriented programming&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/inheritance-composition-python/&quot;&gt;inheritance&lt;/a&gt;, and &lt;a href=&quot;https://realpython.com/python-assert-statement/&quot;&gt;assertions&lt;/a&gt;. Having a good understanding of code &lt;a href=&quot;https://realpython.com/python-testing/&quot;&gt;testing&lt;/a&gt; is a plus.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/python-unittest-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-unittest-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that shows you how to use Python’s unittest to write tests for your code.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;container border rounded text-wrap-pretty my-3&quot;&gt;

  &lt;p class=&quot;my-3&quot;&gt;&lt;mark class=&quot;marker-highlight&quot;&gt;&lt;strong&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@quiz&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt; Take the Quiz:&lt;/strong&gt;&lt;/mark&gt; Test your knowledge with our interactive “Python&#x27;s unittest: Writing Unit Tests for Your Code” quiz. You’ll receive a score upon completion to help you track your learning progress:&lt;/p&gt;

  &lt;hr&gt;

  &lt;div class=&quot;row my-3&quot;&gt;
    &lt;div class=&quot;col-xs-12 col-sm-4 col-md-3 align-self-center&quot;&gt;

      &lt;a href=&quot;/quizzes/python-unittest/&quot; tabindex=&quot;-1&quot;&gt;
        &lt;div class=&quot;embed-responsive embed-responsive-16by9&quot;&gt;

            &lt;img class=&quot;card-img-top m-0 p-0 embed-responsive-item rounded&quot; style=&quot;object-fit: contain; background: #aadfe5;&quot; alt=&quot;Python&#x27;s unittest: Writing Unit Tests for Your Code&quot; src=&quot;https://files.realpython.com/media/Python-unittest_Watermarked.f6549bba7422.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/Python-unittest_Watermarked.f6549bba7422.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Python-unittest_Watermarked.f6549bba7422.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Python-unittest_Watermarked.f6549bba7422.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Python-unittest_Watermarked.f6549bba7422.jpg 1920w&quot; sizes=&quot;(min-width: 1200px) 142px, (min-width: 1000px) 122px, (min-width: 780px) 112px, (min-width: 580px) 139px, calc(100vw - 62px)&quot;&gt;


          &lt;div class=&quot;card-img-overlay d-flex align-items-center&quot;&gt;
            &lt;div class=&quot;mx-auto&quot;&gt;
              &lt;span class=&quot;text-light&quot; style=&quot;opacity: 0.90;&quot;&gt;&lt;span class=&quot;icon baseline scale2x&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@quiz&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/a&gt;

    &lt;/div&gt;
    &lt;div class=&quot;col&quot;&gt;
      &lt;div class=&quot;mt-3 d-md-none&quot;&gt;&lt;/div&gt; 
      &lt;p class=&quot;small text-muted mb-0&quot;&gt;&lt;strong&gt;Interactive Quiz&lt;/strong&gt;&lt;/p&gt;
      &lt;a href=&quot;/quizzes/python-unittest/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Python&#x27;s unittest: Writing Unit Tests for Your Code&lt;/span&gt;&lt;/a&gt; 
      &lt;p class=&quot;text-muted mb-0 small&quot;&gt;In this quiz, you&#x27;ll test your understanding of Python testing with the unittest framework from the standard library. With this knowledge, you&#x27;ll be able to create basic tests, execute them, and find bugs before your users do.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;testing-your-python-code&quot;&gt;Testing Your Python Code&lt;a class=&quot;headerlink&quot; href=&quot;#testing-your-python-code&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Code testing&lt;/strong&gt; or &lt;a href=&quot;https://en.wikipedia.org/wiki/Software_testing&quot;&gt;software testing&lt;/a&gt; is a fundamental part of a modern software development cycle. Through code testing, you can verify that a given software project works as expected and fulfills its requirements. Testing enforces code quality and robustness.&lt;/p&gt;
&lt;p&gt;You’ll do code testing during the development stage of an application or project. You’ll write tests that isolate sections of your code and verify its correctness. A well-written battery or suite of tests can also serve as documentation for the project at hand.&lt;/p&gt;
&lt;p&gt;You’ll find several different concepts and techniques around testing. Most of them surpass the scope of this tutorial. However, &lt;a href=&quot;https://en.wikipedia.org/wiki/Unit_testing&quot;&gt;unit test&lt;/a&gt; is an important and relevant concept. A unit test is a test that operates on an individual unit of software. A unit test aims to validate that the tested unit works as designed.&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;unit&lt;/strong&gt; is often a small part of a program that takes a few inputs and produces an output. &lt;a href=&quot;https://realpython.com/defining-your-own-python-function/&quot;&gt;Functions&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/python-classes/#providing-behavior-with-methods&quot;&gt;methods&lt;/a&gt;, and other &lt;a href=&quot;https://realpython.com/python-callable-instances/&quot;&gt;callables&lt;/a&gt; are good examples of units that you’d need to test.&lt;/p&gt;
&lt;p&gt;In Python, there are several tools to help you write, organize, run, and automate your unit test. In the Python &lt;a href=&quot;https://docs.python.org/3/library/index.html&quot;&gt;standard library&lt;/a&gt;, you’ll find two of these tools:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;doctest&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;unittest&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Python’s &lt;code&gt;doctest&lt;/code&gt; module is a lightweight &lt;a href=&quot;https://en.wikipedia.org/wiki/Test_automation#Framework_approach_in_automation&quot;&gt;testing framework&lt;/a&gt; that provides quick and straightforward &lt;a href=&quot;https://en.wikipedia.org/wiki/Test_automation&quot;&gt;test automation&lt;/a&gt;. It can read the test cases from your project’s documentation and your code’s docstrings. This framework is shipped with the Python interpreter as part of the batteries-included philosophy.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To dive deeper into &lt;code&gt;doctest&lt;/code&gt;, check out the &lt;a href=&quot;https://realpython.com/python-doctest/&quot;&gt;Python’s doctest: Document and Test Your Code at Once&lt;/a&gt; tutorial.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;unittest&lt;/code&gt; &lt;a href=&quot;https://realpython.com/python-modules-packages/#python-packages&quot;&gt;package&lt;/a&gt; is also a testing framework. However, it provides a more complete solution than &lt;code&gt;doctest&lt;/code&gt;. In the following sections, you’ll learn and work with &lt;a href=&quot;https://docs.python.org/3/library/unittest.html#module-unittest&quot;&gt;&lt;code&gt;unittest&lt;/code&gt;&lt;/a&gt; to create suitable unit tests for your Python code.&lt;/p&gt;
&lt;h2 id=&quot;getting-to-know-pythons-unittest&quot;&gt;Getting to Know Python’s &lt;code&gt;unittest&lt;/code&gt;&lt;a class=&quot;headerlink&quot; href=&quot;#getting-to-know-pythons-unittest&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;unittest&lt;/code&gt; package provides a unit test framework inspired by &lt;a href=&quot;https://en.wikipedia.org/wiki/JUnit&quot;&gt;JUnit&lt;/a&gt;, which is a unit test framework for the &lt;a href=&quot;https://realpython.com/java-vs-python/&quot;&gt;Java&lt;/a&gt; language. The &lt;code&gt;unittest&lt;/code&gt; framework is directly available in the standard library, so you don’t have to install anything to use this tool.&lt;/p&gt;
&lt;p&gt;The framework uses an &lt;a href=&quot;https://realpython.com/python3-object-oriented-programming/&quot;&gt;object-oriented&lt;/a&gt; approach and supports some essential concepts that facilitate test creation, organization, preparation, and automation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Test case&lt;/strong&gt;: An individual unit of testing. It examines the output for a given input set.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test suite&lt;/strong&gt;: A collection of test cases, test suites, or both. They’re grouped and executed as a whole.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test fixture&lt;/strong&gt;: A group of actions required to set up an environment for testing. It also includes the teardown processes after the tests run.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test runner&lt;/strong&gt;: A component that handles the execution of tests and communicates the results to the user.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In the following sections, you’ll dive into using the &lt;code&gt;unittest&lt;/code&gt; package to create test cases, suites of tests, fixtures, and, of course, run your tests.&lt;/p&gt;
&lt;h2 id=&quot;organizing-your-tests-with-the-testcase-class&quot;&gt;Organizing Your Tests With the &lt;code&gt;TestCase&lt;/code&gt; Class&lt;a class=&quot;headerlink&quot; href=&quot;#organizing-your-tests-with-the-testcase-class&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;unittest&lt;/code&gt; package defines the &lt;code&gt;TestCase&lt;/code&gt; class, which is primarily designed for writing unit tests. To start writing your test cases, you just need to import the class and subclass it. Then, you’ll add methods whose names should begin with &lt;code&gt;test&lt;/code&gt;. These methods will test a given unit of code using different inputs and check for the expected results.&lt;/p&gt;
&lt;p&gt;Here’s a quick test case that tests the built-in &lt;a href=&quot;https://realpython.com/python-absolute-value/&quot;&gt;&lt;code&gt;abs()&lt;/code&gt;&lt;/a&gt; function:&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-unittest/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-unittest/ »&lt;/a&gt;&lt;/h2&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>The Real Python Podcast – Episode #202: Pydantic Data Validation &amp; Python Web Security Practices</title>
      <id>https://realpython.com/podcasts/rpp/202/</id>
      <link href="https://realpython.com/podcasts/rpp/202/"/>
      <updated>2024-04-26T12:00:00+00:00</updated>
      <summary>How do you verify and validate the data coming into your Python web application? What tools and security best practices should you consider as a developer? Christopher Trudeau is back on the show this week, bringing another batch of PyCoder&#x27;s Weekly articles and projects.</summary>
      <content type="html">
        &lt;p&gt;How do you verify and validate the data coming into your Python web application? What tools and security best practices should you consider as a developer? Christopher Trudeau is back on the show this week, bringing another batch of PyCoder&#x27;s Weekly articles and projects.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Quiz: What Is the __pycache__ Folder in Python?</title>
      <id>https://realpython.com/quizzes/python-pycache/</id>
      <link href="https://realpython.com/quizzes/python-pycache/"/>
      <updated>2024-04-26T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll have the opportunity to test your knowledge of the __pycache__ folder, including when, where, and why Python creates these folders.</summary>
      <content type="html">
        &lt;p&gt;As your Python project grows, you typically organize your code in modules and packages for easier maintenance and reusability. When you do that, you&amp;rsquo;ll likely notice the sudden emergence of a &lt;code&gt;__pycache__&lt;/code&gt; folder alongside your original files, popping up in various locations unexpectedly.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>What&#x27;s Lazy Evaluation in Python?</title>
      <id>https://realpython.com/python-lazy-evaluation/</id>
      <link href="https://realpython.com/python-lazy-evaluation/"/>
      <updated>2024-04-24T14:00:00+00:00</updated>
      <summary>This tutorial explores lazy evaluation in Python and looks at the advantages and disadvantages of using lazy and eager evaluation methods. By the end of this tutorial, you&#x27;ll clearly understand which approach is best for you, depending on your needs.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Being lazy is not always a bad thing. Every line of code you write has at least one expression that Python needs to evaluate. Python lazy evaluation is when Python takes the lazy option and delays working out the value returned by an expression until that value is needed.&lt;/p&gt;
&lt;p&gt;An &lt;strong&gt;expression&lt;/strong&gt; in Python is a unit of code that evaluates to a value. Examples of expressions include object names, function calls, expressions with arithmetic operators, literals that create built-in object types such as lists, and more. However, not all statements are expressions. For example, &lt;code&gt;if&lt;/code&gt; statements and &lt;code&gt;for&lt;/code&gt; loop statements don’t return a value.&lt;/p&gt;
&lt;p&gt;Python needs to evaluate every expression it encounters to use its value. In this tutorial, you’ll learn about the different ways Python evaluates these expressions. You’ll understand why some expressions are evaluated immediately, while others are evaluated later in the program’s execution. So, &lt;strong&gt;what’s lazy evaluation in Python?&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/python-lazy-evaluation-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-lazy-evaluation-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that shows you how to use lazy evaluation in Python.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;container border rounded text-wrap-pretty my-3&quot;&gt;

  &lt;p class=&quot;my-3&quot;&gt;&lt;mark class=&quot;marker-highlight&quot;&gt;&lt;strong&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@quiz&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt; Take the Quiz:&lt;/strong&gt;&lt;/mark&gt; Test your knowledge with our interactive “What&#x27;s Lazy Evaluation in Python?” quiz. You’ll receive a score upon completion to help you track your learning progress:&lt;/p&gt;

  &lt;hr&gt;

  &lt;div class=&quot;row my-3&quot;&gt;
    &lt;div class=&quot;col-xs-12 col-sm-4 col-md-3 align-self-center&quot;&gt;

      &lt;a href=&quot;/quizzes/python-lazy-evaluation/&quot; tabindex=&quot;-1&quot;&gt;
        &lt;div class=&quot;embed-responsive embed-responsive-16by9&quot;&gt;

            &lt;img class=&quot;card-img-top m-0 p-0 embed-responsive-item rounded&quot; style=&quot;object-fit: contain; background: #ffc873;&quot; alt=&quot;What&#x27;s Lazy Evaluation in Python?&quot; src=&quot;https://files.realpython.com/media/Eager-vs-Lazy-Evaluation-in-Python-Pros-and-Cons_Watermarked.15b852ab57a6.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/Eager-vs-Lazy-Evaluation-in-Python-Pros-and-Cons_Watermarked.15b852ab57a6.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Eager-vs-Lazy-Evaluation-in-Python-Pros-and-Cons_Watermarked.15b852ab57a6.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Eager-vs-Lazy-Evaluation-in-Python-Pros-and-Cons_Watermarked.15b852ab57a6.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Eager-vs-Lazy-Evaluation-in-Python-Pros-and-Cons_Watermarked.15b852ab57a6.jpg 1920w&quot; sizes=&quot;(min-width: 1200px) 142px, (min-width: 1000px) 122px, (min-width: 780px) 112px, (min-width: 580px) 139px, calc(100vw - 62px)&quot;&gt;


          &lt;div class=&quot;card-img-overlay d-flex align-items-center&quot;&gt;
            &lt;div class=&quot;mx-auto&quot;&gt;
              &lt;span class=&quot;text-light&quot; style=&quot;opacity: 0.90;&quot;&gt;&lt;span class=&quot;icon baseline scale2x&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@quiz&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/a&gt;

    &lt;/div&gt;
    &lt;div class=&quot;col&quot;&gt;
      &lt;div class=&quot;mt-3 d-md-none&quot;&gt;&lt;/div&gt; 
      &lt;p class=&quot;small text-muted mb-0&quot;&gt;&lt;strong&gt;Interactive Quiz&lt;/strong&gt;&lt;/p&gt;
      &lt;a href=&quot;/quizzes/python-lazy-evaluation/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;What&#x27;s Lazy Evaluation in Python?&lt;/span&gt;&lt;/a&gt; 
      &lt;p class=&quot;text-muted mb-0 small&quot;&gt;In this quiz, you&#x27;ll test your understanding of the differences between lazy and eager evaluation in Python. By working through this quiz, you&#x27;ll revisit how Python optimizes memory use and computational overhead by deciding when to compute values.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;in-short-python-lazy-evaluation-generates-objects-only-when-needed&quot;&gt;In Short: Python Lazy Evaluation Generates Objects Only When Needed&lt;a class=&quot;headerlink&quot; href=&quot;#in-short-python-lazy-evaluation-generates-objects-only-when-needed&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;An expression evaluates to a value. However, you can separate the type of evaluation of expressions into two types:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Eager evaluation&lt;/li&gt;
&lt;li&gt;Lazy evaluation&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Eager evaluation&lt;/strong&gt; refers to those cases when Python evaluates an expression as soon as it encounters it. Here are some examples of expressions that are evaluated eagerly:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;linenos&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;15&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 3&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;random&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 5&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;random&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;randint&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 6&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 7&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 8&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 9&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;[2, 4, 6, 8, 10]&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt;11&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;numbers&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt;12&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;[2, 4, 6, 8, 10]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Interactive environments, such as the &lt;a href=&quot;https://realpython.com/python-repl/&quot;&gt;standard Python REPL&lt;/a&gt; used in this example, display the value of an expression when the line only contains the expression. This code section shows a few examples of statements and expressions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Lines 1 and 2&lt;/strong&gt;: The first example includes the addition operator &lt;code&gt;+&lt;/code&gt;, which Python evaluates as soon as it encounters it. The REPL shows the value &lt;code&gt;15&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 4 to 6&lt;/strong&gt;: The second example includes two lines:&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;import statement&lt;/strong&gt; includes the keyword &lt;code&gt;import&lt;/code&gt; followed by the name of a module. The module name &lt;code&gt;random&lt;/code&gt; is evaluated eagerly. &lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;function call &lt;code&gt;random.randint()&lt;/code&gt;&lt;/strong&gt; is evaluated eagerly, and its value is returned immediately. All standard functions are evaluated eagerly. You’ll learn about generator functions later, which behave differently.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lines 8 to 12&lt;/strong&gt;: The final example has three lines of code:&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;literal to create a list&lt;/strong&gt; is an expression that’s evaluated eagerly. This expression contains several integer literals, which are themselves expressions evaluated immediately.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;assignment statement&lt;/strong&gt; assigns the object created by the list literal to the name &lt;code&gt;numbers&lt;/code&gt;. This statement is not an expression and doesn’t return a value. However, it includes the list literal on the right-hand side, which is an expression that’s evaluated eagerly.&lt;/li&gt;
&lt;li&gt;The final line contains &lt;strong&gt;the name &lt;code&gt;numbers&lt;/code&gt;,&lt;/strong&gt; which is eagerly evaluated to return the list object.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The list you create in the final example is created in full when you define it. Python needs to allocate memory for the list and all its elements. This memory won’t be freed as long as this list exists in your program. The memory allocation in this example is small and won’t impact the program. However, larger objects require more memory, which can cause performance issues.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Lazy_evaluation&quot;&gt;Lazy evaluation&lt;/a&gt;&lt;/strong&gt; refers to cases when Python doesn’t work out the values of an expression immediately. Instead, the values are returned at the point when they’re required in the program. Lazy evaluation can also be referred to as &lt;em&gt;call-by-need&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This delay of when the program evaluates an expression delays the use of resources to create the value, which can improve the performance of a program by spreading the time-consuming process across a longer time period. It also prevents values that will not be used in the program from being generated. This can occur when the program terminates or moves to another part of its execution before all the generated values are used. &lt;/p&gt;
&lt;p&gt;When large datasets are created using lazily-evaluated expressions, the program doesn’t need to use memory to store the data structure’s contents. The values are only generated when they’re needed.&lt;/p&gt;
&lt;p&gt;An example of lazy evaluation occurs within the &lt;code&gt;for&lt;/code&gt; loop when you iterate using &lt;code&gt;range()&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;python&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1_000_001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;This is iteration &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;a href=&quot;https://realpython.com/python-range/&quot;&gt;built-in &lt;code&gt;range()&lt;/code&gt;&lt;/a&gt; is the constructor for Python’s &lt;code&gt;range&lt;/code&gt; object. The &lt;code&gt;range&lt;/code&gt; object does not store all of the one million integers it represents. Instead, the &lt;code&gt;for&lt;/code&gt; loop creates a &lt;code&gt;range_iterator&lt;/code&gt; from the &lt;code&gt;range&lt;/code&gt; object, which generates the next number in the sequence when it’s needed. Therefore, the program never needs to have all the values stored in memory at the same time.&lt;/p&gt;
&lt;p&gt;Lazy evaluation also allows you to create infinite data structures, such as a live stream of audio or video data that continuously updates with new information, since the program doesn’t need to store all the values in memory at the same time. Infinite data structures are not possible with eager evaluation since they can’t be stored in memory.&lt;/p&gt;
&lt;p&gt;There are disadvantages to deferred evaluation. Any errors raised by an expression are also deferred to a later point in the program. This delay can make debugging harder.&lt;/p&gt;
&lt;p&gt;The lazy evaluation of the integers represented by &lt;code&gt;range()&lt;/code&gt; in a &lt;code&gt;for&lt;/code&gt; loop is one example of lazy evaluation. You’ll learn about more examples in the following section of this tutorial.&lt;/p&gt;
&lt;h2 id=&quot;what-are-examples-of-lazy-evaluation-in-python&quot;&gt;What Are Examples of Lazy Evaluation in Python?&lt;a class=&quot;headerlink&quot; href=&quot;#what-are-examples-of-lazy-evaluation-in-python&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In the previous section, you learned about using &lt;code&gt;range()&lt;/code&gt; in a &lt;code&gt;for&lt;/code&gt; loop, which leads to lazy evaluation of the integers represented by the &lt;code&gt;range&lt;/code&gt; object. There are other expressions in Python that lead to lazy evaluation. In this section, you’ll explore the main ones.&lt;/p&gt;
&lt;h3 id=&quot;other-built-in-data-types&quot;&gt;Other Built-In Data Types&lt;a class=&quot;headerlink&quot; href=&quot;#other-built-in-data-types&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-lazy-evaluation/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-lazy-evaluation/ »&lt;/a&gt;&lt;/h2&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Python Basics Exercises: Scopes</title>
      <id>https://realpython.com/courses/python-basics-exercises-scopes/</id>
      <link href="https://realpython.com/courses/python-basics-exercises-scopes/"/>
      <updated>2024-04-23T14:00:00+00:00</updated>
      <summary>In this Python Basics Exercises course, you&#x27;ll practice working with scope in Python. You&#x27;ll reinforce important coding concepts by working with variables inside and outside of functions and loops. You&#x27;ll also get to practice the LEGB rule.</summary>
      <content type="html">
        &lt;p&gt;On your Python journey, you&amp;rsquo;ve worked with &lt;a href=&quot;https://realpython.com/courses/python-basics-functions-loops/&quot;&gt;functions and loops&lt;/a&gt;. To fully understand functions and loops in Python, you need to be familiar with the issue of scope.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this Python Basics Exercises video course, you&amp;rsquo;ll practice:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Identifying the &lt;strong&gt;scope&lt;/strong&gt; of objects&lt;/li&gt;
&lt;li&gt;Working with the &lt;code&gt;global&lt;/code&gt; keyword&lt;/li&gt;
&lt;li&gt;Exploring the &lt;strong&gt;LEGB rule&lt;/strong&gt; for scope resolution&lt;/li&gt;
&lt;li&gt;Using the &lt;code&gt;return&lt;/code&gt; statement in a function&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Scope can be one of the more difficult programming concepts to understand, so in this video course, you&amp;rsquo;ll get additional practice working with it.&lt;/p&gt;
&lt;p&gt;This video course is part of the Python Basics series, which accompanies &lt;a href=&quot;https://realpython.com/products/python-basics-book/&quot;&gt;&lt;em&gt;Python Basics: A Practical Introduction to Python 3&lt;/em&gt;&lt;/a&gt;. You can also check out the other &lt;a href=&quot;https://realpython.com/learning-paths/python-basics/&quot;&gt;Python Basics courses&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;Note that you&amp;rsquo;ll be using &lt;a href=&quot;https://realpython.com/python-idle/&quot;&gt;IDLE&lt;/a&gt; to &lt;a href=&quot;https://realpython.com/interacting-with-python/&quot;&gt;interact with Python&lt;/a&gt; throughout this course. If you&amp;rsquo;re just getting started, then you might want to check out &lt;a href=&quot;https://realpython.com/courses/setting-up-python/&quot;&gt;Python Basics: Setting Up Python&lt;/a&gt; before diving into this course.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Quiz: Python Sequences: A Comprehensive Guide</title>
      <id>https://realpython.com/quizzes/python-sequences/</id>
      <link href="https://realpython.com/quizzes/python-sequences/"/>
      <updated>2024-04-23T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of sequences in Python. You&#x27;ll revisit the basic characteristics of a sequence, operations common to most sequences, special methods associated with sequences, and how to create user-defined mutable and immutable sequences.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of
&lt;a href=&quot;https://realpython.com/python-sequences/&quot;&gt;Python sequences&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By working through this quiz, you&amp;rsquo;ll revisit the basic characteristics of a sequence, operations common to most sequences, special methods associated with sequences, and how to create user-defined mutable and immutable sequences.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Write Unit Tests for Your Python Code With ChatGPT</title>
      <id>https://realpython.com/chatgpt-unit-tests-python/</id>
      <link href="https://realpython.com/chatgpt-unit-tests-python/"/>
      <updated>2024-04-22T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll learn how to use ChatGPT to generate tests for your Python code. You&#x27;ll use the chat to create doctest, unittest, and pytest tests for your code.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Having a good battery of tests for your code may be a requirement for many Python projects. In practice, writing &lt;strong&gt;unit tests&lt;/strong&gt; is hard and can take a lot of time and effort. Therefore, some developers don’t like to write them. However, with &lt;strong&gt;large language models&lt;/strong&gt; (LLMs) and tools like &lt;strong&gt;ChatGPT&lt;/strong&gt;, you can quickly create robust and complete sets of tests for your Python code.&lt;/p&gt;
&lt;p&gt;In Python, you can use multiple different tools for writing tests. The most commonly used tools include &lt;code&gt;doctest&lt;/code&gt;, &lt;code&gt;unittest&lt;/code&gt;, and &lt;code&gt;pytest&lt;/code&gt;. ChatGPT can be of great help in writing tests with any of these tools.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this tutorial, you’ll:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Prompt ChatGPT to create tests using &lt;strong&gt;&lt;code&gt;doctest&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Use ChatGPT to write &lt;strong&gt;&lt;code&gt;unittest&lt;/code&gt;&lt;/strong&gt; tests, fixtures, and suites&lt;/li&gt;
&lt;li&gt;Craft ChatGPT prompts to write &lt;strong&gt;&lt;code&gt;pytest&lt;/code&gt;&lt;/strong&gt; tests and fixtures&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;alternative prompts&lt;/strong&gt; for cases where the code isn’t available&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To get the most out of this tutorial, you should &lt;a href=&quot;https://realpython.com/chatgpt-coding-mentor-python/#set-up-your-chatgpt-coding-mentor&quot;&gt;set up a ChatGPT account&lt;/a&gt; and know the basics of interacting with this tool using &lt;a href=&quot;https://realpython.com/practical-prompt-engineering/&quot;&gt;prompt engineering&lt;/a&gt;. You should also know the basics of how to &lt;a href=&quot;https://realpython.com/python-testing/&quot;&gt;test code&lt;/a&gt; in Python.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/chatgpt-unit-tests-python/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-chatgpt-unit-tests-python&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; you’ll use to write unit tests for your Python code using ChatGPT.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;benefits-of-using-chatgpt-for-testing-python-code&quot;&gt;Benefits of Using ChatGPT for Testing Python Code&lt;a class=&quot;headerlink&quot; href=&quot;#benefits-of-using-chatgpt-for-testing-python-code&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Having good and up-to-date &lt;a href=&quot;https://realpython.com/python-testing/#unit-tests-vs-integration-tests&quot;&gt;unit tests&lt;/a&gt; for your code is a must for any Python project. Poorly tested code or code without tests may end up being unreliable and weak. With automated tests, you can ensure and show that your code works correctly in different scenarios. So, having tests is important from the technical and commercial point of view.&lt;/p&gt;
&lt;p&gt;Writing good tests is hard and can take a lot of time. That’s why some developers don’t like to write them at all. Using &lt;a href=&quot;https://en.wikipedia.org/wiki/Large_language_model&quot;&gt;large language models (LLM)&lt;/a&gt; like &lt;a href=&quot;https://realpython.com/chatgpt-coding-mentor-python/&quot;&gt;ChatGPT&lt;/a&gt; can be a viable alternative for providing your projects and code with proper tests.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You can use ChatGPT and other LLMs in many useful ways in the context of Python development. Check out the following resources for other use cases and useful insights:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/chatgpt-coding-mentor-python/&quot;&gt;ChatGPT: Your Personal Python Coding Mentor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/podcasts/rpp/174/&quot;&gt;Episode 174: Considering ChatGPT’s Technical Review of a Programming Book&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/document-python-code-with-chatgpt/&quot;&gt;Document Your Python Code and Projects With ChatGPT&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/practical-prompt-engineering/&quot;&gt;Prompt Engineering: A Practical Example&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/build-llm-rag-chatbot-with-langchain/&quot;&gt;Build an LLM RAG Chatbot With LangChain&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;Some of the benefits of using ChatGPT to write tests for your Python code include the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Efficiency and speed&lt;/strong&gt;: It can generate unit tests based on specifications or code snippets. This possibility significantly reduces the time that you need to spend writing tests. So you can focus on writing application logic.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Coverage improvement&lt;/strong&gt;: It can suggest tests for edge cases or scenarios that developers might not immediately consider. This way, you can improve your code’s test &lt;a href=&quot;https://coverage.readthedocs.io/&quot;&gt;coverage&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error reduction&lt;/strong&gt;: It can reduce human error in writing repetitive or boilerplate test code.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Learning and onboarding&lt;/strong&gt;: It can serve as an educational tool for developers who are new to testing frameworks or unfamiliar with best practices in unit testing. The generated tests can help developers learn about testing patterns, &lt;a href=&quot;https://realpython.com/python-assert-statement/&quot;&gt;assertions&lt;/a&gt;, and ways to effectively write tests.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With ChatGPT, you can generate unit tests for your Python code in almost no time. However, you must note that even though the generated tests can look good, you should still review and possibly refine the generated tests.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; For this tutorial, you’ll use a free ChatGPT account that’s powered by the GPT-3.5 LLM. You can use this model for free with the default &lt;a href=&quot;https://chat.openai.com/auth/login&quot;&gt;web interface&lt;/a&gt; by OpenAI. If you use a paid account with GPT-4, then your results might be more accurate and faster.&lt;/p&gt;
&lt;p&gt;Additionally, you’ll use an individual chat thread for each major section in the tutorial. So you’ll use different threads for the &lt;code&gt;doctest&lt;/code&gt;, &lt;code&gt;unittest&lt;/code&gt;, and &lt;code&gt;pytest&lt;/code&gt; sections.&lt;/p&gt;
&lt;p&gt;Using a single chat thread to run all the examples in this tutorial may cause the chat to generate significantly different responses because of the past context, which includes previous prompts and responses.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;In the following sections, you’ll learn the basics of using ChatGPT as an assistant for creating coherent unit tests for your Python projects using different test frameworks and libraries.&lt;/p&gt;
&lt;h2 id=&quot;writing-doctest-tests-with-chatgpt&quot;&gt;Writing &lt;code&gt;doctest&lt;/code&gt; Tests With ChatGPT&lt;a class=&quot;headerlink&quot; href=&quot;#writing-doctest-tests-with-chatgpt&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Python’s &lt;a href=&quot;https://docs.python.org/3/library/doctest.html#module-doctest&quot;&gt;&lt;code&gt;doctest&lt;/code&gt;&lt;/a&gt; module provides a lightweight &lt;strong&gt;testing framework&lt;/strong&gt; that you can use for &lt;a href=&quot;https://en.wikipedia.org/wiki/Test_automation&quot;&gt;test automation&lt;/a&gt;. It can read the test cases from your project’s documentation and your code’s docstrings. This framework comes with the Python interpreter, so you’ll have it at your disposal with any Python installation, which is great.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To dive deeper into how to use the &lt;code&gt;doctest&lt;/code&gt; module for testing your code, check out &lt;a href=&quot;https://realpython.com/python-doctest/&quot;&gt;Python’s &lt;code&gt;doctest&lt;/code&gt;: Document and Test Your Code at Once&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;With &lt;code&gt;doctest&lt;/code&gt;, you can write code that provides examples of how your code works, simulating &lt;a href=&quot;https://realpython.com/python-repl/&quot;&gt;REPL&lt;/a&gt; sessions. These examples work as documentation and also as test cases or unit tests. Each &lt;code&gt;doctest&lt;/code&gt; test needs a statement or expression and an expected result.&lt;/p&gt;
&lt;p&gt;You can use ChatGPT to help you out with writing &lt;code&gt;doctest&lt;/code&gt; tests. First, you need some code to test. The following function tries to tackle the &lt;a href=&quot;https://en.wikipedia.org/wiki/Fizz_buzz#Programming&quot;&gt;FizzBuzz&lt;/a&gt; challenge, where you return &lt;code&gt;&quot;fizz&quot;&lt;/code&gt; for numbers divisible by three, &lt;code&gt;&quot;buzz&quot;&lt;/code&gt; for those divisible by five, and &lt;code&gt;&quot;fizz buzz&quot;&lt;/code&gt; for those divisible by both three and five.&lt;/p&gt;
&lt;p&gt;Go ahead and create a &lt;code&gt;fizzbuzz.py&lt;/code&gt; file and add the following code to it:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;python&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    &lt;span class=&quot;mr-2&quot; aria-label=&quot;Filename&quot;&gt;&lt;code style=&quot;color: inherit;&quot;&gt;fizzbuzz.py&lt;/code&gt;&lt;/span&gt;
    &lt;div class=&quot;noselect&quot;&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fizzbuzz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;fizz&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;buzz&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;fizz buzz&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This function works okay for numbers divisible by &lt;code&gt;3&lt;/code&gt; or &lt;code&gt;5&lt;/code&gt;. However, there is a slight issue with numbers that are divisible by both. Even though you know this, go ahead and start a new chat thread. Then, prompt ChatGPT to write &lt;code&gt;doctest&lt;/code&gt; test for the function:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;You:&lt;/strong&gt; Write &lt;code&gt;doctest&lt;/code&gt; tests for the following function:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;python&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fizzbuzz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;fizz&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;buzz&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;fizz buzz&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;ChatGPT:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Certainly! The function you’ve provided implements a basic FizzBuzz algorithm but with a slight logical error. The check for divisibility by &lt;code&gt;15&lt;/code&gt; should come before checks for &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;5&lt;/code&gt;, as every number divisible by &lt;code&gt;15&lt;/code&gt; is also divisible by both &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;5&lt;/code&gt;, but your current order will never reach the &lt;code&gt;&quot;fizz buzz&quot;&lt;/code&gt; case. Here’s a corrected version of your function with the logical order fixed, followed by &lt;code&gt;doctest&lt;/code&gt; tests for it:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;python&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fizzbuzz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    Returns &#x27;fizz&#x27; if number is divisible by 3,&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &#x27;buzz&#x27; if number is divisible by 5,&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &#x27;fizz buzz&#x27; if number is divisible by 15,&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    and the number itself otherwise.&lt;/span&gt;

&lt;span class=&quot;sd&quot;&gt;    &amp;gt;&amp;gt;&amp;gt; fizzbuzz(3)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &#x27;fizz&#x27;&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &amp;gt;&amp;gt;&amp;gt; fizzbuzz(5)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &#x27;buzz&#x27;&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &amp;gt;&amp;gt;&amp;gt; fizzbuzz(15)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &#x27;fizz buzz&#x27;&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &amp;gt;&amp;gt;&amp;gt; fizzbuzz(4)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    4&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &amp;gt;&amp;gt;&amp;gt; fizzbuzz(30)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &#x27;fizz buzz&#x27;&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &amp;gt;&amp;gt;&amp;gt; fizzbuzz(9)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &#x27;fizz&#x27;&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &amp;gt;&amp;gt;&amp;gt; fizzbuzz(10)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &#x27;buzz&#x27;&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &amp;gt;&amp;gt;&amp;gt; fizzbuzz(2)&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    2&lt;/span&gt;
&lt;span class=&quot;sd&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;fizz buzz&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;fizz&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;elif&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;buzz&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;To use these &lt;code&gt;doctest&lt;/code&gt; tests, you’ll need to include them in the docstring of your function as shown above. Then, you can run the tests with the &lt;code&gt;doctest&lt;/code&gt; module by adding the following to your script:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;python&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;doctest&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;doctest&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;testmod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This code will automatically run the tests defined in the docstrings when you execute the Python file. Remember, the corrected order of conditions in the function ensures that numbers divisible by both &lt;code&gt;3&lt;/code&gt; and &lt;code&gt;5&lt;/code&gt; return &lt;code&gt;&quot;fizz buzz&quot;&lt;/code&gt;, which is crucial for the FizzBuzz logic.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Wow! That great! The chat not only wrote the &lt;code&gt;doctest&lt;/code&gt; tests for you. It also fixed the issue, explained important concepts around &lt;code&gt;doctest&lt;/code&gt;, and provided instructions on how to run the tests. Note that the tests cover both regular and edge cases, which is pretty convenient.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/chatgpt-unit-tests-python/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/chatgpt-unit-tests-python/ »&lt;/a&gt;&lt;/h2&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>The Real Python Podcast – Episode #201: Decoupling Systems to Get Closer to the Data</title>
      <id>https://realpython.com/podcasts/rpp/201/</id>
      <link href="https://realpython.com/podcasts/rpp/201/"/>
      <updated>2024-04-19T12:00:00+00:00</updated>
      <summary>What are the benefits of using a decoupled data processing system? How do you write reusable queries for a variety of backend data platforms? This week on the show, Phillip Cloud, the lead maintainer of Ibis, will discuss this portable Python dataframe library.</summary>
      <content type="html">
        &lt;p&gt;What are the benefits of using a decoupled data processing system? How do you write reusable queries for a variety of backend data platforms? This week on the show, Phillip Cloud, the lead maintainer of Ibis, will discuss this portable Python dataframe library.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Quiz: Python&#x27;s unittest: Writing Unit Tests for Your Code</title>
      <id>https://realpython.com/quizzes/python-unittest/</id>
      <link href="https://realpython.com/quizzes/python-unittest/"/>
      <updated>2024-04-19T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of Python testing with the unittest framework from the standard library. With this knowledge, you&#x27;ll be able to create basic tests, execute them, and find bugs before your users do.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of testing your Python code with the &lt;a href=&quot;https://realpython.com/python-unittest/&quot;&gt;unittest framework&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Testing in Python is a huge topic and can come with a lot of complexity, but it doesn&amp;rsquo;t need to be hard. You can create simple tests for your application in a few easy steps and then build on them from there.&lt;/p&gt;
&lt;p&gt;With this quiz, you can check your understanding of testing with Python&amp;rsquo;s &lt;code&gt;unittest&lt;/code&gt; framework. Good luck!&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>How to Format Floats Within F-Strings in Python</title>
      <id>https://realpython.com/how-to-python-f-string-format-float/</id>
      <link href="https://realpython.com/how-to-python-f-string-format-float/"/>
      <updated>2024-04-17T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll learn how to use Python format specifiers within an f-string to allow you to neatly format a float to your required precision.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;You’ll often need to format and round a Python &lt;a href=&quot;https://docs.python.org/3/library/functions.html#float&quot;&gt;&lt;code&gt;float&lt;/code&gt;&lt;/a&gt; to display the results of your calculations neatly within strings. In earlier versions of Python, this was a messy thing to do because you needed to round your numbers &lt;em&gt;first&lt;/em&gt; and then use either string concatenation or the &lt;a href=&quot;https://realpython.com/python-string-formatting/#1-old-style-string-formatting-operator&quot;&gt;old string formatting&lt;/a&gt; technique to do this for you. &lt;/p&gt;
&lt;p&gt;Since Python 3.6, the &lt;a href=&quot;https://peps.python.org/pep-0498/&quot;&gt;literal string interpolation&lt;/a&gt;, more commonly known as a &lt;strong&gt;formatted string literal&lt;/strong&gt; or &lt;strong&gt;&lt;a href=&quot;https://realpython.com/python-f-strings/&quot;&gt;f-string&lt;/a&gt;&lt;/strong&gt;, allows you to customize the content of your strings in a more readable way.&lt;/p&gt;
&lt;p&gt;An f-string is a literal string prefixed with a lowercase or uppercase letter &lt;code&gt;f&lt;/code&gt; and contains zero or more &lt;strong&gt;replacement fields&lt;/strong&gt; enclosed within a pair of curly braces &lt;code&gt;{...}&lt;/code&gt;. Each field contains an &lt;strong&gt;expression&lt;/strong&gt; that produces a value. You can calculate the field’s content, but you can also use function calls or even variables.&lt;/p&gt;
&lt;p&gt;While most strings have a constant value, f-strings are evaluated at runtime. This makes it possible for you to pass different data into the replacement fields of the same f-string and produce different output. This extensibility of f-strings makes them a great way to embed dynamic content neatly inside strings. However, even though f-strings have largely replaced the earlier methods, they do have their short-comings.&lt;/p&gt;
&lt;p&gt;For example, one of the most common attacks performed on a &lt;a href=&quot;https://en.wikipedia.org/wiki/Relational_database&quot;&gt;relational database&lt;/a&gt; is a &lt;a href=&quot;https://en.wikipedia.org/wiki/SQL_injection&quot;&gt;SQL injection&lt;/a&gt; attack. Often, users provide parameters to SQL queries, and if the query is formed within an f-string, it may be possible to damage a database by passing in rogue commands. F-strings can also be used in a &lt;a href=&quot;https://en.wikipedia.org/wiki/Denial-of-service_attack&quot;&gt;denial-of-service&lt;/a&gt; attack by attacking &lt;a href=&quot;https://realpython.com/python-logging/&quot;&gt;Python’s logging module&lt;/a&gt; code.&lt;/p&gt;
&lt;p&gt;In older versions of Python, f-strings had a number of &lt;a href=&quot;https://realpython.com/python312-f-strings/#f-strings-had-some-limitations-before-python-312&quot;&gt;other limitations&lt;/a&gt; that were only fixed with &lt;a href=&quot;https://realpython.com/python312-f-strings/&quot;&gt;Python version 3.12&lt;/a&gt;. This version is used throughout this tutorial.&lt;/p&gt;
&lt;p&gt;Take a look at the example below. It shows you how to embed the result of a calculation within an f-string:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;One third, expressed as a float is: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;One third, expressed as a float is: 0.3333333333333333&#x27;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Without any explicit rounding, once an expression has produced its value it’ll be inserted into the string using a default number of decimal places. Here, the result is shown to sixteen decimal places, which, in most cases, is more precision than you’ll ever need. So you’ll likely want to round your final answer to a more practical number of digits.&lt;/p&gt;
&lt;p&gt;In this tutorial, you’ll learn how to use a Python f-string to format floats to meet your requirements.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/how-to-python-f-string-format-float-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-how-to-python-f-string-format-float-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code and exercise solutions &lt;/a&gt;you’ll use for learning how to format floats within f-strings in Python.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;container border rounded text-wrap-pretty my-3&quot;&gt;

  &lt;p class=&quot;my-3&quot;&gt;&lt;mark class=&quot;marker-highlight&quot;&gt;&lt;strong&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@quiz&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt; Take the Quiz:&lt;/strong&gt;&lt;/mark&gt; Test your knowledge with our interactive “Format Floats Within F-Strings” quiz. You’ll receive a score upon completion to help you track your learning progress:&lt;/p&gt;

  &lt;hr&gt;

  &lt;div class=&quot;row my-3&quot;&gt;
    &lt;div class=&quot;col-xs-12 col-sm-4 col-md-3 align-self-center&quot;&gt;

      &lt;a href=&quot;/quizzes/format-floats-within-f-strings/&quot; tabindex=&quot;-1&quot;&gt;
        &lt;div class=&quot;embed-responsive embed-responsive-16by9&quot;&gt;

            &lt;img class=&quot;card-img-top m-0 p-0 embed-responsive-item rounded&quot; style=&quot;object-fit: contain; background: #ff7e74;&quot; alt=&quot;How to Format Floats Within F-Strings in Python&quot; src=&quot;https://files.realpython.com/media/How-to-Display-a-Fixed-Amount-of-Digits-in-a-String_Watermarked.9c8579fa6ac2.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/How-to-Display-a-Fixed-Amount-of-Digits-in-a-String_Watermarked.9c8579fa6ac2.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/How-to-Display-a-Fixed-Amount-of-Digits-in-a-String_Watermarked.9c8579fa6ac2.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/How-to-Display-a-Fixed-Amount-of-Digits-in-a-String_Watermarked.9c8579fa6ac2.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/How-to-Display-a-Fixed-Amount-of-Digits-in-a-String_Watermarked.9c8579fa6ac2.jpg 1920w&quot; sizes=&quot;(min-width: 1200px) 142px, (min-width: 1000px) 122px, (min-width: 780px) 112px, (min-width: 580px) 139px, calc(100vw - 62px)&quot;&gt;


          &lt;div class=&quot;card-img-overlay d-flex align-items-center&quot;&gt;
            &lt;div class=&quot;mx-auto&quot;&gt;
              &lt;span class=&quot;text-light&quot; style=&quot;opacity: 0.90;&quot;&gt;&lt;span class=&quot;icon baseline scale2x&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@quiz&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/a&gt;

    &lt;/div&gt;
    &lt;div class=&quot;col&quot;&gt;
      &lt;div class=&quot;mt-3 d-md-none&quot;&gt;&lt;/div&gt; 
      &lt;p class=&quot;small text-muted mb-0&quot;&gt;&lt;strong&gt;Interactive Quiz&lt;/strong&gt;&lt;/p&gt;
      &lt;a href=&quot;/quizzes/format-floats-within-f-strings/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Format Floats Within F-Strings&lt;/span&gt;&lt;/a&gt; 
      &lt;p class=&quot;text-muted mb-0 small&quot;&gt;In this quiz, you&#x27;ll test your understanding of how to format floats within f-strings in Python. This knowledge will let you control the precision and appearance of floating-point numbers when you incorporate them into formatted strings.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;how-to-format-and-round-a-float-within-a-python-f-string&quot;&gt;How to Format and Round a Float Within a Python F-String&lt;a class=&quot;headerlink&quot; href=&quot;#how-to-format-and-round-a-float-within-a-python-f-string&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;To format a &lt;code&gt;float&lt;/code&gt; for neat display within a Python f-string, you can use a &lt;strong&gt;format specifier&lt;/strong&gt;. In its most basic form, this allows you to define the &lt;strong&gt;precision&lt;/strong&gt;, or number of decimal places, the &lt;code&gt;float&lt;/code&gt; will be displayed with.&lt;/p&gt;
&lt;p&gt;The code below displays the same calculation as before, only it’s displayed more neatly:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;One third, rounded to two decimal places is: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.2f&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;One third, rounded to two decimal places is: 0.33&#x27;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;To use Python’s format specifiers in a replacement field, you separate them from the expression with a colon (&lt;code&gt;:&lt;/code&gt;). As you can see, your &lt;code&gt;float&lt;/code&gt; has been &lt;a href=&quot;https://realpython.com/python-rounding/&quot;&gt;rounded&lt;/a&gt; to two decimal places. You achieved this by adding the format specifier &lt;code&gt;.2f&lt;/code&gt; into the replacement field. The &lt;code&gt;2&lt;/code&gt; is the precision, while the lowercase &lt;code&gt;f&lt;/code&gt; is an example of a &lt;strong&gt;presentation type&lt;/strong&gt;. You’ll see more of these later.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you use a format specifier, you don’t actually change the underlying number. You only improve its display.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Python’s f-strings also have their own &lt;a href=&quot;https://docs.python.org/3/library/string.html#format-specification-mini-language&quot;&gt;mini-language&lt;/a&gt; that allows you to format your output in a variety of different ways. Although this tutorial will focus on rounding, this is certainly not the only thing you can use them for. As you’ll see later, their mini-language is also used in other string formatting techniques.&lt;/p&gt;
&lt;p&gt;In addition to displaying the result of calculations, the precision part of a format specifier can also be applied directly to variables and the return values of function calls:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;total_price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cost&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;1.2&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost_price&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tax&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;
&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;£&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;,.2f&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; + £&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost_price&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tax&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;,.2f&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; = £&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cost_price&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;,.2f&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;&#x27;£1,000.00 + £200.00 = £1,200.00&#x27;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This time, you’ve used multiple replacement fields in the same string. The first one formats a literal number, the second formats the result of a calculation, while the third formats the return value from a function call. Also, by inserting a comma (&lt;code&gt;,&lt;/code&gt;) before the decimal point (&lt;code&gt;.&lt;/code&gt;) in the format specifier, you add a thousands separator to your final output.&lt;/p&gt;
&lt;p&gt;In everyday use, you display numbers with a fixed amount of decimals, but when performing scientific or engineering calculations, you may prefer to format them using &lt;a href=&quot;https://en.wikipedia.org/wiki/Significant_figures&quot;&gt;significant figures&lt;/a&gt;. Your results are then assumed to be accurate to the number of significant figures you display them with.&lt;/p&gt;
&lt;p&gt;If you want to round numbers to significant figures, you use the lowercase letter &lt;code&gt;g&lt;/code&gt; in the format specifier. You can also use an uppercase &lt;code&gt;G&lt;/code&gt;, but this automatically switches the format to scientific notation for large numbers.&lt;/p&gt;
&lt;p&gt;Suppose you have a circle with a radius of 10.203 meters. To work out the area, you could use this code:&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/how-to-python-f-string-format-float/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/how-to-python-f-string-format-float/ »&lt;/a&gt;&lt;/h2&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Using raise for Effective Exceptions</title>
      <id>https://realpython.com/courses/using-raise-for-effective-exceptions/</id>
      <link href="https://realpython.com/courses/using-raise-for-effective-exceptions/"/>
      <updated>2024-04-16T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn how to raise exceptions in Python, which will improve your ability to efficiently handle errors and exceptional situations in your code. This way, you&#x27;ll write more reliable, robust, and maintainable code.</summary>
      <content type="html">
        &lt;p&gt;In your Python journey, you&amp;rsquo;ll come across situations where you need to signal that something is going wrong in your code. For example, maybe a file doesn&amp;rsquo;t exist, a network or database connection fails, or your code gets invalid input. A common approach to tackle these issues is to &lt;strong&gt;raise an exception&lt;/strong&gt;, notifying the user that an error has occurred. That&amp;rsquo;s what Python&amp;rsquo;s &lt;code&gt;raise&lt;/code&gt; statement is for.&lt;/p&gt;
&lt;p&gt;Learning about the &lt;code&gt;raise&lt;/code&gt; statement allows you to effectively handle errors and exceptional situations in your code. This way, you&amp;rsquo;ll develop more robust programs and higher-quality code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course, you&amp;rsquo;ll learn how to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Raise exceptions in Python using the &lt;strong&gt;&lt;code&gt;raise&lt;/code&gt;&lt;/strong&gt; statement&lt;/li&gt;
&lt;li&gt;Decide &lt;strong&gt;which exceptions&lt;/strong&gt; to raise and &lt;strong&gt;when&lt;/strong&gt; to raise them in your code&lt;/li&gt;
&lt;li&gt;Explore common &lt;strong&gt;use cases&lt;/strong&gt; for raising exceptions in Python&lt;/li&gt;
&lt;li&gt;Apply &lt;strong&gt;best practices&lt;/strong&gt; for raising exceptions in your Python code&lt;/li&gt;
&lt;/ul&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Build a Blog Using Django, GraphQL, and Vue</title>
      <id>https://realpython.com/python-django-blog/</id>
      <link href="https://realpython.com/python-django-blog/"/>
      <updated>2024-04-15T14:00:00+00:00</updated>
      <summary>In this step-by-step project, you&#x27;ll build a blog from the ground up. You&#x27;ll turn your Django blog data models into a GraphQL API and consume it in a Vue application for users to read. You&#x27;ll end up with an admin site and a user-facing site you can continue to refine for your own use.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Are you a regular Django user? Do you find yourself wanting to decouple your back end and front end? Do you want to handle data persistence in the API while you display the data in a single-page app (SPA) in the browser using a JavaScript framework like React or Vue? &lt;/p&gt;
&lt;p&gt;If you answered yes to any of these questions, then you’re in luck. This tutorial will take you through the process of building a Django blog back end and a &lt;a href=&quot;https://vuejs.org/&quot;&gt;Vue&lt;/a&gt; front end, using &lt;a href=&quot;https://graphql.org/&quot;&gt;GraphQL&lt;/a&gt; to communicate between them.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://realpython.com/intermediate-python-project-ideas/&quot;&gt;Projects&lt;/a&gt; are an effective way to learn and solidify concepts. This tutorial is structured as a step-by-step project so you can learn in a hands-on way and take breaks as needed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this tutorial, you’ll learn how to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Translate your &lt;strong&gt;Django models&lt;/strong&gt; into a &lt;strong&gt;GraphQL API&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Run the &lt;strong&gt;Django server&lt;/strong&gt; and a &lt;strong&gt;Vue application&lt;/strong&gt; on your computer at the same time&lt;/li&gt;
&lt;li&gt;Administer your blog posts in the &lt;strong&gt;Django admin&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Consume a GraphQL API in Vue to &lt;strong&gt;show data in the browser&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can download all the source code you’ll use to build your Django blog application by clicking the link below:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/django-vue-graphql/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-django-vue-graphql&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that you’ll use to build a blog using Django, GraphQL, and Vue.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;demo-a-django-blog-admin-a-graphql-api-and-a-vue-front-end&quot;&gt;Demo: A Django Blog Admin, a GraphQL API, and a Vue Front End&lt;a class=&quot;headerlink&quot; href=&quot;#demo-a-django-blog-admin-a-graphql-api-and-a-vue-front-end&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://realpython.com/build-a-blog-from-scratch-django/&quot;&gt;Blog applications&lt;/a&gt; are a common starter project because they involve create, read, update, and delete (CRUD) operations. In this project, you’ll use the Django admin to do the heavy CRUD lifting and you’ll focus on providing a GraphQL API for your blog data.&lt;/p&gt;
&lt;p&gt;You’ll use &lt;a href=&quot;https://vuejs.org/&quot;&gt;Vue.js 3&lt;/a&gt; and its &lt;a href=&quot;https://vuejs.org/glossary/#composition-api&quot;&gt;composition API&lt;/a&gt; for the front end of your blog. Vue lets you create dynamic interfaces pretty smoothly, thanks to its reactive data binding and easy-to-manage components. Plus, since you’re dealing with data from a GraphQL API, you can leverage the Vue Apollo plugin.&lt;/p&gt;
&lt;p&gt;Here’s a demonstration of the completed project in action:&lt;/p&gt;
&lt;figure&gt;
  &lt;div class=&quot;embed-responsive embed-responsive-16by9 rounded mb-3 border&quot;&gt;
    &lt;iframe loading=&quot;lazy&quot; class=&quot;embed-responsive-item&quot; src=&quot;https://player.vimeo.com/video/918625881?background=1&quot; frameborder=&quot;0&quot; allow=&quot;fullscreen&quot; allowfullscreen&gt;&lt;/iframe&gt;
  &lt;/div&gt;

&lt;/figure&gt;

&lt;p&gt;Next, you’ll make sure you have all the necessary background information and tools before you dive in and build your blog application.&lt;/p&gt;
&lt;h2 id=&quot;project-overview&quot;&gt;Project Overview&lt;a class=&quot;headerlink&quot; href=&quot;#project-overview&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For this project, you’ll create a small blogging application with some rudimentary features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Authors&lt;/strong&gt; can write many posts.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Posts&lt;/strong&gt; can have many tags and can be either published or unpublished.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You’ll build the back end of this blog in Django, complete with an admin for adding new blog content. Then you’ll expose the content data as a GraphQL API and use Vue to display that data in the browser.&lt;/p&gt;
&lt;p&gt;You’ll accomplish this in several high-level steps. At the end of each step, you’ll find a link to the source code for that stage of the project.&lt;/p&gt;
&lt;p&gt;If you’re curious about how the source code for each step looks, then you can click the link below:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/django-vue-graphql/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-django-vue-graphql&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that you’ll use to build a blog using Django, GraphQL, and Vue.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;a class=&quot;headerlink&quot; href=&quot;#prerequisites&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You’ll be best equipped for this tutorial if you already have a solid foundation in some web application concepts. You should understand how &lt;a href=&quot;https://realpython.com/python-requests/&quot;&gt;HTTP requests and responses&lt;/a&gt; and APIs work. You can check out &lt;a href=&quot;https://realpython.com/python-api/&quot;&gt;Python &amp;amp; APIs: A Winning Combo for Reading Public Data&lt;/a&gt; to understand the details of using GraphQL APIs vs REST APIs.&lt;/p&gt;
&lt;p&gt;Because you’ll use Django to build the back end for your blog, you’ll want to be familiar with &lt;a href=&quot;https://realpython.com/django-setup/&quot;&gt;starting a Django project&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/customize-django-admin-python/&quot;&gt;customizing the Django admin&lt;/a&gt;. If you haven’t used Django much before, you might also want to try building another Django-only project first. For a good introduction, check out &lt;a href=&quot;https://realpython.com/get-started-with-django-1/&quot;&gt;Get Started with Django Part 1: Build a Portfolio App&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;And because you’ll be using Vue on the front end, some experience with &lt;a href=&quot;https://realpython.com/python-vs-javascript/&quot;&gt;JavaScript&lt;/a&gt; will also help. If you’ve only used a JavaScript framework like &lt;a href=&quot;https://jquery.com/&quot;&gt;jQuery&lt;/a&gt; in the past, the &lt;a href=&quot;https://vuejs.org/guide/introduction.html&quot;&gt;Vue introduction&lt;/a&gt; is a good foundation.&lt;/p&gt;
&lt;p&gt;Familiarity with JSON is also important because GraphQL queries are JSON-like and return data in JSON format. You can read about &lt;a href=&quot;https://realpython.com/python-json/&quot;&gt;Working with JSON Data in Python&lt;/a&gt; for an introduction.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-django-blog/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-django-blog/ »&lt;/a&gt;&lt;/h2&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Quiz: Write Pythonic and Clean Code With namedtuple</title>
      <id>https://realpython.com/quizzes/python-namedtuple/</id>
      <link href="https://realpython.com/quizzes/python-namedtuple/"/>
      <updated>2024-04-15T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of Python&#x27;s namedtuple() function from the collections module. This function allows you to create immutable sequence types that you can access using descriptive field names and dot notation.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of
&lt;a href=&quot;https://realpython.com/python-namedtuple/&quot;&gt;Python&amp;rsquo;s &lt;code&gt;namedtuple()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By working through this quiz, you&amp;rsquo;ll revisit how to create &lt;code&gt;namedtuple&lt;/code&gt; classes, identify and take advantage of the cool features of &lt;code&gt;namedtuple&lt;/code&gt;, use &lt;code&gt;namedtuple&lt;/code&gt; instances to write Pythonic code, and decide when to use a &lt;code&gt;namedtuple&lt;/code&gt; over a similar data structure.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>The Real Python Podcast – Episode #200: Avoiding Error Culture and Getting Help Inside Python</title>
      <id>https://realpython.com/podcasts/rpp/200/</id>
      <link href="https://realpython.com/podcasts/rpp/200/"/>
      <updated>2024-04-12T12:00:00+00:00</updated>
      <summary>What is error culture, and how do you avoid it within your organization? How do you navigate alert and notification fatigue? Hey, it&#x27;s episode #200! Real Python&#x27;s editor-in-chief, Dan Bader, joins us this week to celebrate. Christopher Trudeau also returns to bring another batch of PyCoder&#x27;s Weekly articles and projects.</summary>
      <content type="html">
        &lt;p&gt;What is error culture, and how do you avoid it within your organization? How do you navigate alert and notification fatigue? Hey, it&#x27;s episode #200! Real Python&#x27;s editor-in-chief, Dan Bader, joins us this week to celebrate. Christopher Trudeau also returns to bring another batch of PyCoder&#x27;s Weekly articles and projects.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Quiz: Format Floats Within F-Strings</title>
      <id>https://realpython.com/quizzes/format-floats-within-f-strings/</id>
      <link href="https://realpython.com/quizzes/format-floats-within-f-strings/"/>
      <updated>2024-04-12T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of how to format floats within f-strings in Python. This knowledge will let you control the precision and appearance of floating-point numbers when you incorporate them into formatted strings.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of
&lt;a href=&quot;https://realpython.com/how-to-python-f-string-format-float/&quot;&gt;How to Format Floats Within F-Strings in Python&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll often need to format and round a Python &lt;code&gt;float&lt;/code&gt; to display the results of your calculations neatly within strings. While there are several ways to format numbers in Python, formatted string literals or &lt;a href=&quot;https://realpython.com/python-f-strings/&quot;&gt;f-strings&lt;/a&gt; are usually the most frequent choice in modern Python programs. Knowing how to use f-strings effectively can make your code more readable and concise.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Quiz: What&#x27;s Lazy Evaluation in Python?</title>
      <id>https://realpython.com/quizzes/python-lazy-evaluation/</id>
      <link href="https://realpython.com/quizzes/python-lazy-evaluation/"/>
      <updated>2024-04-12T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of the differences between lazy and eager evaluation in Python. By working through this quiz, you&#x27;ll revisit how Python optimizes memory use and computational overhead by deciding when to compute values.</summary>
      <content type="html">
        &lt;p&gt;Lazy evaluation is a programming concept where the evaluation of an expression is deferred until its value is actually needed. This can lead to performance enhancements by avoiding unnecessary calculations, and it can also allow for the creation of potentially infinite data structures. As you embark on this quiz, you&amp;rsquo;ll test your knowledge of lazy evaluation in Python and learn how to apply this concept to write cleaner and more efficient code.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Pydantic: Simplifying Data Validation in Python</title>
      <id>https://realpython.com/python-pydantic/</id>
      <link href="https://realpython.com/python-pydantic/"/>
      <updated>2024-04-10T14:00:00+00:00</updated>
      <summary>Discover the power of Pydantic, Python&#x27;s most popular data parsing, validation, and serialization library. In this hands-on tutorial, you&#x27;ll learn how to make your code more robust, trustworthy, and easier to debug with Pydantic.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Pydantic is a powerful data validation and settings management library for Python, engineered to enhance the robustness and reliability of your codebase. From basic tasks, such as checking whether a variable is an integer, to more complex tasks, like ensuring highly-nested dictionary keys and values have the correct data types, Pydantic can handle just about any data validation scenario with minimal boilerplate code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this tutorial, you’ll learn how to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Work with &lt;strong&gt;data schemas&lt;/strong&gt; with Pydantic’s &lt;code&gt;BaseModel&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Write &lt;strong&gt;custom validators&lt;/strong&gt; for complex use cases&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Validate function arguments&lt;/strong&gt; with Pydantic’s &lt;code&gt;@validate_call&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Manage settings and &lt;strong&gt;configure applications&lt;/strong&gt; with &lt;code&gt;pydantic-settings&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Throughout this tutorial, you’ll get hands-on examples of Pydantic’s functionalities, and by the end you’ll have a solid foundation for your own validation use cases. Before starting this tutorial, you’ll benefit from having an intermediate understanding of Python and &lt;a href=&quot;https://realpython.com/python3-object-oriented-programming/&quot;&gt;object-oriented programming&lt;/a&gt;. &lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/pydantic-simplifying-data-validation-in-python/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-pydantic-simplifying-data-validation-in-python&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that you’ll use to help you learn how Pydantic can help you simplify data validation in Python.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;pythons-pydantic-library&quot;&gt;Python’s Pydantic Library&lt;a class=&quot;headerlink&quot; href=&quot;#pythons-pydantic-library&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;One of Python’s main attractions is that it’s a dynamically typed language. Dynamic typing means that variable types are determined at runtime, unlike statically typed languages where they are explicitly declared at compile time. While dynamic typing is great for rapid development and ease of use, you often need more robust type checking and data validation for real-world applications. This is where Python’s Pydantic library has you covered.&lt;/p&gt;
&lt;p&gt;Pydantic has quickly gained popularity, and it’s now the most widely used data validation library for Python. In this first section, you’ll get an overview of Pydantic and a preview of the library’s powerful features. You’ll also learn how to install Pydantic along with the additional dependencies you’ll need for this tutorial.&lt;/p&gt;
&lt;h3 id=&quot;getting-familiar-with-pydantic&quot;&gt;Getting Familiar With Pydantic&lt;a class=&quot;headerlink&quot; href=&quot;#getting-familiar-with-pydantic&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Pydantic is a powerful Python library that leverages &lt;a href=&quot;https://realpython.com/python-type-checking/&quot;&gt;type hints&lt;/a&gt; to help you easily validate and serialize your data schemas. This makes your code more robust, readable, concise, and easier to debug. Pydantic also integrates well with many popular static typing tools and IDEs, which allows you to catch schema issues before running your code.&lt;/p&gt;
&lt;p&gt;Some of Pydantic’s distinguishing features include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Customization&lt;/strong&gt;: There’s almost no limit to the kinds of data you can validate with Pydantic. From primitive Python types to highly nested data structures, Pydantic lets you validate and serialize nearly any Python object.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Flexibility&lt;/strong&gt;: Pydantic gives you control over how strict or lax you want to be when validating your data. In some cases, you might want to coerce incoming data to the correct type. For example, you could accept data that’s intended to be a float but is received as an integer. In other cases, you might want to strictly enforce the data types you’re receiving. Pydantic enables you to do either. &lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Serialization&lt;/strong&gt;: You can serialize and deserialize Pydantic objects as &lt;a href=&quot;https://realpython.com/python-dicts/&quot;&gt;dictionaries&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-json/&quot;&gt;JSON&lt;/a&gt; strings. This means that you can seamlessly convert your Pydantic objects to and from JSON. This capability has led to self-documenting APIs and integration with just about any tool that supports JSON schemas.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: Thanks to its core validation logic written in &lt;a href=&quot;https://www.rust-lang.org/&quot;&gt;Rust&lt;/a&gt;, Pydantic is exceptionally fast. This performance advantage gives you swift and reliable data processing, especially in high-throughput applications such as REST APIs that need to scale to a large number of requests.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Ecosystem and Industry Adoption&lt;/strong&gt;: Pydantic is a dependency of &lt;a href=&quot;https://docs.pydantic.dev/2.0/why/#ecosystem&quot;&gt;many popular Python libraries&lt;/a&gt; such as &lt;a href=&quot;https://realpython.com/fastapi-python-web-apis/&quot;&gt;FastAPI&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/build-llm-rag-chatbot-with-langchain/&quot;&gt;LangChain&lt;/a&gt;, and &lt;a href=&quot;https://realpython.com/polars-python/&quot;&gt;Polars&lt;/a&gt;. It’s also used by most of the &lt;a href=&quot;https://docs.pydantic.dev/2.0/why/#using-pydantic&quot;&gt;largest tech companies&lt;/a&gt; and throughout many other industries. This is a testament to Pydantic’s community support, reliability, and resilience.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are a few key features that make Pydantic an attractive data validation library, and you’ll get to see these in action throughout this tutorial. Up next, you’ll get an overview of how to install Pydantic along with its various dependencies.&lt;/p&gt;
&lt;h3 id=&quot;installing-pydantic&quot;&gt;Installing Pydantic&lt;a class=&quot;headerlink&quot; href=&quot;#installing-pydantic&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Pydantic is available on &lt;a href=&quot;https://pypi.org/&quot;&gt;PyPI&lt;/a&gt;, and you can install it with &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;pip&lt;/a&gt;. Open a terminal or command prompt, create a new virtual environment, and then run the following command to install Pydantic:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;console&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--yellow&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Shell&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp gp-VirtualEnv&quot;&gt;(venv)&lt;/span&gt; &lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;python&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;-m&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;pip&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;install&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;pydantic
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This command will install the latest version of Pydantic from PyPI onto your machine. To verify that the installation was successful, start a &lt;a href=&quot;https://realpython.com/python-repl/&quot;&gt;Python REPL&lt;/a&gt; and import Pydantic:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;pydantic&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;If the import runs without error, then you’ve successfully installed Pydantic, and you now have the core of Pydantic installed on your system.&lt;/p&gt;
&lt;h3 id=&quot;adding-optional-dependencies&quot;&gt;Adding Optional Dependencies&lt;a class=&quot;headerlink&quot; href=&quot;#adding-optional-dependencies&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;You can install optional dependencies with Pydantic as well. For example, you’ll be working with email validation in this tutorial, and you can include these dependencies in your install:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;console&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--yellow&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Shell&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp gp-VirtualEnv&quot;&gt;(venv)&lt;/span&gt; &lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;python&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;-m&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;pip&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;install&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;pydantic[email]&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Pydantic has a separate package for &lt;a href=&quot;https://docs.pydantic.dev/latest/concepts/pydantic_settings/&quot;&gt;settings management&lt;/a&gt;, which you’ll also cover in this tutorial. To install this, run the following command:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;console&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--yellow&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Shell&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp gp-VirtualEnv&quot;&gt;(venv)&lt;/span&gt; &lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;python&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;-m&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;pip&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;install&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;pydantic-settings
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/button&gt;
    &lt;template class=&quot;codeblock__copied-template&quot;&gt;
      &lt;span class=&quot;small&quot;&gt;&lt;span class=&quot;icon baseline mr-1 text-success&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.654fd44c148d.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;With that, you’ve installed all the dependencies you’ll need for this tutorial, and you’re ready to start exploring Pydantic. You’ll start by covering models—Pydantic’s primary way of defining data schemas.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-pydantic/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-pydantic/ »&lt;/a&gt;&lt;/h2&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Generating QR Codes With Python</title>
      <id>https://realpython.com/courses/generating-qr-codes/</id>
      <link href="https://realpython.com/courses/generating-qr-codes/"/>
      <updated>2024-04-09T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn how to use Python to generate QR codes, from your standard black-and-white QR codes to beautiful ones with your favorite colors. You&#x27;ll learn how to format QR codes, rotate them, and even replace the static background with moving images.</summary>
      <content type="html">
        &lt;p&gt;From restaurant e-menus to airline boarding passes, &lt;strong&gt;QR codes&lt;/strong&gt; have numerous applications that impact your day-to-day life and enrich the user&amp;rsquo;s experience. Wouldn&amp;rsquo;t it be great to make them look good, too? With the help of this video course, you&amp;rsquo;ll learn how to use Python to generate beautiful QR codes for your personal use case. &lt;/p&gt;
&lt;p&gt;In its most basic format, a QR code contains black squares and dots on a white background, with information that any smartphone or device with a dedicated QR scanner can decode. Unlike a traditional bar code, which holds information horizontally, a QR code holds the data in two dimensions, and it can hold over a hundred times more information.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course, you&amp;rsquo;ll learn how to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Generate&lt;/strong&gt; a basic black-and-white QR code&lt;/li&gt;
&lt;li&gt;Change the &lt;strong&gt;size&lt;/strong&gt; and &lt;strong&gt;margins&lt;/strong&gt; of the QR code&lt;/li&gt;
&lt;li&gt;Create &lt;strong&gt;colorful&lt;/strong&gt; QR codes&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rotate&lt;/strong&gt; the QR code&lt;/li&gt;
&lt;li&gt;Replace the static background with an &lt;strong&gt;animated GIF&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>The Real Python Podcast – Episode #199: Leveraging Documents and Data to Create a Custom LLM Chatbot</title>
      <id>https://realpython.com/podcasts/rpp/199/</id>
      <link href="https://realpython.com/podcasts/rpp/199/"/>
      <updated>2024-04-05T12:00:00+00:00</updated>
      <summary>How do you customize a LLM chatbot to address a collection of documents and data? What tools and techniques can you use to build embeddings into a vector database? This week on the show, Calvin Hendryx-Parker is back to discuss developing an AI-powered, Large Language Model-driven chat interface.</summary>
      <content type="html">
        &lt;p&gt;How do you customize a LLM chatbot to address a collection of documents and data? What tools and techniques can you use to build embeddings into a vector database? This week on the show, Calvin Hendryx-Parker is back to discuss developing an AI-powered, Large Language Model-driven chat interface.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Quiz: Build Command-Line Interfaces With Python&#x27;s argparse</title>
      <id>https://realpython.com/quizzes/command-line-interfaces-python-argparse/</id>
      <link href="https://realpython.com/quizzes/command-line-interfaces-python-argparse/"/>
      <updated>2024-04-03T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of creating command-line interfaces (CLIs) in Python using the argparse module. This knowledge is essential for creating user-friendly command-line apps, which are common in development, data science, and systems administration.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of
&lt;a href=&quot;https://realpython.com/command-line-interfaces-python-argparse/&quot;&gt;Creating Command-line Interfaces in Python with argparse&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Command-line apps may not be common in the general user&amp;rsquo;s space, but they&amp;rsquo;re present in development, data science, systems administration, and many other operations. Every command-line app needs a user-friendly &lt;a href=&quot;https://en.wikipedia.org/wiki/Command-line_interface&quot;&gt;command-line interface (CLI)&lt;/a&gt; so that you can interact with the app itself. In Python, you can create full-featured CLIs with the &lt;code&gt;argparse&lt;/code&gt; module from the standard library.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Python Deep Learning: PyTorch vs Tensorflow</title>
      <id>https://realpython.com/courses/deep-learning-pytorch-tensorflow/</id>
      <link href="https://realpython.com/courses/deep-learning-pytorch-tensorflow/"/>
      <updated>2024-04-02T14:00:00+00:00</updated>
      <summary>PyTorch vs Tensorflow: Which one should you use? Learn about these two popular deep learning libraries and how to choose the best one for your project.</summary>
      <content type="html">
        &lt;p&gt;PyTorch vs TensorFlow: What&amp;rsquo;s the difference? Both are open source Python libraries that use graphs to perform numerical computation on data. Both are used extensively in academic research and commercial code. Both are extended by a variety of APIs, cloud computing platforms, and model repositories.&lt;/p&gt;
&lt;p&gt;If they&amp;rsquo;re so similar, then which one is best for your project?&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course, you&amp;rsquo;ll learn:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What the differences are between &lt;strong&gt;PyTorch&lt;/strong&gt; and &lt;strong&gt;TensorFlow&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;What &lt;strong&gt;tools&lt;/strong&gt; and &lt;strong&gt;resources&lt;/strong&gt; are available for each&lt;/li&gt;
&lt;li&gt;How to choose the &lt;strong&gt;best option&lt;/strong&gt; for your specific use case&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You&amp;rsquo;ll start by taking a close look at both platforms, beginning with the slightly older TensorFlow, before exploring some considerations that can help you determine which choice is best for your project. Let&amp;rsquo;s get started!&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>The Real Python Podcast – Episode #198: Build a Video Game With Python Turtle &amp; Visualize Data in Seaborn</title>
      <id>https://realpython.com/podcasts/rpp/198/</id>
      <link href="https://realpython.com/podcasts/rpp/198/"/>
      <updated>2024-03-29T12:00:00+00:00</updated>
      <summary>Can you build a Space Invaders clone using Python&#x27;s built-in turtle module? What advantages does the Seaborn data visualization library provide compared to Matplotlib? Christopher Trudeau is back on the show this week, along with special guest Real Python core team member Bartosz Zaczyński. We&#x27;re sharing another batch of PyCoder&#x27;s Weekly articles and projects.</summary>
      <content type="html">
        &lt;p&gt;Can you build a Space Invaders clone using Python&#x27;s built-in turtle module? What advantages does the Seaborn data visualization library provide compared to Matplotlib? Christopher Trudeau is back on the show this week, along with special guest Real Python core team member Bartosz Zaczyński. We&#x27;re sharing another batch of PyCoder&#x27;s Weekly articles and projects.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Quiz: A Practical Introduction to Web Scraping in Python</title>
      <id>https://realpython.com/quizzes/python-web-scraping-practical-introduction/</id>
      <link href="https://realpython.com/quizzes/python-web-scraping-practical-introduction/"/>
      <updated>2024-03-27T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of web scraping in Python. Web scraping is a powerful tool for data collection and analysis. By working through this quiz, you&#x27;ll revisit how to parse website data using string methods, regular expressions, and HTML parsers, as well as how to interact with forms and other website components.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of
&lt;a href=&quot;https://realpython.com/python-web-scraping-practical-introduction/&quot;&gt;Web Scraping in Python&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By working through this quiz, you&amp;rsquo;ll revisit how to parse website data using string methods and regular expressions, how to parse website data using an HTML parser, and how to interact with forms and other website components.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Finding Python Easter Eggs</title>
      <id>https://realpython.com/courses/finding-python-easter-eggs/</id>
      <link href="https://realpython.com/courses/finding-python-easter-eggs/"/>
      <updated>2024-03-26T14:00:00+00:00</updated>
      <summary>Python has its fair share of hidden surprises, commonly known as Easter eggs. From clever jokes to secret messages, these little mysteries are often meant to be discovered by curious geeks like you!</summary>
      <content type="html">
        &lt;p&gt;In this &lt;strong&gt;Code Conversation&lt;/strong&gt;, you&amp;rsquo;ll follow a chat between Philipp and Bartosz as they go on an Easter egg hunt. Along the way, you&amp;rsquo;ll:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Learn about Easter egg hunt traditions&lt;/li&gt;
&lt;li&gt;Uncover the first Easter egg in software&lt;/li&gt;
&lt;li&gt;Explore Easter eggs in Python&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There won&amp;rsquo;t be many code examples in this Code Conversation, so you can lean back and join Philipp and Bartosz on their Easter egg hunt.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Quiz: Get Started With Django: Build a Portfolio App</title>
      <id>https://realpython.com/quizzes/get-started-with-django/</id>
      <link href="https://realpython.com/quizzes/get-started-with-django/"/>
      <updated>2024-03-26T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of Django, a fully featured Python web framework. By working through this quiz, you&#x27;ll revisit the steps to create a fully functioning web application and learn about some of Django&#x27;s most important features.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of &lt;a href=&quot;https://realpython.com/learning-paths/django-web-development/&quot;&gt;Django&lt;/a&gt;. By working through this quiz, you&amp;rsquo;ll revisit the steps you need to &lt;a href=&quot;https://realpython.com/get-started-with-django-1/&quot;&gt;get started with Django&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll touch on topics that will help you create a fully functioning web application and revisit some of the most important features of Django.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll also investigate the architecture of a Django site, remember how to set up a new Django project with multiple apps, build models and views, and create and connect Django templates.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>The Real Python Podcast – Episode #197: Using Python in Bioinformatics and the Laboratory</title>
      <id>https://realpython.com/podcasts/rpp/197/</id>
      <link href="https://realpython.com/podcasts/rpp/197/"/>
      <updated>2024-03-22T12:00:00+00:00</updated>
      <summary>How is Python being used to automate processes in the laboratory? How can it speed up scientific work with DNA sequencing? This week on the show, Chemical Engineering PhD Student Parsa Ghadermazi is here to discuss Python in bioinformatics.</summary>
      <content type="html">
        &lt;p&gt;How is Python being used to automate processes in the laboratory? How can it speed up scientific work with DNA sequencing? This week on the show, Chemical Engineering PhD Student Parsa Ghadermazi is here to discuss Python in bioinformatics.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>SQLite and SQLAlchemy in Python: Move Your Data Beyond Flat Files</title>
      <id>https://realpython.com/courses/sqlite-sqlalchemy-python-beyond-flat-files/</id>
      <link href="https://realpython.com/courses/sqlite-sqlalchemy-python-beyond-flat-files/"/>
      <updated>2024-03-19T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn how to store and retrieve data using Python, SQLite, and SQLAlchemy as well as with flat files. Using SQLite with Python brings with it the additional benefit of accessing data with SQL. By adding SQLAlchemy, you can work with data in terms of objects and methods.</summary>
      <content type="html">
        &lt;p&gt;All programs process data in one form or another, and many need to be able to save and retrieve that data from one invocation to the next. Python, &lt;a href=&quot;https://www.sqlite.org/index.html&quot;&gt;SQLite&lt;/a&gt;, and &lt;a href=&quot;https://www.sqlalchemy.org/&quot;&gt;SQLAlchemy&lt;/a&gt; give your programs database functionality, allowing you to store data in a single file without the need for a database server.&lt;/p&gt;
&lt;p&gt;You can achieve similar results using &lt;a href=&quot;https://en.wikipedia.org/wiki/Flat-file_database&quot;&gt;flat files&lt;/a&gt; in any number of formats, including CSV, JSON, XML, and even custom formats. Flat files are often human-readable text files&amp;mdash;though they can also be binary data&amp;mdash;with a structure that can be parsed by a computer program. You&amp;rsquo;ll explore using SQL databases and flat files for data storage and manipulation and learn how to decide which approach is right for your program.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course, you&amp;rsquo;ll learn how to use:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Flat files&lt;/strong&gt; for data storage&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SQL&lt;/strong&gt; to improve access to persistent data&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SQLite&lt;/strong&gt; for data storage&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SQLAlchemy&lt;/strong&gt; to work with data as Python objects&lt;/li&gt;
&lt;/ul&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>The Real Python Podcast – Episode #196: Exploring Duck Typing in Python &amp; Dynamics of Monkey Patching</title>
      <id>https://realpython.com/podcasts/rpp/196/</id>
      <link href="https://realpython.com/podcasts/rpp/196/"/>
      <updated>2024-03-15T12:00:00+00:00</updated>
      <summary>What are the advantages of determining the type of an object by how it behaves? What coding circumstances are not a good fit for duck typing? Christopher Trudeau is back on the show this week, bringing another batch of PyCoder&#x27;s Weekly articles and projects.</summary>
      <content type="html">
        &lt;p&gt;What are the advantages of determining the type of an object by how it behaves? What coding circumstances are not a good fit for duck typing? Christopher Trudeau is back on the show this week, bringing another batch of PyCoder&#x27;s Weekly articles and projects.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>Python Basics Exercises: Dictionaries</title>
      <id>https://realpython.com/courses/basics-exercises-dictionaries/</id>
      <link href="https://realpython.com/courses/basics-exercises-dictionaries/"/>
      <updated>2024-03-12T14:00:00+00:00</updated>
      <summary>One of the most useful data structures in Python is the dictionary. In this video course, you’ll practice working with Python dictionaries, see how dictionaries differ from lists and tuples, and define and use dictionaries in your own code.</summary>
      <content type="html">
        &lt;p&gt;In plain English, a dictionary is a book containing the definitions of words. Each entry in a dictionary has two parts: the word being defined, and its definition.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Python dictionaries&lt;/strong&gt;, like lists and tuples, store a collection of objects. However, instead of storing objects in a sequence, dictionaries hold
information in pairs of data called &lt;strong&gt;key-value pairs&lt;/strong&gt;. That is, each object in a dictionary has two parts: a &lt;strong&gt;key&lt;/strong&gt; and a &lt;strong&gt;value&lt;/strong&gt;. Each key is assigned a single value, which defines the relationship between the two sets.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course, you&amp;rsquo;ll practice:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;What a dictionary &lt;strong&gt;is&lt;/strong&gt; and how it&amp;rsquo;s &lt;strong&gt;structured&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;How dictionaries &lt;strong&gt;differ&lt;/strong&gt; from other data structures&lt;/li&gt;
&lt;li&gt;How to &lt;strong&gt;define&lt;/strong&gt; and &lt;strong&gt;use&lt;/strong&gt; dictionaries in your own code&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This video course is part of the Python Basics series, which accompanies &lt;a href=&quot;https://realpython.com/products/python-basics-book/&quot;&gt;&lt;em&gt;Python Basics: A Practical Introduction to Python 3&lt;/em&gt;&lt;/a&gt;. You can also check out the other &lt;a href=&quot;https://realpython.com/learning-paths/python-basics/&quot;&gt;Python Basics courses&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;Note that you&amp;rsquo;ll be using &lt;a href=&quot;https://realpython.com/python-idle/&quot;&gt;IDLE&lt;/a&gt; to &lt;a href=&quot;https://realpython.com/interacting-with-python/&quot;&gt;interact with Python&lt;/a&gt; throughout this course.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  
    <entry>
      <title>The Real Python Podcast – Episode #195: Building a Healthy Developer Mindset While Learning Python</title>
      <id>https://realpython.com/podcasts/rpp/195/</id>
      <link href="https://realpython.com/podcasts/rpp/195/"/>
      <updated>2024-03-08T12:00:00+00:00</updated>
      <summary>How do you get yourself unstuck when facing a programming problem? How do you develop a positive developer mindset while learning Python? This week on the show, Bob Belderbos from Pybites is here to talk about learning Python and building healthy developer habits.</summary>
      <content type="html">
        &lt;p&gt;How do you get yourself unstuck when facing a programming problem? How do you develop a positive developer mindset while learning Python? This week on the show, Bob Belderbos from Pybites is here to talk about learning Python and building healthy developer habits.&lt;/p&gt;
        &lt;hr /&gt;
        &lt;p&gt;&lt;em&gt;[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short &amp;amp; sweet Python Trick delivered to your inbox every couple of days. &lt;a href=&quot;https://realpython.com/python-tricks/?utm_source=realpython&amp;amp;utm_medium=rss&amp;amp;utm_campaign=footer&quot;&gt;&amp;gt;&amp;gt; Click here to learn more and see examples&lt;/a&gt; ]&lt;/em&gt;&lt;/p&gt;
      </content>
    </entry>
  

</feed>
