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

  
    <entry>
      <title>Python String Formatting: Available Tools and Their Features</title>
      <id>https://realpython.com/python-string-formatting/</id>
      <link href="https://realpython.com/python-string-formatting/"/>
      <updated>2024-12-01T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll learn about the main tools for string formatting in Python, as well as their strengths and weaknesses. These tools include f-strings, the .format() method, and the modulo operator.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;String formatting is essential in Python for creating dynamic and well-structured text by inserting values into strings. This tutorial covers various methods, including f-strings, the &lt;code&gt;.format()&lt;/code&gt; method, and the modulo operator (&lt;code&gt;%&lt;/code&gt;). Each method has unique features and benefits for different use cases. The string formatting mini-language provides additional control over the output format, allowing for aligned text, numeric formatting, and more.&lt;/p&gt;
&lt;p&gt;F-strings provide an intuitive and efficient way to embed expressions inside string literals. The &lt;code&gt;.format()&lt;/code&gt; method offers flexibility for lazy interpolation and is compatible with Python’s formatting mini-language. The modulo operator is an older technique still found in legacy code. Understanding these methods will help you choose the best option for your specific string formatting 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;&lt;strong&gt;String formatting in Python&lt;/strong&gt; involves inserting and formatting values within strings using interpolation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python supports different types of string formatting&lt;/strong&gt;, including f-strings, the &lt;code&gt;.format()&lt;/code&gt; method, and the modulo operator (&lt;code&gt;%&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;F-strings&lt;/strong&gt; are generally the most readable and efficient option for eager interpolation in Python.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python’s string formatting mini-language&lt;/strong&gt; offers features like alignment, type conversion, and numeric formatting.&lt;/li&gt;
&lt;li&gt;While &lt;strong&gt;f-strings&lt;/strong&gt; are more readable and efficient compared to &lt;code&gt;.format()&lt;/code&gt; and the &lt;code&gt;%&lt;/code&gt; operator, the &lt;code&gt;.format()&lt;/code&gt; method supports lazy evaluation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To get the most out of this tutorial, you should be familiar with Python’s &lt;a href=&quot;https://realpython.com/python-strings/&quot;&gt;string data type&lt;/a&gt; and the available &lt;a href=&quot;https://realpython.com/python-string-interpolation/&quot;&gt;string interpolation&lt;/a&gt; tools. Having a basic knowledge of the string &lt;a href=&quot;https://realpython.com/python-format-mini-language/&quot;&gt;formatting mini-language&lt;/a&gt; is also a plus.&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/python-string-formatting-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-string-formatting-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 Python’s string formatting tools.&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.6e7b751c31b4.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 String Formatting: Available Tools and Their Features” 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-string-formatting/&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: #abe0e6;&quot; alt=&quot;Python String Formatting: Available Tools and Their Features&quot; src=&quot;https://files.realpython.com/media/Python-String-Formatting-Best-Practices_Watermarked-2.e0a560b0184d.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-String-Formatting-Best-Practices_Watermarked-2.e0a560b0184d.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Python-String-Formatting-Best-Practices_Watermarked-2.e0a560b0184d.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Python-String-Formatting-Best-Practices_Watermarked-2.e0a560b0184d.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Python-String-Formatting-Best-Practices_Watermarked-2.e0a560b0184d.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.6e7b751c31b4.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-string-formatting/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Python String Formatting: Available Tools and Their Features&lt;/span&gt;&lt;/a&gt; 
      &lt;p class=&quot;text-muted mb-0 small&quot;&gt;You can take this quiz to test your understanding of the available tools for string formatting in Python, as well as their strengths and weaknesses. These tools include f-strings, the .format() method, and the modulo operator.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;interpolating-and-formatting-strings-in-python&quot;&gt;Interpolating and Formatting Strings in Python&lt;a class=&quot;headerlink&quot; href=&quot;#interpolating-and-formatting-strings-in-python&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/String_interpolation&quot;&gt;String interpolation&lt;/a&gt; involves generating strings by inserting other strings or objects into specific places in a base string or template. For example, here’s how you can do some string interpolation using an f-string:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Bob&quot;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;!&quot;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;Hello, Bob!&#x27;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this quick example, you first have a Python &lt;a href=&quot;https://realpython.com/python-variables/&quot;&gt;variable&lt;/a&gt; containing a string object, &lt;code&gt;&quot;Bob&quot;&lt;/code&gt;. Then, you create a new string using an f-string. In this string, you insert the content of your &lt;code&gt;name&lt;/code&gt; variable using a replacement field. When you run this last line of code, Python builds a final string, &lt;code&gt;&#x27;Hello, Bob!&#x27;&lt;/code&gt;. The insertion of &lt;code&gt;name&lt;/code&gt; into the f-string is an interpolation.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To dive deeper into string interpolation, check out the &lt;a href=&quot;https://realpython.com/python-string-interpolation/&quot;&gt;String Interpolation in Python: Exploring Available Tools&lt;/a&gt; tutorial.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;When you do string interpolation, you may need to &lt;em&gt;format&lt;/em&gt; the interpolated values to produce a well-formatted final string. To do this, you can use different string interpolation tools that support string formatting. In Python, you have these three tools:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https://realpython.com/python-f-strings/&quot;&gt;F-strings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;The &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; method&lt;/li&gt;
&lt;li&gt;The &lt;a href=&quot;https://realpython.com/python-string-interpolation/#using-the-modulo-operator-for-interpolation&quot;&gt;modulo operator (&lt;code&gt;%&lt;/code&gt;)&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The first two tools support the string formatting mini-language, a feature that allows you to fine-tune your strings. The third tool is a bit old and has fewer formatting options. However, you can use it to do some minimal formatting.&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 built-in &lt;a href=&quot;https://docs.python.org/3/library/functions.html#format&quot;&gt;&lt;code&gt;format()&lt;/code&gt;&lt;/a&gt; function is yet another tool that supports the format specification mini-language. This function is typically used for date and number formatting, but you won’t cover it in this tutorial.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;In the following sections, you’ll start by learning a bit about the string formatting mini-language. Then, you’ll dive into using this language, f-strings, and the &lt;code&gt;.format()&lt;/code&gt; method to format your strings. Finally, you’ll learn about the formatting capabilities of the modulo operator.&lt;/p&gt;
&lt;h2 id=&quot;using-f-strings-to-format-strings&quot;&gt;Using F-Strings to Format Strings&lt;a class=&quot;headerlink&quot; href=&quot;#using-f-strings-to-format-strings&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Python &lt;a href=&quot;https://dbader.org/blog/cool-new-features-in-python-3-6&quot;&gt;3.6&lt;/a&gt; added a string interpolation and formatting tool called &lt;strong&gt;formatted string literals&lt;/strong&gt;, or &lt;a href=&quot;https://realpython.com/python-f-strings/&quot;&gt;f-strings&lt;/a&gt; for short. As you’ve already learned, f-strings let you embed Python objects and expressions inside your strings. To create an f-string, you must prefix the string with an &lt;code&gt;f&lt;/code&gt; or &lt;code&gt;F&lt;/code&gt; and insert replacement fields in the string literal. Each replacement field must contain a variable, object, or expression:&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.6e7b751c31b4.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;The number is &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;The number is 42&#x27;&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;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; plus &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; is &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;5 plus 10 is 15&#x27;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the first example, you define an f-string that embeds the number &lt;code&gt;42&lt;/code&gt; directly into the resulting string. In the second example, you insert two variables and an expression into the string.&lt;/p&gt;
&lt;p&gt;Formatted string literals are a Python parser feature that converts f-strings into a series of string constants and expressions. These are then joined up to build the final string.&lt;/p&gt;
&lt;h3 id=&quot;using-the-formatting-mini-language-with-f-strings&quot;&gt;Using the Formatting Mini-Language With F-Strings&lt;a class=&quot;headerlink&quot; href=&quot;#using-the-formatting-mini-language-with-f-strings&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;When you use f-strings to create strings through interpolation, you need to use &lt;strong&gt;replacement fields&lt;/strong&gt;. In f-strings, you can define a replacement field using curly brackets (&lt;code&gt;{}&lt;/code&gt;) as in the examples below:&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.6e7b751c31b4.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;debit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;300.00&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;credit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;450.00&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Debit: $&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debit&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;, Credit: $&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;credit&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;, Balance: $&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;credit&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debit&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;Debit: $300, Credit: $450.0, Balance: $150.0&#x27;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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.6e7b751c31b4.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-string-formatting/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-string-formatting/ »&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 Check if a Python String Contains a Substring</title>
      <id>https://realpython.com/python-string-contains-substring/</id>
      <link href="https://realpython.com/python-string-contains-substring/"/>
      <updated>2024-12-01T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll learn the best way to check whether a Python string contains a substring. You&#x27;ll also learn about idiomatic ways to inspect the substring further, match substrings with conditions using regular expressions, and search for substrings in pandas.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;To check if a string contains another string in Python, use the &lt;code&gt;in&lt;/code&gt; &lt;a href=&quot;https://realpython.com/python-in-operator/&quot;&gt;membership operator&lt;/a&gt;. This is the recommended method for confirming the presence of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Substring&quot;&gt;substring&lt;/a&gt; within a string. The &lt;code&gt;in&lt;/code&gt; operator is intuitive and readable, making it a straightforward way to evaluate substring existence.&lt;/p&gt;
&lt;p&gt;Additionally, you can use string methods like &lt;code&gt;.count()&lt;/code&gt; and &lt;code&gt;.index()&lt;/code&gt; to gather more detailed information about substrings, such as their frequency and position. For more complex substring searches, use regular expressions with the &lt;code&gt;re&lt;/code&gt; module. When you’re dealing with tabular data, then &lt;a href=&quot;https://realpython.com/learning-paths/pandas-data-science/&quot;&gt;pandas&lt;/a&gt; provides efficient tools for searching for substrings within DataFrame 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;The &lt;strong&gt;&lt;code&gt;in&lt;/code&gt; membership operator&lt;/strong&gt; is the recommended way to check if a Python string contains a substring.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Converting input text&lt;/strong&gt; to lowercase generalizes substring checks by &lt;strong&gt;removing case sensitivity&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;.count()&lt;/code&gt; method &lt;strong&gt;counts occurrences&lt;/strong&gt; of a substring, while &lt;code&gt;.index()&lt;/code&gt; finds the first occurrence’s &lt;strong&gt;position&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Regular expressions&lt;/strong&gt; in the &lt;code&gt;re&lt;/code&gt; module allow for advanced substring searches based on &lt;strong&gt;complex conditions&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;.str.contains()&lt;/code&gt; method in &lt;strong&gt;pandas&lt;/strong&gt; identifies which DataFrame entries contain a specific substring.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Understanding these methods and tools enables you to effectively check for substrings in Python strings, catering to various needs from simple checks to complex data analysis.&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-string-contains-substring-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-string-contains-substring-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 check if a string contains a substring.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;how-to-confirm-that-a-python-string-contains-another-string&quot;&gt;How to Confirm That a Python String Contains Another String&lt;a class=&quot;headerlink&quot; href=&quot;#how-to-confirm-that-a-python-string-contains-another-string&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you need to check whether a string contains a substring, use Python’s &lt;a href=&quot;https://realpython.com/python-in-operator/&quot;&gt;membership operator &lt;code&gt;in&lt;/code&gt;&lt;/a&gt;. In Python, this is the recommended way to confirm the existence of a substring in a string:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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;raw_file_content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&quot;Hi there and welcome.&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;This is a special hidden file with a SECRET secret.&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;I don&#x27;t want to tell you The Secret,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;but I do want to secretly tell you that I have one.&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;hll&quot;&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;secret&quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw_file_content&lt;/span&gt;
&lt;/span&gt;&lt;span class=&quot;go&quot;&gt;True&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.6e7b751c31b4.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.6e7b751c31b4.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;in&lt;/code&gt; membership operator gives you a quick and readable way to check whether a substring is present in a string. You may notice that the line of code almost reads like English.&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; If you want to check whether the substring is &lt;em&gt;not&lt;/em&gt; in the string, then you can use &lt;a href=&quot;https://realpython.com/python-in-operator/#pythons-not-in-operator&quot;&gt;&lt;code&gt;not in&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;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.6e7b751c31b4.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;s2&quot;&gt;&quot;secret&quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw_file_content&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;False&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.6e7b751c31b4.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.6e7b751c31b4.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;Because the substring &lt;code&gt;&quot;secret&quot;&lt;/code&gt; is present in &lt;code&gt;raw_file_content&lt;/code&gt;, the &lt;code&gt;not in&lt;/code&gt; operator returns &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;When you use &lt;code&gt;in&lt;/code&gt;, the expression returns a &lt;a href=&quot;https://realpython.com/python-boolean/&quot;&gt;Boolean value&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;True&lt;/code&gt; if Python found the substring&lt;/li&gt;
&lt;li&gt;&lt;code&gt;False&lt;/code&gt; if Python didn’t find the substring&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can use this intuitive syntax in &lt;a href=&quot;https://realpython.com/python-conditional-statements/&quot;&gt;conditional statements&lt;/a&gt; to make decisions in your 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.6e7b751c31b4.svg#regular--rectangle-terminal&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;/span&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style=&quot;position: relative;&quot;&gt;
    &lt;div class=&quot;highlight highlight--with-header&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;secret&quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;raw_file_content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;   &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Found!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Found!&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.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this code snippet, you use the membership operator to check whether &lt;code&gt;&quot;secret&quot;&lt;/code&gt; is a substring of &lt;code&gt;raw_file_content&lt;/code&gt;. If it is, then you’ll print a message to the &lt;a href=&quot;https://realpython.com/terminal-commands/&quot;&gt;terminal&lt;/a&gt;. Any indented code will only execute if the Python string that you’re checking contains the substring that you provide.&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; Python considers &lt;a href=&quot;https://docs.python.org/3.12/reference/expressions.html#membership-test-operations&quot;&gt;empty strings always as a substring&lt;/a&gt; of any other string, so checking for the empty string in a string returns &lt;code&gt;True&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.6e7b751c31b4.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;s2&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;secret&quot;&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;True&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.6e7b751c31b4.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.6e7b751c31b4.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 may be surprising because Python considers &lt;a href=&quot;https://docs.python.org/3/library/stdtypes.html#truth-value-testing&quot;&gt;emtpy strings as false&lt;/a&gt;, but it’s an edge case that is helpful to keep in mind.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The membership operator &lt;code&gt;in&lt;/code&gt; is your best friend if you just need to check whether a Python string contains a substring.&lt;/p&gt;
&lt;p&gt;However, what if you want to know more about the substring? If you read through the text stored in &lt;code&gt;raw_file_content&lt;/code&gt;, then you’ll notice that the substring occurs more than once, and even in different variations!&lt;/p&gt;
&lt;p&gt;Which of these occurrences did Python find? Does capitalization make a difference? How often does the substring show up in the text? And what’s the location of these substrings? If you need the answer to any of these questions, then keep on reading.&lt;/p&gt;
&lt;h2 id=&quot;generalize-your-check-by-removing-case-sensitivity&quot;&gt;Generalize Your Check by Removing Case Sensitivity&lt;a class=&quot;headerlink&quot; href=&quot;#generalize-your-check-by-removing-case-sensitivity&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Python strings are case sensitive. If the substring that you provide uses different capitalization than the same word in your text, then Python won’t find it. For example, if you check for the lowercase word &lt;code&gt;&quot;secret&quot;&lt;/code&gt; on a &lt;a href=&quot;https://realpython.com/python-strings/#case-conversion&quot;&gt;title-case version&lt;/a&gt; of the original text, the membership operator check returns &lt;code&gt;False&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.6e7b751c31b4.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;title_cased_file_content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&quot;Hi There And Welcome.&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;This Is A Special Hidden File With A Secret Secret.&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;I Don&#x27;t Want To Tell You The Secret,&lt;/span&gt;
&lt;span class=&quot;gp&quot;&gt;... &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;But I Do Want To Secretly Tell You That I Have One.&quot;&quot;&quot;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;secret&quot;&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;title_cased_file_content&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;False&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.6e7b751c31b4.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.6e7b751c31b4.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;Despite the fact that the word &lt;em&gt;secret&lt;/em&gt; appears multiple times in the title-case text &lt;code&gt;title_cased_file_content&lt;/code&gt;, it &lt;em&gt;never&lt;/em&gt; shows up in all lowercase. That’s why the check that you perform with the membership operator returns &lt;code&gt;False&lt;/code&gt;. Python can’t find the all-lowercase string &lt;code&gt;&quot;secret&quot;&lt;/code&gt; in the provided text.&lt;/p&gt;
&lt;p&gt;Humans have a different approach to language than computers do. This is why you’ll often want to disregard capitalization when you check whether a string contains a substring in Python.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-string-contains-substring/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-string-contains-substring/ »&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 Exceptions: An Introduction</title>
      <id>https://realpython.com/python-exceptions/</id>
      <link href="https://realpython.com/python-exceptions/"/>
      <updated>2024-12-01T14:00:00+00:00</updated>
      <summary>In this beginner tutorial, you&#x27;ll learn what exceptions are good for in Python. You&#x27;ll see how to raise exceptions and how to handle them with try ... except blocks.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Python exceptions provide a mechanism for handling errors that occur during the execution of a program. Unlike syntax errors, which are detected by the parser, Python raises exceptions when an error occurs in syntactically correct code. Knowing how to raise, catch, and handle exceptions effectively helps to ensure your program behaves as expected, even when encountering errors.&lt;/p&gt;
&lt;p&gt;In Python, you handle exceptions using a &lt;code&gt;try&lt;/code&gt; … &lt;code&gt;except&lt;/code&gt; block. This structure allows you to execute code normally while responding to any exceptions that may arise. You can also use &lt;code&gt;else&lt;/code&gt; to run code if no exceptions occur, and the &lt;code&gt;finally&lt;/code&gt; clause to execute code regardless of whether an exception was raised.&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;Exceptions in Python occur when &lt;strong&gt;syntactically correct code&lt;/strong&gt; results in an &lt;strong&gt;error&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You can &lt;strong&gt;handle exceptions&lt;/strong&gt; using the &lt;code&gt;try&lt;/code&gt;, &lt;code&gt;except&lt;/code&gt;, &lt;code&gt;else&lt;/code&gt;, and &lt;code&gt;finally&lt;/code&gt; &lt;strong&gt;keywords&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The &lt;code&gt;try&lt;/code&gt; … &lt;code&gt;except&lt;/code&gt; block&lt;/strong&gt; lets you execute code and handle exceptions that arise.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python 3&lt;/strong&gt; introduced more built-in exceptions compared to &lt;strong&gt;Python 2&lt;/strong&gt;, making error handling more granular.&lt;/li&gt;
&lt;li&gt;It’s &lt;strong&gt;bad practice&lt;/strong&gt; to &lt;strong&gt;catch all exceptions&lt;/strong&gt; at once using &lt;code&gt;except Exception&lt;/code&gt; or the bare &lt;code&gt;except&lt;/code&gt; clause.&lt;/li&gt;
&lt;li&gt;Combining &lt;code&gt;try&lt;/code&gt;, &lt;code&gt;except&lt;/code&gt;, and &lt;code&gt;pass&lt;/code&gt; allows your program to &lt;strong&gt;continue silently&lt;/strong&gt; without handling the exception.&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;try&lt;/code&gt; … &lt;code&gt;except&lt;/code&gt; is not inherently bad, but you should &lt;strong&gt;use it judiciously&lt;/strong&gt; to handle only &lt;strong&gt;known issues&lt;/strong&gt; appropriately.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this tutorial, you’ll get to know Python exceptions and all relevant keywords for exception handling by walking through a practical example of handling a platform-related exception. Finally, you’ll also learn how to create your own custom Python exceptions.&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-exceptions-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-exceptions-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that shows you how exceptions work 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.6e7b751c31b4.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 Exceptions: An Introduction” 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-exceptions/&quot; tabindex=&quot;-1&quot;&gt;
        &lt;div class=&quot;embed-responsive embed-responsive-16by9&quot;&gt;

            &lt;img class=&quot;card-img-top m-0 p-0 embed-responsive-item rounded&quot; style=&quot;object-fit: contain; background: #ffc873;&quot; alt=&quot;An Introduction to Python Exceptions&quot; src=&quot;https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.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_Exceptions_Watermark.47f814fbeced.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Python_Exceptions_Watermark.47f814fbeced.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.6e7b751c31b4.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-exceptions/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Python Exceptions: An Introduction&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 exceptions. You&#x27;ll cover the difference between syntax errors and exceptions and learn how to raise exceptions, make assertions, and use the try and except block.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;understanding-exceptions-and-syntax-errors&quot;&gt;Understanding Exceptions and Syntax Errors&lt;a class=&quot;headerlink&quot; href=&quot;#understanding-exceptions-and-syntax-errors&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://realpython.com/invalid-syntax-python/&quot;&gt;Syntax errors&lt;/a&gt; occur when the parser detects an incorrect statement. Observe the following example:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pytb&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 Traceback&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;x&quot;&gt;&amp;gt;&amp;gt;&amp;gt; print(0 / 0))&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&quot;&amp;lt;stdin&amp;gt;&quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;w&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;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;                &lt;/span&gt;&lt;span class=&quot;pm&quot;&gt;^&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;SyntaxError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;unmatched &#x27;)&#x27;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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.6e7b751c31b4.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 arrow indicates where the parser ran into the &lt;strong&gt;syntax error&lt;/strong&gt;. Additionally, the error message gives you a hint about what went wrong. In this example, there was one bracket too many. Remove it and run your code again:&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.6e7b751c31b4.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;print&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;o&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&quot;&amp;lt;stdin&amp;gt;&quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;ZeroDivisionError&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;division by zero&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.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This time, you ran into an &lt;strong&gt;exception error&lt;/strong&gt;. This type of error occurs whenever syntactically correct Python code results in an error. The last line of the message indicates what type of exception error you ran into.&lt;/p&gt;
&lt;p&gt;Instead of just writing &lt;em&gt;exception error&lt;/em&gt;, Python details what &lt;em&gt;type&lt;/em&gt; of exception error it encountered. In this case, it was a &lt;code&gt;ZeroDivisionError&lt;/code&gt;. Python comes with &lt;a href=&quot;https://docs.python.org/3/library/exceptions.html&quot;&gt;various built-in exceptions&lt;/a&gt; as well as the possibility to create user-defined exceptions.&lt;/p&gt;
&lt;h2 id=&quot;raising-an-exception-in-python&quot;&gt;Raising an Exception in Python&lt;a class=&quot;headerlink&quot; href=&quot;#raising-an-exception-in-python&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;There are scenarios where you might want to stop your program by raising an exception if a condition occurs. You can do this with the &lt;a href=&quot;https://realpython.com/python-raise-exception/&quot;&gt;&lt;code&gt;raise&lt;/code&gt;&lt;/a&gt; keyword:&lt;/p&gt;
&lt;figure class=&quot;js-lightbox&quot;&gt;&lt;a href=&quot;https://files.realpython.com/media/raise.3931e8819e08.png&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/raise.3931e8819e08.png&quot; width=&quot;1394&quot; height=&quot;311&quot; srcset=&quot;/cdn-cgi/image/width=348,format=auto/https://files.realpython.com/media/raise.3931e8819e08.png 348w, /cdn-cgi/image/width=464,format=auto/https://files.realpython.com/media/raise.3931e8819e08.png 464w, /cdn-cgi/image/width=697,format=auto/https://files.realpython.com/media/raise.3931e8819e08.png 697w, /cdn-cgi/image/width=1394,format=auto/https://files.realpython.com/media/raise.3931e8819e08.png 1394w&quot; sizes=&quot;(min-width: 1200px) 690px, (min-width: 780px) calc(-5vw + 669px), (min-width: 580px) 510px, calc(100vw - 30px)&quot; alt=&quot;Illustration of  raise statement usage&quot; data-asset=&quot;394&quot;&gt;&lt;/a&gt;&lt;/figure&gt;

&lt;p&gt;You can even complement the statement with a custom message. Assume that you’re writing a tiny toy program that expects only numbers up to &lt;code&gt;5&lt;/code&gt;. You can raise an error when an unwanted condition occurs:&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;low.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;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;The number should not exceed 5. (&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;=}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;)&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/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.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this example, you raised an &lt;code&gt;Exception&lt;/code&gt; object and passed it an informative custom message. You built the message using an &lt;a href=&quot;https://realpython.com/python-f-strings/&quot;&gt;f-string&lt;/a&gt; and a &lt;a href=&quot;https://realpython.com/python-f-strings/#self-documenting-expressions-for-debugging&quot;&gt;self-documenting expression&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;When you run &lt;code&gt;low.py&lt;/code&gt;, you’ll get the following output:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pytb&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 Traceback&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;gt&quot;&gt;Traceback (most recent call last):&lt;/span&gt;
  File &lt;span class=&quot;nb&quot;&gt;&quot;./low.py&quot;&lt;/span&gt;, line &lt;span class=&quot;m&quot;&gt;3&lt;/span&gt;, in &lt;span class=&quot;n&quot;&gt;&amp;lt;module&amp;gt;&lt;/span&gt;
&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;ne&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sa&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;The number should not exceed 5. (&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;=}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;)&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;gr&quot;&gt;Exception&lt;/span&gt;: &lt;span class=&quot;n&quot;&gt;The number should not exceed 5. (number=10)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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.6e7b751c31b4.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 program comes to a halt and displays the exception to your &lt;a href=&quot;https://realpython.com/terminal-commands/&quot;&gt;terminal&lt;/a&gt; or &lt;a href=&quot;https://realpython.com/python-repl/&quot;&gt;REPL&lt;/a&gt;, offering you helpful clues about what went wrong. Note that the final call to &lt;a href=&quot;https://realpython.com/python-print/&quot;&gt;&lt;code&gt;print()&lt;/code&gt;&lt;/a&gt; never executed, because Python raised the exception before it got to that line of code.&lt;/p&gt;
&lt;p&gt;With the &lt;code&gt;raise&lt;/code&gt; keyword, you can raise any exception object in Python and stop your program when an unwanted condition occurs.&lt;/p&gt;
&lt;h2 id=&quot;debugging-during-development-with-assert&quot;&gt;Debugging During Development With &lt;code&gt;assert&lt;/code&gt;&lt;a class=&quot;headerlink&quot; href=&quot;#debugging-during-development-with-assert&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-exceptions/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-exceptions/ »&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>Logging in Python</title>
      <id>https://realpython.com/python-logging/</id>
      <link href="https://realpython.com/python-logging/"/>
      <updated>2024-11-30T14:00:00+00:00</updated>
      <summary>If you use Python&#x27;s print() function to get information about the flow of your programs, then logging is the natural next step for you. This tutorial will guide you through creating your first logs and show you ways to curate them to grow with your projects.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Logging in Python lets you record important information about your program’s execution. You use the built-in &lt;code&gt;logging&lt;/code&gt; module to capture logs, which provide insights into application flow, errors, and usage patterns. With Python logging, you can create and configure loggers, set log levels, and format log messages without installing additional packages. You can also generate log files to store records for later analysis.&lt;/p&gt;
&lt;p&gt;Using a logging library instead of &lt;code&gt;print()&lt;/code&gt; calls gives you better control over log management, formatting, and output destinations. The &lt;code&gt;logging&lt;/code&gt; module also allows you to set severity levels, simplifying the management and filtering of log data.&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;Logging in a computer&lt;/strong&gt; involves recording program execution information for analysis.&lt;/li&gt;
&lt;li&gt;You can use logging for &lt;strong&gt;debugging&lt;/strong&gt;, &lt;strong&gt;performance analysis&lt;/strong&gt;, and &lt;strong&gt;monitoring usage patterns&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No installation is needed&lt;/strong&gt; for Python logging as the &lt;strong&gt;&lt;code&gt;logging&lt;/code&gt; module&lt;/strong&gt; is part of Python’s standard library.&lt;/li&gt;
&lt;li&gt;Logging in Python works by &lt;strong&gt;configuring loggers&lt;/strong&gt; and &lt;strong&gt;setting log levels&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Using a &lt;strong&gt;logging library&lt;/strong&gt; provides &lt;strong&gt;structured logging&lt;/strong&gt; and control over log output.&lt;/li&gt;
&lt;li&gt;You should &lt;strong&gt;prefer logging over &lt;code&gt;print()&lt;/code&gt;&lt;/strong&gt; because it &lt;strong&gt;decreases the maintainance burden&lt;/strong&gt; and allows you to manage log levels.&lt;/li&gt;
&lt;li&gt;You can &lt;strong&gt;generate a log file&lt;/strong&gt; in Python by setting &lt;code&gt;filename&lt;/code&gt; and &lt;code&gt;encoding&lt;/code&gt; through &lt;code&gt;basicConfig()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You’ll do the coding for this tutorial in the &lt;a href=&quot;https://realpython.com/python-repl/&quot;&gt;Python standard REPL&lt;/a&gt;. If you prefer Python files, then you’ll find a full logging example as a script in the materials of this tutorial. You can download this script by clicking the link below:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/python-logging-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-logging-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that you’ll use to learn about logging 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.6e7b751c31b4.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 “Logging 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/logging-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: #e6c7ab;&quot; alt=&quot;Logging in Python&quot; src=&quot;https://files.realpython.com/media/Logging-in-Python_Watermarked.4ca8974dc55c.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/Logging-in-Python_Watermarked.4ca8974dc55c.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Logging-in-Python_Watermarked.4ca8974dc55c.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Logging-in-Python_Watermarked.4ca8974dc55c.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Logging-in-Python_Watermarked.4ca8974dc55c.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.6e7b751c31b4.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/logging-in-python/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Logging 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&#x27;s logging module. With this knowledge, you&#x27;ll be able to add logging to your applications, which can help you debug errors and analyze performance.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;If you commonly use Python’s &lt;a href=&quot;https://realpython.com/python-print/&quot;&gt;&lt;code&gt;print()&lt;/code&gt;&lt;/a&gt; function to get information about the flow of your programs, then logging is the natural next step for you. This tutorial will guide you through creating your first logs and show you how to make logging grow with your projects.&lt;/p&gt;
&lt;h2 id=&quot;starting-with-pythons-logging-module&quot;&gt;Starting With Python’s Logging Module&lt;a class=&quot;headerlink&quot; href=&quot;#starting-with-pythons-logging-module&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The &lt;a href=&quot;https://docs.python.org/3/library/logging.html#module-logging/&quot;&gt;&lt;code&gt;logging&lt;/code&gt; module&lt;/a&gt; in Python’s standard library is a ready-to-use, powerful module that’s designed to meet the needs of beginners as well as enterprise teams.&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; Since logs offer a variety of insights, the &lt;code&gt;logging&lt;/code&gt; module is often used by other third-party Python libraries, too. Once you’re more advanced in the practice of logging, you can integrate your log messages with the ones from those libraries to produce a homogeneous log for your application.&lt;/p&gt;
&lt;p&gt;To leverage this versatility, it’s a good idea to get a better understanding of how the &lt;code&gt;logging&lt;/code&gt; module works under the hood. For example, you could take &lt;a href=&quot;https://realpython.com/python-logging-source-code/&quot;&gt;a stroll through the &lt;code&gt;logging&lt;/code&gt; module’s source code&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The main component of the &lt;code&gt;logging&lt;/code&gt; module is something called the &lt;strong&gt;logger&lt;/strong&gt;. You can think of the logger as a reporter in your code that decides what to record, at what level of detail, and where to store or send these records.&lt;/p&gt;
&lt;h3 id=&quot;exploring-the-root-logger&quot;&gt;Exploring the Root Logger&lt;a class=&quot;headerlink&quot; href=&quot;#exploring-the-root-logger&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;To get a first impression of how the &lt;code&gt;logging&lt;/code&gt; module and a logger work, open the &lt;a href=&quot;https://realpython.com/python-repl/&quot;&gt;Python standard REPL&lt;/a&gt; and enter the code below:&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.6e7b751c31b4.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;logging&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;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warning&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Remain calm!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;WARNING:root:Remain calm!&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.6e7b751c31b4.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.6e7b751c31b4.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 output shows the severity level before each message along with &lt;code&gt;root&lt;/code&gt;, which is the name the &lt;code&gt;logging&lt;/code&gt; module gives to its default logger. This output shows the default format that can be configured to include things like a timestamp or other details.&lt;/p&gt;
&lt;p&gt;In the example above, you’re sending a message on the &lt;code&gt;root&lt;/code&gt; logger. The &lt;strong&gt;log level&lt;/strong&gt; of the message is &lt;code&gt;WARNING&lt;/code&gt;. Log levels are an important aspect of logging. By default, there are five standard levels indicating the severity of events. Each has a corresponding function that can be used to log events at that level of severity. &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; There’s also a &lt;code&gt;NOTSET&lt;/code&gt; log level, which you’ll encounter later in this tutorial when you learn about custom logging handlers.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Here are the five default log levels, in order of increasing severity:&lt;/p&gt;
&lt;div class=&quot;table-responsive&quot;&gt;
&lt;table class=&quot;table table-hover&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Log Level&lt;/th&gt;
&lt;th&gt;Function&lt;/th&gt;
&lt;th&gt;Description&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;DEBUG&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;logging.debug()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Provides detailed information that’s valuable to you as a developer.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;INFO&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;logging.info()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Provides general information about what’s going on with your program.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;WARNING&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;logging.warning()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Indicates that there’s something you should look into.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ERROR&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;logging.error()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Alerts you to an unexpected problem that’s occured in your program.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CRITICAL&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;logging.critical()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Tells you that a serious error has occurred and may have crashed your app.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;logging&lt;/code&gt; module provides you with a default logger that allows you to get started with logging without needing to do much configuration. However, the &lt;code&gt;logging&lt;/code&gt; functions listed in the table above reveal a quirk that you may not expect:&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.6e7b751c31b4.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;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;debug&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;This is a debug message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;This is an info message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warning&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;This is a warning message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;WARNING:root:This is a warning message&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;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;This is an error message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;ERROR:root:This is an error message&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;logging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;critical&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;This is a critical message&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;CRITICAL:root:This is a critical message&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.6e7b751c31b4.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.6e7b751c31b4.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;Notice that the &lt;code&gt;debug()&lt;/code&gt; and &lt;code&gt;info()&lt;/code&gt; messages didn’t get logged. This is because, by default, the logging module logs the messages with a severity level of &lt;code&gt;WARNING&lt;/code&gt; or above. You can change that by configuring the logging module to log events of all levels. &lt;/p&gt;
&lt;h3 id=&quot;adjusting-the-log-level&quot;&gt;Adjusting the Log Level&lt;a class=&quot;headerlink&quot; href=&quot;#adjusting-the-log-level&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;To set up your basic logging configuration and adjust the log level, the &lt;code&gt;logging&lt;/code&gt; module comes with a &lt;a href=&quot;https://docs.python.org/3/library/logging.html#logging.basicConfig&quot;&gt;&lt;code&gt;basicConfig()&lt;/code&gt;&lt;/a&gt; function. As a Python developer, this &lt;a href=&quot;https://en.wikipedia.org/wiki/Camel_case&quot;&gt;camel-cased&lt;/a&gt; function name may look unusual to you as it doesn’t follow the &lt;a href=&quot;https://realpython.com/python-pep8/#naming-conventions&quot;&gt;PEP 8 naming conventions&lt;/a&gt;:&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-logging/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-logging/ »&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>What Does if __name__ == &quot;__main__&quot; Do in Python?</title>
      <id>https://realpython.com/if-name-main-python/</id>
      <link href="https://realpython.com/if-name-main-python/"/>
      <updated>2024-11-30T14:00:00+00:00</updated>
      <summary>Learn how to use Python&#x27;s if __name__ == &quot;__main__&quot; idiom to control code execution. Discover its purpose, mechanics, best practices, and when to use or avoid it. This tutorial explores its role in managing script behavior and module imports for clean and effective Python programming.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;The &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt; idiom is a Python construct that helps control code execution in scripts. It’s a conditional statement that allows you to define code that runs only when the file is executed as a script, not when it’s imported as a module.&lt;/p&gt;
&lt;p&gt;When you run a Python script, the interpreter assigns the value &lt;code&gt;&quot;__main__&quot;&lt;/code&gt; to the &lt;code&gt;__name__&lt;/code&gt; variable. If Python imports the code as a module, then it sets &lt;code&gt;__name__&lt;/code&gt; to the module’s name instead. By encapsulating code within &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt;, you can ensure that it only runs in the intended context.&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’s &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt; idiom allows code to run &lt;strong&gt;only when the script is executed&lt;/strong&gt;, not when it’s imported.&lt;/li&gt;
&lt;li&gt;The idiom checks if &lt;strong&gt;the &lt;code&gt;__name__&lt;/code&gt; variable equals &lt;code&gt;&quot;__main__&quot;&lt;/code&gt;&lt;/strong&gt;, confirming that the script is the &lt;strong&gt;top-level module&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Using this idiom helps &lt;strong&gt;prevent unintended code execution&lt;/strong&gt; during module imports.&lt;/li&gt;
&lt;li&gt;It’s useful for adding &lt;strong&gt;script-specific logic&lt;/strong&gt;, such as user input or test cases, without affecting module imports.&lt;/li&gt;
&lt;li&gt;Best practices suggest &lt;strong&gt;using this idiom minimally&lt;/strong&gt; and placing it at the bottom of the script for clarity.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You’ve likely encountered Python’s &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt; idiom when reading other people’s code. No wonder—&lt;a href=&quot;https://github.com/search?q=__name__+%3D%3D+%22__main__%22&amp;amp;type=code&quot;&gt;it’s widespread&lt;/a&gt;! Understanding Python’s &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt; idiom will help you to manage script execution and module imports effectively. In this tutorial you’ll explore its mechanics, appropriate usage, and best practices.&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.6e7b751c31b4.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 Name-Main Idiom” 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-name-main-idiom/&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: #ffc774;&quot; alt=&#x27;What Does if __name__ == &quot;__main__&quot; Do in Python?&#x27; src=&quot;https://files.realpython.com/media/The-Name-Main-Idiom_Watermarked.9d5e12c539b3.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/The-Name-Main-Idiom_Watermarked.9d5e12c539b3.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/The-Name-Main-Idiom_Watermarked.9d5e12c539b3.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/The-Name-Main-Idiom_Watermarked.9d5e12c539b3.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/The-Name-Main-Idiom_Watermarked.9d5e12c539b3.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.6e7b751c31b4.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-name-main-idiom/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Python Name-Main Idiom&lt;/span&gt;&lt;/a&gt; 
      &lt;p class=&quot;text-muted mb-0 small&quot;&gt;Test your knowledge of Python&#x27;s if __name__ == &quot;__main__&quot; idiom by answering a series of questions! You&#x27;ve probably encountered the name-main idiom and might have even used it in your own scripts. But did you use it correctly?&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/if-name-main-python-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-if-name-main-python-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that you’ll use to learn about the name-main idiom.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;in-short-it-allows-you-to-execute-code-when-the-file-runs-as-a-script-but-not-when-its-imported-as-a-module&quot;&gt;In Short: It Allows You to Execute Code When the File Runs as a Script, but Not When It’s Imported as a Module&lt;a class=&quot;headerlink&quot; href=&quot;#in-short-it-allows-you-to-execute-code-when-the-file-runs-as-a-script-but-not-when-its-imported-as-a-module&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For most practical purposes, you can think of the conditional block that you open with &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt; as a way to store code that should only run when your file is executed as a script.&lt;/p&gt;
&lt;p&gt;You’ll see what that means in a moment. For now, say you have the following file:&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;echo.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;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;echo&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;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;repetitions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;    &lt;/span&gt;&lt;span class=&quot;sd&quot;&gt;&quot;&quot;&quot;Imitate a real-world echo.&quot;&quot;&quot;&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 3&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;echoes&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;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lower&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&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;n&quot;&gt;repetitions&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;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 4&lt;/span&gt;    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;echoes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 5&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 6&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; 7&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;nb&quot;&gt;input&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Yell something at a mountain: &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;linenos&quot;&gt; 8&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;echo&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;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.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this example, you define a function, &lt;code&gt;echo()&lt;/code&gt;, that mimics a real-world echo by gradually printing fewer and fewer of the final letters of the input text.&lt;/p&gt;
&lt;p&gt;Below that, in lines 6 to 8, you use the &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt; idiom. This code starts with the conditional statement &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt; in line 6. In the indented lines, 7 and 8, you then collect user input and call &lt;code&gt;echo()&lt;/code&gt; with that input. These two lines will execute when you run &lt;code&gt;echo.py&lt;/code&gt; as a script from your command line:&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.6e7b751c31b4.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;echo.py
&lt;span class=&quot;go&quot;&gt;Yell something at a mountain: HELLOOOO ECHOOOOOOOOOO&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;ooo&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;oo&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;o&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.6e7b751c31b4.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.6e7b751c31b4.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 run the file as a script by passing the file object to your Python interpreter, the expression &lt;code&gt;__name__ == &quot;__main__&quot;&lt;/code&gt; returns &lt;code&gt;True&lt;/code&gt;. The code block under &lt;code&gt;if&lt;/code&gt; then runs, so Python collects user input and calls &lt;code&gt;echo()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Try it out yourself! You can download all the code files that you’ll use in this tutorial from the link below:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/if-name-main-python-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-if-name-main-python-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that you’ll use to learn about the name-main idiom.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;At the same time, if you import &lt;code&gt;echo()&lt;/code&gt; in another module or a console session, then the nested code won’t run:&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.6e7b751c31b4.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;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;echo&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;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;echo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Please help me I&#x27;m stuck on a mountain&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;ain&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;in&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;n&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.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this case, you want to use &lt;code&gt;echo()&lt;/code&gt; in the context of another script or interpreter session, so you won’t need to collect user input. Running &lt;code&gt;input()&lt;/code&gt; would mess with your code by producing a side effect when importing &lt;code&gt;echo&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;When you nest the code that’s specific to the script usage of your file under the &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt; idiom, then you avoid running code that’s irrelevant for imported modules.&lt;/p&gt;
&lt;p&gt;Nesting code under &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt; allows you to cater to different use cases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Script:&lt;/strong&gt; When run as a script, your code prompts the user for input, calls &lt;code&gt;echo()&lt;/code&gt;, and prints the result.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Module:&lt;/strong&gt; When you import &lt;code&gt;echo&lt;/code&gt; as a module, then &lt;code&gt;echo()&lt;/code&gt; gets defined, but no code executes. You provide &lt;code&gt;echo()&lt;/code&gt; to the main code session without any side effects.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By implementing the &lt;code&gt;if __name__ == &quot;__main__&quot;&lt;/code&gt; idiom in your code, you set up an additional entry point that allows you to use &lt;code&gt;echo()&lt;/code&gt; right from the command line.&lt;/p&gt;
&lt;p&gt;There you go! You’ve now covered the most important information about this topic. Still, there’s more to find out, and there are some subtleties that can help you build a deeper understanding of this code specifically and Python more generally.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/if-name-main-python/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/if-name-main-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&#x27;s F-String for String Interpolation and Formatting</title>
      <id>https://realpython.com/python-f-strings/</id>
      <link href="https://realpython.com/python-f-strings/"/>
      <updated>2024-11-30T14:00:00+00:00</updated>
      <summary>Python&#x27;s f-strings provide a readable way to interpolate and format strings. They&#x27;re readable, concise, and less prone to error than traditional string interpolation and formatting tools, such as the .format() method and the modulo operator (%). F-strings are also faster than those tools!</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Python &lt;strong&gt;f-strings&lt;/strong&gt; offer a concise and efficient way to interpolate variables, objects, and expressions directly into strings. By prefixing a string with &lt;code&gt;f&lt;/code&gt; or &lt;code&gt;F&lt;/code&gt;, you can embed expressions within curly braces (&lt;code&gt;{}&lt;/code&gt;), which are evaluated at runtime. &lt;/p&gt;
&lt;p&gt;This makes f-strings faster and more readable compared to older approaches like the &lt;a href=&quot;https://realpython.com/python-modulo-string-formatting/&quot;&gt;modulo (&lt;code&gt;%&lt;/code&gt;)&lt;/a&gt; operator or the string &lt;a href=&quot;https://realpython.com/python-formatted-output/#using-the-format-method-for-string-interpolation&quot;&gt;&lt;code&gt;.format()&lt;/code&gt;&lt;/a&gt; method. Additionally, f-strings support advanced string formatting using Python’s string &lt;a href=&quot;https://realpython.com/python-format-mini-language/&quot;&gt;format mini-language&lt;/a&gt;.&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;An f-string in Python is a string literal &lt;strong&gt;prefixed with &lt;code&gt;f&lt;/code&gt; or &lt;code&gt;F&lt;/code&gt;&lt;/strong&gt;, allowing for the &lt;strong&gt;embedding of expressions&lt;/strong&gt; within &lt;strong&gt;curly braces &lt;code&gt;{}&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;To include dynamic content in an f-string, place your expression or variable inside the braces to &lt;strong&gt;interpolate&lt;/strong&gt; its value into the string.&lt;/li&gt;
&lt;li&gt;An &lt;strong&gt;f-string error&lt;/strong&gt; in Python often occurs due to syntax issues, such as &lt;strong&gt;unmatched braces&lt;/strong&gt; or &lt;strong&gt;invalid expressions&lt;/strong&gt; within the string.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;F-string calculation&lt;/strong&gt; in Python involves &lt;strong&gt;writing expressions&lt;/strong&gt; within the curly braces of an f-string, which Python evaluates at runtime.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Python 3.12&lt;/strong&gt; improved f-strings by allowing &lt;strong&gt;nested expressions&lt;/strong&gt; and the use of &lt;strong&gt;backslashes&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This tutorial will guide you through the features and advantages of f-strings, including interpolation and formatting. By familiarizing yourself with these features, you’ll be able to effectively use f-strings in your Python projects.&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-f-string-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-f-string-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that shows you how to do string interpolation and formatting with Python’s f-strings.&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.6e7b751c31b4.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 F-Strings” quiz. You’ll receive a score upon completion to help you track your learning progress:&lt;/p&gt;

  &lt;hr&gt;

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

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

            &lt;img class=&quot;card-img-top m-0 p-0 embed-responsive-item rounded&quot; style=&quot;object-fit: contain; background: #e5c5ac;&quot; alt=&quot;F-Strings in Python 3.6&quot; src=&quot;https://files.realpython.com/media/Python-f-Strings-Guide_Watermark.2c8be8b1d392.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-f-Strings-Guide_Watermark.2c8be8b1d392.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Python-f-Strings-Guide_Watermark.2c8be8b1d392.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Python-f-Strings-Guide_Watermark.2c8be8b1d392.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Python-f-Strings-Guide_Watermark.2c8be8b1d392.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.6e7b751c31b4.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/f-strings/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Python F-Strings&lt;/span&gt;&lt;/a&gt; 
      &lt;p class=&quot;text-muted mb-0 small&quot;&gt;In this quiz, you&#x27;ll test your knowledge of Python f-strings. With this knowledge, you&#x27;ll be able to include all sorts of Python expressions inside your strings.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;interpolating-and-formatting-strings-before-python-36&quot;&gt;Interpolating and Formatting Strings Before Python 3.6&lt;a class=&quot;headerlink&quot; href=&quot;#interpolating-and-formatting-strings-before-python-36&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Before Python 3.6, you had two main tools for interpolating values, variables, and expressions inside string literals:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The &lt;a href=&quot;https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting&quot;&gt;string interpolation operator&lt;/a&gt; (&lt;code&gt;%&lt;/code&gt;), or modulo operator&lt;/li&gt;
&lt;li&gt;The &lt;a href=&quot;https://docs.python.org/3/library/stdtypes.html#str.format&quot;&gt;&lt;code&gt;str.format()&lt;/code&gt;&lt;/a&gt; method&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You’ll get a refresher on these two string interpolation tools in the following sections. You’ll also learn about the string formatting capabilities that these tools offer in Python.&lt;/p&gt;
&lt;h3 id=&quot;the-modulo-operator&quot;&gt;The Modulo Operator (&lt;code&gt;%&lt;/code&gt;)&lt;a class=&quot;headerlink&quot; href=&quot;#the-modulo-operator&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The modulo operator (&lt;code&gt;%&lt;/code&gt;) was the first tool for string interpolation and formatting in Python and has been in the language since the beginning. Here’s what using this operator looks like in practice:&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.6e7b751c31b4.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;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Jane&quot;&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;!&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;Hello, Jane!&#x27;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this quick example, you use the &lt;code&gt;%&lt;/code&gt; operator to interpolate the value of your &lt;code&gt;name&lt;/code&gt; variable into a string literal. The interpolation operator takes two operands:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A string literal containing one or more conversion specifiers&lt;/li&gt;
&lt;li&gt;The object or objects that you’re interpolating into the string literal&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;strong&gt;conversion specifiers&lt;/strong&gt; work as replacement fields. In the above example, you use the &lt;code&gt;%s&lt;/code&gt; combination of characters as a conversion specifier. The &lt;code&gt;%&lt;/code&gt; symbol marks the start of the specifier, while the &lt;code&gt;s&lt;/code&gt; letter is the &lt;strong&gt;conversion type&lt;/strong&gt; and tells the operator that you want to convert the input object into a string.&lt;/p&gt;
&lt;p&gt;If you want to insert more than one object into your target string, then you can use a &lt;a href=&quot;https://realpython.com/python-tuple/&quot;&gt;tuple&lt;/a&gt;. Note that the number of objects in the tuple must match the number of format specifiers in the string:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Jane&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;age&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;

&lt;span class=&quot;gp&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;! You&#x27;re &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; years old.&quot;&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;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;age&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;Hello, Jane! You&#x27;re 25 years old.&#x27;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
    
    &lt;button class=&quot;codeblock__copy btn btn-outline-secondary border m-1 px-1 d-hover-only&quot; title=&quot;Copy to clipboard&quot;&gt;&lt;span class=&quot;icon baseline&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In this example, you use a tuple of values as the right-hand operand to &lt;code&gt;%&lt;/code&gt;. Note that you’ve used a string and an integer. Because you use the &lt;code&gt;%s&lt;/code&gt; specifier, Python converts both objects to strings.&lt;/p&gt;
&lt;p&gt;You can also use dictionaries as the right-hand operand in your interpolation expressions. To do this, you need to create conversion specifiers that enclose key names in parentheses:&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.6e7b751c31b4.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;s2&quot;&gt;&quot;Hello, &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(name)s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;! You&#x27;re &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%(age)s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; years old.&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;name&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Jane&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;age&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&quot;Hello, Jane! You&#x27;re 25 years old.&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.6e7b751c31b4.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.6e7b751c31b4.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 syntax provides a readable approach to string interpolation with the &lt;code&gt;%&lt;/code&gt; operator. You can use descriptive key names instead of relying on the positional order of values.&lt;/p&gt;
&lt;p&gt;When you use the &lt;code&gt;%&lt;/code&gt; operator for string interpolation, you can use conversion specifiers. They provide some string formatting capabilities that take advantage of &lt;strong&gt;conversion types&lt;/strong&gt;, &lt;strong&gt;conversion flags&lt;/strong&gt;, and some characters like the period (&lt;code&gt;.&lt;/code&gt;) and the asterisk (&lt;code&gt;*&lt;/code&gt;). Consider the following example:&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.6e7b751c31b4.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;s2&quot;&gt;&quot;Balance: $&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%.2f&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;5425.9292&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;&#x27;Balance: $5425.93&#x27;&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;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Name: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Age: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;%5s&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;35&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Name: John&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;Age:    35&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.6e7b751c31b4.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.6e7b751c31b4.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-f-strings/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-f-strings/ »&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 Virtual Environments: A Primer</title>
      <id>https://realpython.com/python-virtual-environments-a-primer/</id>
      <link href="https://realpython.com/python-virtual-environments-a-primer/"/>
      <updated>2024-11-30T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll learn how to use a Python virtual environment to manage your Python projects. You&#x27;ll also gain a deep understanding of the structure of virtual environments created with the venv module, as well as the rationale behind using virtual environments.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Creating a Python virtual environment allows you to manage dependencies separately for different projects, preventing conflicts and maintaining cleaner setups. With &lt;a href=&quot;https://docs.python.org/3/library/venv.html&quot;&gt;Python’s &lt;code&gt;venv&lt;/code&gt; module&lt;/a&gt;, you can create isolated environments that use different versions of libraries or Python itself. This tutorial guides you through creating, activating, and managing these environments efficiently.&lt;/p&gt;
&lt;p&gt;You can create a &lt;a href=&quot;https://docs.python.org/3/library/venv.html#venv-def&quot;&gt;virtual environment&lt;/a&gt; using the command &lt;code&gt;python -m venv venv/&lt;/code&gt;, replacing &lt;code&gt;venv/&lt;/code&gt; with your preferred folder name. Activation varies by platform. Use &lt;code&gt;venv\Scripts\activate&lt;/code&gt; on Windows and &lt;code&gt;source venv/bin/activate&lt;/code&gt; on macOS and Linux. Once activated, you can install and manage packages within this isolated space.&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 virtual environments provide &lt;strong&gt;lightweight and isolated&lt;/strong&gt; Python development environments.&lt;/li&gt;
&lt;li&gt;You can use Python’s &lt;strong&gt;&lt;code&gt;venv&lt;/code&gt; module&lt;/strong&gt; to manage dependencies &lt;strong&gt;independently for each project&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You create and &lt;strong&gt;set up a venv in Python&lt;/strong&gt; using the command &lt;code&gt;python -m venv path/to/venv/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You &lt;strong&gt;refer&lt;/strong&gt; to a virtual environment by the &lt;strong&gt;folder name&lt;/strong&gt; that you used when creating the venv.&lt;/li&gt;
&lt;li&gt;You &lt;strong&gt;activate a venv on Windows&lt;/strong&gt; with &lt;code&gt;venv\Scripts\activate&lt;/code&gt;, and &lt;strong&gt;on macOS and Linux&lt;/strong&gt; with &lt;code&gt;source venv/bin/activate&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You can &lt;strong&gt;enable a venv in VS Code&lt;/strong&gt; by opening the Command Palette and choosing &lt;em&gt;Python: Select Interpreter&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Working with virtual environments is a common and effective practice in Python development. Gaining a better understanding of how they work, why you need them, and what you can do with them will help you master your Python programming workflow.&lt;/p&gt;
&lt;p&gt;Throughout the tutorial, you can select code examples for either Windows, Linux, or macOS. Pick your platform at the top right of the relevant code blocks to get the commands that you need, and feel free to switch between them if you want to learn how to work with virtual environments on other operating systems.&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 Cheat Sheet:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/python-virtual-environments-a-primer-pdf/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-virtual-environments-a-primer-pdf&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download a free cheat sheet&lt;/a&gt; that summarizes the main venv commands you’ll learn about in this tutorial.&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.6e7b751c31b4.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 Virtual Environments: A Primer” 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-virtual-environments-a-primer/&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 Virtual Environments (venv)&quot; src=&quot;https://files.realpython.com/media/Python-Virtual-Environments_Watermarked.4c787192d42f.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-Virtual-Environments_Watermarked.4c787192d42f.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Python-Virtual-Environments_Watermarked.4c787192d42f.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Python-Virtual-Environments_Watermarked.4c787192d42f.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Python-Virtual-Environments_Watermarked.4c787192d42f.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.6e7b751c31b4.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-virtual-environments-a-primer/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Python Virtual Environments: A Primer&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 virtual environments. With this knowledge, you&#x27;ll be able to avoid dependency conflicts and help other developers reproduce your development environment.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;how-can-you-work-with-a-python-virtual-environment&quot;&gt;How Can You Work With a Python Virtual Environment?&lt;a class=&quot;headerlink&quot; href=&quot;#how-can-you-work-with-a-python-virtual-environment&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you just need to get a virtual environment up and running to continue working on your favorite project, then this section is for you.&lt;/p&gt;
&lt;p&gt;This tutorial uses &lt;a href=&quot;https://docs.python.org/3/library/venv.html&quot;&gt;Python’s &lt;code&gt;venv&lt;/code&gt; module&lt;/a&gt; to create virtual environments. This module is part of Python’s standard library, and it’s been the &lt;a href=&quot;https://docs.python.org/3/library/venv.html#creating-virtual-environments&quot;&gt;officially recommended&lt;/a&gt; way to create virtual environments since Python 3.5.&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; There are other great third-party tools for creating virtual environments,
such as &lt;a href=&quot;#the-conda-package-and-environment-manager&quot;&gt;conda&lt;/a&gt; and &lt;a href=&quot;#the-virtualenv-project&quot;&gt;virtualenv&lt;/a&gt;,
that you’ll learn more about later in this tutorial.
Either of these tools can help you set up a virtual environment and also go beyond just that.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;For basic usage, &lt;code&gt;venv&lt;/code&gt; is an excellent choice because it already comes packaged with your Python installation. With that in mind, you’re ready to create your first virtual environment.&lt;/p&gt;
&lt;h3 id=&quot;create-it&quot;&gt;Create It&lt;a class=&quot;headerlink&quot; href=&quot;#create-it&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Any time you’re working on a Python project that uses external dependencies you’re &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;installing with &lt;code&gt;pip&lt;/code&gt;&lt;/a&gt;,
it’s best to first create a virtual environment:&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.6e7b751c31b4.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-linux&quot; role=&quot;presentation&quot;&gt;
    &lt;a class=&quot;nav-link link-unstyled text-body small &quot; id=&quot;linux-tab-1&quot; data-toggle=&quot;tab&quot; href=&quot;#linux-1&quot; role=&quot;tab&quot; aria-controls=&quot;linux-1&quot; aria-selected=&quot;false&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.6e7b751c31b4.svg#v4--linux&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Linux&lt;/a&gt;
  &lt;/li&gt;


  &lt;li class=&quot;nav-item mb-0 js-platform-widget-tab-macos&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;#macos-1&quot; role=&quot;tab&quot; aria-controls=&quot;macos-1&quot; aria-selected=&quot;false&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.6e7b751c31b4.svg#v4--apple&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;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.6e7b751c31b4.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;py&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;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.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This command allows the &lt;a href=&quot;https://docs.python.org/3/using/windows.html#launcher&quot;&gt;Python launcher for Windows&lt;/a&gt; to select an appropriate version of Python to execute. It comes bundled with the official installation and is the most convenient way to execute &lt;a href=&quot;https://realpython.com/python-coding-setup-windows/&quot;&gt;Python on Windows&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;You can bypass the launcher and run the Python executable directly using the &lt;code&gt;python&lt;/code&gt; command, but if you haven’t configured the &lt;code&gt;PATH&lt;/code&gt; and &lt;code&gt;PATHEXT&lt;/code&gt; variables,
then you might need to provide the full path:&lt;/p&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.6e7b751c31b4.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;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Users&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AppData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Local&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Programs&lt;/span&gt;&lt;span class=&quot;p&quot;&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;Python312&lt;/span&gt;&lt;span class=&quot;p&quot;&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;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.6e7b751c31b4.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.6e7b751c31b4.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 system path shown above assumes that you installed Python 3.12 using the Windows installer provided by the &lt;a href=&quot;https://www.python.org/downloads/&quot;&gt;Python downloads page&lt;/a&gt;. The path to the Python executable on your system might be different. Working with PowerShell, you can find the path using the &lt;code&gt;where.exe python&lt;/code&gt; command.&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 don’t need to include the backslash (&lt;code&gt;\&lt;/code&gt;) at the end of the name of your virtual environment, but it’s a helpful reminder that you’re creating a folder.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div aria-labelledby=&quot;linux-tab-1&quot; class=&quot;tab-pane fade &quot; id=&quot;linux-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.6e7b751c31b4.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;python3&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;/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.6e7b751c31b4.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.6e7b751c31b4.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;Many Linux operating systems ship with a version of Python 3.
If &lt;code&gt;python3&lt;/code&gt; doesn’t work, then you’ll have to first &lt;a href=&quot;https://realpython.com/installing-python/&quot;&gt;install Python&lt;/a&gt; and you may need to use the specific name of the executable version that you installed, for example, &lt;code&gt;python3.12&lt;/code&gt; for Python 3.12.x. If that’s the case for you, remember to replace mentions of &lt;code&gt;python3&lt;/code&gt; in the code blocks with your specific version number.&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 don’t need to include the slash (&lt;code&gt;/&lt;/code&gt;) at the end of the name of your virtual environment, but it’s a helpful reminder that you’re creating a folder.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div aria-labelledby=&quot;macos-tab-1&quot; class=&quot;tab-pane fade &quot; id=&quot;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.6e7b751c31b4.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;python3&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;/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.6e7b751c31b4.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.6e7b751c31b4.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;Older versions of macOS come with a system installation of Python 2.7.x that you should &lt;em&gt;never&lt;/em&gt; use to run your scripts. If you’re working on macOS &amp;lt; 12.3 and invoke the Python interpreter with &lt;code&gt;python&lt;/code&gt; instead of &lt;code&gt;python3&lt;/code&gt;, then
you might accidentally start up the outdated system Python interpreter.&lt;/p&gt;
&lt;p&gt;If running &lt;code&gt;python3&lt;/code&gt; doesn’t work, then you’ll have to first &lt;a href=&quot;https://realpython.com/installing-python/&quot;&gt;install a modern version of Python&lt;/a&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 don’t need to include the slash (&lt;code&gt;/&lt;/code&gt;) at the end of the name of your virtual environment, but it’s a helpful reminder that you’re creating a folder.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This command creates a new virtual environment named &lt;em&gt;venv&lt;/em&gt; using Python’s built-in &lt;code&gt;venv&lt;/code&gt; module. The first &lt;code&gt;venv&lt;/code&gt; that you use in the command specifies the module, and the second &lt;code&gt;venv/&lt;/code&gt; sets the name for your virtual environment. You could name it differently, but calling it &lt;em&gt;venv&lt;/em&gt; is a good practice for consistency.&lt;/p&gt;
&lt;h3 id=&quot;activate-it&quot;&gt;Activate It&lt;a class=&quot;headerlink&quot; href=&quot;#activate-it&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Great! Your project now has its own virtual environment. Generally, before you start to use it, you’ll &lt;strong&gt;activate&lt;/strong&gt; the environment by executing a script that comes with the installation:&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-2&quot; data-toggle=&quot;tab&quot; href=&quot;#windows-2&quot; role=&quot;tab&quot; aria-controls=&quot;windows-2&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.6e7b751c31b4.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-2&quot; data-toggle=&quot;tab&quot; href=&quot;#linux-macos-2&quot; role=&quot;tab&quot; aria-controls=&quot;linux-macos-2&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.6e7b751c31b4.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.6e7b751c31b4.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-2&quot; class=&quot;tab-pane fade show active&quot; id=&quot;windows-2&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.6e7b751c31b4.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;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;/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.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;If your attempt to run this command produces an error, then you’ll first have to &lt;a href=&quot;https://realpython.com/python-coding-setup-windows/#loosening-your-execution-policy&quot;&gt;loosen the execution policy&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div aria-labelledby=&quot;linux-macos-tab-2&quot; class=&quot;tab-pane fade &quot; id=&quot;linux-macos-2&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.6e7b751c31b4.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;&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;
&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.6e7b751c31b4.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.6e7b751c31b4.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;Before you run this command, make sure that you’re in the folder containing the virtual environment you just created. If you’ve named your virtual environment something other than &lt;em&gt;venv&lt;/em&gt;, then you’ll have to use that name in the path instead of &lt;em&gt;venv&lt;/em&gt; when you source the activation script.&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 work with your virtual environment without activating it. To do this, you &lt;a href=&quot;#it-runs-from-anywhere-with-absolute-paths&quot;&gt;provide the full path&lt;/a&gt; to its Python interpreter when executing a command. However, you’ll likely want to activate the virtual environment after you create it to save yourself the effort of having to repeatedly type long pathnames.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Once you can see the name of your virtual environment in your command prompt—in this case &lt;code&gt;(venv)&lt;/code&gt;—then you’ll know that your virtual environment is active. Now you’re all set and ready to install your external packages!&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-virtual-environments-a-primer/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-virtual-environments-a-primer/ »&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 #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>Continuous Integration and Deployment for Python With GitHub Actions</title>
      <id>https://realpython.com/github-actions-python/</id>
      <link href="https://realpython.com/github-actions-python/"/>
      <updated>2024-11-27T14:00:00+00:00</updated>
      <summary>With most software following agile methodologies, it&#x27;s essential to have robust DevOps systems in place to manage, maintain, and automate common tasks with a continually changing codebase. By using GitHub Actions, you can automate your workflows efficiently, especially for Python projects.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Creating software is an achievement worth celebrating. But software is never static. Bugs need to be fixed, features need to be added, and security demands regular updates. In today’s landscape, with agile methodologies dominating, robust &lt;a href=&quot;https://realpython.com/learning-paths/python-devops/&quot;&gt;DevOps&lt;/a&gt; systems are crucial for managing an evolving codebase. That’s where GitHub Actions shine, empowering Python developers to automate workflows and ensure their projects adapt seamlessly to change.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;GitHub Actions&lt;/strong&gt; for Python empowers developers to automate workflows efficiently. This enables teams to maintain software quality while adapting to constant change.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Continuous Integration and Continuous Deployment (CI/CD)&lt;/strong&gt; systems help produce well-tested, high-quality software and streamline deployment. GitHub Actions makes CI/CD accessible to all, allowing automation and customization of workflows directly in your repository. This free service enables developers to execute their software development processes efficiently, improving productivity and code reliability.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this tutorial, you’ll learn how to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;GitHub Actions&lt;/strong&gt; and &lt;strong&gt;workflows&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Automate &lt;strong&gt;linting, testing, and deployment&lt;/strong&gt; of a Python project&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Secure credentials&lt;/strong&gt; used for automation&lt;/li&gt;
&lt;li&gt;Automate &lt;strong&gt;security and dependency updates&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This tutorial will use an existing codebase, &lt;a href=&quot;https://pypi.org/project/realpython-reader/&quot;&gt;Real Python Reader&lt;/a&gt;, as a starting point for which you’ll create a CI/CD pipeline. You can &lt;a href=&quot;https://github.com/realpython/reader/fork&quot;&gt;fork the Real Python Reader code on GitHub&lt;/a&gt; to follow along. Be sure to deselect the &lt;em&gt;Copy the &lt;code&gt;master&lt;/code&gt; branch only&lt;/em&gt; option when forking. Alternatively, if you prefer, you can &lt;a href=&quot;https://realpython.com/pypi-publish-python-package/&quot;&gt;build your own Real Python Reader&lt;/a&gt; using a previous tutorial.&lt;/p&gt;
&lt;p&gt;In order to get the most out of this tutorial, you should be comfortable with &lt;a href=&quot;https://realpython.com/what-is-pip/&quot;&gt;&lt;code&gt;pip&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/pypi-publish-python-package/&quot;&gt;building Python packages&lt;/a&gt;, &lt;a href=&quot;https://realpython.com/python-git-github-intro/&quot;&gt;Git&lt;/a&gt;, and have some familiarity with &lt;a href=&quot;https://realpython.com/python-yaml/&quot;&gt;YAML&lt;/a&gt; syntax.&lt;/p&gt;
&lt;p&gt;Before you dig into GitHub Actions, it may be helpful to take a step back and learn about the benefits of CI/CD. This will help you understand the kinds of problems that GitHub Actions can solve.&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/github-actions-python-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-github-actions-python-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 CI/CD for Python With GitHub Actions.&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.6e7b751c31b4.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 “GitHub Actions for 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/github-actions-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: #abe0e6;&quot; alt=&quot;Continuous Integration and Deployment for Python With GitHub Actions&quot; src=&quot;https://files.realpython.com/media/Using-GitHub-Actions-to-Release-a-Python-Package_Watermarked.76b89b43805f.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/Using-GitHub-Actions-to-Release-a-Python-Package_Watermarked.76b89b43805f.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/Using-GitHub-Actions-to-Release-a-Python-Package_Watermarked.76b89b43805f.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/Using-GitHub-Actions-to-Release-a-Python-Package_Watermarked.76b89b43805f.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/Using-GitHub-Actions-to-Release-a-Python-Package_Watermarked.76b89b43805f.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.6e7b751c31b4.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/github-actions-python/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;GitHub Actions for 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 GitHub Actions for Python. By working through this quiz, you&#x27;ll revisit how to use GitHub Actions and workflows to automate linting, testing, and deployment of a Python project.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;unlocking-the-benefits-of-cicd&quot;&gt;Unlocking the Benefits of CI/CD&lt;a class=&quot;headerlink&quot; href=&quot;#unlocking-the-benefits-of-cicd&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Continuous Integration (CI)&lt;/strong&gt; and &lt;strong&gt;Continuous Deployment (CD)&lt;/strong&gt;, commonly known as &lt;strong&gt;CI/CD&lt;/strong&gt;, are essential practices in modern software development. These practices automate the integration of code changes, the execution of tests, and the deployment of applications. This helps teams and open-source contributors to deliver code changes more frequently in a reliable and structured way.&lt;/p&gt;
&lt;p&gt;Moreover, when publishing open-source Python packages, CI/CD will ensure that all pull requests (PRs) and contributions to your package will meet the needs of the project while standardizing the code quality.&lt;/p&gt;
&lt;div class=&quot;alert alert-primary&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To learn more about what a pull request is and how to create one, you can read &lt;a href=&quot;https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests&quot;&gt;GitHub’s official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;More frequent deployments with smaller code changes &lt;strong&gt;reduce the risk of unintended breaking changes&lt;/strong&gt; that can occur with larger, more complex releases. For example, even though developers can format all code using the same linting tools and rules, policy can automatically block PRs from being merged if the code’s tests don’t pass.&lt;/p&gt;
&lt;p&gt;In the next section, you’ll learn how GitHub Workflows can help you implement CI/CD on a repository hosted on GitHub.&lt;/p&gt;
&lt;h2 id=&quot;exploring-github-workflows&quot;&gt;Exploring GitHub Workflows&lt;a class=&quot;headerlink&quot; href=&quot;#exploring-github-workflows&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;GitHub Workflows are a powerful feature of &lt;a href=&quot;https://docs.github.com/en/actions&quot;&gt;GitHub Actions&lt;/a&gt;. They allow you to define custom automation workflows for your repositories. Whether you want to build, test, or deploy your code, GitHub Workflows provide a flexible and customizable solution that any project on GitHub can use for free, whether the repository is public or private.&lt;/p&gt;
&lt;p&gt;Even though there are many CI/CD providers, GitHub Actions has become the default among open-source projects on GitHub because of its expansive ecosystem, flexibility, and low or no cost.&lt;/p&gt;
&lt;h3 id=&quot;anatomy-of-a-workflow-file&quot;&gt;Anatomy of a Workflow File&lt;a class=&quot;headerlink&quot; href=&quot;#anatomy-of-a-workflow-file&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Workflow files are declaratively written YAML files with a predefined structure that must be adhered to for a workflow to run successfully. Your YAML workflow files are stored and defined in a &lt;code&gt;.github/workflows/&lt;/code&gt; folder in your project’s root directory. &lt;/p&gt;
&lt;p&gt;Your workflow folder can have multiple workflow files, each of which will perform a certain task. You can name these workflow files anything you’d like. However, for the sake of simplicity and readability, it’s common practice to name them after the tasks they achieve, such as &lt;code&gt;test.yml&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Each file has a few elements that are required, but many, many more that are optional. The GitHub Actions &lt;a href=&quot;https://docs.github.com/en/actions&quot;&gt;documentation&lt;/a&gt; is thorough and well-written, so be sure to check it out after you’ve finished reading this tutorial.&lt;/p&gt;
&lt;p&gt;There are three main parts that make up the bulk of a workflow file: &lt;strong&gt;triggers&lt;/strong&gt;, &lt;strong&gt;jobs&lt;/strong&gt;, and &lt;strong&gt;steps&lt;/strong&gt;. You’ll cover these in the next sections.&lt;/p&gt;
&lt;h3 id=&quot;workflow-triggers&quot;&gt;Workflow Triggers&lt;a class=&quot;headerlink&quot; href=&quot;#workflow-triggers&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/github-actions-python/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/github-actions-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>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>Speed Up Your Python Program With Concurrency</title>
      <id>https://realpython.com/python-concurrency/</id>
      <link href="https://realpython.com/python-concurrency/"/>
      <updated>2024-11-25T14:00:00+00:00</updated>
      <summary>In this tutorial, you&#x27;ll explore concurrency in Python, including multi-threaded and asynchronous solutions for I/O-bound tasks, and multiprocessing for CPU-bound tasks. By the end of this tutorial, you&#x27;ll know how to choose the appropriate concurrency model for your program&#x27;s needs.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;Concurrency refers to the ability of a program to manage multiple tasks at once, improving performance and responsiveness. It encompasses different models like threading, asynchronous tasks, and multiprocessing, each offering unique benefits and trade-offs. In Python, threads and asynchronous tasks facilitate concurrency on a single processor, while multiprocessing allows for true parallelism by utilizing multiple CPU cores.&lt;/p&gt;
&lt;p&gt;Understanding concurrency is crucial for optimizing programs, especially those that are I/O-bound or CPU-bound. Efficient concurrency management can significantly enhance a program’s performance by reducing wait times and better utilizing system resources.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this tutorial, you’ll learn how to:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Understand&lt;/strong&gt; the different forms of &lt;strong&gt;concurrency&lt;/strong&gt; in Python&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Implement&lt;/strong&gt; multi-threaded and asynchronous solutions for &lt;strong&gt;I/O-bound&lt;/strong&gt; tasks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Leverage&lt;/strong&gt; multiprocessing for &lt;strong&gt;CPU-bound&lt;/strong&gt; tasks to achieve true parallelism&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Choose&lt;/strong&gt; the appropriate concurrency model based on your program’s needs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To get the most out of this tutorial, you should be familiar with &lt;a href=&quot;https://realpython.com/learning-paths/python-basics/&quot;&gt;Python basics&lt;/a&gt;, including &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-for-loop/&quot;&gt;loops&lt;/a&gt;. A rudimentary understanding of system processes and CPU operations will also be helpful. You can download the sample code for this tutorial by clicking the link below:&lt;/p&gt;
&lt;div class=&quot;alert alert-warning&quot; role=&quot;alert&quot;&gt;
&lt;p&gt;&lt;strong markdown=&quot;1&quot;&gt;Get Your Code:&lt;/strong&gt; &lt;a href=&quot;https://realpython.com/bonus/python-concurrency-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-concurrency-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that you’ll use to learn about speeding up your Python program with concurrency.&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.6e7b751c31b4.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 Concurrency” 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-concurrency/&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;Speed Up Your Python Program With Concurrency&quot; src=&quot;https://files.realpython.com/media/An-Overview-of-Concurrency-in-Python_Watermarked.c54c399ccb32.jpg&quot; width=&quot;1920&quot; height=&quot;1080&quot; srcset=&quot;/cdn-cgi/image/width=480,format=auto/https://files.realpython.com/media/An-Overview-of-Concurrency-in-Python_Watermarked.c54c399ccb32.jpg 480w, /cdn-cgi/image/width=640,format=auto/https://files.realpython.com/media/An-Overview-of-Concurrency-in-Python_Watermarked.c54c399ccb32.jpg 640w, /cdn-cgi/image/width=960,format=auto/https://files.realpython.com/media/An-Overview-of-Concurrency-in-Python_Watermarked.c54c399ccb32.jpg 960w, /cdn-cgi/image/width=1920,format=auto/https://files.realpython.com/media/An-Overview-of-Concurrency-in-Python_Watermarked.c54c399ccb32.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.6e7b751c31b4.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-concurrency/&quot; class=&quot;stretched-link&quot;&gt;&lt;span class=&quot;my-0 h4&quot;&gt;Python Concurrency&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 concurrency. You&#x27;ll revisit the different forms of concurrency in Python, how to implement multi-threaded and asynchronous solutions for I/O-bound tasks, and how to achieve true parallelism for CPU-bound tasks.&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;

&lt;h2 id=&quot;exploring-concurrency-in-python&quot;&gt;Exploring Concurrency in Python&lt;a class=&quot;headerlink&quot; href=&quot;#exploring-concurrency-in-python&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In this section, you’ll get familiar with the terminology surrounding concurrency. You’ll also learn that concurrency can take different forms depending on the problem it aims to solve. Finally, you’ll discover how the different concurrency models translate to Python.&lt;/p&gt;
&lt;h3 id=&quot;what-is-concurrency&quot;&gt;What Is Concurrency?&lt;a class=&quot;headerlink&quot; href=&quot;#what-is-concurrency&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The dictionary definition of concurrency is &lt;strong&gt;simultaneous occurrence&lt;/strong&gt;. In Python, the things that are occurring simultaneously are called by different names, including these:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Thread&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Task&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Process&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;At a high level, they all refer to a sequence of instructions that run in order. You can think of them as different &lt;strong&gt;trains of thought&lt;/strong&gt;. Each one can be stopped at certain points, and the CPU or brain that’s processing them can switch to a different one. The state of each train of thought is saved so it can be restored right where it was interrupted.&lt;/p&gt;
&lt;p&gt;You might wonder why Python uses different words for the same concept. It turns out that threads, tasks, and processes are only the same if you view them from a high-level perspective. Once you start digging into the details, you’ll find that they all represent slightly different things. You’ll see more of how they’re different as you progress through the examples.&lt;/p&gt;
&lt;p&gt;Now, you’ll consider the &lt;em&gt;simultaneous&lt;/em&gt; part of that definition. You have to be a little careful because, when you get down to the details, you’ll discover that only multiple &lt;a href=&quot;https://en.wikipedia.org/wiki/Process_(computing)&quot;&gt;system processes&lt;/a&gt; can enable Python to run these trains of thought at literally the same time.&lt;/p&gt;
&lt;p&gt;In contrast, &lt;a href=&quot;https://en.wikipedia.org/wiki/Thread_(computing)&quot;&gt;threads&lt;/a&gt; and &lt;a href=&quot;https://en.wikipedia.org/wiki/Asynchrony_(computer_programming)&quot;&gt;asynchronous tasks&lt;/a&gt; always run on a single processor, which means they can only run one at a time. They just cleverly find ways to take turns to speed up the overall process. Even though they don’t run different trains of thought simultaneously, they still fall under the concept of &lt;strong&gt;concurrency&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; Threads in most other programming languages often run in parallel. To learn why Python threads can’t, check out &lt;a href=&quot;https://realpython.com/python-gil/&quot;&gt;What Is the Python Global Interpreter Lock (GIL)?&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you’re curious about even more details, then you can also read about &lt;a href=&quot;https://realpython.com/python-parallel-processing/&quot;&gt;Bypassing the GIL for Parallel Processing in Python&lt;/a&gt; or check out the experimental &lt;a href=&quot;https://realpython.com/python313-free-threading-jit/&quot;&gt;free threading&lt;/a&gt; introduced in &lt;a href=&quot;https://realpython.com/python313-new-features/&quot;&gt;Python 3.13&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;The way the threads, tasks, or processes take turns differs. In a multi-threaded approach, the operating system actually knows about each thread and can interrupt it at any time to start running a different thread. This mechanism is also true for processes. It’s called &lt;a href=&quot;https://en.wikipedia.org/wiki/Preemption_%28computing%29#Preemptive_multitasking&quot;&gt;preemptive multitasking&lt;/a&gt; since the operating system can preempt your thread or process to make the switch.&lt;/p&gt;
&lt;p&gt;Preemptive multitasking is handy in that the code in the thread doesn’t need to do anything special to make the switch. It can also be difficult because of that &lt;em&gt;at any time&lt;/em&gt; phrase. The &lt;a href=&quot;https://en.wikipedia.org/wiki/Context_switch&quot;&gt;context switch&lt;/a&gt; can happen in the middle of a single Python statement, even a trivial one like &lt;code&gt;x = x + 1&lt;/code&gt;. This is because Python statements typically consist of several low-level &lt;a href=&quot;https://en.wikipedia.org/wiki/Bytecode&quot;&gt;bytecode&lt;/a&gt; instructions.&lt;/p&gt;
&lt;p&gt;On the other hand, asynchronous tasks use &lt;a href=&quot;https://en.wikipedia.org/wiki/Cooperative_multitasking&quot;&gt;cooperative multitasking&lt;/a&gt;. The tasks must cooperate with each other by announcing when they’re ready to be switched out without the operating system’s involvement. This means that the code in the task has to change slightly to make it happen.&lt;/p&gt;
&lt;p&gt;The benefit of doing this extra work upfront is that you always know where your task will be swapped out, making it easier to reason about the flow of execution. A task won’t be swapped out in the middle of a Python statement unless that statement is appropriately marked. You’ll see later how this can simplify parts of your design.&lt;/p&gt;
&lt;h3 id=&quot;what-is-parallelism&quot;&gt;What Is Parallelism?&lt;a class=&quot;headerlink&quot; href=&quot;#what-is-parallelism&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;So far, you’ve looked at concurrency that happens on a single &lt;a href=&quot;https://en.wikipedia.org/wiki/Processor_(computing)&quot;&gt;processor&lt;/a&gt;. What about all of those &lt;a href=&quot;https://en.wikipedia.org/wiki/Multi-core_processor&quot;&gt;CPU cores&lt;/a&gt; your cool, new laptop has? How can you make use of them in Python? The answer is to execute separate processes!&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;process&lt;/strong&gt; can be thought of as almost a completely different program, though technically, it’s usually defined as a collection of resources including memory, &lt;a href=&quot;https://en.wikipedia.org/wiki/File_descriptor&quot;&gt;file handles&lt;/a&gt;, and things like that. One way to think about it is that each process runs in its own Python interpreter.&lt;/p&gt;
&lt;p&gt;Because they’re different processes, each of your trains of thought in a program leveraging &lt;strong&gt;multiprocessing&lt;/strong&gt; can run on a different CPU core. Running on a different core means that they can actually run at the same time, which is fabulous. There are some complications that arise from doing this, but Python does a pretty good job of smoothing them over most of the time.&lt;/p&gt;
&lt;/div&gt;&lt;h2&gt;&lt;a href=&quot;https://realpython.com/python-concurrency/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-concurrency/ »&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 range(): Represent Numerical Ranges</title>
      <id>https://realpython.com/python-range/</id>
      <link href="https://realpython.com/python-range/"/>
      <updated>2024-11-24T14:00:00+00:00</updated>
      <summary>Master the Python range() function and learn how it works under the hood. You most commonly use ranges in loops. In this tutorial, you&#x27;ll learn how to iterate over ranges but also identify when there are better alternatives.</summary>
      <content type="html">
        &lt;div&gt;&lt;p&gt;In Python, the &lt;code&gt;range()&lt;/code&gt; function generates a sequence of numbers, often used in loops for iteration. By default, it creates numbers starting from 0 up to but not including a specified stop value. You can also reverse the sequence with &lt;code&gt;reversed()&lt;/code&gt;. If you need to count backwards, then you can use a negative step, like &lt;code&gt;range(start, stop, -1)&lt;/code&gt;, which counts down from &lt;code&gt;start&lt;/code&gt; to &lt;code&gt;stop&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;range()&lt;/code&gt; function is not just about iterating over numbers. It can also be used in various programming scenarios beyond simple loops. By mastering &lt;code&gt;range()&lt;/code&gt;, you can write more efficient and readable Python code. Explore how &lt;code&gt;range()&lt;/code&gt; can simplify your code and when alternatives might be more appropriate.&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 range in Python is an object representing an &lt;strong&gt;interval of integers&lt;/strong&gt;, often used for looping.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;range()&lt;/code&gt; function can be used to &lt;strong&gt;generate sequences&lt;/strong&gt; of numbers that can be &lt;strong&gt;converted to lists&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;for i in range(5)&lt;/code&gt; is a loop that &lt;strong&gt;iterates&lt;/strong&gt; over the numbers from 0 to 4, inclusive.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;range parameters&lt;/strong&gt; &lt;code&gt;start&lt;/code&gt;, &lt;code&gt;stop&lt;/code&gt;, and &lt;code&gt;step&lt;/code&gt; define where the sequence begins, ends, and the interval between numbers.&lt;/li&gt;
&lt;li&gt;Ranges can go &lt;strong&gt;backward&lt;/strong&gt; in Python by using a negative step value and &lt;strong&gt;reversed&lt;/strong&gt; by using &lt;code&gt;reversed()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A range is a Python object that represents an interval of integers. Usually, the numbers are consecutive, but you can also specify that you want to space them out. You can create ranges by calling &lt;code&gt;range()&lt;/code&gt; with one, two, or three arguments, as the following examples show:&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.6e7b751c31b4.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;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[0, 1, 2, 3, 4]&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;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[1, 2, 3, 4, 5, 6]&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;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]&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.6e7b751c31b4.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.6e7b751c31b4.svg#@check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;Copied!&lt;/span&gt;
    &lt;/template&gt;
    
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In each example, you use &lt;a href=&quot;https://realpython.com/python-list/#using-the-list-constructor&quot;&gt;&lt;code&gt;list()&lt;/code&gt;&lt;/a&gt; to explicitly list the individual elements of each range. You’ll study these examples in more detail later on.&lt;/p&gt;
&lt;p&gt;A range can be an effective tool. However, throughout this tutorial, you’ll also explore alternatives that may work better in some situations. You can click the link below to download the code that you’ll see in this tutorial:&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-range-code/&quot; class=&quot;alert-link&quot; data-toggle=&quot;modal&quot; data-target=&quot;#modal-python-range-code&quot; data-focus=&quot;false&quot; markdown=&quot;1&quot;&gt;Click here to download the free sample code&lt;/a&gt; that shows you how to represent numerical ranges in Python.&lt;/p&gt;
&lt;/div&gt;
&lt;h2 id=&quot;construct-numerical-ranges&quot;&gt;Construct Numerical Ranges&lt;a class=&quot;headerlink&quot; href=&quot;#construct-numerical-ranges&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In Python, &lt;code&gt;range()&lt;/code&gt; is &lt;strong&gt;built in&lt;/strong&gt;. This means that you can always call &lt;code&gt;range()&lt;/code&gt; without doing any preparations first. Calling &lt;code&gt;range()&lt;/code&gt; constructs a &lt;strong&gt;range object&lt;/strong&gt; that you can put to use. Later, you’ll see practical examples of how to use range objects.&lt;/p&gt;
&lt;p&gt;You can provide &lt;code&gt;range()&lt;/code&gt; with one, two, or three &lt;strong&gt;integer&lt;/strong&gt; arguments. This corresponds to three different use cases:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Ranges counting from zero&lt;/li&gt;
&lt;li&gt;Ranges of consecutive numbers&lt;/li&gt;
&lt;li&gt;Ranges stepping over numbers&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;You’ll learn how to use each of these next.&lt;/p&gt;
&lt;h3 id=&quot;count-from-zero&quot;&gt;Count From Zero&lt;a class=&quot;headerlink&quot; href=&quot;#count-from-zero&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;When you call &lt;code&gt;range()&lt;/code&gt; with one argument, you create a range that counts from zero and up to, but not including, the number you provided:&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.6e7b751c31b4.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;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;range(0, 5)&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.6e7b751c31b4.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.6e7b751c31b4.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’ve created a range from zero to five. To see the individual elements in the range, you can use &lt;code&gt;list()&lt;/code&gt; to convert the range to a &lt;a href=&quot;https://realpython.com/python-list/&quot;&gt;list&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;codeblock mb-3 w-100&quot; aria-label=&quot;Code block&quot; data-syntax-language=&quot;pycon&quot; data-is-repl=&quot;true&quot;&gt;
  &lt;div class=&quot;codeblock__header d-flex justify-content-between codeblock--blue&quot;&gt;
    &lt;span class=&quot;mr-2 noselect&quot; aria-label=&quot;Language&quot;&gt;Python&lt;/span&gt;
    
    &lt;div class=&quot;noselect&quot;&gt;
      
        &lt;span class=&quot;codeblock__output-toggle&quot; title=&quot;Toggle prompts and output&quot; role=&quot;button&quot;&gt;&lt;span class=&quot;icon baseline js-codeblock-output-on codeblock__header--icon-lower&quot; aria-hidden=&quot;true&quot;&gt;&lt;svg aria-hidden=&quot;true&quot;&gt;&lt;use href=&quot;/static/icons.6e7b751c31b4.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;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[0, 1, 2, 3, 4]&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.6e7b751c31b4.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.6e7b751c31b4.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;Inspecting &lt;code&gt;range(5)&lt;/code&gt; shows that it contains the numbers zero, one, two, three, and four. Five itself is not a part of the range. One nice property of these ranges is that the argument, &lt;code&gt;5&lt;/code&gt; in this case, is the same as the number of elements in the range.&lt;/p&gt;
&lt;h3 id=&quot;count-from-start-to-stop&quot;&gt;Count From Start to Stop&lt;a class=&quot;headerlink&quot; href=&quot;#count-from-start-to-stop&quot; title=&quot;Permanent link&quot;&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;You can call &lt;code&gt;range()&lt;/code&gt; with two arguments. The first value will be the start of the range. As before, the range will count up to, but not include, the second value:&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.6e7b751c31b4.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;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;range(1, 7)&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.6e7b751c31b4.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.6e7b751c31b4.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 representation of a range object just shows you the arguments that you provided, so it’s not super helpful in this case. You can use &lt;code&gt;list()&lt;/code&gt; to inspect the individual elements:&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.6e7b751c31b4.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;list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;go&quot;&gt;[1, 2, 3, 4, 5, 6]&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.6e7b751c31b4.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.6e7b751c31b4.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-range/?utm_source=realpython&amp;utm_medium=rss&quot;&gt;Read the full article at https://realpython.com/python-range/ »&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 #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>Differences Between Python&#x27;s Mutable and Immutable Types</title>
      <id>https://realpython.com/courses/differences-mutable-immutable-types/</id>
      <link href="https://realpython.com/courses/differences-mutable-immutable-types/"/>
      <updated>2024-10-01T14:00:00+00:00</updated>
      <summary>In this video course, you&#x27;ll learn how Python mutable and immutable data types work internally and how you can take advantage of mutability or immutability to power your code.</summary>
      <content type="html">
        &lt;p&gt;As a Python developer, you&amp;rsquo;ll have to deal with &lt;strong&gt;mutable&lt;/strong&gt; and &lt;strong&gt;immutable&lt;/strong&gt; objects sooner or later. Mutable objects are those that allow you to change their value or data in place without affecting the object&amp;rsquo;s identity. In contrast, immutable objects don&amp;rsquo;t allow this kind of operation. You&amp;rsquo;ll just have the option of creating new objects of the same type with different values.&lt;/p&gt;
&lt;p&gt;In Python, mutability is a characteristic that may profoundly influence your decision when choosing which data type to use in solving a given programming problem. Therefore, you need to know how mutable and immutable objects work in Python.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;In this video course, you&amp;rsquo;ll:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Understand how &lt;strong&gt;mutability&lt;/strong&gt; and &lt;strong&gt;immutability&lt;/strong&gt; work under the hood in Python&lt;/li&gt;
&lt;li&gt;Explore immutable and mutable &lt;strong&gt;built-in data types&lt;/strong&gt; in Python&lt;/li&gt;
&lt;li&gt;Identify and avoid some common &lt;strong&gt;mutability-related gotchas&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Understand and control how mutability affects your &lt;strong&gt;custom classes&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 #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>
  
    <entry>
      <title>Advanced Python import Techniques</title>
      <id>https://realpython.com/courses/advanced-import-techniques/</id>
      <link href="https://realpython.com/courses/advanced-import-techniques/"/>
      <updated>2024-09-24T14:00:00+00:00</updated>
      <summary>The Python import system is as powerful as it is useful. In this in-depth video course, you&#x27;ll learn how to harness this power to improve the structure and maintainability of your code.</summary>
      <content type="html">
        &lt;p&gt;In Python, you use the &lt;strong&gt;&lt;code&gt;import&lt;/code&gt;&lt;/strong&gt; keyword to make code in one &lt;strong&gt;module&lt;/strong&gt; available in another. Imports in Python are important for &lt;strong&gt;structuring your code&lt;/strong&gt; effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.&lt;/p&gt;
&lt;p&gt;This video course provides a comprehensive overview of Python&amp;rsquo;s &lt;code&gt;import&lt;/code&gt; statement and how it works. The import system is powerful, and this course will teach you how to harness this power. While you&amp;rsquo;ll cover many of the concepts behind Python&amp;rsquo;s import system, this video course is mostly example driven, so you&amp;rsquo;ll learn from the numerous code examples shared throughout.&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 &lt;strong&gt;modules&lt;/strong&gt;, &lt;strong&gt;packages&lt;/strong&gt;, and &lt;strong&gt;namespace packages&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Manage namespaces and avoid &lt;strong&gt;shadowing&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Avoid &lt;strong&gt;circular imports&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Import modules &lt;strong&gt;dynamically&lt;/strong&gt; at runtime&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Customize&lt;/strong&gt; Python&amp;rsquo;s import system&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 #221: Thriving as a Developer With ADHD</title>
      <id>https://realpython.com/podcasts/rpp/221/</id>
      <link href="https://realpython.com/podcasts/rpp/221/"/>
      <updated>2024-09-20T12:00:00+00:00</updated>
      <summary>What are strategies for being a productive developer with ADHD? How can you help your team members with ADHD to succeed and complete projects? This week on the show, we speak with Chris Ferdinandi about his website and podcast &quot;ADHD For the Win!&quot;</summary>
      <content type="html">
        &lt;p&gt;What are strategies for being a productive developer with ADHD? How can you help your team members with ADHD to succeed and complete projects? This week on the show, we speak with Chris Ferdinandi about his website and podcast &quot;ADHD For the Win!&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>
  

</feed>
