<?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-12-10T14:00:00+00:00</updated>
  <id>https://realpython.com/</id>
  <author>
    <name>Real Python</name>
  </author>

  
    <entry>
      <title>Documenting Python Projects With Sphinx and Read the Docs</title>
      <id>https://realpython.com/courses/python-sphinx/</id>
      <link href="https://realpython.com/courses/python-sphinx/"/>
      <updated>2024-12-10T14:00:00+00:00</updated>
      <summary>In this video series, you&#x27;ll create project documentation from scratch using Sphinx, the de facto standard for Python. You&#x27;ll also hook your code repository up to Read The Docs to automatically build and publish your code documentation.</summary>
      <content type="html">
        &lt;p&gt;&lt;a href=&quot;https://www.sphinx-doc.org/&quot;&gt;Sphinx&lt;/a&gt; is a document generation tool that&amp;rsquo;s become the de facto standard for Python projects. It uses the &lt;a href=&quot;https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html&quot;&gt;reStructuredText&lt;/a&gt; (RST) markup language to define document structure and styling, and it can output in a wide variety of formats, including &lt;a href=&quot;https://realpython.com/html-css-python/&quot;&gt;HTML&lt;/a&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/EPUB&quot;&gt;ePub&lt;/a&gt;, &lt;a href=&quot;https://en.wikipedia.org/wiki/Man_page&quot;&gt;man pages&lt;/a&gt;, and much more. Sphinx is extendable and has plugins for incorporating &lt;a href=&quot;https://docs.python.org/3/library/pydoc.html&quot;&gt;pydoc&lt;/a&gt; comments from your code into your docs and for using &lt;a href=&quot;https://jupyterbook.org/en/stable/content/myst.html&quot;&gt;MyST Markdown&lt;/a&gt; instead of RST.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://readthedocs.org/&quot;&gt;Read the Docs&lt;/a&gt; is a free document hosting site where many Python projects host their documentation. It integrates with &lt;a href=&quot;https://realpython.com/python-git-github-intro/&quot;&gt;GitHub&lt;/a&gt;, &lt;a href=&quot;https://about.gitlab.com/&quot;&gt;GitLab&lt;/a&gt;, and &lt;a href=&quot;https://bitbucket.org/&quot;&gt;Bitbucket&lt;/a&gt; to automatically pull new documentation sources from your repositories and build their Sphinx sources.&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;Write your documentation with &lt;strong&gt;Sphinx&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Structure and style your document with &lt;strong&gt;RST syntax&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Incorporate your &lt;strong&gt;pydoc&lt;/strong&gt; comments into your documentation&lt;/li&gt;
&lt;li&gt;Host your documentation on &lt;strong&gt;Read the Docs&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With these skills, you&amp;rsquo;ll be able to write clear, reliable documentation that&amp;rsquo;ll help your users get the most out of your project.&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 Roundup: December 2024</title>
      <id>https://realpython.com/python-news-december-2024/</id>
      <link href="https://realpython.com/python-news-december-2024/"/>
      <updated>2024-12-09T14:00:00+00:00</updated>
      <summary>This month brings exciting updates, including improvements to template strings in PEP 750 and the release of Python 3.14.0a2. There are new features to explore in many third-party libraries, such as Pydantic. Also, PyCon&#x27;s call for proposals deadline is near and the popular Advent of Code is back.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;The Python community has kept up its momentum this month, delivering a host of exciting updates. From the promising improvements to &lt;strong&gt;template strings&lt;/strong&gt; in PEP 750 to the release of &lt;strong&gt;Python 3.14.0a2&lt;/strong&gt;, innovation is front and center. Developers are exploring new tools like &lt;strong&gt;partial validation in Pydantic&lt;/strong&gt; 2.10, while popular projects adapt to the end of life of Python 3.8.&lt;/p&gt;
&lt;p&gt;December also welcomes the return of the beloved &lt;strong&gt;Advent of Code&lt;/strong&gt;, challenging programmers and problem solvers with daily puzzles. And for those planning ahead, the &lt;strong&gt;PyCon 2025&lt;/strong&gt; call for proposals is closing soon, marking a final opportunity to contribute to the community’s largest annual gathering.&lt;/p&gt;
&lt;p&gt;Whether you’re diving into cutting-edge features, revisiting Python fundamentals, or connecting with fellow developers, there’s something for everyone this month. Let’s round up the most important developments of last month and see how they can inspire your next Python project!&lt;/p&gt;
&lt;h2 id=&quot;pep-750-template-strings-updated&quot;&gt;PEP 750 – Template Strings Updated&lt;a class=&quot;headerlink&quot; href=&quot;#pep-750-template-strings-updated&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The authors of &lt;a href=&quot;https://peps.python.org/pep-0750/&quot;&gt;PEP 750&lt;/a&gt;, which you might have first encountered &lt;a href=&quot;https://realpython.com/python-news-september-2024/#pep-750-draft-introduces-tag-strings&quot;&gt;introducing tag strings&lt;/a&gt;, have revised and significantly updated their proposal.&lt;/p&gt;
&lt;p&gt;The central point of the PEP is now renamed as &lt;strong&gt;template strings&lt;/strong&gt;, or t-strings, and aligns its syntax with the beloved &lt;a href=&quot;https://realpython.com/python-f-strings/&quot;&gt;f-strings&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;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;n&quot;&gt;planet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;World&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;greeting&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{planet}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;!&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.57da6203435d.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.57da6203435d.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;Looks very familiar! The main practical difference to f-strings is that t-strings are &lt;strong&gt;lazily evaluated&lt;/strong&gt;, which opens the door to common string interpolation scenarios that f-strings can’t handle, such as:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Performance-optimized logging&lt;/li&gt;
&lt;li&gt;Internationalization&lt;/li&gt;
&lt;li&gt;Template reuse&lt;/li&gt;
&lt;li&gt;HTML templating&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All of the above are examples of string interpolation that requires lazy evaluation.&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; Template reuse can come in handy, for example, when you’re creating template prompts in &lt;a href=&quot;https://realpython.com/build-llm-rag-chatbot-with-langchain/&quot;&gt;LLM-powered applications&lt;/a&gt; and application frameworks.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;If the PEP gets accepted and implemented, then you’ll be able to use t-strings where you currently have to resort to &lt;a href=&quot;https://realpython.com/python-string-interpolation/#creating-strings-with-the-strformat-method&quot;&gt;&lt;code&gt;str.format()&lt;/code&gt;&lt;/a&gt;, using &lt;a href=&quot;https://realpython.com/python-string-interpolation/#building-templates-with-the-template-class&quot;&gt;a &lt;code&gt;Template&lt;/code&gt; object&lt;/a&gt; from the &lt;code&gt;string&lt;/code&gt; module, or even the older &lt;a href=&quot;https://realpython.com/python-modulo-string-formatting/&quot;&gt;modulo (&lt;code&gt;%&lt;/code&gt;) string formatting&lt;/a&gt; approach.&lt;/p&gt;
&lt;p&gt;As the Python community continues to innovate, PEP 750 is a testament to the ongoing efforts to enhance language features while keeping them intuitive for developers. If you want to dive deeper into this proposal and its potential impact, then you can read more in the detailed &lt;a href=&quot;https://peps.python.org/pep-0750/#examples&quot;&gt;examples&lt;/a&gt; section of the PEP.&lt;/p&gt;
&lt;h2 id=&quot;python-3140a2-released&quot;&gt;Python 3.14.0a2 Released&lt;a class=&quot;headerlink&quot; href=&quot;#python-3140a2-released&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The Python 3.14 team rolled out the second alpha release of Python 3.14, &lt;a href=&quot;https://www.python.org/downloads/release/python-3140a2/&quot;&gt;version 3.14.0a2&lt;/a&gt;. This release is part of the ongoing development process for Python 3.14, with a total of seven alpha releases planned.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-news-december-2024/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-news-december-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>Using and Creating Global Variables in Your Python Functions</title>
      <id>https://realpython.com/python-use-global-variable-in-function/</id>
      <link href="https://realpython.com/python-use-global-variable-in-function/"/>
      <updated>2024-12-08T14:00:00+00:00</updated>
      <summary>In this tutorial, 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;div&gt;&lt;p&gt;In Python, &lt;strong&gt;global variables&lt;/strong&gt; are accessible across your entire program, including within functions. Understanding how Python handles global variables is key to writing efficient code. This tutorial will guide you through accessing and modifying global variables in Python functions using the &lt;code&gt;global&lt;/code&gt; keyword and the &lt;code&gt;globals()&lt;/code&gt; function. You’ll also learn to manage scope and avoid potential conflicts between local and global variables.&lt;/p&gt;
&lt;p&gt;You’ll explore how to create global variables inside functions and apply strategies to minimize their use, ensuring your code remains clean and maintainable. After reading this tutorial, you’ll be adept at managing global variables and understanding their impact on your Python code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this tutorial, you’ll understand that:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A global variable in Python&lt;/strong&gt; is a variable defined at the module level, accessible throughout the program.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Accessing and modifying global variables&lt;/strong&gt; inside Python functions can be achieved using the &lt;code&gt;global&lt;/code&gt; keyword or the &lt;code&gt;globals()&lt;/code&gt; function.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python handles name conflicts&lt;/strong&gt; by searching scopes from local to built-in, potentially causing name shadowing challenges.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Creating global variables inside a function&lt;/strong&gt; is possible using the &lt;code&gt;global&lt;/code&gt; keyword or &lt;code&gt;globals()&lt;/code&gt;, but it’s generally not recommended.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Strategies to avoid global variables&lt;/strong&gt; include using constants, passing arguments, and employing classes and methods to encapsulate state.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To follow along with this tutorial, you should have a solid understanding of Python programming, including fundamental concepts such as &lt;a href=&quot;https://realpython.com/python-variables/&quot;&gt;variables&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/python-data-types/&quot;&gt;data types&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/python-scope-legb-rule/&quot;&gt;scope&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/python-mutable-vs-immutable-types/&quot;&gt;mutability&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/defining-your-own-python-function/&quot;&gt;functions&lt;/a&gt;, and &lt;a href=&quot;https://realpython.com/python-classes/&quot;&gt;classes&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-use-global-variable-in-function-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-use-global-variable-in-function-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 understand when and how to work with global variables in your Python functions.&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.57da6203435d.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 “Using and Creating Global Variables in Your Python Functions” 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-use-global-variable-in-function/&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;Using and Creating Global Variables in Your Python Functions&quot; src=&quot;https://files.realpython.com/media/How-to-Create-or-Use-a-Global-Variable-in-a-Function_Watermarked.0d7d127a3219.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-Create-or-Use-a-Global-Variable-in-a-Function_Watermarked.0d7d127a3219.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/How-to-Create-or-Use-a-Global-Variable-in-a-Function_Watermarked.0d7d127a3219.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/How-to-Create-or-Use-a-Global-Variable-in-a-Function_Watermarked.0d7d127a3219.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/How-to-Create-or-Use-a-Global-Variable-in-a-Function_Watermarked.0d7d127a3219.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.57da6203435d.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-use-global-variable-in-function/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Using and Creating Global Variables in Your Python Functions&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 use global variables in Python functions. With this knowledge, you&#x27;ll be able to share data across an entire program, modify and create global variables within functions, and understand when to avoid using global variables.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;using-global-variables-in-python-functions&quot;&gt;Using Global Variables in Python Functions&lt;a class=&quot;headerlink&quot; href=&quot;#using-global-variables-in-python-functions&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Global &lt;a href=&quot;https://realpython.com/python-variables/&quot;&gt;variables&lt;/a&gt; are those that you can access and modify from anywhere in your code. In Python, you’ll typically define global variables at the &lt;a href=&quot;https://realpython.com/python-modules-packages/&quot;&gt;module&lt;/a&gt; level. So, the containing module is their &lt;strong&gt;scope&lt;/strong&gt;. &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 also define global variables inside &lt;a href=&quot;https://realpython.com/defining-your-own-python-function/&quot;&gt;functions&lt;/a&gt;, as you’ll learn in the section &lt;a href=&quot;#creating-global-variables-inside-a-function&quot;&gt;Creating Global Variables Inside a Function&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Once you’ve defined a global variable, you can use it from within the module itself or from within other modules in your code. You can also use global variables in your functions. However, those cases can get a bit confusing because of differences between &lt;strong&gt;accessing&lt;/strong&gt; and &lt;strong&gt;modifying&lt;/strong&gt; global variables in functions.&lt;/p&gt;
&lt;p&gt;To understand these differences, consider that Python can look for variables in four different scopes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;a href=&quot;https://realpython.com/python-scope-legb-rule/#functions-the-local-scope&quot;&gt;local&lt;/a&gt;, or function-level, scope, which exists inside functions&lt;/li&gt;
&lt;li&gt;The &lt;a href=&quot;https://realpython.com/python-scope-legb-rule/#nested-functions-the-enclosing-scope&quot;&gt;enclosing&lt;/a&gt;, or non-local, scope, which appears in nested functions&lt;/li&gt;
&lt;li&gt;The &lt;a href=&quot;https://realpython.com/python-scope-legb-rule/#modules-the-global-scope&quot;&gt;global&lt;/a&gt; scope, which exists at the module level&lt;/li&gt;
&lt;li&gt;The &lt;a href=&quot;https://realpython.com/python-scope-legb-rule/#builtins-the-built-in-scope&quot;&gt;built-in&lt;/a&gt; scope, which is a special scope for Python’s built-in names&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To illustrate, say that you’re inside an &lt;a href=&quot;https://realpython.com/inner-functions-what-are-they-good-for/&quot;&gt;inner&lt;/a&gt; function. In that case, Python can look for names in all four scopes.&lt;/p&gt;
&lt;p&gt;When you access a variable in that inner function, Python first looks inside that function. If the variable doesn’t exist there, then Python continues with the enclosing scope of the outer function. If the variable isn’t defined there either, then Python moves to the global and built-in scopes in that order. If Python finds the variable, then you get the value back. Otherwise, you get a &lt;code&gt;NameError&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.57da6203435d.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;# Global scope&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;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;outer_func&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;c1&quot;&gt;# Non-local scope&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;inner_func&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;c1&quot;&gt;# Local scope&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;some_variable&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;inner_func&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;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outer_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&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;gr&quot;&gt;NameError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;name &#x27;some_variable&#x27; is not defined&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;some_variable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hello from global scope!&quot;&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;outer_func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Hello from global scope!&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.57da6203435d.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.57da6203435d.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;When you launch an &lt;a href=&quot;https://realpython.com/interacting-with-python/&quot;&gt;interactive session&lt;/a&gt;, it starts off at the module level of global scope. In this example, you have &lt;code&gt;outer_func()&lt;/code&gt;, which defines &lt;code&gt;inner_func()&lt;/code&gt; as a nested function. From the perspective of this nested function, its own code block represents the local scope, while the &lt;code&gt;outer_func()&lt;/code&gt; code block before the call to &lt;code&gt;inner_func()&lt;/code&gt; represents the non-local scope.&lt;/p&gt;
&lt;p&gt;If you call &lt;code&gt;outer_func()&lt;/code&gt; without defining &lt;code&gt;some_variable&lt;/code&gt; in either of your current scopes, then you get a &lt;code&gt;NameError&lt;/code&gt; exception because the name isn’t defined.&lt;/p&gt;
&lt;p&gt;If you define &lt;code&gt;some_variable&lt;/code&gt; in the global scope and then call &lt;code&gt;outer_func()&lt;/code&gt;, then you get &lt;code&gt;Hello!&lt;/code&gt; on your screen. Internally, Python has searched the local, non-local, and global scopes to find &lt;code&gt;some_variable&lt;/code&gt; and print its content. Note that you can define this variable in any of the three scopes, and Python will find it.&lt;/p&gt;
&lt;p&gt;This search mechanism makes it possible to use global variables from inside functions. However, while taking advantage of this feature, you can face a few issues. For example, accessing a variable works, but directly modifying a variable doesn’t work:&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.57da6203435d.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;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;42&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;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;access_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;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&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;access_number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;42&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;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;modify_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;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&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;modify_number&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;number&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;42&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.57da6203435d.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.57da6203435d.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;code&gt;access_number()&lt;/code&gt; function works fine. It looks for &lt;code&gt;number&lt;/code&gt; and finds it in the global scope. In contrast, &lt;code&gt;modify_number()&lt;/code&gt; doesn’t work as expected. Why doesn’t this function update the value of your global variable, &lt;code&gt;number&lt;/code&gt;? The problem is the scope of the variable. You can’t directly modify a variable from a high-level scope like global in a lower-level scope like local.&lt;/p&gt;
&lt;p&gt;Internally, Python assumes that any name directly assigned within a function is local to that function. Therefore, the local name, &lt;code&gt;number&lt;/code&gt;, shadows its global sibling.&lt;/p&gt;
&lt;p&gt;In this sense, global variables behave as read-only names. You can access their values, but you can’t modify them.&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; The discussion about modifying global variables inside functions revolves around &lt;a href=&quot;https://realpython.com/python-assignment-operator/#assignment-statements-and-the-assignment-operator&quot;&gt;assignment&lt;/a&gt; operations rather than &lt;a href=&quot;https://en.wikipedia.org/wiki/In-place_algorithm&quot;&gt;in-place&lt;/a&gt; mutations of &lt;a href=&quot;https://realpython.com/python-mutable-vs-immutable-types/&quot;&gt;mutable&lt;/a&gt; objects. You’ll learn about the effects of mutability on global variables in the section &lt;a href=&quot;#understanding-how-mutability-affects-global-variables&quot;&gt;Understanding How Mutability Affects Global Variables&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-use-global-variable-in-function/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-use-global-variable-in-function/ »&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>Effective Python Testing With pytest</title>
      <id>https://realpython.com/pytest-python-testing/</id>
      <link href="https://realpython.com/pytest-python-testing/"/>
      <updated>2024-12-08T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll learn how to take your testing to the next level with pytest. You&#x27;ll cover intermediate and advanced pytest features such as fixtures, marks, parameters, and plugins. With pytest, you can make your test suites fast, effective, and less painful to maintain.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;&lt;code&gt;pytest&lt;/code&gt; is a popular testing framework for Python that simplifies the process of writing and executing tests. To start using &lt;code&gt;pytest&lt;/code&gt;, install it with &lt;code&gt;pip&lt;/code&gt; in a virtual environment. &lt;code&gt;pytest&lt;/code&gt; offers several advantages over &lt;code&gt;unittest&lt;/code&gt; that ships with Python, such as less boilerplate code, more readable output, and a rich plugin ecosystem.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;pytest&lt;/code&gt; comes packed with features to boost your productivity. Its fixtures allow for explicit dependency declarations, making tests more understandable and reducing implicit dependencies. Parametrization in &lt;code&gt;pytest&lt;/code&gt; helps prevent redundant test code by enabling multiple test cases from a single test function definition. This framework is highly customizable, so you can tailor it to your project’s needs.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this tutorial, you’ll understand that:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Using &lt;code&gt;pytest&lt;/code&gt; requires &lt;strong&gt;installing it with &lt;code&gt;pip&lt;/code&gt;&lt;/strong&gt; in a virtual environment to set up the &lt;code&gt;pytest&lt;/code&gt; command.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pytest&lt;/code&gt; allows for less code, easier readability, and more features &lt;strong&gt;compared to &lt;code&gt;unittest&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Managing &lt;strong&gt;test dependencies&lt;/strong&gt; and &lt;strong&gt;state&lt;/strong&gt; with &lt;code&gt;pytest&lt;/code&gt; is made efficient through the use of &lt;strong&gt;fixtures&lt;/strong&gt;, which provide explicit dependency declarations.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parametrization&lt;/strong&gt; in &lt;code&gt;pytest&lt;/code&gt; helps avoid redundant test code by allowing multiple test scenarios from a single test function.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Assertion introspection&lt;/strong&gt; in &lt;code&gt;pytest&lt;/code&gt; provides detailed information about failures in the test report.&lt;/li&gt;
&lt;/ul&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-mastery-course/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-mastery-course&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;5 Thoughts On Python Mastery&lt;/a&gt;, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.&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.57da6203435d.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 “Effective Testing with Pytest” 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/effective-testing-with-pytest/&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: #b9abe6;&quot; alt=&quot;Effective Python Testing With Pytest&quot; src=&quot;https://files.realpython.com/media/Intermediate-Advanced-PyTest-Features_Watermarked.43fb169e7121.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/Intermediate-Advanced-PyTest-Features_Watermarked.43fb169e7121.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Intermediate-Advanced-PyTest-Features_Watermarked.43fb169e7121.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Intermediate-Advanced-PyTest-Features_Watermarked.43fb169e7121.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Intermediate-Advanced-PyTest-Features_Watermarked.43fb169e7121.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.57da6203435d.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/effective-testing-with-pytest/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Effective Testing with Pytest&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 pytest, a Python testing tool. With this knowledge, you&#x27;ll be able to write more efficient and effective tests, ensuring your code behaves as expected.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;how-to-install-pytest&quot;&gt;How to Install &lt;code&gt;pytest&lt;/code&gt;&lt;a class=&quot;headerlink&quot; href=&quot;#how-to-install-pytest&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;To follow along with some of the examples in this tutorial, you’ll need to install &lt;code&gt;pytest&lt;/code&gt;. As most &lt;a href=&quot;https://realpython.com/python-modules-packages/&quot;&gt;Python packages&lt;/a&gt;, &lt;code&gt;pytest&lt;/code&gt; is available on &lt;a href=&quot;https://realpython.com/pypi-publish-python-package/&quot;&gt;PyPI&lt;/a&gt;. You can install it in a &lt;a href=&quot;https://realpython.com/python-virtual-environments-a-primer/&quot;&gt;virtual environment&lt;/a&gt; using &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;&lt;code&gt;pip&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;ul class=&quot;nav nav-tabs justify-content-end js-platform-widget-tabs&quot; role=&quot;tablist&quot;&gt;

  &lt;li class=&quot;nav-item mb-0 js-platform-widget-tab-windows&quot; role=&quot;presentation&quot;&gt;
    &lt;a class=&quot;nav-link link-unstyled text-body active small&quot; id=&quot;windows-tab-1&quot; data-toggle=&quot;tab&quot; href=&quot;#windows-1&quot; role=&quot;tab&quot; aria-controls=&quot;windows-1&quot; aria-selected=&quot;true&quot;&gt;&lt;span class=&quot;icon baseline text-muted mr-1&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.57da6203435d.svg#brands--windows&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Windows&lt;/a&gt;
  &lt;/li&gt;




  &lt;li class=&quot;nav-item mb-0 js-platform-widget-tab-linuxmacos&quot; role=&quot;presentation&quot;&gt;
    &lt;a class=&quot;nav-link link-unstyled text-body small&quot; id=&quot;macos-tab-1&quot; data-toggle=&quot;tab&quot; href=&quot;#linux-macos-1&quot; role=&quot;tab&quot; aria-controls=&quot;linux-macos-1&quot; aria-selected=&quot;false&quot;&gt;&lt;span class=&quot;icon baseline text-muted&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.57da6203435d.svg#v4--linux&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;span class=&quot;icon baseline text-muted mr-1&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.57da6203435d.svg#v4--apple&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Linux + macOS&lt;/a&gt;
  &lt;/li&gt;

&lt;/ul&gt;
&lt;div class=&quot;tab-content mt-2 mb-0 js-platform-widget-content&quot;&gt;
&lt;div aria-labelledby=&quot;windows-tab-1&quot; class=&quot;tab-pane fade show active&quot; id=&quot;windows-1&quot; role=&quot;tabpanel&quot;&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pscon&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;Windows PowerShell&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.57da6203435d.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;PS&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;python&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;-m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;venv&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;venv&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;PS&amp;gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;venv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Scripts&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;activate&lt;/span&gt;
&lt;span class=&quot;gp gp-VirtualEnv&quot;&gt;(venv)&lt;/span&gt; &lt;span class=&quot;gp&quot;&gt;PS&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;python&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;-m&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pip&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pytest&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.57da6203435d.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.57da6203435d.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;/div&gt;
&lt;div aria-labelledby=&quot;linux-macos-tab-1&quot; class=&quot;tab-pane fade &quot; id=&quot;linux-macos-1&quot; role=&quot;tabpanel&quot;&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.57da6203435d.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;$ &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;venv&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;venv
&lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;venv/bin/activate
&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;pytest
&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.57da6203435d.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.57da6203435d.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;/div&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;pytest&lt;/code&gt; command will now be available in your installation environment.&lt;/p&gt;
&lt;h2 id=&quot;what-makes-pytest-so-useful&quot;&gt;What Makes &lt;code&gt;pytest&lt;/code&gt; So Useful?&lt;a class=&quot;headerlink&quot; href=&quot;#what-makes-pytest-so-useful&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you’ve written unit &lt;a href=&quot;https://realpython.com/python-testing/&quot;&gt;tests&lt;/a&gt; for your Python code before, then you may have used Python’s built-in &lt;strong&gt;&lt;code&gt;unittest&lt;/code&gt;&lt;/strong&gt; module. &lt;code&gt;unittest&lt;/code&gt; provides a solid base on which to build your test suite, but it has a few shortcomings.&lt;/p&gt;
&lt;p&gt;A number of third-party testing frameworks attempt to address some of the issues with &lt;code&gt;unittest&lt;/code&gt;, and &lt;code&gt;pytest&lt;/code&gt; has proven to be one of the most popular. &lt;code&gt;pytest&lt;/code&gt; is a feature-rich, plugin-based ecosystem for testing your Python code.&lt;/p&gt;
&lt;p&gt;If you haven’t had the pleasure of using &lt;code&gt;pytest&lt;/code&gt; yet, then you’re in for a treat! Its philosophy and features will make your testing experience more productive and enjoyable. With &lt;code&gt;pytest&lt;/code&gt;, common tasks require less code and advanced tasks can be achieved through a variety of time-saving commands and plugins. It’ll even run your existing tests out of the box, including those written with &lt;code&gt;unittest&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;As with most frameworks, some development patterns that make sense when you first start using &lt;code&gt;pytest&lt;/code&gt; can start causing pains as your test suite grows. This tutorial will help you understand some of the tools &lt;code&gt;pytest&lt;/code&gt; provides to keep your testing efficient and effective even as it scales.&lt;/p&gt;
&lt;h3 id=&quot;less-boilerplate&quot;&gt;Less Boilerplate&lt;a class=&quot;headerlink&quot; href=&quot;#less-boilerplate&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Most functional tests follow the Arrange-Act-Assert model:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Arrange&lt;/strong&gt;, or set up, the conditions for the test&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Act&lt;/strong&gt; by calling some function or method&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Assert&lt;/strong&gt; that some end condition is true&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Testing frameworks typically hook into your test’s &lt;a href=&quot;https://realpython.com/python-assert-statement/&quot;&gt;assertions&lt;/a&gt; so that they can provide information when an assertion fails. &lt;code&gt;unittest&lt;/code&gt;, for example, provides a number of helpful assertion utilities out of the box. However, even a small set of tests requires a fair amount of &lt;a href=&quot;https://en.wikipedia.org/wiki/Boilerplate_code&quot;&gt;boilerplate code&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Imagine you’d like to write a test suite just to make sure that &lt;code&gt;unittest&lt;/code&gt; is working properly in your project. You might want to write one test that always passes and one that always fails:&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; background: inherit;&quot;&gt;test_with_unittest.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;unittest&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TryTesting&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TestCase&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_always_passes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assertTrue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;test_always_fails&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assertTrue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;False&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.57da6203435d.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.57da6203435d.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 then run those tests from the command line using the &lt;code&gt;discover&lt;/code&gt; option of &lt;code&gt;unittest&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;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.57da6203435d.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;unittest&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;discover
&lt;span class=&quot;go&quot;&gt;F.&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;======================================================================&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;FAIL: test_always_fails (test_with_unittest.TryTesting)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  File &quot;...\effective-python-testing-with-pytest\test_with_unittest.py&quot;,&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;  line 10, in test_always_fails&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;    self.assertTrue(False)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;AssertionError: False is not true&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;----------------------------------------------------------------------&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;Ran 2 tests in 0.006s&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;FAILED (failures=1)&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.57da6203435d.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.57da6203435d.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 expected, one test passed and one failed. You’ve proven that &lt;code&gt;unittest&lt;/code&gt; is working, but look at what you had to do:&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/pytest-python-testing/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/pytest-python-testing/ »&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 Timer Functions: Three Ways to Monitor Your Code</title>
      <id>https://realpython.com/python-timer/</id>
      <link href="https://realpython.com/python-timer/"/>
      <updated>2024-12-08T14:00:00+00:00</updated>
      <summary>In this step-by-step tutorial, you&#x27;ll learn how to use Python timer functions to monitor how quickly your programs are running. You&#x27;ll use classes, context managers, and decorators to measure your program&#x27;s running time. You&#x27;ll also learn the benefits of each method and which to use given the situation.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;A timer is a powerful tool for monitoring the performance of your Python code. By using the &lt;code&gt;time.perf_counter()&lt;/code&gt; function, you can measure execution time with exceptional precision, making it ideal for benchmarking. Using a timer involves recording timestamps before and after a specific code block and calculating the time difference to determine how long your code took to run.&lt;/p&gt;
&lt;p&gt;In this tutorial, you’ll explore three different approaches to implementing timers: classes, decorators, and context managers. Each method offers unique advantages, and you’ll learn when and how to use them to achieve optimal results. Plus, you’ll have a fully functional Python timer that can be applied to any program to measure execution time efficiently.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this tutorial, you’ll understand that:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;time.perf_counter()&lt;/code&gt;&lt;/strong&gt; is the best choice for accurate timing in Python due to its high resolution.&lt;/li&gt;
&lt;li&gt;You can create &lt;strong&gt;custom timer classes&lt;/strong&gt; to encapsulate timing logic and reuse it across multiple parts of your program.&lt;/li&gt;
&lt;li&gt;Using &lt;strong&gt;decorators&lt;/strong&gt; lets you seamlessly add timing functionality to existing functions without altering their code.&lt;/li&gt;
&lt;li&gt;You can leverage &lt;strong&gt;context managers&lt;/strong&gt; to neatly measure execution time in specific code blocks, improving both resource management and code clarity.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Along the way, you’ll gain deeper insights into how &lt;a href=&quot;https://realpython.com/python-classes/&quot;&gt;classes&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/primer-on-python-decorators/&quot;&gt;decorators&lt;/a&gt;, and &lt;a href=&quot;https://realpython.com/python-with-statement/&quot;&gt;context managers&lt;/a&gt; work in Python. As you explore real-world examples, you’ll discover how these concepts can not only help you measure code performance but also enhance your overall Python programming skills.&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;Decorators Q&amp;amp;A Transcript:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/decorators-qa-2019/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-decorators-qa-2019&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to get access to a 25-page chat log from our Python decorators Q&amp;amp;A session&lt;/a&gt; in the Real Python Community Slack where we discussed common decorator questions.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;python-timers&quot;&gt;Python Timers&lt;a class=&quot;headerlink&quot; href=&quot;#python-timers&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;First, you’ll take a look at some example code that you’ll use throughout the tutorial. Later, you’ll add a &lt;strong&gt;Python timer&lt;/strong&gt; to this code to monitor its performance. You’ll also learn some of the simplest ways to measure the running time of this example.&lt;/p&gt;
&lt;h3 id=&quot;python-timer-functions&quot;&gt;Python Timer Functions&lt;a class=&quot;headerlink&quot; href=&quot;#python-timer-functions&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;If you check out the built-in &lt;a href=&quot;https://docs.python.org/3/library/time.html&quot;&gt;&lt;code&gt;time&lt;/code&gt;&lt;/a&gt; module in Python, then you’ll notice several functions that can measure time:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/3/library/time.html#time.monotonic&quot;&gt;&lt;code&gt;monotonic()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/3/library/time.html#time.perf_counter&quot;&gt;&lt;code&gt;perf_counter()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/3/library/time.html#time.process_time&quot;&gt;&lt;code&gt;process_time()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://docs.python.org/3/library/time.html#time.time&quot;&gt;&lt;code&gt;time()&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href=&quot;https://realpython.com/python37-new-features/&quot;&gt;Python 3.7&lt;/a&gt; introduced several new functions, like &lt;a href=&quot;https://docs.python.org/3/library/time.html#time.thread_time&quot;&gt;&lt;code&gt;thread_time()&lt;/code&gt;&lt;/a&gt;, as well as &lt;strong&gt;nanosecond&lt;/strong&gt; versions of all the functions above, named with an &lt;code&gt;_ns&lt;/code&gt; suffix. For example, &lt;a href=&quot;https://docs.python.org/3/library/time.html#time.perf_counter_ns&quot;&gt;&lt;code&gt;perf_counter_ns()&lt;/code&gt;&lt;/a&gt; is the nanosecond version of &lt;code&gt;perf_counter()&lt;/code&gt;. You’ll learn more about these functions later. For now, note what the documentation has to say about &lt;code&gt;perf_counter()&lt;/code&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. (&lt;a href=&quot;https://docs.python.org/3/library/time.html#time.perf_counter&quot;&gt;Source&lt;/a&gt;)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;First, you’ll use &lt;code&gt;perf_counter()&lt;/code&gt; to create a Python timer. &lt;a href=&quot;#other-python-timer-functions&quot;&gt;Later&lt;/a&gt;, you’ll compare this with other Python timer functions and learn why &lt;code&gt;perf_counter()&lt;/code&gt; is usually the best choice.&lt;/p&gt;
&lt;h3 id=&quot;example-download-tutorials&quot;&gt;Example: Download Tutorials&lt;a class=&quot;headerlink&quot; href=&quot;#example-download-tutorials&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;To better compare the different ways that you can add a Python timer to your code, you’ll apply different Python timer functions to the same code example throughout this tutorial. If you already have code that you’d like to measure, then feel free to follow the examples with that instead.&lt;/p&gt;
&lt;p&gt;The example that you’ll use in this tutorial is a short function that uses the &lt;a href=&quot;https://pypi.org/project/realpython-reader/&quot;&gt;&lt;code&gt;realpython-reader&lt;/code&gt;&lt;/a&gt; package to download the latest tutorials available here on Real Python. To learn more about the Real Python Reader and how it works, check out &lt;a href=&quot;https://realpython.com/pypi-publish-python-package/&quot;&gt;How to Publish an Open-Source Python Package to PyPI&lt;/a&gt;. You can install &lt;code&gt;realpython-reader&lt;/code&gt; on your system with &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;&lt;code&gt;pip&lt;/code&gt;&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;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.57da6203435d.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;$ &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;realpython-reader
&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.57da6203435d.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.57da6203435d.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;Then, you can &lt;a href=&quot;https://realpython.com/python-import/&quot;&gt;import&lt;/a&gt; the package as &lt;code&gt;reader&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You’ll store the example in a file named &lt;code&gt;latest_tutorial.py&lt;/code&gt;. The code consists of one function that downloads and prints the latest tutorial from Real Python:&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; background: inherit;&quot;&gt;latest_tutorial.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;linenos&quot;&gt; 1&lt;/span&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;reader&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 2&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 3&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 4&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;&quot;&quot;&quot;Download and print the latest tutorial from Real Python&quot;&quot;&quot;&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 5&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;tutorial&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;feed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_article&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;linenos&quot;&gt; 6&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;tutorial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&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;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;linenos&quot;&gt; 9&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;main&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.57da6203435d.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.57da6203435d.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;code&gt;realpython-reader&lt;/code&gt; handles most of the hard work:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Line 1&lt;/strong&gt; imports &lt;code&gt;feed&lt;/code&gt; from &lt;code&gt;realpython-reader&lt;/code&gt;. This module contains functionality for downloading tutorials from the &lt;a href=&quot;https://realpython.com/contact/#rss-atom-feed&quot;&gt;Real Python feed&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 5&lt;/strong&gt; downloads the latest tutorial from Real Python. The number &lt;code&gt;0&lt;/code&gt; is an offset, where &lt;code&gt;0&lt;/code&gt; means the most recent tutorial, &lt;code&gt;1&lt;/code&gt; is the previous tutorial, and so on.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 7&lt;/strong&gt; prints the tutorial to the console.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Line 9&lt;/strong&gt; calls &lt;code&gt;main()&lt;/code&gt; when you run the script.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When you run this example, your output will typically look something like this:&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.57da6203435d.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;$ &lt;/span&gt;python&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;latest_tutorial.py
&lt;span class=&quot;gp&quot;&gt;# &lt;/span&gt;Python&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;Timer&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;Functions:&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;Three&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;Ways&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;to&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;Monitor&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;Your&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;Code

&lt;span class=&quot;go&quot;&gt;A timer is a powerful tool for monitoring the performance of your Python&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;code. By using the `time.perf_counter()` function, you can measure execution&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;time with exceptional precision, making it ideal for benchmarking. Using a&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;timer involves recording timestamps before and after a specific code block and&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;calculating the time difference to determine how long your code took to run.&lt;/span&gt;

&lt;span class=&quot;go&quot;&gt;[ ... ]&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Read the full article at https://realpython.com/python-timer/ »&lt;/span&gt;

&lt;span class=&quot;go&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.57da6203435d.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.57da6203435d.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;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-timer/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-timer/ »&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>Asynchronous Tasks With Django and Celery</title>
      <id>https://realpython.com/asynchronous-tasks-with-django-and-celery/</id>
      <link href="https://realpython.com/asynchronous-tasks-with-django-and-celery/"/>
      <updated>2024-12-08T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll learn how to integrate Celery and Django using Redis as a message broker. You&#x27;ll refactor the synchronous email sending functionality of an existing Django app into an asynchronous task that you&#x27;ll run with Celery instead.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Integrating &lt;strong&gt;Celery&lt;/strong&gt; with your Django application allows you to offload time-consuming tasks, ensuring smooth user experiences. Celery is a &lt;strong&gt;distributed task queue&lt;/strong&gt; that processes tasks asynchronously, preventing delays in your web app’s response time. By using Celery with Django, you can efficiently manage tasks like sending emails, processing images, and analyzing data without slowing down your application.&lt;/p&gt;
&lt;p&gt;Celery works by leveraging a &lt;strong&gt;message broker&lt;/strong&gt; like &lt;a href=&quot;https://realpython.com/python-redis/&quot;&gt;Redis&lt;/a&gt; to communicate between your Django app and &lt;strong&gt;Celery workers&lt;/strong&gt;. This setup enables you to handle tasks outside the main execution thread, improving your app’s performance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this tutorial, you’ll understand that:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Celery is a &lt;strong&gt;distributed task queue&lt;/strong&gt; that handles tasks outside the main Django app flow.&lt;/li&gt;
&lt;li&gt;Python’s Celery excels at &lt;strong&gt;offloading work&lt;/strong&gt; and &lt;strong&gt;scheduling tasks&lt;/strong&gt; independently.&lt;/li&gt;
&lt;li&gt;Using Celery in Django helps &lt;strong&gt;maintain app responsiveness&lt;/strong&gt; during time-intensive tasks.&lt;/li&gt;
&lt;li&gt;Configuring Celery in Django involves &lt;strong&gt;setting up a message broker&lt;/strong&gt; and &lt;strong&gt;defining tasks&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;Celery worker&lt;/strong&gt; performs tasks &lt;strong&gt;asynchronously&lt;/strong&gt;, freeing up the main app.&lt;/li&gt;
&lt;li&gt;Running a task in Celery requires calling the task with &lt;code&gt;.delay()&lt;/code&gt; or &lt;code&gt;.apply_async()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Celery is not a message queue but &lt;strong&gt;uses a message broker like Redis&lt;/strong&gt; for communication.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You’re in the right place if you’ve never used Celery in a Django app before, or if you’ve peeked into Celery’s documentation but couldn’t find your way around. You’ll learn how to configure Celery in Django to handle tasks asynchronously, ensuring your application remains responsive and efficient.&lt;/p&gt;
&lt;p&gt;To focus this tutorial on the essentials, you’ll integrate Celery into an existing Django app. Go ahead and download the code for that app so that you can follow along:&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-celery-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-django-celery-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download free the sample code&lt;/a&gt; you’ll use to integrate Celery into your Django app.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;python-celery-basics&quot;&gt;Python Celery Basics&lt;a class=&quot;headerlink&quot; href=&quot;#python-celery-basics&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://docs.celeryq.dev/en/stable/index.html&quot;&gt;Celery&lt;/a&gt; is a distributed task &lt;a href=&quot;https://realpython.com/queue-in-python/&quot;&gt;queue&lt;/a&gt; that can collect, record, schedule, and perform tasks outside of your main program.&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; Celery &lt;a href=&quot;https://docs.celeryq.dev/en/stable/faq.html?highlight=windows#does-celery-support-windows&quot;&gt;dropped support for Windows&lt;/a&gt; in version 4, so while &lt;a href=&quot;https://stackoverflow.com/a/47331438/5717580&quot;&gt;you may still be able to get it to work&lt;/a&gt; on Windows, you’re better off using a different task queue, such as &lt;a href=&quot;https://huey.readthedocs.io/en/latest/index.html&quot;&gt;huey&lt;/a&gt; or &lt;a href=&quot;https://dramatiq.io/index.html&quot;&gt;Dramatiq&lt;/a&gt;, instead.&lt;/p&gt;
&lt;p&gt;In this tutorial, you’ll focus on using Celery on UNIX systems, so if you’re trying to set up a distributed task queue on &lt;em&gt;Windows&lt;/em&gt;, then this might not be the right tutorial for you.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To receive tasks from your program and send results to a back end, Celery requires a &lt;a href=&quot;https://en.wikipedia.org/wiki/Message_broker&quot;&gt;message broker&lt;/a&gt; for communication. &lt;strong&gt;Redis&lt;/strong&gt; and &lt;strong&gt;RabbitMQ&lt;/strong&gt; are two message brokers that developers often use together with Celery.&lt;/p&gt;
&lt;p&gt;In this tutorial, you’ll use &lt;a href=&quot;https://redis.io/&quot;&gt;Redis&lt;/a&gt; as the message broker. To challenge yourself, you can stray from the instructions and use &lt;a href=&quot;https://www.rabbitmq.com/&quot;&gt;RabbitMQ&lt;/a&gt; as a message broker instead.&lt;/p&gt;
&lt;p&gt;If you want to keep track of the results of your task runs, then you also need to set up a &lt;strong&gt;results back end&lt;/strong&gt; database.&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; Connecting Celery to a results back end is optional. Once you instruct Celery to run a task, it’ll do its duty whether you keep track of the task result or not.&lt;/p&gt;
&lt;p&gt;However, keeping a record of all task results is often helpful, especially if you’re distributing tasks to multiple queues. To persist information about task results, you need a database back end.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;You can use &lt;a href=&quot;https://docs.celeryq.dev/en/latest/userguide/configuration.html#std-setting-result_backend&quot;&gt;many different databases&lt;/a&gt; to keep track of Celery task results. In this tutorial, you’ll work with &lt;a href=&quot;https://realpython.com/python-redis/&quot;&gt;Redis&lt;/a&gt; both as a message broker and as a results back end. By using Redis, you limit the dependencies that you need to install because it can take on both roles.&lt;/p&gt;
&lt;p&gt;You won’t do any work with the recorded task results in the scope of this tutorial. However, as a next step, you could inspect the results with the Redis command-line interface (CLI) or pull information into a dedicated page in your Django project.&lt;/p&gt;
&lt;h3 id=&quot;why-use-celery&quot;&gt;Why Use Celery?&lt;a class=&quot;headerlink&quot; href=&quot;#why-use-celery&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;There are two main reasons why most developers want to start using Celery:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Offloading work&lt;/strong&gt; from your app to distributed processes that can run independently of your app&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scheduling task execution&lt;/strong&gt; at a specific time, sometimes as recurring events&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Celery is an excellent choice for both of these use cases. It defines itself as “a task queue with focus on real-time processing, while also supporting task scheduling” (&lt;a href=&quot;https://docs.celeryq.dev/en/stable/index.html&quot;&gt;Source&lt;/a&gt;). &lt;/p&gt;
&lt;p&gt;Even though both of these functionalities are part of Celery, they’re often addressed separately:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Celery workers&lt;/strong&gt; are worker processes that run tasks independently from one another and outside the context of your main service.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Celery beat&lt;/strong&gt; is a scheduler that orchestrates when to run tasks. You can use it to schedule periodic tasks as well.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Celery workers are the backbone of Celery. Even if you aim to schedule recurring tasks using Celery beat, a Celery worker will pick up your instructions and handle them at the scheduled time. What Celery beat adds to the mix is a time-based scheduler for Celery workers.&lt;/p&gt;
&lt;p&gt;In this tutorial, you’ll learn how to integrate Celery with Django to perform operations asynchronously from the main execution thread of your app using Celery workers.&lt;/p&gt;
&lt;p&gt;You won’t tackle task scheduling with Celery beat in this tutorial, but once you understand the basics of Celery tasks, you’ll be well equipped to set up &lt;a href=&quot;https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html&quot;&gt;periodic tasks&lt;/a&gt; with Celery beat.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/asynchronous-tasks-with-django-and-celery/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/asynchronous-tasks-with-django-and-celery/ »&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>How to Run Your Python Scripts and Code</title>
      <id>https://realpython.com/run-python-scripts/</id>
      <link href="https://realpython.com/run-python-scripts/"/>
      <updated>2024-12-08T14:00:00+00:00</updated>
      <summary>A Python script or program is a file containing executable Python code. Being able to run Python scripts and code is probably the most important skill that you need as a Python developer. By running your code, you&#x27;ll know if it works as planned.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Running a Python script is a fundamental task for any Python developer. You can execute a Python &lt;code&gt;.py&lt;/code&gt; file through various methods depending on your environment and platform. On Windows, Linux, and macOS, use the command line by typing &lt;code&gt;python script_name.py&lt;/code&gt; to run your script. You can also use the &lt;code&gt;python&lt;/code&gt; command with the &lt;code&gt;-m&lt;/code&gt; option to execute modules. This tutorial covers these methods and more, ensuring you can run Python scripts efficiently.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this tutorial, you’ll understand that:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Running a Python &lt;code&gt;.py&lt;/code&gt; script&lt;/strong&gt; involves using the &lt;code&gt;python&lt;/code&gt; command followed by the script’s filename in the terminal or command prompt.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Running Python from the command prompt&lt;/strong&gt; requires you to open the command prompt, navigate to the script’s directory, and execute it using &lt;code&gt;python script_name.py&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Running a &lt;code&gt;.py&lt;/code&gt; file in Windows&lt;/strong&gt; can be done directly from the command prompt or by double-clicking the file if Python is associated with &lt;code&gt;.py&lt;/code&gt; files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Running a Python script without Python installed&lt;/strong&gt; is possible by using online interpreters or converting scripts to executables, but it’s more flexible to install Python and run scripts natively.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To get the most out of this tutorial, you should know the basics of working with your operating system’s &lt;a href=&quot;https://realpython.com/terminal-commands/&quot;&gt;terminal&lt;/a&gt; and file manager. It’d also be beneficial for you to be familiar with a Python-friendly &lt;a href=&quot;https://realpython.com/python-ides-code-editors-guide/&quot;&gt;IDE or code editor&lt;/a&gt; and with the standard Python &lt;a href=&quot;https://realpython.com/python-repl/&quot;&gt;REPL&lt;/a&gt; (Read-Eval-Print Loop).&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 Download:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/python-tricks-sample-pdf/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-tricks-sample-pdf&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Get a sample chapter from Python Tricks: The Book&lt;/a&gt; that shows you Python’s best practices with simple examples you can apply instantly to write more beautiful + Pythonic 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.57da6203435d.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 “How to Run Your Python Scripts” 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/run-python-scripts/&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: #e6c7ab;&quot; alt=&quot;How to Run Your Python Scripts&quot; src=&quot;https://files.realpython.com/media/How-to-Run-A-Python-Script_Watermarked.65fe32bf5487.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-Run-A-Python-Script_Watermarked.65fe32bf5487.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/How-to-Run-A-Python-Script_Watermarked.65fe32bf5487.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/How-to-Run-A-Python-Script_Watermarked.65fe32bf5487.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/How-to-Run-A-Python-Script_Watermarked.65fe32bf5487.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.57da6203435d.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/run-python-scripts/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;How to Run Your Python Scripts&lt;/span&gt;&lt;/a&gt; 
      &lt;p class=&quot;text-muted mb-0 small&quot;&gt;One of the most important skills you need to build as a Python developer is to be able to run Python scripts and code. Test your understanding on how good you are with running your code.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;what-scripts-and-modules-are&quot;&gt;What Scripts and Modules Are&lt;a class=&quot;headerlink&quot; href=&quot;#what-scripts-and-modules-are&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In computing, the term &lt;strong&gt;script&lt;/strong&gt; refers to a text file containing a logical sequence of orders that you can run to accomplish a specific task. These orders are typically expressed in a &lt;a href=&quot;https://en.wikipedia.org/wiki/Scripting_language&quot;&gt;scripting language&lt;/a&gt;, which is a &lt;a href=&quot;https://en.wikipedia.org/wiki/Programming_language&quot;&gt;programming language&lt;/a&gt; that allows you to manipulate, customize, and automate tasks.&lt;/p&gt;
&lt;p&gt;Scripting languages are usually &lt;a href=&quot;https://en.wikipedia.org/wiki/Interpreter_(computing)&quot;&gt;interpreted&lt;/a&gt; at &lt;a href=&quot;https://en.wikipedia.org/wiki/Runtime_(program_lifecycle_phase)&quot;&gt;runtime&lt;/a&gt; rather than &lt;a href=&quot;https://en.wikipedia.org/wiki/Compiler&quot;&gt;compiled&lt;/a&gt;. So, scripts are typically run by some kind of interpreter, which is responsible for executing each order in a sequence.&lt;/p&gt;
&lt;p&gt;Python is an interpreted language. Because of that, Python programs are commonly called scripts. However, this terminology isn’t completely accurate because Python programs can be way more complex than a simple, sequential script.&lt;/p&gt;
&lt;p&gt;In general, a file containing executable Python code is called a script—or an &lt;strong&gt;entry-point script&lt;/strong&gt; in more complex applications—which is a common term for a top-level &lt;strong&gt;program&lt;/strong&gt;. On the other hand, a file containing Python code that’s designed to be imported and used from another Python file is called a &lt;strong&gt;module&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;So, the main difference between a &lt;a href=&quot;https://realpython.com/python-modules-packages/&quot;&gt;module&lt;/a&gt; and a script is that modules store &lt;strong&gt;importable code&lt;/strong&gt; while scripts hold &lt;strong&gt;executable code&lt;/strong&gt;.&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; Importable code is code that defines something but doesn’t perform a specific action. Some examples include function and class definitions. In contrast, executable code is code that performs specific actions. Some examples include &lt;a href=&quot;https://realpython.com/defining-your-own-python-function/#function-calls-and-definition&quot;&gt;function calls&lt;/a&gt;, loops, and &lt;a href=&quot;https://realpython.com/python-conditional-statements/&quot;&gt;conditionals&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;In the following sections, you’ll learn how to run Python scripts, programs, and code in general. To kick things off, you’ll start by learning how to run them from your operating system’s command line or terminal.&lt;/p&gt;
&lt;h2 id=&quot;how-to-run-python-scripts-from-the-command-line&quot;&gt;How to Run Python Scripts From the Command Line&lt;a class=&quot;headerlink&quot; href=&quot;#how-to-run-python-scripts-from-the-command-line&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In Python programming, you’ll write programs in plain text files. By convention, files containing Python code use the &lt;code&gt;.py&lt;/code&gt; extension, and there’s no distinction between scripts or executable programs and modules. All of them will use the same extension.&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; On &lt;a href=&quot;https://realpython.com/python-coding-setup-windows/&quot;&gt;Windows&lt;/a&gt; systems, the extension can also be &lt;code&gt;.pyw&lt;/code&gt; for those applications that should use the &lt;code&gt;pythonw.exe&lt;/code&gt; launcher.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;To create a Python script, you can use any Python-friendly &lt;a href=&quot;https://realpython.com/python-ides-code-editors-guide/&quot;&gt;code editor or IDE&lt;/a&gt; (integrated development environment). To keep moving forward in this tutorial, you’ll need to create a basic script, so fire up your favorite text editor and create a new &lt;code&gt;hello.py&lt;/code&gt; file containing the following code:&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; background: inherit;&quot;&gt;hello.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;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hello, World!&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.57da6203435d.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.57da6203435d.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 is the classic &lt;code&gt;&quot;Hello, World!&quot;&lt;/code&gt; program in Python. The executable code consists of a call to the built-in &lt;a href=&quot;https://realpython.com/python-print/&quot;&gt;&lt;code&gt;print()&lt;/code&gt;&lt;/a&gt; function that displays the &lt;code&gt;&quot;Hello, World!&quot;&lt;/code&gt; message on your screen.&lt;/p&gt;
&lt;p&gt;With this small program ready, you’re ready to learn different ways to run it. You’ll start by running the program from your command line, which is arguably the most commonly used approach to running scripts.&lt;/p&gt;
&lt;h3 id=&quot;using-the-python-command&quot;&gt;Using the &lt;code&gt;python&lt;/code&gt; Command&lt;a class=&quot;headerlink&quot; href=&quot;#using-the-python-command&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;To run Python scripts with the &lt;code&gt;python&lt;/code&gt; command, you need to open a command-line window and type in the word &lt;code&gt;python&lt;/code&gt; followed by the path to your target script:&lt;/p&gt;
&lt;ul class=&quot;nav nav-tabs justify-content-end js-platform-widget-tabs&quot; role=&quot;tablist&quot;&gt;

  &lt;li class=&quot;nav-item mb-0 js-platform-widget-tab-windows&quot; role=&quot;presentation&quot;&gt;
    &lt;a class=&quot;nav-link link-unstyled text-body active small&quot; id=&quot;windows-tab-1&quot; data-toggle=&quot;tab&quot; href=&quot;#windows-1&quot; role=&quot;tab&quot; aria-controls=&quot;windows-1&quot; aria-selected=&quot;true&quot;&gt;&lt;span class=&quot;icon baseline text-muted mr-1&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.57da6203435d.svg#brands--windows&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Windows&lt;/a&gt;
  &lt;/li&gt;




  &lt;li class=&quot;nav-item mb-0 js-platform-widget-tab-linuxmacos&quot; role=&quot;presentation&quot;&gt;
    &lt;a class=&quot;nav-link link-unstyled text-body small&quot; id=&quot;macos-tab-1&quot; data-toggle=&quot;tab&quot; href=&quot;#linux-macos-1&quot; role=&quot;tab&quot; aria-controls=&quot;linux-macos-1&quot; aria-selected=&quot;false&quot;&gt;&lt;span class=&quot;icon baseline text-muted&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.57da6203435d.svg#v4--linux&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;span class=&quot;icon baseline text-muted mr-1&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.57da6203435d.svg#v4--apple&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Linux + macOS&lt;/a&gt;
  &lt;/li&gt;

&lt;/ul&gt;
&lt;div class=&quot;tab-content mt-2 mb-0 js-platform-widget-content&quot;&gt;
&lt;div aria-labelledby=&quot;windows-tab-1&quot; class=&quot;tab-pane fade show active&quot; id=&quot;windows-1&quot; role=&quot;tabpanel&quot;&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pscon&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;Windows PowerShell&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.57da6203435d.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;PS&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;python&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Hello, World!&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;PS&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;py&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Hello, World!&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.57da6203435d.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.57da6203435d.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;/div&gt;
&lt;div aria-labelledby=&quot;linux-macos-tab-1&quot; class=&quot;tab-pane fade &quot; id=&quot;linux-macos-1&quot; role=&quot;tabpanel&quot;&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.57da6203435d.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;$ &lt;/span&gt;python&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;./hello.py
&lt;span class=&quot;go&quot;&gt;Hello, World!&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.57da6203435d.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.57da6203435d.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;/div&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/run-python-scripts/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/run-python-scripts/ »&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>When to Use a List Comprehension in Python</title>
      <id>https://realpython.com/list-comprehension-python/</id>
      <link href="https://realpython.com/list-comprehension-python/"/>
      <updated>2024-12-07T14:00:00+00:00</updated>
      <summary>Python list comprehensions help you to create lists while performing sophisticated filtering, mapping, and conditional logic on their members. In this tutorial, you&#x27;ll learn when to use a list comprehension in Python and how to create them effectively.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;List comprehensions in Python provide a concise way to create lists by embedding a loop and optional conditional logic in a single line. You use a list comprehension to transform and filter elements from an iterable efficiently. It allows you to replace complex loops and &lt;code&gt;map()&lt;/code&gt; functions with more readable and often faster expressions. By understanding list comprehensions, you can optimize your code for better performance and clarity.&lt;/p&gt;
&lt;p&gt;You can add conditional logic to a list comprehension in Python by appending an &lt;code&gt;if&lt;/code&gt; statement to the end of the expression. This allows you to filter elements based on a condition, effectively replacing the need for &lt;code&gt;filter()&lt;/code&gt; in many cases. Additionally, you can use conditional expressions at the beginning to choose between different outcomes, enhancing the versatility of your list comprehensions.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this tutorial, you’ll understand that:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;list comprehension&lt;/strong&gt; in Python is a tool for creating lists by iterating over an iterable and optionally applying a condition.&lt;/li&gt;
&lt;li&gt;You should use &lt;strong&gt;list comprehensions&lt;/strong&gt; instead of loops when you want concise, readable code that performs transformations or filtering.&lt;/li&gt;
&lt;li&gt;You add &lt;strong&gt;conditional logic&lt;/strong&gt; to a list comprehension by including an &lt;code&gt;if&lt;/code&gt; statement within the comprehension.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;list comprehension&lt;/strong&gt; can be faster than a &lt;code&gt;for&lt;/code&gt; loop because it’s optimized for performance by Python’s internal mechanisms.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;Python list comprehension&lt;/strong&gt; is not lazy—it generates and stores the entire list in memory eagerly.&lt;/li&gt;
&lt;li&gt;The difference between &lt;strong&gt;list comprehensions&lt;/strong&gt; and &lt;code&gt;map()&lt;/code&gt; is that the former creates a list, while the latter returns a &lt;code&gt;map&lt;/code&gt; object, which is iterable.&lt;/li&gt;
&lt;li&gt;To &lt;strong&gt;optimize performance&lt;/strong&gt; with list comprehensions, use them for small to medium-sized lists and profile different approaches to choose the fastest one.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this tutorial, you’ll explore how to leverage list comprehensions to simplify your code. You’ll also gain an understanding of the trade-offs that come with using them so that you can determine when other approaches are preferable.&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/list-comprehension-python-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-list-comprehension-python-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free code&lt;/a&gt; that shows you how and when to use list comprehensions 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.57da6203435d.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 “When to Use a List Comprehension 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/list-comprehension-python/&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;When to Use a List Comprehension in Python&quot; src=&quot;https://files.realpython.com/media/List-Comprehensions-in-Python_Watermarked.39cf85bdd5d0.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/List-Comprehensions-in-Python_Watermarked.39cf85bdd5d0.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/List-Comprehensions-in-Python_Watermarked.39cf85bdd5d0.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/List-Comprehensions-in-Python_Watermarked.39cf85bdd5d0.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/List-Comprehensions-in-Python_Watermarked.39cf85bdd5d0.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.57da6203435d.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/list-comprehension-python/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;When to Use a List Comprehension 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 Python list comprehensions. You&#x27;ll revisit how to rewrite loops as list comprehensions, how to choose between comprehensions and loops, and how to use conditional logic in your comprehensions.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;transforming-lists-in-python&quot;&gt;Transforming Lists in Python&lt;a class=&quot;headerlink&quot; href=&quot;#transforming-lists-in-python&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;There are a few different ways to create and add items to a &lt;a href=&quot;https://realpython.com/python-lists-tuples/&quot;&gt;lists&lt;/a&gt; in Python. In this section, you’ll explore &lt;code&gt;for&lt;/code&gt; loops and the &lt;code&gt;map()&lt;/code&gt; function to perform these tasks. Then, you’ll move on to learn about how to use list comprehensions and when list comprehensions can benefit your Python program.&lt;/p&gt;
&lt;h3 id=&quot;use-for-loops&quot;&gt;Use &lt;code&gt;for&lt;/code&gt; Loops&lt;a class=&quot;headerlink&quot; href=&quot;#use-for-loops&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The most common type of loop is the &lt;a href=&quot;https://realpython.com/python-for-loop/&quot;&gt;&lt;code&gt;for&lt;/code&gt; loop&lt;/a&gt;. You can use a &lt;code&gt;for&lt;/code&gt; loop to create a list of elements in three steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Instantiate an empty list.&lt;/li&gt;
&lt;li&gt;Loop over an iterable or &lt;a href=&quot;https://realpython.com/python-range/&quot;&gt;range&lt;/a&gt; of elements.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/python-append/&quot;&gt;Append&lt;/a&gt; each element to the end of the list.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;If you want to create a list containing the first ten perfect squares, then you can complete these steps in three lines of code:&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.57da6203435d.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;squares&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;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;nb&quot;&gt;range&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;gp&quot;&gt;... &lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;squares&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&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;o&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;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squares&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]&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.57da6203435d.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.57da6203435d.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;Here, you instantiate an empty list, &lt;code&gt;squares&lt;/code&gt;. Then, you use a &lt;code&gt;for&lt;/code&gt; loop to iterate over &lt;code&gt;range(10)&lt;/code&gt;. Finally, you multiply each &lt;a href=&quot;https://realpython.com/python-numbers/&quot;&gt;number&lt;/a&gt; by itself and append the result to the end of the list.&lt;/p&gt;
&lt;h3 id=&quot;work-with-map-objects&quot;&gt;Work With &lt;code&gt;map&lt;/code&gt; Objects&lt;a class=&quot;headerlink&quot; href=&quot;#work-with-map-objects&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;For an alternative approach that’s based in &lt;a href=&quot;https://realpython.com/python-functional-programming/&quot;&gt;functional programming&lt;/a&gt;, you can use &lt;a href=&quot;https://realpython.com/python-map-function/&quot;&gt;&lt;code&gt;map()&lt;/code&gt;&lt;/a&gt;. You pass in a function and an iterable, and &lt;code&gt;map()&lt;/code&gt; will create an object. This object contains the result that you’d get from running each iterable element through the supplied function.&lt;/p&gt;
&lt;p&gt;As an example, consider a situation in which you need to calculate the price after tax for a list of transactions:&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.57da6203435d.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;prices&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.09&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;23.56&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;57.84&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;4.56&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;6.78&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;TAX_RATE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.08&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;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_price_with_tax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;price&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;price&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;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TAX_RATE&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;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;final_prices&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_price_with_tax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prices&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;final_prices&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&amp;lt;map object at 0x7f34da341f90&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;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;final_prices&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[1.1772000000000002, 25.4448, 62.467200000000005, 4.9248, 7.322400000000001]&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.57da6203435d.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.57da6203435d.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;Here, you have an iterable, &lt;code&gt;prices&lt;/code&gt;, and a function, &lt;code&gt;get_price_with_tax()&lt;/code&gt;. You pass both of these arguments to &lt;code&gt;map()&lt;/code&gt; and store the resulting &lt;code&gt;map&lt;/code&gt; object in &lt;code&gt;final_prices&lt;/code&gt;. Finally, you convert &lt;code&gt;final_prices&lt;/code&gt; into a list using &lt;code&gt;list()&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id=&quot;leverage-list-comprehensions&quot;&gt;Leverage List Comprehensions&lt;a class=&quot;headerlink&quot; href=&quot;#leverage-list-comprehensions&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;List comprehensions are a third way of making or transforming lists. With this elegant approach, you could rewrite the &lt;code&gt;for&lt;/code&gt; loop from the first example in just a single line of code:&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.57da6203435d.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;squares&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;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&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;nb&quot;&gt;range&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;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;squares&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]&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.57da6203435d.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.57da6203435d.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;Rather than creating an empty list and adding each element to the end, you simply define the list and its contents at the same time by following this format:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;python_syntax&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 Syntax&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;n&quot;&gt;new_list&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;expression&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;member&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterable&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.57da6203435d.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.57da6203435d.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;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/list-comprehension-python/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/list-comprehension-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>How to Round Numbers in Python</title>
      <id>https://realpython.com/python-rounding/</id>
      <link href="https://realpython.com/python-rounding/"/>
      <updated>2024-12-07T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll learn what kinds of mistakes you might make when rounding numbers and how you can best manage or avoid them. It&#x27;s a great place to start for the early-intermediate Python developer interested in using Python for finance, data science, or scientific computing.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Rounding numbers in Python is an essential task, especially when dealing with data precision. Python’s built-in &lt;code&gt;round()&lt;/code&gt; function uses the rounding half to even strategy, which rounds numbers like &lt;code&gt;2.5&lt;/code&gt; to &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;3.5&lt;/code&gt; to &lt;code&gt;4&lt;/code&gt;. This method helps minimize rounding bias in datasets. To round numbers to specific decimal places, you can use the &lt;code&gt;round()&lt;/code&gt; function with a second argument specifying the number of decimals.&lt;/p&gt;
&lt;p&gt;For more advanced rounding strategies, you can explore Python’s &lt;a href=&quot;https://realpython.com/python-fractions/#floating-point-and-decimal-numbers&quot;&gt;&lt;code&gt;decimal&lt;/code&gt;&lt;/a&gt; module or use &lt;a href=&quot;https://realpython.com/numpy-tutorial/&quot;&gt;NumPy&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/learning-paths/pandas-data-science/&quot;&gt;pandas&lt;/a&gt; for data science applications. NumPy arrays and pandas DataFrames offer methods for rounding numbers efficiently. In NumPy, you can use functions like &lt;code&gt;np.round()&lt;/code&gt;, &lt;code&gt;np.ceil()&lt;/code&gt;, &lt;code&gt;np.floor()&lt;/code&gt;, and &lt;code&gt;np.trunc()&lt;/code&gt; to apply different rounding strategies. For pandas, the &lt;code&gt;df.round()&lt;/code&gt; method allows rounding of entire DataFrames or specific columns.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this tutorial, you’ll understand that:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Python uses the &lt;strong&gt;rounding half to even&lt;/strong&gt; strategy, where ties round to the nearest even number.&lt;/li&gt;
&lt;li&gt;Python’s default rounding strategy minimizes &lt;strong&gt;rounding bias&lt;/strong&gt; in large datasets.&lt;/li&gt;
&lt;li&gt;You can round numbers to &lt;strong&gt;specific decimal places&lt;/strong&gt; using Python’s &lt;code&gt;round()&lt;/code&gt; function with a second argument.&lt;/li&gt;
&lt;li&gt;Different &lt;strong&gt;rounding strategies&lt;/strong&gt; can be applied using Python’s &lt;code&gt;decimal&lt;/code&gt; module or custom functions for precision control.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;NumPy&lt;/strong&gt; and &lt;strong&gt;pandas&lt;/strong&gt; provide methods for rounding numbers in &lt;strong&gt;arrays&lt;/strong&gt; and &lt;strong&gt;DataFrames&lt;/strong&gt;, offering flexibility in data manipulation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You won’t get a treatise on numeric precision in computing, although you’ll touch briefly on the subject. Only a familiarity with the &lt;a href=&quot;https://realpython.com/products/python-basics-book/&quot;&gt;fundamentals of Python&lt;/a&gt; is necessary, and the math should feel familiar if you’ve had high school algebra.&lt;/p&gt;
&lt;p&gt;You’ll start by looking at Python’s built-in rounding mechanism.&lt;/p&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.57da6203435d.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 “Rounding Numbers 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-rounding/&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 Round Numbers in Python&quot; src=&quot;https://files.realpython.com/media/How-to-Round-Numbers-in-Python_Watermark.6fb8573ce8f5.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-Round-Numbers-in-Python_Watermark.6fb8573ce8f5.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/How-to-Round-Numbers-in-Python_Watermark.6fb8573ce8f5.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/How-to-Round-Numbers-in-Python_Watermark.6fb8573ce8f5.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/How-to-Round-Numbers-in-Python_Watermark.6fb8573ce8f5.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.57da6203435d.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-rounding/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Rounding Numbers in Python&lt;/span&gt;&lt;/a&gt; 
      &lt;p class=&quot;text-muted mb-0 small&quot;&gt;Test your knowledge of rounding numbers in Python.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&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-rounding-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-rounding-code&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 learn about rounding numbers in Python.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;pythons-built-in-round-function&quot;&gt;Python’s Built-in &lt;code&gt;round()&lt;/code&gt; Function&lt;a class=&quot;headerlink&quot; href=&quot;#pythons-built-in-round-function&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Python has a built-in &lt;code&gt;round()&lt;/code&gt; function that takes two numeric arguments, &lt;code&gt;n&lt;/code&gt; and &lt;code&gt;ndigits&lt;/code&gt;, and returns the &lt;a href=&quot;https://realpython.com/python-numbers/&quot;&gt;number&lt;/a&gt; &lt;code&gt;n&lt;/code&gt; rounded to &lt;code&gt;ndigits&lt;/code&gt;. The &lt;code&gt;ndigits&lt;/code&gt; argument defaults to zero, so leaving it out results in a number rounded to an integer. As you’ll see, &lt;code&gt;round()&lt;/code&gt; may not work quite as you expect.&lt;/p&gt;
&lt;p&gt;The way most people are taught to round a number goes something like this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Round the number &lt;code&gt;n&lt;/code&gt; to &lt;code&gt;p&lt;/code&gt; decimal places by first shifting the decimal point in &lt;code&gt;n&lt;/code&gt; by &lt;code&gt;p&lt;/code&gt; places. To do that, multiply &lt;code&gt;n&lt;/code&gt; by 10ᵖ (10 raised to the &lt;code&gt;p&lt;/code&gt; power) to get a new number, &lt;code&gt;m&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Then look at the digit &lt;code&gt;d&lt;/code&gt; in the first decimal place of &lt;code&gt;m&lt;/code&gt;. If &lt;code&gt;d&lt;/code&gt; is less than 5, round &lt;code&gt;m&lt;/code&gt; down to the nearest integer. Otherwise, round &lt;code&gt;m&lt;/code&gt; up.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Finally, shift the decimal point back &lt;code&gt;p&lt;/code&gt; places by dividing &lt;code&gt;m&lt;/code&gt; by 10ᵖ.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It’s an algorithm! For example, the number &lt;code&gt;2.5&lt;/code&gt; rounded to the nearest whole number is &lt;code&gt;3&lt;/code&gt;. The number &lt;code&gt;1.64&lt;/code&gt; rounded to one decimal place is &lt;code&gt;1.6&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now open up an interpreter session and round &lt;code&gt;2.5&lt;/code&gt; to the nearest whole number using Python’s built-in &lt;a href=&quot;https://docs.python.org/3/library/functions.html#round&quot;&gt;&lt;code&gt;round()&lt;/code&gt;&lt;/a&gt; function:&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.57da6203435d.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;nb&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;2.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;2&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.57da6203435d.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.57da6203435d.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;Gasp!&lt;/p&gt;
&lt;p&gt;Check out how &lt;code&gt;round()&lt;/code&gt; handles the number &lt;code&gt;1.5&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.57da6203435d.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;nb&quot;&gt;round&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;2&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.57da6203435d.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.57da6203435d.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;So, &lt;code&gt;round()&lt;/code&gt; rounds &lt;code&gt;1.5&lt;/code&gt; up to &lt;code&gt;2&lt;/code&gt;, and &lt;code&gt;2.5&lt;/code&gt; down to &lt;code&gt;2&lt;/code&gt;!&lt;/p&gt;
&lt;p&gt;Before you go raising an issue on the Python bug tracker, rest assured you that &lt;code&gt;round(2.5)&lt;/code&gt; is &lt;em&gt;supposed&lt;/em&gt; to return &lt;code&gt;2&lt;/code&gt;. There’s a good reason why &lt;code&gt;round()&lt;/code&gt; behaves the way it does.&lt;/p&gt;
&lt;p&gt;In this tutorial, you’ll learn that there are more ways to round a number than you might expect, each with unique advantages and disadvantages. &lt;code&gt;round()&lt;/code&gt; behaves according to a particular rounding strategy—which may or may not be the one you need for a given situation.&lt;/p&gt;
&lt;p&gt;You might be wondering, &lt;em&gt;Can the way I round numbers really have that much of an impact?&lt;/em&gt; Next up, take a look at just how extreme the effects of rounding can be.&lt;/p&gt;
&lt;h2 id=&quot;how-much-impact-can-rounding-have&quot;&gt;How Much Impact Can Rounding Have?&lt;a class=&quot;headerlink&quot; href=&quot;#how-much-impact-can-rounding-have&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Suppose you have an incredibly lucky day and find $100 on the ground. Rather than spending all your money at once, you decide to play it smart and invest your money by buying some shares of different stocks.&lt;/p&gt;
&lt;p&gt;The value of a stock depends on supply and demand. The more people there are who want to buy a stock, the more value that stock has, and vice versa. In high-volume stock markets, the value of a particular stock can fluctuate on a second-by-second basis.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-rounding/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-rounding/ »&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>Linear Regression in Python</title>
      <id>https://realpython.com/linear-regression-in-python/</id>
      <link href="https://realpython.com/linear-regression-in-python/"/>
      <updated>2024-12-07T14:00:00+00:00</updated>
      <summary>In this step-by-step tutorial, you&#x27;ll get started with linear regression in Python. Linear regression is one of the fundamental statistical and machine learning techniques, and Python is a popular choice for machine learning.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Linear regression is a foundational statistical tool for modeling the relationship between a dependent variable and one or more independent variables. It’s widely used in &lt;a href=&quot;https://realpython.com/tutorials/data-science/&quot;&gt;data science&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/tutorials/machine-learning/&quot;&gt;machine learning&lt;/a&gt; to predict outcomes and understand relationships between variables. In Python, implementing linear regression can be straightforward with the help of third-party libraries such as scikit-learn and statsmodels.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this tutorial, you’ll understand that:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Linear regression&lt;/strong&gt; is a statistical method for modeling the relationship between a dependent variable and one or more independent variables by fitting a linear equation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Implementing linear regression in Python&lt;/strong&gt; involves using libraries like scikit-learn and statsmodels to fit models and make predictions.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;formula for linear regression&lt;/strong&gt; is 𝑦 = 𝛽₀ + 𝛽₁𝑥₁ + ⋯ + 𝛽ᵣ𝑥ᵣ + 𝜀, representing the linear relationship between variables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Simple linear regression&lt;/strong&gt; involves one independent variable, whereas &lt;strong&gt;multiple linear regression&lt;/strong&gt; involves two or more.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;scikit-learn&lt;/strong&gt; library provides a convenient and efficient interface for performing linear regression in Python.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To implement linear regression in Python, you typically follow a five-step process: import necessary packages, provide and transform data, create and fit a regression model, evaluate the results, and make predictions. This approach allows you to perform both simple and multiple linear regressions, as well as polynomial regression, using Python’s robust ecosystem of scientific libraries.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;&lt;p&gt;&lt;strong&gt;Free Bonus:&lt;/strong&gt; &lt;a href=&quot;#&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-numpy-learning-guide&quot; data-focus=&quot;false&quot;&gt;Click here to get access to a free NumPy Resources Guide&lt;/a&gt; that points you to the best tutorials, videos, and books for improving your NumPy skills.&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.57da6203435d.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 “Linear Regression 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/linear-regression-in-python/&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: #abe5b2;&quot; alt=&quot;Linear Regression in Python&quot; src=&quot;https://files.realpython.com/media/Linear-Regression-in-Python_Watermarked.479f82188ace.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/Linear-Regression-in-Python_Watermarked.479f82188ace.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Linear-Regression-in-Python_Watermarked.479f82188ace.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Linear-Regression-in-Python_Watermarked.479f82188ace.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Linear-Regression-in-Python_Watermarked.479f82188ace.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.57da6203435d.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/linear-regression-in-python/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Linear Regression 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 knowledge of linear regression in Python. Linear regression is one of the fundamental statistical and machine learning techniques, and Python is a popular choice for machine learning.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;regression&quot;&gt;Regression&lt;a class=&quot;headerlink&quot; href=&quot;#regression&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Regression analysis is one of the most important fields in statistics and machine learning. There are many regression methods available. Linear regression is one of them.&lt;/p&gt;
&lt;h3 id=&quot;what-is-regression&quot;&gt;What Is Regression?&lt;a class=&quot;headerlink&quot; href=&quot;#what-is-regression&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Regression searches for relationships among &lt;strong&gt;variables&lt;/strong&gt;. For example, you can observe several employees of some company and try to understand how their salaries depend on their &lt;strong&gt;features&lt;/strong&gt;, such as experience, education level, role, city of employment, and so on.&lt;/p&gt;
&lt;p&gt;This is a regression problem where data related to each employee represents one &lt;strong&gt;observation&lt;/strong&gt;. The presumption is that the experience, education, role, and city are the independent features, while the salary depends on them.&lt;/p&gt;
&lt;p&gt;Similarly, you can try to establish the mathematical dependence of housing prices on area, number of bedrooms, distance to the city center, and so on.&lt;/p&gt;
&lt;p&gt;Generally, in regression analysis, you consider some phenomenon of interest and have a number of observations. Each observation has two or more features. Following the assumption that at least one of the features depends on the others, you try to establish a relation among them.&lt;/p&gt;
&lt;p&gt;In other words, you need to find a &lt;strong&gt;function that maps some features or variables to others&lt;/strong&gt; sufficiently well.&lt;/p&gt;
&lt;p&gt;The dependent features are called the &lt;strong&gt;dependent variables&lt;/strong&gt;, &lt;strong&gt;outputs&lt;/strong&gt;, or &lt;strong&gt;responses&lt;/strong&gt;. The independent features are called the &lt;strong&gt;independent variables&lt;/strong&gt;, &lt;strong&gt;inputs&lt;/strong&gt;, &lt;strong&gt;regressors&lt;/strong&gt;, or &lt;strong&gt;predictors&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Regression problems usually have one continuous and unbounded dependent variable. The inputs, however, can be continuous, discrete, or even categorical data such as gender, nationality, or brand.&lt;/p&gt;
&lt;p&gt;It’s a common practice to denote the outputs with 𝑦 and the inputs with 𝑥. If there are two or more independent variables, then they can be represented as the vector 𝐱 = (𝑥₁, …, 𝑥ᵣ), where 𝑟 is the number of inputs.&lt;/p&gt;
&lt;h3 id=&quot;when-do-you-need-regression&quot;&gt;When Do You Need Regression?&lt;a class=&quot;headerlink&quot; href=&quot;#when-do-you-need-regression&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Typically, you need regression to answer whether and how some phenomenon influences the other or how &lt;strong&gt;several&lt;/strong&gt; variables are related. For example, you can use it to determine &lt;em&gt;if&lt;/em&gt; and &lt;em&gt;to what extent&lt;/em&gt; experience or gender impacts salaries.&lt;/p&gt;
&lt;p&gt;Regression is also useful when you want to &lt;strong&gt;forecast&lt;/strong&gt; a response using a new set of predictors. For example, you could try to predict electricity consumption of a household for the next hour given the outdoor temperature, time of day, and number of residents in that household.&lt;/p&gt;
&lt;p&gt;Regression is used in many different fields, including economics, computer science, and the social sciences. Its importance rises every day with the availability of large amounts of data and increased awareness of the practical value of data.&lt;/p&gt;
&lt;h2 id=&quot;linear-regression&quot;&gt;Linear Regression&lt;a class=&quot;headerlink&quot; href=&quot;#linear-regression&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Linear regression is probably one of the most important and widely used regression techniques. It’s among the simplest regression methods. One of its main advantages is the ease of interpreting results.&lt;/p&gt;
&lt;h3 id=&quot;problem-formulation&quot;&gt;Problem Formulation&lt;a class=&quot;headerlink&quot; href=&quot;#problem-formulation&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;When implementing linear regression of some dependent variable 𝑦 on the set of independent variables 𝐱 = (𝑥₁, …, 𝑥ᵣ), where 𝑟 is the number of predictors, you assume a linear relationship between 𝑦 and 𝐱: 𝑦 = 𝛽₀ + 𝛽₁𝑥₁ + ⋯ + 𝛽ᵣ𝑥ᵣ + 𝜀. This equation is the &lt;strong&gt;regression equation&lt;/strong&gt;. 𝛽₀, 𝛽₁, …, 𝛽ᵣ are the &lt;strong&gt;regression coefficients&lt;/strong&gt;, and 𝜀 is the &lt;strong&gt;random error&lt;/strong&gt;.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/linear-regression-in-python/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/linear-regression-in-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>Python GUI Programming With Tkinter</title>
      <id>https://realpython.com/python-gui-tkinter/</id>
      <link href="https://realpython.com/python-gui-tkinter/"/>
      <updated>2024-12-07T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll learn the basics of GUI programming with Tkinter, the de facto Python GUI framework. Master GUI programming concepts such as widgets, geometry managers, and event handlers. Then, put it all together by building two applications: a temperature converter and a text editor.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Tkinter is Python’s standard GUI framework, making it convenient for developing graphical user interfaces. As a cross-platform library, Tkinter ensures your applications appear native across Windows, macOS, and Linux. Despite criticisms of its outdated appearance, Tkinter remains a practical choice for quickly creating functional and cross-platform GUI applications.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;By the end of this tutorial, you’ll understand that:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;GUI&lt;/strong&gt; refers to graphical user interfaces, and &lt;strong&gt;Tkinter&lt;/strong&gt; is Python’s built-in library for creating them.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tkinter&lt;/strong&gt; is included with most Python installations, so separate installation is often unnecessary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tkinter&lt;/strong&gt; is still a relevant choice for building simple, cross-platform GUI applications.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Widgets&lt;/strong&gt; in a &lt;strong&gt;Tkinter&lt;/strong&gt; application can be arranged using geometry managers like &lt;strong&gt;&lt;code&gt;.pack()&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;.place()&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;&lt;code&gt;.grid()&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interactive GUI applications&lt;/strong&gt; with &lt;strong&gt;Tkinter&lt;/strong&gt; are created by binding events, such as button clicks, to Python functions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You’ll cover getting started with Tkinter, managing widgets, and creating interactive applications. Once you’ve mastered these skills by working through the exercises at the end of each section, you’ll tie everything together by building two applications. The first is a &lt;strong&gt;temperature converter&lt;/strong&gt;, and the second is a &lt;strong&gt;text editor&lt;/strong&gt;. It’s time to dive right in and learn how to build an application with Tkinter!&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; This tutorial is adapted from the chapter “Graphical User Interfaces” of &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;.&lt;/p&gt;
&lt;p&gt;The book uses Python’s built-in &lt;a href=&quot;https://realpython.com/python-idle/&quot;&gt;IDLE&lt;/a&gt; editor to create and edit Python files and interact with the Python shell. In this tutorial, references to IDLE have been removed in favor of more general language.&lt;/p&gt;
&lt;p&gt;The bulk of the material in this tutorial has been left unchanged, and you should have no problems running the example code from the editor and environment of your choice.&lt;/p&gt;
&lt;/div&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-mastery-course/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-mastery-course&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;5 Thoughts On Python Mastery&lt;/a&gt;, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.&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.57da6203435d.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 GUI Programming With Tkinter” 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-gui-programming-with-tkinter/&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: #abe5b2;&quot; alt=&quot;Python GUI Programming With Tkinter&quot; src=&quot;https://files.realpython.com/media/Python-GUI-Programming-With-Tkinter_Watermarked.9e1a9c9070e4.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-GUI-Programming-With-Tkinter_Watermarked.9e1a9c9070e4.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Python-GUI-Programming-With-Tkinter_Watermarked.9e1a9c9070e4.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Python-GUI-Programming-With-Tkinter_Watermarked.9e1a9c9070e4.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Python-GUI-Programming-With-Tkinter_Watermarked.9e1a9c9070e4.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.57da6203435d.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-gui-programming-with-tkinter/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Python GUI Programming With Tkinter&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 GUI Programming With Tkinter, the de facto Python GUI framework. Check your knowledge of GUI programming concepts such as widgets, geometry managers, and event handlers.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;building-your-first-python-gui-application-with-tkinter&quot;&gt;Building Your First Python GUI Application With Tkinter&lt;a class=&quot;headerlink&quot; href=&quot;#building-your-first-python-gui-application-with-tkinter&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The foundational element of a Tkinter GUI is the &lt;strong&gt;window&lt;/strong&gt;. Windows are the containers in which all other GUI elements live. These other GUI elements, such as text boxes, labels, and buttons, are known as &lt;strong&gt;widgets&lt;/strong&gt;. Widgets are contained inside of windows.&lt;/p&gt;
&lt;p&gt;First, create a window that contains a single widget. Start up a new &lt;a href=&quot;https://realpython.com/interacting-with-python/#using-the-python-interpreter-interactively&quot;&gt;Python shell&lt;/a&gt; session and follow along!&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; The code examples in this tutorial have all been tested on Windows, macOS, and Ubuntu Linux 20.04 with Python version 3.10.&lt;/p&gt;
&lt;p&gt;If you’ve &lt;a href=&quot;https://realpython.com/installing-python/&quot;&gt;installed Python&lt;/a&gt; with the official installers available for &lt;a href=&quot;https://www.python.org/downloads/windows/&quot;&gt;Windows&lt;/a&gt; and &lt;a href=&quot;https://www.python.org/downloads/mac-osx/&quot;&gt;macOS&lt;/a&gt; from &lt;a href=&quot;https://python.org&quot;&gt;python.org&lt;/a&gt;, then you should have no problem running the sample code. You can safely skip the rest of this note and continue with the tutorial!&lt;/p&gt;
&lt;p&gt;If you haven’t installed Python with the official installers, or there’s no official distribution for your system, then here are some tips for getting up and going.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Python on macOS with Homebrew:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Python distribution for macOS available on &lt;a href=&quot;https://github.com/Homebrew/homebrew-core/pull/34424&quot;&gt;Homebrew&lt;/a&gt; doesn’t come bundled with the &lt;a href=&quot;https://www.tcl.tk/&quot;&gt;Tcl/Tk&lt;/a&gt; dependency required by Tkinter. The default system version is used instead. This version may be outdated and prevent you from importing the Tkinter module. To avoid this problem, use the &lt;a href=&quot;https://www.python.org/downloads/mac-osx/&quot;&gt;official macOS installer&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ubuntu Linux 20.04:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To conserve memory space, the default version of the Python interpreter that comes pre-installed on Ubuntu Linux 20.04 has no support for Tkinter. However, if you want to continue using the Python interpreter bundled with your operating system, then install the following package:&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.57da6203435d.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;$ &lt;/span&gt;sudo&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;apt-get&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;install&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;python3-tk
&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.57da6203435d.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.57da6203435d.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 installs the Python GUI Tkinter module.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Other Linux Flavors:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you’re unable to get a working Python installation on your flavor of Linux, then you can build Python with the correct version of Tcl/Tk from the source code. For a step-by-step walk-through of this process, check out the &lt;a href=&quot;https://realpython.com/installing-python/#compiling-python-from-source&quot;&gt;Python 3 Installation &amp;amp; Setup Guide&lt;/a&gt;. You may also try using &lt;a href=&quot;https://realpython.com/intro-to-pyenv/&quot;&gt;pyenv&lt;/a&gt; to manage multiple Python versions.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;With your Python shell open, the first thing you need to do is import the Python GUI Tkinter module:&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.57da6203435d.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;tkinter&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;tk&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.57da6203435d.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.57da6203435d.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;A &lt;strong&gt;window&lt;/strong&gt; is an instance of Tkinter’s &lt;code&gt;Tk&lt;/code&gt; class. Go ahead and create a new window and assign it to the &lt;a href=&quot;https://realpython.com/python-variables/&quot;&gt;variable&lt;/a&gt; &lt;code&gt;window&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.57da6203435d.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;window&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Tk&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.57da6203435d.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.57da6203435d.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;When you execute the above code, a new window pops up on your screen. How it looks depends on your operating system:&lt;/p&gt;
&lt;figure class=&quot;js-lightbox&quot;&gt;&lt;a href=&quot;https://files.realpython.com/media/17_4_tk_window.662fec42e4f9.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img loading=&quot;lazy&quot; class=&quot;img-fluid mx-auto d-block &quot; src=&quot;https://files.realpython.com/media/17_4_tk_window.662fec42e4f9.jpg&quot; width=&quot;853&quot; height=&quot;337&quot; srcset=&quot;/cdn-cgi/image/width=213,format=auto/https://files.realpython.com/media/17_4_tk_window.662fec42e4f9.jpg 213w, /cdn-cgi/image/width=284,format=auto/https://files.realpython.com/media/17_4_tk_window.662fec42e4f9.jpg 284w, /cdn-cgi/image/width=426,format=auto/https://files.realpython.com/media/17_4_tk_window.662fec42e4f9.jpg 426w, /cdn-cgi/image/width=853,format=auto/https://files.realpython.com/media/17_4_tk_window.662fec42e4f9.jpg 853w&quot; sizes=&quot;(min-width: 1200px) 690px, (min-width: 780px) calc(-5vw + 669px), (min-width: 580px) 510px, calc(100vw - 30px)&quot; alt=&quot;A blank Tkinter application window on Windows 10, macOS, and Ubuntu Linux&quot; data-asset=&quot;2147&quot;&gt;&lt;/a&gt;&lt;/figure&gt;

&lt;p&gt;Throughout the rest of this tutorial, you’ll see Windows screenshots.&lt;/p&gt;
&lt;h3 id=&quot;adding-a-widget&quot;&gt;Adding a Widget&lt;a class=&quot;headerlink&quot; href=&quot;#adding-a-widget&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Now that you have a window, you can add a widget. Use the &lt;code&gt;tk.Label&lt;/code&gt; class to add some text to a window. Create a &lt;code&gt;Label&lt;/code&gt; widget with the text &lt;code&gt;&quot;Hello, Tkinter&quot;&lt;/code&gt; and assign it to a variable called &lt;code&gt;greeting&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.57da6203435d.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;greeting&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tk&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Label&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hello, Tkinter&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.57da6203435d.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.57da6203435d.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 window you created earlier doesn’t change. You just created a &lt;code&gt;Label&lt;/code&gt; widget, but you haven’t added it to the window yet. There are several ways to add widgets to a window. Right now, you can use the &lt;code&gt;Label&lt;/code&gt; widget’s &lt;code&gt;.pack()&lt;/code&gt; 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.57da6203435d.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;greeting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&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.57da6203435d.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.57da6203435d.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 window now looks like this:&lt;/p&gt;
&lt;figure class=&quot;js-lightbox&quot;&gt;&lt;a href=&quot;https://files.realpython.com/media/17_4_tk_hello_win10.724293a495ad.jpg&quot; target=&quot;_blank&quot;&gt;&lt;img loading=&quot;lazy&quot; class=&quot;img-fluid mx-auto d-block w-50&quot; src=&quot;https://files.realpython.com/media/17_4_tk_hello_win10.724293a495ad.jpg&quot; width=&quot;489&quot; height=&quot;155&quot; srcset=&quot;/cdn-cgi/image/width=122,format=auto/https://files.realpython.com/media/17_4_tk_hello_win10.724293a495ad.jpg 122w, /cdn-cgi/image/width=163,format=auto/https://files.realpython.com/media/17_4_tk_hello_win10.724293a495ad.jpg 163w, /cdn-cgi/image/width=244,format=auto/https://files.realpython.com/media/17_4_tk_hello_win10.724293a495ad.jpg 244w, /cdn-cgi/image/width=489,format=auto/https://files.realpython.com/media/17_4_tk_hello_win10.724293a495ad.jpg 489w&quot; sizes=&quot;(min-width: 1200px) 690px, (min-width: 780px) calc(-5vw + 669px), (min-width: 580px) 510px, calc(100vw - 30px)&quot; alt=&#x27;Example &quot;Hello, world&quot; Tkinter application on Windows 10&#x27; data-asset=&quot;2146&quot;&gt;&lt;/a&gt;&lt;/figure&gt;

&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-gui-tkinter/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-gui-tkinter/ »&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 #231: Good Python Programming Practices When New to the Language</title>
      <id>https://realpython.com/podcasts/rpp/231/</id>
      <link href="https://realpython.com/podcasts/rpp/231/"/>
      <updated>2024-12-06T12:00:00+00:00</updated>
      <summary>What advice would you give to someone moving from another language to Python? What good programming practices are inherent to the language? 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 advice would you give to someone moving from another language to Python? What good programming practices are inherent to the language? 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>Handling or Preventing Errors in Python: LBYL vs EAFP</title>
      <id>https://realpython.com/courses/handling-preventing-errors-lbyl-eafp/</id>
      <link href="https://realpython.com/courses/handling-preventing-errors-lbyl-eafp/"/>
      <updated>2024-12-03T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn about two popular coding styles in Python: Look Before You Leap (LBYL) and Easier to Ask Forgiveness than Permission (EAFP). You can use these styles to deal with errors and exceptional situations in your code. You&#x27;ll dive into the discussion on LBYL vs EAFP in Python.</summary>
      <content type="html">
        &lt;p&gt;Dealing with errors and exceptional situations is a common requirement in programming. You can either &lt;em&gt;prevent errors&lt;/em&gt; before they happen or &lt;em&gt;handle errors&lt;/em&gt; after they&amp;rsquo;ve happened. In general, you&amp;rsquo;ll have two coding styles matching these strategies: &lt;strong&gt;look before you leap&lt;/strong&gt; (LBYL), and &lt;strong&gt;easier to ask forgiveness than permission&lt;/strong&gt; (EAFP). In this video course, you&amp;rsquo;ll dive into the questions and considerations surrounding LBYL vs EAFP in Python.&lt;/p&gt;
&lt;p&gt;By learning about Python&amp;rsquo;s LBYL and EAFP coding styles, you&amp;rsquo;ll be able to decide which strategy and coding style to use when you&amp;rsquo;re dealing with errors in your 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;Use the &lt;strong&gt;LBYL&lt;/strong&gt; and &lt;strong&gt;EAFP&lt;/strong&gt; styles in your Python code&lt;/li&gt;
&lt;li&gt;Understand the &lt;strong&gt;pros&lt;/strong&gt; and &lt;strong&gt;cons&lt;/strong&gt; of LBYL vs EAFP&lt;/li&gt;
&lt;li&gt;Decide &lt;strong&gt;when to use&lt;/strong&gt; either LBYL or EAFP&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 #230: marimo: Reactive Notebooks and Deployable Web Apps in Python</title>
      <id>https://realpython.com/podcasts/rpp/230/</id>
      <link href="https://realpython.com/podcasts/rpp/230/"/>
      <updated>2024-11-29T12:00:00+00:00</updated>
      <summary>What are common issues with using notebooks for Python development? How do you know the current state, share reproducible results, or create interactive applications? This week on the show, we speak with Akshay Agrawal about the open-source reactive marimo notebook for Python.</summary>
      <content type="html">
        &lt;p&gt;What are common issues with using notebooks for Python development? How do you know the current state, share reproducible results, or create interactive applications? This week on the show, we speak with Akshay Agrawal about the open-source reactive marimo notebook for 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>Managing Dependencies With Python Poetry</title>
      <id>https://realpython.com/courses/managing-dependencies-with-poetry/</id>
      <link href="https://realpython.com/courses/managing-dependencies-with-poetry/"/>
      <updated>2024-11-26T14:00:00+00:00</updated>
      <summary>Learn how Python Poetry can help you start new projects, maintain existing ones, and master dependency management.</summary>
      <content type="html">
        &lt;p&gt;When your Python project relies on external packages, you need to make sure you&amp;rsquo;re using the right version of each package. After an update, a package might not work as it did before. A &lt;strong&gt;dependency manager&lt;/strong&gt; like Python Poetry helps you specify, install, and resolve external packages in your projects. This way, you can be sure that you always work with the correct dependency version on every machine.&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;Create a &lt;strong&gt;new project&lt;/strong&gt; using Poetry&lt;/li&gt;
&lt;li&gt;Add Poetry to an &lt;strong&gt;existing project&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Configure your project through &lt;strong&gt;&lt;code&gt;pyproject.toml&lt;/code&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Pin your project&amp;rsquo;s &lt;strong&gt;dependency versions&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Install dependencies from a &lt;strong&gt;&lt;code&gt;poetry.lock&lt;/code&gt;&lt;/strong&gt; file&lt;/li&gt;
&lt;li&gt;Run basic Poetry commands using the &lt;strong&gt;Poetry CLI&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 #229: The Joy of Tinkering &amp; Python Free-Threading Performance</title>
      <id>https://realpython.com/podcasts/rpp/229/</id>
      <link href="https://realpython.com/podcasts/rpp/229/"/>
      <updated>2024-11-22T12:00:00+00:00</updated>
      <summary>What keeps your spark alive for developing software and learning Python? Do you like to try new frameworks, build toy projects, or collaborate with other developers? 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 keeps your spark alive for developing software and learning Python? Do you like to try new frameworks, build toy projects, or collaborate with other developers? 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: Expression vs Statement in Python: What&#x27;s the Difference?</title>
      <id>https://realpython.com/quizzes/python-expression-vs-statement/</id>
      <link href="https://realpython.com/quizzes/python-expression-vs-statement/"/>
      <updated>2024-11-21T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of Python expressions vs statements. Knowing the difference between these two is crucial for writing efficient and readable Python code.</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-expression-vs-statement/&quot;&gt;Expression vs Statement in Python: What&amp;rsquo;s the Difference?&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;By working through this quiz, you&amp;rsquo;ll revisit the key differences between expressions and statements in Python, and how to use them effectively in your 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>Quiz: Interacting With Python</title>
      <id>https://realpython.com/quizzes/interacting-with-python/</id>
      <link href="https://realpython.com/quizzes/interacting-with-python/"/>
      <updated>2024-11-21T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of the different ways of interacting with Python. By working through this quiz, you&#x27;ll revisit key concepts related to Python interaction in interactive mode using the REPL, through Python script files, and within IDEs and code editors.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of the different ways you can &lt;a href=&quot;https://realpython.com/interacting-with-python/&quot;&gt;interact with Python&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By working through this quiz, you&amp;rsquo;ll revisit key concepts related to Python interaction in interactive mode using the Read-Eval-Print Loop (REPL), through Python script files, and within Integrated Development Environments (IDEs) and code editors.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll also test your knowledge of some other options that may be useful, such as Jupyter Notebooks.&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: NumPy Practical Examples: Useful Techniques</title>
      <id>https://realpython.com/quizzes/numpy-example/</id>
      <link href="https://realpython.com/quizzes/numpy-example/"/>
      <updated>2024-11-20T12:00:00+00:00</updated>
      <summary>This quiz will test your understanding of working with NumPy arrays. You won&#x27;t find all the answers in the tutorial, so you&#x27;ll need to do some extra investigating. By finding all the answers, you&#x27;re sure to learn some interesting things along the way.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of the techniques covered in the tutorial
&lt;a href=&quot;https://realpython.com/numpy-example/&quot;&gt;NumPy Practical Examples: Useful Techniques&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By working through the questions, you&amp;rsquo;ll review your understanding of NumPy arrays and also expand on what you learned in the tutorial. &lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll need to do some research outside of the tutorial to answer all the questions. Embrace this challenge and let it take you on a learning journey.&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>Working With TOML and Python</title>
      <id>https://realpython.com/courses/working-with-toml-python/</id>
      <link href="https://realpython.com/courses/working-with-toml-python/"/>
      <updated>2024-11-19T14:00:00+00:00</updated>
      <summary>TOML is a configuration file format that&#x27;s becoming increasingly popular in the Python community. In this video course, you&#x27;ll learn the syntax of TOML and explore how you can work with TOML files in your own projects.</summary>
      <content type="html">
        &lt;p&gt;TOML&amp;mdash;Tom&amp;rsquo;s Obvious Minimal Language&amp;mdash;is a reasonably new configuration file format that the Python community has embraced over the last couple of years. TOML plays an essential part in the Python ecosystem. Many of your favorite tools rely on TOML for configuration, and you&amp;rsquo;ll use &lt;code&gt;pyproject.toml&lt;/code&gt; when you build and distribute your own packages.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course, you&amp;rsquo;ll learn more about TOML and how you can use it. In particular, you&amp;rsquo;ll:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Learn and understand the &lt;strong&gt;syntax&lt;/strong&gt; of TOML&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;tomli&lt;/code&gt; and &lt;code&gt;tomllib&lt;/code&gt; to &lt;strong&gt;parse&lt;/strong&gt; TOML documents&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;tomli_w&lt;/code&gt; to &lt;strong&gt;write&lt;/strong&gt; data structures as TOML&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;tomlkit&lt;/code&gt; when you need &lt;strong&gt;more control&lt;/strong&gt; over your TOML files&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 #228: Maintaining the Foundations of Python &amp; Cautionary Tales</title>
      <id>https://realpython.com/podcasts/rpp/228/</id>
      <link href="https://realpython.com/podcasts/rpp/228/"/>
      <updated>2024-11-15T12:00:00+00:00</updated>
      <summary>How do you build a sustainable open-source project and community? What lessons can be learned from Python&#x27;s history and the current mess that the WordPress community is going through? This week on the show, we speak with Paul Everitt from JetBrains about navigating open-source funding and the start of the Python Software Foundation.</summary>
      <content type="html">
        &lt;p&gt;How do you build a sustainable open-source project and community? What lessons can be learned from Python&#x27;s history and the current mess that the WordPress community is going through? This week on the show, we speak with Paul Everitt from JetBrains about navigating open-source funding and the start of the Python Software Foundation.&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: Namespaces and Scope in Python</title>
      <id>https://realpython.com/quizzes/python-namespaces-scope/</id>
      <link href="https://realpython.com/quizzes/python-namespaces-scope/"/>
      <updated>2024-11-14T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of Python namespaces and variable scope. These concepts are crucial for organizing the symbolic names assigned to objects in a Python program and ensuring they don&#x27;t interfere with one another.</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-namespaces-scope/&quot;&gt;Python Namespaces and Scope&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll revisit how Python organizes symbolic names and objects in namespaces, when Python creates a new namespace, how namespaces are implemented, and how variable scope determines symbolic name visibility.&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 Dictionary Comprehensions: How and When to Use Them</title>
      <id>https://realpython.com/quizzes/python-dictionary-comprehension/</id>
      <link href="https://realpython.com/quizzes/python-dictionary-comprehension/"/>
      <updated>2024-11-13T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of Python dictionary comprehensions. Dictionary comprehensions are a concise and quick way to create, transform, and filter dictionaries in Python, and can significantly enhance your code&#x27;s conciseness and readability.</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-dictionary-comprehension/&quot;&gt;Python Dictionary Comprehensions: How and When to Use Them&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Understanding dictionary comprehensions is crucial for you as a Python developer because they provide a Pythonic tool for dictionary manipulation and can be a valuable addition to your programming toolkit.&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: Basic Input and Output in Python</title>
      <id>https://realpython.com/quizzes/python-input-output/</id>
      <link href="https://realpython.com/quizzes/python-input-output/"/>
      <updated>2024-11-13T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of Python&#x27;s built-in functions for user interaction, namely input() and print(). These functions allow you to capture user input from the keyboard and display output to the console, respectively.</summary>
      <content type="html">
        &lt;p&gt;In this quiz, you&amp;rsquo;ll test your understanding of how to use Python&amp;rsquo;s built-in functions &lt;code&gt;input()&lt;/code&gt; and &lt;code&gt;print()&lt;/code&gt; for &lt;a href=&quot;https://realpython.com/python-input-output/&quot;&gt;basic input and output&lt;/a&gt; operations.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll also revisit how to use &lt;code&gt;readline&lt;/code&gt; to improve the user experience when collecting input, and how to format output using the &lt;code&gt;sep&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; keyword arguments of &lt;code&gt;print()&lt;/code&gt;.&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>Formatting Floats Inside Python F-Strings</title>
      <id>https://realpython.com/courses/format-floats-f-strings/</id>
      <link href="https://realpython.com/courses/format-floats-f-strings/"/>
      <updated>2024-11-12T14:00:00+00:00</updated>
      <summary>In this video course, 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;p&gt;You&amp;rsquo;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&amp;rsquo;s content, but you can also use function calls or even variables.&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 #227: New PEPs: Template Strings &amp; External Wheel Hosting</title>
      <id>https://realpython.com/podcasts/rpp/227/</id>
      <link href="https://realpython.com/podcasts/rpp/227/"/>
      <updated>2024-11-08T12:00:00+00:00</updated>
      <summary>Have you wanted the flexibility of f-strings but need safety checks in place? What if you could have deferred evaluation for logging or avoiding injection attacks? 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;Have you wanted the flexibility of f-strings but need safety checks in place? What if you could have deferred evaluation for logging or avoiding injection attacks? 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>Introduction to Web Scraping With Python</title>
      <id>https://realpython.com/courses/introduction-to-web-scraping-with-python/</id>
      <link href="https://realpython.com/courses/introduction-to-web-scraping-with-python/"/>
      <updated>2024-11-05T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn all about web scraping in Python. You&#x27;ll see how to parse data from websites and interact with HTML forms using tools such as Beautiful Soup and MechanicalSoup.</summary>
      <content type="html">
        &lt;p&gt;&lt;strong&gt;Web scraping&lt;/strong&gt; is the process of collecting and parsing raw data from the Web, and the Python community has come up with some pretty powerful web scraping tools.&lt;/p&gt;
&lt;p&gt;The Internet hosts perhaps the greatest source of information on the planet. Many disciplines, such as &lt;a href=&quot;https://realpython.com/learning-paths/data-science-python-core-skills/&quot;&gt;data science&lt;/a&gt;, business intelligence, and investigative reporting, can benefit enormously from collecting and analyzing data from websites. &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;Parse website data using &lt;strong&gt;string methods&lt;/strong&gt; and &lt;strong&gt;regular expressions&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Parse website data using an &lt;strong&gt;HTML parser&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Interact with &lt;strong&gt;forms&lt;/strong&gt; and other website components&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>Quiz: Variables in Python: Usage and Best Practices</title>
      <id>https://realpython.com/quizzes/python-variables/</id>
      <link href="https://realpython.com/quizzes/python-variables/"/>
      <updated>2024-11-05T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of variables in Python. Variables are symbolic names that refer to objects or values stored in your computer&#x27;s memory, and they&#x27;re essential building blocks for any Python program.</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-variables/&quot;&gt;Variables in Python: Usage and Best Practices&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;By working through this quiz, you&amp;rsquo;ll revisit how to create and assign values to variables, change a variable&amp;rsquo;s data type dynamically, use variables to create expressions, counters, accumulators, and Boolean flags, follow best practices for naming variables, and create, access, and use variables in their scopes.&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 #226: PySheets: Spreadsheets in the Browser Using PyScript</title>
      <id>https://realpython.com/podcasts/rpp/226/</id>
      <link href="https://realpython.com/podcasts/rpp/226/"/>
      <updated>2024-11-01T12:00:00+00:00</updated>
      <summary>What goes into building a spreadsheet application in Python that runs in the browser? How do you make it launch quickly, and where do you store the cells of data? This week on the show, we speak with Chris Laffra about his project, PySheets, and his book &quot;Communication for Engineers.&quot;</summary>
      <content type="html">
        &lt;p&gt;What goes into building a spreadsheet application in Python that runs in the browser? How do you make it launch quickly, and where do you store the cells of data? This week on the show, we speak with Chris Laffra about his project, PySheets, and his book &quot;Communication for Engineers.&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>Python&#x27;s Magic Methods in Classes</title>
      <id>https://realpython.com/courses/magic-methods-classes/</id>
      <link href="https://realpython.com/courses/magic-methods-classes/"/>
      <updated>2024-10-29T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn what magic methods are in Python, how they work, and how to use them in your custom classes to support powerful features in your object-oriented code.</summary>
      <content type="html">
        &lt;p&gt;As a Python developer who wants to harness the power of object-oriented programming, you&amp;rsquo;ll love to learn how to customize your classes using &lt;strong&gt;special methods&lt;/strong&gt;, also known as &lt;strong&gt;magic methods&lt;/strong&gt; or &lt;strong&gt;dunder methods&lt;/strong&gt;. A special method is a method whose name starts and ends with a double underscore. These methods have special meanings in Python.&lt;/p&gt;
&lt;p&gt;Python automatically calls magic methods as a response to certain operations, such as instantiation, sequence indexing, attribute managing, and much more. Magic methods support core object-oriented features in Python, so learning about them is fundamental for you as a Python programmer.&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;Learn what Python&amp;rsquo;s &lt;strong&gt;special&lt;/strong&gt; or &lt;strong&gt;magic methods&lt;/strong&gt; are&lt;/li&gt;
&lt;li&gt;Understand the &lt;strong&gt;magic&lt;/strong&gt; behind magic methods in Python&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Customize&lt;/strong&gt; different &lt;strong&gt;behaviors&lt;/strong&gt; of your custom classes with special methods&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>Quiz: Using .__repr__() vs .__str__() in Python</title>
      <id>https://realpython.com/quizzes/python-repr-vs-str/</id>
      <link href="https://realpython.com/quizzes/python-repr-vs-str/"/>
      <updated>2024-10-29T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of Python&#x27;s dunder repr and dunder str special methods. These methods allow you to control how a program displays an object, making your classes more readable and easier to debug and maintain.</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-repr-vs-str/&quot;&gt;Python&amp;rsquo;s &lt;code&gt;.__repr__()&lt;/code&gt; and &lt;code&gt;.__str__()&lt;/code&gt;&lt;/a&gt; special methods. These methods allow you to control how a program displays an object, making your classes more readable and easier to debug and maintain.&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: Beautiful Soup: Build a Web Scraper With Python</title>
      <id>https://realpython.com/quizzes/beautiful-soup-web-scraper-python/</id>
      <link href="https://realpython.com/quizzes/beautiful-soup-web-scraper-python/"/>
      <updated>2024-10-28T12:00:00+00:00</updated>
      <summary>In this quiz, you&#x27;ll test your understanding of web scraping using Python. By working through this quiz, you&#x27;ll revisit how to inspect the HTML structure of a target site, decipher data encoded in URLs, and use Requests and Beautiful Soup for scraping and parsing data.</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/beautiful-soup-web-scraper-python/&quot;&gt;web scraping with Python, Requests, and Beautiful Soup&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By working through this quiz, you&amp;rsquo;ll revisit how to inspect the HTML structure of your target site with your browser&amp;rsquo;s developer tools, decipher data encoded in URLs, use Requests and Beautiful Soup for scraping and parsing internet data, and gain an understanding of what a web scraping pipeline looks like.&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 #225: Python Getting Faster and Leaner &amp; Ideas for Django Projects</title>
      <id>https://realpython.com/podcasts/rpp/225/</id>
      <link href="https://realpython.com/podcasts/rpp/225/"/>
      <updated>2024-10-25T12:00:00+00:00</updated>
      <summary>What changes are happening under the hood in the latest versions of Python? How are these updates laying the groundwork for a faster Python in the coming years? 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 changes are happening under the hood in the latest versions of Python? How are these updates laying the groundwork for a faster Python in the coming years? 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: How to Reset a pandas DataFrame Index</title>
      <id>https://realpython.com/quizzes/pandas-reset-index/</id>
      <link href="https://realpython.com/quizzes/pandas-reset-index/"/>
      <updated>2024-10-25T12:00:00+00:00</updated>
      <summary>This quiz will challenge your knowledge of resetting indexes in pandas DataFrames. You won&#x27;t find all the answers in the tutorial, so you&#x27;ll need to do some extra investigating. By finding all the answers, you&#x27;re sure to learn some interesting things along the way.</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/pandas-reset-index/&quot;&gt;how to reset a pandas DataFrame index&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;By working through the questions, you&amp;rsquo;ll review your knowledge of indexing and also expand on what you learned in the tutorial. &lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll need to do some research outside of the tutorial to answer all the questions. Embrace this challenge and let it take you on a learning journey.&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>Understanding Python&#x27;s Global Interpreter Lock (GIL)</title>
      <id>https://realpython.com/courses/understanding-global-interpreter-lock-gil/</id>
      <link href="https://realpython.com/courses/understanding-global-interpreter-lock-gil/"/>
      <updated>2024-10-22T14:00:00+00:00</updated>
      <summary>Python&#x27;s Global Interpreter Lock or GIL, in simple words, is a mutex (or a lock) that allows only one thread to hold the control of the Python interpreter at any one time. In this video course you&#x27;ll learn how the GIL affects the performance of your Python programs.</summary>
      <content type="html">
        &lt;p&gt;The Python Global Interpreter Lock or &lt;a href=&quot;https://wiki.python.org/moin/GlobalInterpreterLock&quot;&gt;GIL&lt;/a&gt;, in simple words, is a mutex (or a lock) that allows only one &lt;a href=&quot;https://realpython.com/intro-to-python-threading/&quot;&gt;thread&lt;/a&gt; to hold the control of the Python interpreter. &lt;/p&gt;
&lt;p&gt;This means that only one thread can be in a state of execution at any point in time. The impact of the GIL isn&amp;rsquo;t visible to developers who execute single-threaded programs, but it can be a performance bottleneck in CPU-bound and multi-threaded code.&lt;/p&gt;
&lt;p&gt;Since the GIL allows only one thread to execute at a time even in a multi-threaded architecture with more than one CPU core, the GIL has gained a reputation as an &amp;ldquo;infamous&amp;rdquo; feature of Python.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course you&amp;rsquo;ll learn&lt;/strong&gt; how the GIL affects the performance of your Python programs, and how you can mitigate the impact it might have on your 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>The Real Python Podcast – Episode #224: Narwhals: Expanding DataFrame Compatibility Between Libraries</title>
      <id>https://realpython.com/podcasts/rpp/224/</id>
      <link href="https://realpython.com/podcasts/rpp/224/"/>
      <updated>2024-10-18T12:00:00+00:00</updated>
      <summary>How does a Python tool support all types of DataFrames and their various features? Could a lightweight library be used to add compatibility for newer formats like Polars or PyArrow? This week on the show, we speak with Marco Gorelli about his project, Narwhals.</summary>
      <content type="html">
        &lt;p&gt;How does a Python tool support all types of DataFrames and their various features? Could a lightweight library be used to add compatibility for newer formats like Polars or PyArrow? This week on the show, we speak with Marco Gorelli about his project, Narwhals.&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>Using Type Hints for Multiple Return Types in Python</title>
      <id>https://realpython.com/courses/type-hints-multiple-return-types/</id>
      <link href="https://realpython.com/courses/type-hints-multiple-return-types/"/>
      <updated>2024-10-15T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn how to define multiple return types using type hints in Python. This course covers working with single or multiple pieces of data, defining type aliases, and performing type checking using a third-party static type checker tool.</summary>
      <content type="html">
        &lt;p&gt;In Python, &lt;strong&gt;type hinting&lt;/strong&gt; is an optional yet useful feature for making your code easier to read, reason about, and debug. With type hints, you let other developers know the expected data types for variables, function arguments, and return values. As you write code for applications that require greater flexibility, you may need to specify &lt;strong&gt;multiple return types&lt;/strong&gt; to make your code more robust and adaptable to different situations.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll encounter different use cases where you may want to annotate multiple return types within a single function in Python. In other words, the data returned can vary in type. In this video course, you&amp;rsquo;ll walk through examples of how to specify multiple return types for a function that parses a string from an email address to grab the domain name.&lt;/p&gt;
&lt;p&gt;In addition, you&amp;rsquo;ll see examples of how to specify type hints for callback functions or functions that take another function as input. With these examples, you&amp;rsquo;ll be ready to express type hints in functional programming.&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 #223: Exploring the New Features of Python 3.13</title>
      <id>https://realpython.com/podcasts/rpp/223/</id>
      <link href="https://realpython.com/podcasts/rpp/223/"/>
      <updated>2024-10-11T12:00:00+00:00</updated>
      <summary>Python 3.13 is here! Our regular guests, Geir Arne Hjelle and Christopher Trudeau, return to discuss the new version. This year, Geir Arne coordinated a series of preview articles with members of the Real Python team and a showcase tutorial, &quot;Python 3.13: Cool New Features for You to Try.&quot; Christopher&#x27;s video course &quot;What&#x27;s New in Python 3.13&quot; covers the topics from the article and shows the new features in action.</summary>
      <content type="html">
        &lt;p&gt;Python 3.13 is here! Our regular guests, Geir Arne Hjelle and Christopher Trudeau, return to discuss the new version. This year, Geir Arne coordinated a series of preview articles with members of the Real Python team and a showcase tutorial, &quot;Python 3.13: Cool New Features for You to Try.&quot; Christopher&#x27;s video course &quot;What&#x27;s New in Python 3.13&quot; covers the topics from the article and shows the new features in action.&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 New in Python 3.13</title>
      <id>https://realpython.com/courses/new-features-python-313/</id>
      <link href="https://realpython.com/courses/new-features-python-313/"/>
      <updated>2024-10-08T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn about the new features in Python 3.13. You&#x27;ll take a tour of the new REPL and error messages and see how you can try out the experimental free threading and JIT versions of Python 3.13 yourself.</summary>
      <content type="html">
        &lt;p&gt;&lt;a href=&quot;https://www.python.org/downloads/release/python-3120/&quot;&gt;Python 3.13&lt;/a&gt; was published on &lt;a href=&quot;https://peps.python.org/pep-0719/&quot;&gt;October 7, 2024&lt;/a&gt;. This new version is a major step forward for the language, although several of the biggest changes are happening under the hood and won&amp;rsquo;t be immediately visible to you.&lt;/p&gt;
&lt;p&gt;In a sense, Python 3.13 is laying the groundwork for some future improvements, especially to the language&amp;rsquo;s performance. As you watch the course, you&amp;rsquo;ll learn more about the background for this and dive into some new features that are fully available now.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course, you&amp;rsquo;ll learn about some of the improvements in the new version, including:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Improvements made to the &lt;strong&gt;interactive interpreter (REPL)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Clearer error messages&lt;/strong&gt; that can help you fix common mistakes&lt;/li&gt;
&lt;li&gt;Advancements done in &lt;strong&gt;removing the global interpreter lock (GIL)&lt;/strong&gt; and making Python free-threaded&lt;/li&gt;
&lt;li&gt;The implementation of an &lt;strong&gt;experimental Just-In-Time (JIT) compiler&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;A host of minor upgrades to &lt;strong&gt;Python&amp;rsquo;s static type system&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this video course, you&amp;rsquo;ll explore these changes and see how this new version of Python can work for you.&lt;/p&gt;
&lt;p&gt;If you want to try any of the examples in this video course, then you&amp;rsquo;ll need to use Python 3.13. The &lt;a href=&quot;https://realpython.com/installing-python/&quot;&gt;Python 3 Installation &amp;amp; Setup Guide&lt;/a&gt; and &lt;a href=&quot;https://realpython.com/python-pre-release/&quot;&gt;How Can You Install a Pre-Release Version of Python?&lt;/a&gt; walk you through several options for adding a new version of Python to your system.&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 #222: Using Virtual Environments in Docker &amp; Comparing Python Dev Tools</title>
      <id>https://realpython.com/podcasts/rpp/222/</id>
      <link href="https://realpython.com/podcasts/rpp/222/"/>
      <updated>2024-09-27T12:00:00+00:00</updated>
      <summary>Should you use a Python virtual environment in a Docker container? What are the advantages of using the same development practices locally and inside a container? 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;Should you use a Python virtual environment in a Docker container? What are the advantages of using the same development practices locally and inside a container? 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>
  

</feed>
