<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ David Fagbuyiro - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ David Fagbuyiro - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Tue, 26 May 2026 16:22:49 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/Davidking/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How Arrays Work in Python – Array Methods Explained with Code Examples ]]>
                </title>
                <description>
                    <![CDATA[ In this tutorial, you'll learn what an array is in Python. You'll also learn some possible ways to add elements to an existing array.  In Python, there is no need to use a specific data type for arrays. You can simply use a list with all the attribut... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-arrays-work-in-python/</link>
                <guid isPermaLink="false">66bae7d4e853354c56ba118d</guid>
                
                    <category>
                        <![CDATA[ arrays ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ David Fagbuyiro ]]>
                </dc:creator>
                <pubDate>Wed, 12 Jul 2023 22:19:23 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/07/pexels-andreas-fickl-4405941.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this tutorial, you'll learn what an array is in Python. You'll also learn some possible ways to add elements to an existing array. </p>
<p>In Python, there is no need to use a specific data type for arrays. You can simply use a list with all the attributes of an array. </p>
<p>If you want to create an array that includes both integers and floating-point numbers, you can use Python's array module.</p>
<h2 id="heading-what-is-an-array">What is an Array?</h2>
<p>An array is a unique type of variable that has the capacity to store more than one value at once. </p>
<p>Let's say you have a list of objects like country names. You could store the countries in separate variables as follows:</p>
<pre><code class="lang-python">Country1 = <span class="hljs-string">"Germany"</span>;

Country2 = <span class="hljs-string">"France"</span>;

Country3 = <span class="hljs-string">"Denmark"</span>;
</code></pre>
<p>But suppose you wanted to search through all of the countries to discover a certain one. What if you had 200 countries instead of only 3?</p>
<p>The alternative is to store all these values in an array.</p>
<p>Arrays are useful for storing and manipulating multiple values of the same data type. They act like a variable that can hold a collection of values, all of which are the same type. These values are stored together in bordering memory.</p>
<h2 id="heading-python-array-methods">Python Array Methods</h2>
<p>You can use various built-in Python methods when working with lists and arrays. Below are the methods you can use on arrays and lists in Python.</p>
<h3 id="heading-the-append-method">The <code>Append()</code> method</h3>
<p>If you want to add an item to the end of a list, you can utilize the append method.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">'apple'</span>, <span class="hljs-string">'banana'</span>, <span class="hljs-string">'cherry'</span>]
fruits.append(<span class="hljs-string">'orange'</span>)
print(fruits)  


<span class="hljs-comment"># Output: ['apple', 'banana', 'cherry', 'orange']</span>
</code></pre>
<p>The <code>append()</code> method is used to add elements to the end of a list. In this case, 'orange' is appended to the <code>fruits</code> list, resulting in the list containing four elements: 'apple', 'banana', 'cherry', and 'orange'.</p>
<p>Here's another example for you:</p>
<p>Let's create an array containing the names of cars:</p>
<pre><code class="lang-python">
cars = [<span class="hljs-string">"Lexus"</span>, <span class="hljs-string">"Toyota"</span>, <span class="hljs-string">"Mercedez"</span>]
</code></pre>
<p>You can use the <code>append()</code> method to add a new element to an existing list/array just as seen below.</p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>]
fruits.append(<span class="hljs-string">"orange"</span>)
print(fruits)


<span class="hljs-comment"># Output: ['apple', 'banana', 'cherry', 'orange']</span>
</code></pre>
<p>Output:</p>
<pre><code>[<span class="hljs-string">"Lexus"</span>, <span class="hljs-string">"Toyota"</span>, <span class="hljs-string">"Mercedez"</span>, <span class="hljs-string">"Honda"</span>]
</code></pre><h3 id="heading-the-clear-method">The <code>Clear()</code> method</h3>
<p>The <code>clear()</code> method removes all the elements from the list, just as the name implies.</p>
<p><strong>Example:</strong></p>
<p>Below is an example using the <code>clear()</code> method:</p>
<pre><code class="lang-python">cars = [<span class="hljs-string">"Lexus"</span>, <span class="hljs-string">"Toyota"</span>, <span class="hljs-string">"Mercedez"</span>]

cars.clear()

print(cars)
</code></pre>
<p>Output:</p>
<p>Based on the explanation of the <code>clear()</code> method above, the result of this expression will be [] empty because we have cleared the entire list.</p>
<h3 id="heading-the-copy-method">The <code>Copy()</code> method</h3>
<p>This function creates and returns an identical copy of the original list.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>]
fruits_copy = fruits.copy()
print(fruits_copy)


<span class="hljs-comment"># Output: ['apple', 'banana', 'cherry']</span>
</code></pre>
<p>In the above example, the copy() method creates a new array called fruits_copy, which is a shallow copy of the fruits array. Modifying the fruits_copy array will not affect the original fruits array.</p>
<p>Here's another example using the <code>copy()</code> method:</p>
<pre><code class="lang-python">cars = [<span class="hljs-string">"Lexus"</span>, <span class="hljs-string">"Toyota"</span>, <span class="hljs-string">"Mercedez"</span>]


x = cars.copy()


print(x)


<span class="hljs-comment"># Output of the above using the copy () method will be:</span>


[<span class="hljs-string">"Lexus"</span>, <span class="hljs-string">"Toyota"</span>, <span class="hljs-string">"Mercedez"</span>]
</code></pre>
<h3 id="heading-the-count-method">The <code>Count()</code> method</h3>
<p>This method returns the number of elements with the specified value.</p>
<p><strong>Example:</strong> </p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>, <span class="hljs-string">"banana"</span>]
count = fruits.count(<span class="hljs-string">"banana"</span>)
print(count)


<span class="hljs-comment"># Output: 2</span>
</code></pre>
<p>The code above creates a list called <strong>fruits</strong> with four elements: 'apple', 'banana', 'cherry', and another 'banana'.  The <code>count()</code> method is then used on the <code>fruits</code> list to count the number of occurrences of the element 'banana'. It returns the count, which in this case is 2, as 'banana' appears twice in the list.</p>
<p>Finally, the count value is printed to the console, resulting in the output: 2.</p>
<p>Here's another example of using the <code>count()</code> method:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Return the number of times the value "Lexus" appears in the car list.</span>

cars = [<span class="hljs-string">"Lexus"</span>, <span class="hljs-string">"Toyota"</span>, <span class="hljs-string">"Mercedez"</span>, <span class="hljs-string">"Lexus"</span>]

x = cars.count(<span class="hljs-string">"Lexus"</span>)

print(x)
</code></pre>
<p>Output of this would return int "2" as the result because "Lexus" appears twice in the cars list.</p>
<h3 id="heading-the-extend-method">The <code>Extend()</code> method</h3>
<p>With this method, you can add the elements of a list (or any iterable) to the end of the current list.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>]
additional_fruits = [<span class="hljs-string">"orange"</span>, <span class="hljs-string">"grape"</span>]
fruits.extend(additional_fruits)
print(fruits)

<span class="hljs-comment"># Output: ['apple', 'banana', 'cherry', 'orange', 'grape']</span>
</code></pre>
<p>In the example above, the <code>extend()</code> method is used to add the elements from the <code>additional_fruits</code> list to the <code>fruits</code> array. The resulting array contains all the elements from both arrays.</p>
<p>Note that the <code>extend()</code> method modifies the original array in place and does not return a new array.</p>
<h3 id="heading-the-index-method">The <code>index()</code> method</h3>
<p>This method returns the index of the first element with the specified value.</p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>]
index = fruits.index(<span class="hljs-string">"banana"</span>)

print(index)

<span class="hljs-comment"># Output: 1</span>
</code></pre>
<p>This code above creates a list of <strong>fruits</strong> containing 'apple', 'banana', and 'cherry'. It then finds the index position of 'banana' in the list and assigns it to the variable 'index'. Finally, it prints the value of 'index', which in this case would be 1.</p>
<h3 id="heading-the-insert-method">The <code>insert()</code> method</h3>
<p>This array method adds an element at the specified position.</p>
<pre><code class="lang-python">numbers = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]
numbers.insert(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
print(numbers)

<span class="hljs-comment"># Output: [1, 2, 3, 4, 5, 6]</span>
</code></pre>
<p>From the code above, we have an array numbers with elements [1, 2, 3, 5, 6]. We want to insert the number 4 at index 3 (which is the fourth position in the array, as Python is 0-indexed). By calling insert(3, 4), we insert the element 4 at the specified index, and the existing elements are shifted to the right to make room for the new element. The resulting array is [1, 2, 3, 4, 5, 6].</p>
<h3 id="heading-the-pop-method">The <code>pop()</code> method</h3>
<p>This method removes the element at the specified position.</p>
<p><strong>Example</strong>:</p>
<pre><code class="lang-python"><span class="hljs-comment"># To delete an item from an array/list, you can utilize the pop() method.</span>

<span class="hljs-comment"># Delete the second element of the car array:</span>

cars = [<span class="hljs-string">"Lexus"</span>, <span class="hljs-string">"Toyota"</span>, <span class="hljs-string">"Mercedez"</span>]

cars.pop(<span class="hljs-number">2</span>)

print(cars)
</code></pre>
<p>And here's the output:</p>
<pre><code class="lang-python">[<span class="hljs-string">'Lexus'</span>, <span class="hljs-string">'Toyota'</span>]
</code></pre>
<p>This code above deletes the second element from the 'cars' array using the 'pop()' method and then prints the updated array.</p>
<p>Here's another example using the pop() method:</p>
<pre><code class="lang-python"><span class="hljs-comment"># To delete an item from an array/list, you can utilize the pop() method.</span>

<span class="hljs-comment"># Delete the second element of the car array:</span>

cars = [<span class="hljs-string">"Lexus"</span>, <span class="hljs-string">"Toyota"</span>, <span class="hljs-string">"Mercedez"</span>]

cars.pop(<span class="hljs-number">2</span>)

print(cars)
</code></pre>
<p>Output:</p>
<pre><code>
[<span class="hljs-string">'Lexus'</span>, <span class="hljs-string">'Toyota'</span>]
</code></pre><p>The code deletes the second element from the <code>cars</code> array using the <code>pop()</code> method and then prints the modified array. The resulting array will contain only the first and third elements: ['Lexus', 'Toyota'].</p>
<h3 id="heading-the-remove-method">The <code>remove()</code> method</h3>
<p>This method removes the first item with the specified value.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>]
fruits.remove(<span class="hljs-string">"banana"</span>)
print(fruits)

<span class="hljs-comment"># Output Below:</span>

[<span class="hljs-string">"apple"</span>, <span class="hljs-string">"cherry"</span>]
</code></pre>
<p>The code above creates a list called <code>fruits</code> with three elements: 'apple', 'banana', and 'cherry'. The <code>remove()</code> method is then used to remove the element 'banana' from the list. </p>
<p>After removing 'banana', the updated list is printed using the <code>print()</code> function. The output will be <code>['apple', 'cherry']</code>, as 'banana' is no longer present in the list.</p>
<p>Here's another example using the <code>remove()</code> method:</p>
<pre><code class="lang-python">cars = [<span class="hljs-string">"Lexus"</span>, <span class="hljs-string">"Toyota"</span>, <span class="hljs-string">"Mercedez"</span>]

cars.remove(<span class="hljs-string">"Toyota"</span>)

print(cars)
</code></pre>
<p>The <strong>remove()</strong> function may also be used to remove an element from an array, but it should be noted that it only removes the first instance of the specified value from a list.</p>
<h3 id="heading-the-reverse-method">The <code>reverse()</code> method</h3>
<p>This method reverses the order of the list.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">fruits = [<span class="hljs-string">"apple"</span>, <span class="hljs-string">"banana"</span>, <span class="hljs-string">"cherry"</span>]
fruits.reverse()
print(fruits)


<span class="hljs-comment"># Output: ['cherry', 'banana', 'apple']</span>
</code></pre>
<p>The code above creates a list called fruits with three elements: 'apple', 'banana', and 'cherry'. Then, the <code>reverse()</code> method is called on the fruits list which reverses the order of its elements. Finally, the reversed list is printed using the print() function, resulting in the output ['cherry', 'banana', 'apple']. This means that the original order of the fruits list has been reversed.</p>
<h3 id="heading-the-sort-method">The <code>sort()</code> method</h3>
<p>This method sorts the list, just as the name implies.</p>
<p><strong>Example:</strong></p>
<pre><code class="lang-python">numbers = [<span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>]

numbers.sort()

print(numbers)

<span class="hljs-comment"># Output: [1, 2, 3, 4]</span>
</code></pre>
<p>The code above creates a list called <code>numbers</code> with the elements <code>[4, 2, 1, 3]</code>. The <code>sort()</code> method is then called on the <code>numbers</code> list, which sorts the elements in ascending order. After sorting, the <code>numbers</code> list becomes <code>[1, 2, 3, 4]</code>. Finally, the sorted list is printed to the console using <code>print(numbers)</code>, which outputs <code>[1, 2, 3, 4]</code>.</p>
<p>the <code>sort()</code> method in Python sorts the elements of a list in ascending order by default. If you want to sort the list in descending order, you can pass the parameter <code>reverse=True</code> to the <code>sort()</code> method.</p>
<p>Here's an example of how to sort the <code>numbers</code> list in descending order:</p>
<pre><code class="lang-python">numbers = [<span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>]

numbers.sort(reverse=<span class="hljs-literal">True</span>)

print(numbers)

<span class="hljs-comment"># Output</span>

[<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>]
</code></pre>
<p>By passing <code>reverse=True</code> as an argument to the <code>sort()</code> method, the list is sorted in descending order.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Hopefully, after reading this article, you should now have a basic understanding of what an array is in Python. </p>
<p>You should also know the basic Python array methods that you'll use to modify an array or a list and how to use them.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Truncate Text with CSS and JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ CSS is a powerful tool to have in your programming toolkit. It offers numerous features that allow you to create responsive and attractive websites.  Sometimes you might see an ellipsis (...) that appears to indicate that some content or text is hidd... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-truncate-text-with-css-javascript/</link>
                <guid isPermaLink="false">66bae7d7a05001e4613da06c</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ David Fagbuyiro ]]>
                </dc:creator>
                <pubDate>Tue, 04 Apr 2023 18:06:04 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/04/pexels-pixabay-261763.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p><a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/CSS">CSS</a> is a powerful tool to have in your programming toolkit. It offers numerous features that allow you to create responsive and attractive websites. </p>
<p>Sometimes you might see an ellipsis (...) that appears to indicate that some content or text is hidden. Have you ever wondered how you can code that feature?</p>
<p>In this tutorial, we'll look at how to do multi-line truncations in <code>CSS</code> using <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/javascript">JavaScript</a>. Let's get this party started!</p>
<h2 id="heading-what-is-text-truncation">What is Text Truncation?</h2>
<p>In CSS, text <a target="_blank" href="https://dictionary.cambridge.org/dictionary/english/truncation">truncation</a> is used to truncate text that overflows its container by hiding the extra content and replacing it with ellipses. This technique is useful for creating more compact and visually appealing layouts in situations where the length of the text may vary, such as in navigation menus, table cells, or headings.</p>
<p>The CSS property you use for text truncation is <code>text-overflow</code>. To enable text truncation, you need to set the <code>text-overflow</code> property to "ellipsis" and the <code>white-space</code> property to <code>nowrap</code> to prevent the text from <code>wrapping</code> to the next line. You may also need to set the <code>overflow</code> property to "hidden" to hide any text that <code>overflows</code> the container.</p>
<p>Let's discuss these properties in more detail now.</p>
<h2 id="heading-css-properties-to-truncate-text">CSS Properties to Truncate Text</h2>
<p>Different properties in CSS may be used to truncate text. Below are some of the most common CSS properties:</p>
<p><code>text-overflow</code> : This property specifies how the text content should be displayed when it overflows the element's content area. The <code>text-overflow</code> property can take one of the following values:</p>
<ul>
<li><code>clip</code>: If the text exceeds the element's content area, it is clipped and not shown.</li>
<li><code>ellipsis</code>: If the text exceeds the element's content area, it is truncated, and an ellipsis (...) is inserted at the end.</li>
<li><code>fade</code>: If the text exceeds the element's content area, it is truncated, and a fade effect is added to the end.</li>
</ul>
<p><code>white-space</code>: This property controls how white space characters in text are treated. The white-space attribute has the following possible qualities:</p>
<ul>
<li><code>normal</code>: The browser will break lines of text to fit within the available space.</li>
<li><code>nowrap</code>: The browser will not break lines of text, which can cause the text to overflow the element's content area.</li>
<li><code>pre</code>: The browser will preserve whitespace characters, which can cause the text to overflow the element's content area.</li>
</ul>
<p><code>overflow</code>: This property specifies how the overflowed content should be handled. The overflow property can take one of the following values:</p>
<ul>
<li><code>visible</code>: The overflowed content is visible and not clipped.</li>
<li><code>hidden</code>: The overflowed content is hidden and not visible.</li>
<li><code>scroll</code>: The overflowed content is visible, and scrollbars are added to enable scrolling.</li>
<li><code>auto</code>: The overflowed content is visible and scrollbars are added to the element only if the content overflows.</li>
</ul>
<p>To truncate text, you can use the <code>text-overflow</code> property in combination with the <code>white-space</code> and <code>overflow</code> properties. For</p>
<h2 id="heading-dynamic-truncation">Dynamic Truncation</h2>
<p>You can also implement dynamic truncation using CSS to display text content on a webpage. One common use case for dynamic truncation with CSS is to limit the amount of text displayed in a container while providing a "read more" link or button to allow users to expand the content.</p>
<p>Here's some example HTML and CSS code that implements dynamic CSS in text truncation, with a border around the text to indicate its width:</p>
<p>The HTML code:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"list"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>First Example<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text single-line"</span>&gt;</span>
          mollis, ante non euismod ornare, orci diam ornare orci, eu mattis
          tortor lectus at erat. Nam rutrum erat nec euismod lacinia. Curabitur
          et velit ut mauris euismod tempus. Fusce pharetra augue lectus, quis
          maximus quam auctor pellentesque.  
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Second Example header<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text single-line"</span>&gt;</span>
          Curabitur pharetra, erat a gravida malesuada, augue mi tincidunt odio,
          quis rhoncus tortor metus ut purus. Nunc lectus quam, tempus sed
          mollis id, feugiat a quam. Donec posuere nulla a lacus interdum
          faucibus ut tincidunt nisi. 
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"list"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>First Example<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text single-line"</span>&gt;</span>
                  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam augue nulla, elementum non erat id, cursus feugiat sem. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Nullam velit neque, tincidunt in ipsum vel, accumsan mattis nisi. 
          <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Second Example header<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text single-line"</span>&gt;</span>
            Curabitur pharetra, erat a gravida malesuada, augue mi tincidunt odio, quis rhoncus tortor metus ut purus. Nunc lectus quam, tempus sed mollis id, feugiat a quam. Donec posuere nulla a lacus interdum faucibus ut tincidunt nisi. Curabitur consequat vitae turpis quis lobortis. 
          <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Below is the HTML code output without adding the CSS:</p>
<p><img src="https://i.imgur.com/MKWdlrN.png" alt="Image" width="1852" height="649" loading="lazy"></p>
<p>Here's the CSS code to truncate the overflowing text:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.list</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">flex-direction</span>: column;
}

<span class="hljs-selector-class">.card</span> {
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#1948e3</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">15px</span> <span class="hljs-number">25px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">890px</span>;
}

<span class="hljs-selector-class">.card</span><span class="hljs-selector-pseudo">:not(</span><span class="hljs-selector-pseudo">:last-of-type)</span> {
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">20px</span>;
}

<span class="hljs-selector-class">.text</span><span class="hljs-selector-class">.single-line</span> {
  <span class="hljs-attribute">overflow</span>: hidden;
  <span class="hljs-attribute">text-overflow</span>: ellipsis;
  <span class="hljs-attribute">white-space</span>: nowrap;
}
</code></pre>
<p>Below is the image output of the paragraph after linking the CSS code above with it:</p>
<p><img src="https://i.imgur.com/BQJZJeN.png" alt="Image" width="1777" height="582" loading="lazy"></p>
<p>Below is a GIF to show the behavior of the text after linking it with CSS to truncate the text dynamically:</p>
<p><img src="https://i.imgur.com/wQysyoe.gif" alt="Image" width="1899" height="916" loading="lazy"></p>
<h2 id="heading-how-to-create-multi-line-truncated-text">How to Create Multi-line Truncated Text</h2>
<p>CSS properties effectively truncate a single line of text or multi-line text spanning two lines or fewer. But it is also possible to achieve text truncation for multi-line text using JavaScript.</p>
<p>In the following sections, we will explore two techniques for multi-line text truncation, one using CSS and the other using JavaScript. By examining both approaches, you can decide which technique is best suited to your needs.</p>
<h3 id="heading-how-to-use-css-to-truncate-multi-line-text">How to Use CSS to Truncate Multi-line Text</h3>
<p>CSS properties function well for a single line of text and multi-line text that spans more than two lines.</p>
<p>The first step is to specify the height of the <code>box or element</code>. Next, we multiply the <code>line-height</code> by the number of lines we wish to ignore before truncating to get the <code>max-height</code>.</p>
<p>Here’s how it’s done:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Multi-line Text Truncation overflow!<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"truncate-overflow"</span>&gt;</span>
    Lorem ipsum dolor sit, amet consectetur adipisicing elit. In sint facilis
    explicabo voluptatum exercitationem earum. Quibusdam vitae, iusto temporibus
    corrupti tempore distinctio soluta reiciendis. Ab aspernatur facilis autem
    temporibus veniam.
  <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"truncate-overflow"</span>&gt;</span>
    Multi-line text truncation is a common design pattern that allows designers to show a limited amount of text on a web page or application, while still giving users the ability to see more if they desire.
  <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"truncate-overflow"</span>&gt;</span>
    In CSS, there are several ways to truncate multi-line text, including using the text-overflow property in combination with display, white-space, and overflow.
  <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre>
<p>Below is the image output of the above HTML code:</p>
<p><img src="https://i.imgur.com/EDkUWHB.png" alt="Image" width="964" height="597" loading="lazy"></p>
<p>Now let's set the <code>Overflow</code> to <code>hidden</code> and the <code>max-height</code> to our preferred height, the same as the <code>line-height</code>.</p>
<p>Then we have <code>-webkit-box-orient</code> set to <code>vertical</code>, <code>-webkit-line-clamp</code>, and <code>text-overflow</code> set to <code>ellipsis</code>. We'll also change the <code>display</code> to <code>box</code>:</p>
<pre><code class="lang-css"><span class="hljs-selector-pseudo">:root</span> {
  <span class="hljs-attribute">--lh</span>: <span class="hljs-number">1.4rem</span>;
}

<span class="hljs-selector-tag">html</span> {
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">22rem</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">2rem</span> auto;
  <span class="hljs-attribute">line-height</span>: <span class="hljs-built_in">var</span>(--lh);
}

<span class="hljs-selector-class">.truncate-overflow</span> {
  <span class="hljs-attribute">--max-lines</span>: <span class="hljs-number">3</span>;
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">max-height</span>: <span class="hljs-built_in">calc</span>(var(--lh) * <span class="hljs-built_in">var</span>(--max-lines));
  <span class="hljs-attribute">overflow</span>: hidden;
  <span class="hljs-attribute">padding-right</span>: <span class="hljs-number">1rem</span>;
}
<span class="hljs-selector-class">.truncate-overflow</span><span class="hljs-selector-pseudo">::before</span> {
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">content</span>: <span class="hljs-string">"..."</span>;
  <span class="hljs-attribute">bottom</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">right</span>: <span class="hljs-number">0</span>;
}
<span class="hljs-selector-class">.truncate-overflow</span><span class="hljs-selector-pseudo">::after</span> {
  <span class="hljs-attribute">content</span>: <span class="hljs-string">""</span>;
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">right</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">1rem</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">1rem</span>;
  <span class="hljs-attribute">background</span>: white;
}
</code></pre>
<p>Below is an image showing the multi-line text truncation with pure CSS:</p>
<p><img src="https://i.imgur.com/vYBjwVF.png" alt="Image" width="898" height="629" loading="lazy"></p>
<h3 id="heading-how-to-use-javascript-to-truncate-multi-line-text">How to Use JavaScript to Truncate Multi-line Text</h3>
<p>JavaScript also has the ability to truncate text. Below is an example of how to implement JavaScript to truncate text depending on a certain number of characters.</p>
<p>Let's have a look at how we can do this with JavaScript. To begin, let's define a function named <code>truncate</code> and pass the words to be truncated as parameters. We'll additionally provide a <code>maximum-length parameter</code>.</p>
<p>Here's an example of HTML code for truncating text with JavaScript:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"Truncate.js"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Text Truncate Example Header<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"text"</span>&gt;</span>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in sapien
      eu velit eleifend ullamcorper eget vitae nulla. Aenean euismod purus sed
      neque dictum, nec lobortis ante faucibus.
    <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"truncateText()"</span>&gt;</span>Truncate Text<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre>
<p>Below is an image output of pure HTML code before adding the JavaScript function and the CSS border function:</p>
<p><img src="https://i.imgur.com/KK4868S.png" alt="Image" width="1854" height="377" loading="lazy"></p>
<p>Here's the JavaScript code:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">truncateText</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">var</span> text = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"text"</span>).innerHTML;
  <span class="hljs-keyword">var</span> truncated = text.substring(<span class="hljs-number">0</span>, <span class="hljs-number">50</span>) + <span class="hljs-string">"..."</span>;
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"text"</span>).innerHTML = truncated;
}
</code></pre>
<p>And here's the CSS code:</p>
<pre><code class="lang-css"><span class="hljs-selector-id">#text</span> {
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid black;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
}
</code></pre>
<p>The HTML code in the example contains a paragraph <code>element</code> with the <code>ID</code> "text" that contains some sample text. The <code>truncateText</code> function in the JavaScript code retrieves the text from the paragraph element. </p>
<p>With these modifications, when the "Truncate Text" button is clicked, the text will be truncated to 50 characters and a border will be added to the truncated text. It also inserts an <code>ellipsis</code> at the end. </p>
<p>The function is invoked when the user hits the "Truncate Text" button, and the text in the paragraph element is replaced with the truncated content.</p>
<p>Below is an image output after adding both CSS and JavaScript to the HTML code above:</p>
<p><img src="https://i.imgur.com/xwijXlB.png" alt="Image" width="1857" height="413" loading="lazy"></p>
<p>Below is an illustrative GIF to show the output:</p>
<p><img src="https://i.imgur.com/tkF6DQi.gif" alt="Image" width="1899" height="916" loading="lazy"></p>
<p>This example demonstrates a fundamental approach to truncating text using JavaScript.</p>
<h2 id="heading-how-to-add-an-element-after-the-ellipsis">How to Add an Element After the Ellipsis</h2>
<p>The <code>Ellipsis</code> (...) is used to represent a text truncation, indicating that some content has been omitted from the display. The text-overflow property is used to specify what happens to the text that overflows the container.</p>
<p>Here's an example with the HTML code below:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"parent-box box"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"child-box"</span>&gt;</span>
    You are learning text truncation with javascript, which is done with these
    three steps
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>Here's the image output of the HTML code before introducing the ellipsis:</p>
<p><img src="https://i.imgur.com/HD5eTPz.png" alt="Image" width="890" height="268" loading="lazy"></p>
<p>We added the <code>:after pseudo-element</code> to the parent <code>element.box</code> to accomplish this. The <code>nest div</code> is then given the <code>class.child-box</code> and an <code>inline-block</code> display. This allows the <code>.parent-box pseudo-element</code> to appear after the width of the <code>.child-box</code>.</p>
<p>The <code>hidden overflow</code> is triggered if the defined <code>max-width</code> is exceeded. If there is a <code>text-overflow</code>, we can have the <code>ellipsis</code> and the element of the <code>.parent-box</code>.<br>Below is the CSS code to add an <code>Element</code> after the <code>Ellipsis</code>:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.box</span> {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-built_in">rgb</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>);
  <span class="hljs-attribute">width</span>: <span class="hljs-number">200px</span>;
  <span class="hljs-attribute">overflow</span>: hidden;
  <span class="hljs-attribute">text-overflow</span>: ellipsis;
  <span class="hljs-attribute">white-space</span>: nowrap;
  <span class="hljs-attribute">position</span>: relative; <span class="hljs-comment">/* Add this to make sure the ::after element is positioned relative to the parent */</span>
}

<span class="hljs-selector-class">.child-box</span><span class="hljs-selector-pseudo">::after</span> {
  <span class="hljs-attribute">content</span>: <span class="hljs-string">"+"</span>; <span class="hljs-comment">/* Change the content to the desired element */</span>
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">right</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">bottom</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-class">.no-max-width</span> {
  <span class="hljs-attribute">max-width</span>: none;
}
</code></pre>
<p>Here's the output after introducing the above code:</p>
<p><img src="https://i.imgur.com/gfYrZP4.png" alt="Image" width="892" height="290" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>We have looked at how to truncate text in CSS using a variety of CSS and JavaScript approaches in this tutorial. </p>
<p>We also looked at how to add an <code>element</code> after the <code>ellipsis</code>, which can be tricky in some circumstances.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Temperature Converter Calculator with Python ]]>
                </title>
                <description>
                    <![CDATA[ Temperature conversion is a fundamental topic in physics and engineering. It's also important in many disciplines for research and various applications. For example, temperature measurements are critical for effective data analysis and interpretation... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/temperature-converter-calculator-with-python/</link>
                <guid isPermaLink="false">66bae7e4dcc16de1b125c081</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ David Fagbuyiro ]]>
                </dc:creator>
                <pubDate>Mon, 13 Mar 2023 23:00:14 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/03/pexels-maksim-goncharenok-5995230.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Temperature conversion is a fundamental topic in physics and engineering. It's also important in many disciplines for research and various applications.</p>
<p>For example, temperature measurements are critical for effective data analysis and interpretation in everything from weather forecasting to material research. As a result, having an effective and easy-to-use temperature converter calculator is critical for both professionals and students. </p>
<p>In this tutorial, we'll look at how to create a temperature converter calculator in Python. Python is a popular programming language with a large community and many helpful libraries, making it an excellent choice for creating a temperature converter calculator.</p>
<p>You will learn how to make a powerful and effective temperature conversion calculator that can convert between multiple temperature scales such as Celsius, Fahrenheit, and Kelvin by following the step-by-step directions in this tutorial.</p>
<h3 id="heading-requirements">Requirements:</h3>
<p>To follow along with this tutorial, you will need to have Python installed on your computer. You can download the latest version of Python from the official website.</p>
<p>We will also be using the Python standard library, which should be included with your Python installation.</p>
<h2 id="heading-how-to-build-the-temperature-converter-calculator">How to Build the Temperature Converter Calculator</h2>
<p>The first step in creating our temperature converter calculator is to define the temperature scales that we want to convert between. </p>
<p>In this example, we will be converting between Celsius, Fahrenheit, and Kelvin. We will create a dictionary that maps each temperature scale to a unique identifier.</p>
<pre><code class="lang-python">TEMPERATURE_SCALES = {
    <span class="hljs-string">'Celsius'</span>: <span class="hljs-string">'C'</span>,
    <span class="hljs-string">'Fahrenheit'</span>: <span class="hljs-string">'F'</span>,
    <span class="hljs-string">'Kelvin'</span>: <span class="hljs-string">'K'</span>
}
</code></pre>
<p>Next, we will define a function to convert a temperature from one scale to another. This function will take as input the temperature value, the input temperature scale, and the output temperature scale. It will then use the appropriate conversion formula to convert the temperature to the desired scale.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">convert_temperature</span>(<span class="hljs-params">value, input_scale, output_scale</span>):</span>
    <span class="hljs-keyword">if</span> input_scale == <span class="hljs-string">'C'</span>:
        <span class="hljs-keyword">if</span> output_scale == <span class="hljs-string">'F'</span>:
            <span class="hljs-keyword">return</span> value * <span class="hljs-number">1.8</span> + <span class="hljs-number">32</span>
        <span class="hljs-keyword">elif</span> output_scale == <span class="hljs-string">'K'</span>:
            <span class="hljs-keyword">return</span> value + <span class="hljs-number">273.15</span>
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> value
    <span class="hljs-keyword">elif</span> input_scale == <span class="hljs-string">'F'</span>:
        <span class="hljs-keyword">if</span> output_scale == <span class="hljs-string">'C'</span>:
            <span class="hljs-keyword">return</span> (value - <span class="hljs-number">32</span>) / <span class="hljs-number">1.8</span>
        <span class="hljs-keyword">elif</span> output_scale == <span class="hljs-string">'K'</span>:
            <span class="hljs-keyword">return</span> (value + <span class="hljs-number">459.67</span>) * <span class="hljs-number">5</span> / <span class="hljs-number">9</span>
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> value
    <span class="hljs-keyword">elif</span> input_scale == <span class="hljs-string">'K'</span>:
        <span class="hljs-keyword">if</span> output_scale == <span class="hljs-string">'C'</span>:
            <span class="hljs-keyword">return</span> value - <span class="hljs-number">273.15</span>
        <span class="hljs-keyword">elif</span> output_scale == <span class="hljs-string">'F'</span>:
            <span class="hljs-keyword">return</span> value * <span class="hljs-number">9</span> / <span class="hljs-number">5</span> - <span class="hljs-number">459.67</span>
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> value
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> value
</code></pre>
<p>Now that we have our conversion function, we can create the main program. We will use a while loop to repeatedly prompt the user for input until they choose to quit. </p>
<p>For each conversion, we will prompt the user for the input temperature value, the input temperature scale, and the output temperature scale. We will then call our conversion function to convert the temperature and print the result.</p>
<pre><code class="lang-python"><span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:
    <span class="hljs-comment"># Prompt the user for input</span>
    print(<span class="hljs-string">'Enter the input temperature value:'</span>)
    value = float(input())
    print(<span class="hljs-string">'Enter the input temperature scale (C, F, or K):'</span>)
    input_scale = input().upper()
    print(<span class="hljs-string">'Enter the output temperature scale (C, F, or K):'</span>)
    output_scale = input().upper()

    <span class="hljs-comment"># Convert the temperature and print the result</span>
    result = convert_temperature(value, input_scale, output_scale)
    print(<span class="hljs-string">f'<span class="hljs-subst">{value}</span> <span class="hljs-subst">{input_scale}</span> = <span class="hljs-subst">{result}</span> <span class="hljs-subst">{output_scale}</span>'</span>)

    <span class="hljs-comment"># Prompt the user to continue or quit</span>
    print(<span class="hljs-string">'Enter q to quit, or any other key to continue:'</span>)
    choice = input()
    <span class="hljs-keyword">if</span> choice.lower() == <span class="hljs-string">'q'</span>:
        <span class="hljs-keyword">break</span>
</code></pre>
<p>When you run the program, you will see the following output:</p>
<pre><code class="lang-python">Enter the input temperature value:
</code></pre>
<p>You can then enter the input temperature value, the input temperature scale (C, F, or K), and the output temperature scale (C, F, or K). The program will then convert the temperature and print the result. You can continue to convert temperatures until you choose to quit by entering 'q'.</p>
<p>This is the output of the above code:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/03/image-133.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, we have demonstrated how to create a temperature converter calculator using Python. With just a few lines of code, we can build a robust and efficient temperature converter that can convert between Celsius, Fahrenheit, and Kelvin.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Why You Should Use Java for Backend Development ]]>
                </title>
                <description>
                    <![CDATA[ Java is a well-established and widely-used programming language. You can use it to create desktop and mobile applications, for massive data processing, for backend development, to program embedded devices, and more. Oracle, the company that owns Java... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/java-for-backend-web-development/</link>
                <guid isPermaLink="false">66bae7da02a6638884966f15</guid>
                
                    <category>
                        <![CDATA[ backend ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Backend Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Java ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ David Fagbuyiro ]]>
                </dc:creator>
                <pubDate>Thu, 09 Feb 2023 21:54:09 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/pexels-realtoughcandycom-11035547.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Java is a well-established and widely-used programming language. You can use it to create desktop and mobile applications, for massive data processing, for backend development, to program embedded devices, and more.</p>
<p>Oracle, the company that owns Java, claims that Java is used on 3 billion devices worldwide, making it one of the most popular programming languages.</p>
<p>In this article, we will discuss the history of Java, reasons to use Java for backend development, the top tech companies that already use Java for this purpose, and the top Java frameworks for backend development.</p>
<p>We will also explore the benefits and drawbacks of using Java for backend development.</p>
<h2 id="heading-history-of-the-java-programming-language">History of the Java Programming Language</h2>
<p>Java was launched in 1991 when James Gosling and his team at Sun Microsystems began development on the language.</p>
<p>Soon after, the team shifted its focus to developing the language for the newest specialized market, the World Wide Web.</p>
<p>Java was offered to the public in 1995 for usage in various applications ranging from the internet to computer programming. According to <a target="_blank" href="https://learntocodewith.me/">learncodewith.me</a>, Java is one of the top three programming languages in the world for backend development. It's also ranked first among the top ten programming languages for 2022.</p>
<h2 id="heading-why-you-should-use-java-for-backend-infrastructure">Why You Should Use Java for Backend Infrastructure</h2>
<p>The following are some benefits of using the Java programming language for backend development.</p>
<h3 id="heading-scalability-and-robustness">Scalability and Robustness</h3>
<p>Java has a robust type-checking mechanism that makes it durable. The Java virtual machine (JVM) enables dynamic linking and a secure environment, allowing Java to run anywhere.</p>
<p>Java's automatic memory management and disposal collection make it highly scalable and these features help speed up web application development.</p>
<h3 id="heading-open-source-library">Open Source Library</h3>
<p>The vast majority of Java libraries are free and open-source, with expert support. The use of such libraries speeds up the back-end programming of web projects dramatically.</p>
<p>There are many Java libraries for various uses, including logging, JSON parsing, unit testing, XML and HTML parsing, messaging, PDF and Excel reading, cryptography, and many others.</p>
<h3 id="heading-diversity">Diversity</h3>
<p>Java has long been the dominant programming language for developing Web applications, Android applications, and software tools such as Eclipse, IntelliJ IDEA, NetBeans IDE, and others.</p>
<p>Java's use cases have now grown to include data science applications, machine learning applications, and even IoT applications.</p>
<h3 id="heading-simplicity">Simplicity</h3>
<p>Java syntax is relatively simple, easy to develop, learn, maintain, and understand, and the code is easily debuggable.</p>
<p>Java is also less complex than languages such as C and C++ because many of these languages' sophisticated features, such as explicit pointers, storage classes, operator overloading, and many others, have been eliminated from Java.</p>
<h3 id="heading-security">Security</h3>
<p>Java reduces security concerns and dangers by reducing the use of explicit pointers. A pointer is a value that keeps the memory address of another value and can be exploited to gain unwanted memory access. This issue is solved by removing the concept of pointers.</p>
<h3 id="heading-platform-independency">Platform Independency</h3>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/image-51.png" alt="image chart from techavidan" width="600" height="400" loading="lazy"></p>
<p><em>image from techavidan</em></p>
<p>Java is platform-independent, which means it's a "Write Once, Run Anywhere" (WORA) language.</p>
<p>The compiled code (the byte code of Java) is platform-independent and can run on any machine, irrespective of the operating system. We can run this code on any machine that supports the Java Virtual Machine (JVM), as shown in the figure above.</p>
<h3 id="heading-multithreading-support">Multithreading Support</h3>
<p>Java is a multithreaded language, which means that multiple threads can run at the same time.</p>
<p>A thread is a process's smallest unit. Multithreading allows us to maximize CPU use. Multiple threads share the same memory space, increasing the application's efficiency and performance.</p>
<h2 id="heading-drawbacks-of-the-java-programming-language">Drawbacks of the Java Programming Language</h2>
<h3 id="heading-lack-of-backup-facility">Lack of Backup Facility</h3>
<p>Java is primarily concerned with storage and does not prioritize data backup. This is a huge drawback, and it is losing user interest and ratings as a result.</p>
<h3 id="heading-code-complexity">Code Complexity</h3>
<p>Java code is extensive, which means it contains many words and long, complex sentences that are hard to read and comprehend. This reduces the code's readability.</p>
<p>Java strives to be more manageable, but it must settle for unnecessarily complex programs and detailed explanations for each thing.</p>
<h3 id="heading-speed-and-performance-level">Speed and Performance Level</h3>
<p>Java consumes a large amount of memory and is substantially slower than native languages such as C or C++. This is partly because each bit of code must be interpreted into machine-level code. This slow performance is caused by the JVM's additional level of compilation and abstraction.</p>
<h3 id="heading-memory-capacity">Memory Capacity</h3>
<p>When compared to other languages such as C and C++, Java uses a significant amount of memory. Memory efficiency and system performance may suffer during cleanup execution.</p>
<h2 id="heading-top-companies-that-use-java">Top Companies That Use Java</h2>
<p>To begin with the list, according to statistics based on <a target="_blank" href="https://codegym.cc/groups/posts/771-is-java-still-relevant-what-big-companies-use-it">Codegym</a>, 10,130 companies are said to employ Java in their IT stacks. Not unexpectedly, the United States leads among enterprises that use Java, accounting for more than 60% of Java clients (about 64,000 businesses).</p>
<p><strong>Linkedin:</strong> This application is primarily written in Java, with some C++ parts thrown in for good measure. Java is excellent for LinkedIn's search and analytics. More specifically, it addresses scale concerns, allowing the server to function faster while using fewer resources.</p>
<p><strong>Uber:</strong> Uber is the next big Java-based firm. The organization works with a large amount of real-time data, tracking drivers and incoming ride requests. As a result, Uber should be able to sift the data and match users swiftly. That's where Java comes in, handling requests and transmitting data as quickly as possible.</p>
<p><strong>Microsoft:</strong> Even though Java does not control Windows or anything similar, Microsoft uses it for various purposes. For example, it requires Java to construct its proprietary Edge web browser.</p>
<p><strong>NASA World Wind:</strong> NASA built the Word Wind app, which features a realistic 3D virtual globe and can display precise geographical data, largely thanks to Java (the program uses actual images from satellites to develop 3D representations of the planets). It's an open-source program that runs on practically any operating system because it's developed in Java.</p>
<p><strong>Netflix:</strong> Netflix, the same as PayPal, primarily uses Java for practically everything. And because Netflix is one of the best-known entertainment platforms in the world, there is a significant demand for Java expertise in this organization.</p>
<p>In addition to the tech titans mentioned earlier, Java is used by Airbnb, Google, eBay, Spotify, TripAdvisor, Intel, Pinterest, Groupon, Slack, Flipkart, and many more. Without a doubt, you can find Java practically anywhere.</p>
<h2 id="heading-differences-between-a-java-backend-and-a-nodejs-backend">Differences Between a Java Backend and a Node.js Backend</h2>
<p>Java and Node.js are both popular choices for building server-side applications. But they have some key differences that make each one better suited for different use cases.</p>
<p>In terms of strengths, Java has the advantage of being a statically-typed language, which provides better performance and stability. Java also provides better security features, such as the Java Virtual Machine (JVM), which has memory protection and isolation. Java also has a large number of libraries and tools available, which makes it easier to develop and maintain large-scale applications.</p>
<p>Node.js, on the other hand, has the advantage of being a dynamically-typed language, which makes it easier to learn and more flexible. Node.js is also well-suited for real-time applications, such as chat applications, thanks to its event-driven architecture. Node.js also has a large and active community, which provides a wealth of libraries and tools for various tasks.</p>
<p>In terms of weaknesses, Java can be slower to develop and more complex compared to Node.js, due to its strong typing and verbose syntax. Java also requires more memory compared to Node.js, which can make it less suitable for low-memory devices.</p>
<p>Node.js, on the other hand, has weaker type checking compared to Java, which can lead to unexpected errors. Node.js also has a single-threaded architecture, which means that it is not as well-suited for multi-threaded applications compared to Java.</p>
<p>In conclusion, both Java and Node.js have their own strengths and weaknesses for backend development. Java is well-suited for large-scale and complex applications, while Node.js is well-suited for real-time and scalable applications. The choice between the two ultimately depends on the specific requirements of the project.</p>
<h2 id="heading-top-java-frameworks-for-backend-development">Top Java Frameworks for Backend Development</h2>
<p>There are various Java backend frameworks, each with its own capabilities and features. Below are a few popular examples:</p>
<h3 id="heading-spring">Spring</h3>
<p>The Spring framework is a lightweight Java application framework and an inversion of a control container.</p>
<p>The framework's core functionality can be used by any Java application, although there are enhancements for constructing web applications on top of the Java EE framework.</p>
<h3 id="heading-hibernate">Hibernate</h3>
<p>Hibernate ORM is an object-relational mapping tool written in Java that provides a framework for translating a relational database into an object-oriented domain model.</p>
<h3 id="heading-play">Play</h3>
<p>The Play Framework combines productivity and performance, making it simple to create scalable Java and Scala online applications. Play is developer-friendly, with a "simply hit refresh" workflow and testing support built in.</p>
<p>Because of Play's stateless and non-blocking architecture, applications scale reliably. Play is ideal for current online and mobile apps because it is RESTful by default and includes asset compilers, JSON, and WebSocket support.</p>
<h3 id="heading-struts">Struts</h3>
<p>Apache Struts is a free and open-source web application framework used to create Java EE web applications. It uses and improves the Java Servlet API to encourage developers to use a model-view-controller paradigm. Craig McClanahan designed it and presented it to the Apache Nation.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this article, you learned about the Java programming language's history. You have also learned the key distinctions between Java and Node.js, clearing up any uncertainty.</p>
<p>Finally, you understand what Java is used for, the industries in which it can be found, and some of its distinguishing traits. You can now use it in your own web apps for seamless backend development.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Python Program to Download YouTube Videos ]]>
                </title>
                <description>
                    <![CDATA[ YouTube is a well-known internet video streaming service. There are millions of videos in categories such as education, entertainment, and travel.  You can quickly watch videos with a few mouse clicks, but downloading videos is difficult. But in a re... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/python-program-to-download-youtube-videos/</link>
                <guid isPermaLink="false">66bae7dd6d08bb9d26773834</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Script ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ David Fagbuyiro ]]>
                </dc:creator>
                <pubDate>Mon, 14 Nov 2022 15:56:27 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/11/pexels-christina-morillo-1181671.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>YouTube is a well-known internet video streaming service. There are millions of videos in categories such as education, entertainment, and travel. </p>
<p>You can quickly watch videos with a few mouse clicks, but downloading videos is difficult. But in a recent upgrade, YouTube now allows you to save videos in its download folder for offline viewing. Still, you are unable to save them locally.</p>
<p>In this tutorial, you will learn how to use Python code to download YouTube videos. As you may know, one of Python's great strengths is its huge number of modules and libraries. We will write the Python script using the popular pytube package.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Below are the basic requirements to proceed with this tutorial:</p>
<ul>
<li>Understanding of the Python Programming language</li>
<li>You must have Python 3+ installed on your computer</li>
<li>You must have installed the Python library Pytube</li>
<li>You should have a Python code editor such as Pycharm, Vscode, and so on.</li>
</ul>
<h2 id="heading-pytube-overview-and-installation">Pytube Overview and Installation</h2>
<p>Pytube is a small, dependency-free Python module for accessing videos from the internet.</p>
<p>The native library is not pytube – you must first install it to be able to use it. When you have pip, installation is simple.</p>
<p>To install Pytube using pip, you will need to open your command prompt CLI as an administrator and enter the following command:</p>
<pre><code>pip install pytube
</code></pre><p>The pytube library improves video downloads. Build the YouTube module's object by supplying the URL as a parameter. Then, obtain the video's proper extension and resolution. You can change the name of the file at your leisure – otherwise, the original name will be retained.</p>
<p>Now let's get to the main aspect of writing and implementing the code to download our favorite videos from YouTube.</p>
<pre><code><span class="hljs-keyword">from</span> pytube <span class="hljs-keyword">import</span> YouTube

def Download(link):
    youtubeObject = YouTube(link)
    youtubeObject = youtubeObject.streams.get_highest_resolution()
    <span class="hljs-attr">try</span>:
        youtubeObject.download()
    <span class="hljs-attr">except</span>:
        print(<span class="hljs-string">"An error has occurred"</span>)
    print(<span class="hljs-string">"Download is completed successfully"</span>)


link = input(<span class="hljs-string">"Enter the YouTube video URL: "</span>)
Download(link)
</code></pre><p>You use the <code>from pytube import YouTube</code> function to import the Python Pytube library before continuing with the other aspects. Then you define the function download link.</p>
<p>The <code>youtubeObject = youtubeObject.streams.get_highest_resolution()</code> command will automatically download the highest resolution available.<br>Then I implemented the Try and Except to return an error message if the download fails – else it will print out that the download is completed successfully.</p>
<p>The Link function will ask for the preferred YouTube video link to download, then immediately after you hit the enter button, the video downloading will commence.</p>
<h3 id="heading-the-output">The Output:</h3>
<p><img src="https://paper-attachments.dropboxusercontent.com/s_2A6E5F7B9EF3D136021C2A8815B8956B830A35B9A863E60136A6FD8F4C45E374_1666119447422_pytube.PNG" alt="Image" width="1920" height="1020" loading="lazy"></p>
<p>The video I downloaded was successful. You can see the video in the same Python folder where the file you are working on is located. But if you wish, you can then move the video to your preferred storage location. In my case the video name is "Ronaldo celebrates with Antony.mp4."</p>
<p>However, it would be preferable if you had a reliable internet connection. </p>
<p>This library also has numerous sophisticated features, but we have covered all of the major ones. You can learn more about the pytube library by visiting its official <a target="_blank" href="https://pytube.io/en/latest/">well-written documentation</a>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>We have successfully built a YouTube video downloader script of our own in Python. This helps you avoid the stress of looking for an external website or application to get your preferred video to your local storage. </p>
<p>It also saves you from having to expose your data on a third-party website or phishing link – all in the name of getting a video from YouTube to your local storage.</p>
<p>Hopefully after going through this article, you will understand the process required to download videos from YouTube without the need to download an external application or visit any website you don't trust.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Python Programming Tips to Help You Level Up Your Code ]]>
                </title>
                <description>
                    <![CDATA[ Python is one of the most popular programming languages out there today. Its simplicity and readability make it a favorite among many programmers. So in this tutorial, I'm sharing a few pointers and strategies to help you improve your Python programm... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/python-programming-tips/</link>
                <guid isPermaLink="false">66bae7e0bc530e1bd4143cb7</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ David Fagbuyiro ]]>
                </dc:creator>
                <pubDate>Tue, 11 Oct 2022 19:47:25 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/10/pexels-ankush-rathi-925067.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Python is one of the most popular programming languages out there today. Its simplicity and readability make it a favorite among many programmers.</p>
<p>So in this tutorial, I'm sharing a few pointers and strategies to help you improve your Python programming skills.</p>
<h2 id="heading-how-to-reverse-a-string-in-python">How to Reverse a String in Python</h2>
<p>In Python, there is no built-in method for reversing a string. The quickest (and perhaps easiest?) method is to employ a slice that moves backward, -1.</p>
<p>For instance, suppose we have a string "Freecodecamp" that we wish to reverse. Make a slice that goes backward from the end of the string.</p>
<p>In this case, the slice expression [::-1] indicates to begin at the end of the string and terminate at position 0, then move with step -1, negative one, which implies one step backward.</p>
<pre><code class="lang-python">
<span class="hljs-comment"># Create a variable name "a"</span>
a = <span class="hljs-string">"Freecodecamp"</span>
<span class="hljs-comment"># Then you assign freecodecamp to variable 'a'</span>
print(<span class="hljs-string">"Reverse is"</span>, a[::<span class="hljs-number">-1</span>])
</code></pre>
<p>The below output shows the backward version of the string "freecodecamp":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/10/fcc.PNG" alt="Image" width="600" height="400" loading="lazy">
<em>Reversing a string in Python</em></p>
<h2 id="heading-how-to-print-the-file-path-of-imported-modules">How to Print the File Path of Imported Modules</h2>
<p>Python offers a very simple method for getting the file paths of an imported module. This is useful if you need to quickly identify a file path while working on a project with several subdirectories, or if you're using scripts or applications that are typically accessible via the command line. </p>
<p>If you're in a similar scenario, you may use the following approach to get your module's precise file path:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> os
<span class="hljs-keyword">import</span> socket

print(os)
print(socket)
</code></pre>
<p>That's all. Use this approach to determine the precise location of module files in your code. The module's exact file location should then be printed for you. It will most likely look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/10/import-os.PNG" alt="Image" width="600" height="400" loading="lazy">
<em>output for printing file path</em></p>
<h2 id="heading-how-to-use-enums-in-python">How to Use Enums in Python</h2>
<p>An Enum is a collection of symbolic names that are linked to distinct values. They are comparable to global variables, but they have additional helpful capabilities such as repr(), grouping, type-safety, and a few more. As you can see, making an Enum is as easy as building a class that inherits from Enum.</p>
<p>Enum Properties:</p>
<ul>
<li>Enums can be shown as a string or as a representation.</li>
<li>You can use type to determine the type of an Enum ().</li>
<li>You use the "name" keyword to display the Enum member's name.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyName</span>:</span>
    Python, By, Davidking = range(<span class="hljs-number">3</span>)

print(MyName.Python)
print(MyName.By)
print(MyName.Davidking)
</code></pre>
<p>The output will print out the result according to their number as arranged on the list:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/10/enum.PNG" alt="Image" width="600" height="400" loading="lazy">
<em>Enum in Python</em></p>
<h2 id="heading-how-to-swap-variable-values-in-python">How to Swap Variable Values in Python</h2>
<p>Swapping two variables means switching the values of the variables. In most cases, you do this using the data in memory.</p>
<p>Swapping is as simple as it gets. You can use this step to swap two objects in Python:</p>
<pre><code class="lang-python">a = <span class="hljs-number">1</span>
b = <span class="hljs-number">2</span>

print(<span class="hljs-string">'Before Swapping'</span>)
print(a, b)

a, b = b, a
print(<span class="hljs-string">'After Swapping'</span>)
print(a, b)
</code></pre>
<p>The result will return the new interchanged position/number for the two variables "a, b":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/10/swap.PNG" alt="Image" width="600" height="400" loading="lazy">
<em>swapping in Python Fcc</em></p>
<h2 id="heading-how-to-find-the-most-frequent-value-in-a-list-in-python">How to Find the Most Frequent Value in a List in Python</h2>
<p>You can use this process to sort out the most repeated value from a list. Imagine a game where numbers were given to people randomly and the group with the most frequent value wins the game. You can use this method to find out the game-winner without the need to count manually. </p>
<p>Now let's see how it goes:</p>
<pre><code class="lang-python">test = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>]
print(max(set(test), key = test.count))
</code></pre>
<p>Now the output here will tell us the winner of the game without having to count manually. The output here is "2" when you run the code above.</p>
<h2 id="heading-how-to-check-the-memory-usage-of-an-object-in-python">How to Check the Memory Usage of an Object in Python</h2>
<p>With this simple yet powerful Python method, you can calculate how much memory your Python objects consume.</p>
<p>Here is an example below:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> sys
x = <span class="hljs-number">1</span>
print(sys.getsizeof(x))
</code></pre>
<p>The result of variable X here is "28".</p>
<h2 id="heading-how-to-create-a-file-server-in-python">How to Create a File Server in Python</h2>
<p>Servers are pieces of computer software or hardware that handle requests and provide data to clients across a network. There are several sorts of servers, the most popular of which are web servers, database servers, application servers, and transaction servers.</p>
<p>Python's SimpleHTTPServer module is a handy and simple utility that developers may use for a variety of purposes, the most common of which is to serve files from a directory.</p>
<p>It removes the time-consuming procedure of installing and configuring available cross-platform web servers.</p>
<p>Did you want to build a file server in Python? You can accomplish this by using the simple line of code below:</p>
<pre><code class="lang-python">python -m SimpleHTTPServer 
<span class="hljs-comment"># The default port is 8080</span>
</code></pre>
<h2 id="heading-how-to-check-your-python-version-from-idle">How to Check Your Python Version from IDLE</h2>
<p>You might be curious about the Python version you're using. Well, you can check the installed Python version on your PC just by writing a few lines of code:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> sys
print(<span class="hljs-string">"My Python version Number: {}"</span>.format(sys.version))
</code></pre>
<p>Output:</p>
<pre><code>My Python version <span class="hljs-built_in">Number</span>: <span class="hljs-number">3.10</span><span class="hljs-number">.7</span> (tags/v3<span class="hljs-number">.10</span><span class="hljs-number">.7</span>:<span class="hljs-number">6</span>cc6b13, Sep  <span class="hljs-number">5</span> <span class="hljs-number">2022</span>, <span class="hljs-number">14</span>:<span class="hljs-number">08</span>:<span class="hljs-number">36</span>) [MSC v<span class="hljs-number">.1933</span> <span class="hljs-number">64</span> bit (AMD64)]
</code></pre><p>Here you go, it will print out the version you are using.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Hopefully, after going through this set of python tips and tricks you find it useful and interesting, Thanks for reading and I wish you good luck in your coding career.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ File Handling in Python – How to Create, Read, and Write to a File ]]>
                </title>
                <description>
                    <![CDATA[ In this tutorial, you will learn how to open a file, write to the file, and close it. You will also learn how to read from the file using Python.  By the end of this tutorial, you should know the basics of how to use files in Python. File ]]>
                </description>
                <link>https://www.freecodecamp.org/news/file-handling-in-python/</link>
                <guid isPermaLink="false">66bae7d26d08bb9d26773832</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ David Fagbuyiro ]]>
                </dc:creator>
                <pubDate>Fri, 26 Aug 2022 15:42:16 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/08/Author-Share-ImageFREE--4-.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this tutorial, you will learn how to open a file, write to the file, and close it. You will also learn how to read from the file using Python. </p>
<p>By the end of this tutorial, you should know the basics of how to use files in Python.</p>
<h2 id="heading-file-handling-in-python"><strong>File Handling in Python</strong></h2>
<p>File handling is an important activity in every web app. The types of activities that you can perform on the opened file are controlled by Access Modes. These describe how the file will be used after it has been opened. </p>
<p>These modes also specify where the file handle should be located within the file. Similar to a pointer, a file handle indicates where data should be read or put into the file. </p>
<p>In Python, there are six methods or access modes, which are:</p>
<ol>
<li><strong>Read Only ('r’):</strong> This mode opens the text files for reading only. The start of the file is where the handle is located. It raises the I/O error if the file does not exist. This is the default mode for opening files as well.</li>
<li><strong>Read and Write ('r+’):</strong> This method opens the file for both reading and writing. The start of the file is where the handle is located. If the file does not exist, an I/O error gets raised.</li>
<li><strong>Write Only ('w’):</strong> This mode opens the file for writing only. The data in existing files are modified and overwritten. The start of the file is where the handle is located. If the file does not already exist in the folder, a new one gets created.</li>
<li><strong>Write and Read ('w+’)</strong>: This mode opens the file for both reading and writing. The text is overwritten and deleted from an existing file. The start of the file is where the handle is located.</li>
<li><strong>Append Only ('a’)</strong>: This mode allows the file to be opened for writing. If the file doesn't yet exist, a new one gets created. The handle is set at the end of the file. The newly written data will be added at the end, following the previously written data.</li>
<li><strong>Append and Read (‘a+’):</strong> Using this method, you can read and write in the file. If the file doesn't already exist, one gets created. The handle is set at the end of the file. The newly written text will be added at the end, following the previously written data.</li>
</ol>
<p>Below is the code required to create, write to, and read text files using the Python file handling methods or access modes.</p>
<h2 id="heading-how-to-create-files-in-python">How to Create Files in Python</h2>
<p>In Python, you use the <code>open()</code> function with one of the following options – "x" or "w" – to create a new file:</p>
<ul>
<li><strong>"x" – Create</strong>: this command will create a new file if and only if there is no file already in existence with that name or else it will return an error.</li>
</ul>
<p>Example of creating a file in Python using the "x" command:</p>
<pre><code class="lang-python"><span class="hljs-comment">#creating a text file with the command function "x"</span>

f = open(<span class="hljs-string">"myfile.txt"</span>, <span class="hljs-string">"x"</span>)
</code></pre>
<p>We've now created a new empty text file! But if you retry the code above – for example, if you try to create a new file with the same name as you used above (if you want to reuse the filename above) you will get an error notifying you that the file already exists. It'll look like the image below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/08/idle-shell.png" alt="IDLE Shell showing an error that myfile.txt does not exist" width="600" height="400" loading="lazy"></p>
<ul>
<li><strong>"w" – Write</strong>: this command will create a new text file whether or not there is a file in the memory with the new specified name. It does not return an error if it finds an existing file with the same name – instead it will overwrite the existing file.</li>
</ul>
<p>Example of how to create a file with the "w" command:</p>
<pre><code class="lang-python"><span class="hljs-comment">#creating a text file with the command function "w"</span>

f = open(<span class="hljs-string">"myfile.txt"</span>, <span class="hljs-string">"w"</span>)

<span class="hljs-comment">#This "w" command can also be used create a new file but unlike the the "x" command the "w" command will overwrite any existing file found with the same file name.</span>
</code></pre>
<p>With the code above, whether the file exists or the file doesn't exist in the memory, you can still go ahead and use that code. Just keep in mind that it will overwrite the file if it finds an existing file with the same name.</p>
<h2 id="heading-how-to-write-to-a-file-in-python">How to Write to a File in Python</h2>
<p>There are two methods of writing to a file in Python, which are:</p>
<h3 id="heading-the-write-method">The <code>write()</code> method:</h3>
<p>This function inserts the string into the text file on a single line.</p>
<p>Based on the file we have created above, the below line of code will insert the string into the created text file, which is "myfile.txt.”</p>
<pre><code class="lang-python">
file.write(<span class="hljs-string">"Hello There\n"</span>)
</code></pre>
<h3 id="heading-the-writelines-method">The <code>writelines()</code> method:</h3>
<p>This function inserts multiple strings at the same time. A list of string elements is created, and each string is then added to the text file. </p>
<p>Using the previously created file above, the below line of code will insert the string into the created text file, which is "myfile.txt.”</p>
<pre><code class="lang-python">f.writelines([<span class="hljs-string">"Hello World "</span>, <span class="hljs-string">"You are welcome to Fcc\n"</span>])
</code></pre>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-comment">#This program shows how to write data in a text file.</span>

file = open(<span class="hljs-string">"myfile.txt"</span>,<span class="hljs-string">"w"</span>)
L = [<span class="hljs-string">"This is Lagos \n"</span>,<span class="hljs-string">"This is Python \n"</span>,<span class="hljs-string">"This is Fcc \n"</span>]

<span class="hljs-comment"># i assigned ["This is Lagos \n","This is Python \n","This is Fcc \n"] to #variable L, you can use any letter or word of your choice.</span>
<span class="hljs-comment"># Variable are containers in which values can be stored.</span>
<span class="hljs-comment"># The \n is placed to indicate the end of the line.</span>

file.write(<span class="hljs-string">"Hello There \n"</span>)
file.writelines(L)
file.close()

<span class="hljs-comment"># Use the close() to change file access modes</span>
</code></pre>
<h2 id="heading-how-to-read-from-a-text-file-in-python">How to Read From a Text File in Python</h2>
<p>There are three methods of reading data from a text file in Python. They are:</p>
<h3 id="heading-the-read-method">The <code>read()</code> method:</h3>
<p>This function returns the bytes read as a string. If no n is specified, it then reads the entire file.</p>
<p>Example:</p>
<pre><code class="lang-python">f = open(<span class="hljs-string">"myfiles.txt"</span>, <span class="hljs-string">"r"</span>)
<span class="hljs-comment">#('r’) opens the text files for reading only</span>
print(f.read())
<span class="hljs-comment">#The "f.read" prints out the data in the text file in the shell when run.</span>
</code></pre>
<h3 id="heading-the-readline-method">The readline() method:</h3>
<p>This function reads a line from a file and returns it as a string. It reads at most n bytes for the specified n. But even if n is greater than the length of the line, it does not read more than one line.</p>
<pre><code class="lang-python">f = open(<span class="hljs-string">"myfiles.txt"</span>, <span class="hljs-string">"r"</span>)
print(f.readline())
</code></pre>
<h3 id="heading-the-readlines-method">The <code>readlines()</code> method:</h3>
<p>This function reads all of the lines and returns them as string elements in a list, one for each line.</p>
<p>You can read the first two lines by calling <code>readline()</code> twice, reading the first two lines of the file:</p>
<pre><code class="lang-python">f = open(<span class="hljs-string">"myfiles.txt"</span>, <span class="hljs-string">"r"</span>)
print(f.readline())
print(f.readline())
</code></pre>
<h2 id="heading-how-to-close-a-text-file-in-python">How to Close a Text File in Python</h2>
<p>It is good practice to always close the file when you are done with it.</p>
<h3 id="heading-example-of-closing-a-text-file">Example of closing a text file:</h3>
<p>This function closes the text file when you are done modifying it:</p>
<pre><code class="lang-python">f = open(<span class="hljs-string">"myfiles.txt"</span>, <span class="hljs-string">"r"</span>)
print(f.readline())
f.close()
</code></pre>
<p>The close() function at the end of the code tells Python that well, I am done with this section of either creating or reading – it is just like saying End.</p>
<h3 id="heading-example">Example:</h3>
<p>The program below shows more examples of ways to read and write data in a text file. Each line of code has comments to help you understand what's going on:</p>
<pre><code class="lang-python"><span class="hljs-comment"># Program to show various ways to read and</span>
<span class="hljs-comment"># write data in a text file.</span>

file = open(<span class="hljs-string">"myfile.txt"</span>,<span class="hljs-string">"w"</span>)
L = [<span class="hljs-string">"This is Lagos \n"</span>,<span class="hljs-string">"This is Python \n"</span>,<span class="hljs-string">"This is Fcc \n"</span>]

<span class="hljs-comment">#i assigned ["This is Lagos \n","This is Python \n","This is Fcc \n"]</span>
<span class="hljs-comment">#to variable L</span>

<span class="hljs-comment">#The \n is placed to indicate End of Line</span>

file.write(<span class="hljs-string">"Hello There \n"</span>)
file.writelines(L)
file.close()
<span class="hljs-comment"># use the close() to change file access modes</span>



file = open(<span class="hljs-string">"myfile.txt"</span>,<span class="hljs-string">"r+"</span>) 
print(<span class="hljs-string">"Output of the Read function is "</span>)
print(file.read())
print()

<span class="hljs-comment"># The seek(n) takes the file handle to the nth</span>
<span class="hljs-comment"># byte from the start.</span>
file.seek(<span class="hljs-number">0</span>) 

print( <span class="hljs-string">"The output of the Readline function is "</span>)
print(file.readline()) 
print()

file.seek(<span class="hljs-number">0</span>)

<span class="hljs-comment"># To show difference between read and readline</span>

print(<span class="hljs-string">"Output of Read(12) function is "</span>) 
print(file.read(<span class="hljs-number">12</span>))
print()

file.seek(<span class="hljs-number">0</span>)

print(<span class="hljs-string">"Output of Readline(8) function is "</span>) 
print(file.readline(<span class="hljs-number">8</span>))

file.seek(<span class="hljs-number">0</span>)
<span class="hljs-comment"># readlines function</span>
print(<span class="hljs-string">"Output of Readlines function is "</span>) 
print(file.readlines()) 
print()
file.close()
</code></pre>
<p>This is the output of the above code when run in the shell. I assigned "This is Lagos",  "This is Python", and "This is Fcc" to "L" and then asked it to print using the ''file.read''  function. </p>
<p>The code above shows that the "readline()" function is returning the letter based on the number specified to it, while the "readlines()" function is returning every string assigned to "L" including the \n. That is, the "readlines()" function will print out all data in the file.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/08/output-file.png" alt="IDLE Shell showing the output of the program" width="600" height="400" loading="lazy"></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Hopefully, after going through this tutorial, you should understand what file handling is in Python. We also learned the modes/methods required to create, write, read, and close() a text file using some basic examples from Python.</p>
<p>Thanks for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Code Refactoring Best Practices – with Python Examples ]]>
                </title>
                <description>
                    <![CDATA[ In this article, I will discuss what code refactoring is. We'll go over why you need to refactor your code, the advantages of code refactoring, and some best practices for code refactoring. What is Code Refactoring? Refactoring means organizing your ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/best-practices-for-refactoring-code/</link>
                <guid isPermaLink="false">66bae7cf02a6638884966f13</guid>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ refactoring ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ David Fagbuyiro ]]>
                </dc:creator>
                <pubDate>Thu, 18 Aug 2022 15:15:29 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2022/08/Author-Share-ImageFREE--1-.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this article, I will discuss what code refactoring is. We'll go over why you need to refactor your code, the advantages of code refactoring, and some best practices for code refactoring.</p>
<h2 id="heading-what-is-code-refactoring">What is Code Refactoring?</h2>
<p>Refactoring means organizing your code without modifying its original functionality. Refactoring is the process of making small changes or adjustments to your code without affecting or changing how the code functions while in use.</p>
<h2 id="heading-why-do-you-need-to-refactor-your-code">Why Do You Need to Refactor Your Code?</h2>
<h3 id="heading-it-helps-improve-your-codeprogram">It Helps Improve Your Code/Program</h3>
<p>Refactoring helps you improve the design of your software because developing applications is a difficult task. As you add additional functions, the relevance of the program may be affected. Then, abstractions are no longer as pure as they once were. </p>
<p>You can improve things by refactoring your code regularly to demonstrate a better understanding of the program or code you're working with.</p>
<h3 id="heading-it-gives-you-a-better-understanding-of-the-code">It Gives You a Better Understanding of the Code</h3>
<p>If you refactor properly, your code will be more easily understandable afterwards. You can also make the code easier to understand by improving the design. </p>
<p>It is common knowledge that developers read code far more frequently than they write it. As a result, it's in your best interest to keep things as simple as possible, which considerably improves maintainability. Also, people who read it in the future will be grateful.</p>
<h3 id="heading-refactoring-broadens-your-knowledge">Refactoring Broadens Your Knowledge</h3>
<p>Finally, refactoring is a method of active learning. Even if you didn't write the code, refactoring it gives you a better grasp of what it does.</p>
<h2 id="heading-how-refactoring-helps-your-team">How Refactoring Helps Your Team</h2>
<p>Refactoring is a common activity for software development teams. If done correctly, refactoring your code makes it easier for your team members to understand your code and the changes you've made.</p>
<p>The refactoring stage is often difficult for teams to include in their process. But refactoring, in my opinion, is an essential component of working on any process. Adding comments for me entails leaving the code in a better state than when I found it. </p>
<p>Whatever you do, avoid creating a separation between technical value and business value (creating the feature). You can do this by keeping the code understandable.</p>
<h2 id="heading-refactoring-best-practices-with-python-examples">Refactoring Best Practices – with Python Examples</h2>
<p>If you want to get the most benefit from restructuring your code, you should keep the following in mind:</p>
<ul>
<li>Refactor only tested code. Without tests, refactoring code is, to put it bluntly, gambling. If you don't have tests that verify that the before and after are equivalent, you can't be certain you didn't break the code. No matter how disciplined you are, eventually, you'll introduce bugs.</li>
<li>Minimize refactoring. The modification is riskier the more drastic it is. Divide it into several more compact portions. You get flexibility and mitigation.</li>
<li>Take little, gradual steps. Check to see if everything is working after making a small code change. </li>
<li>Observe the guidelines. Don't begin adding functionality while refactoring is still being done. Refactoring has no impact on the system's output. Before moving on to the next challenge, complete this one.</li>
</ul>
<p>Below is an example of code refactoring using the <strong>Define function</strong> in Python to find the force (F) required to move an object of mass (m) with an acceleration (a) given by the formula “F = m x a”. </p>
<p>Because we are performing a repeated task, it's a good idea to use a function instead of using only the conditional statement. So when we need to in the future, we just have to call the function out for usage.</p>
<h3 id="heading-code-before-refactoring">Code Before Refactoring:</h3>
<table><colgroup></colgroup><tbody><tr><td><p><span>#Using the Force formula F=ma</span><span><br></span><span><br></span><span>while</span><span> </span><span>True</span><span>:</span><span><br></span><span>&nbsp; mass = int(input(</span><span>"Enter the mass value: "</span><span>))</span><span><br></span><span>&nbsp; </span><span>if</span><span> mass &gt; </span><span>0</span><span>:</span><span><br></span><span>&nbsp; &nbsp; </span><span>break</span><span><br></span><span>while</span><span> </span><span>True</span><span>:</span><span><br></span><span>&nbsp; acceleration = int(input(</span><span>"Enter the acceleration: "</span><span>))</span><span><br></span><span>&nbsp; </span><span>if</span><span> acceleration &gt; </span><span>0</span><span>:</span><span><br></span><span>&nbsp; &nbsp; </span><span>break</span><span><br></span><span>print(</span><span>"The Force is"</span><span>, mass * acceleration)</span></p></td></tr></tbody></table>

<p>Notice how the two blocks look nearly identical. What if we’re calculating the “Work done” by a person or machine? Most likely, we’d copy and paste the while loop, changing the variable name and input prompt. Now let's use a function to refactor this.</p>
<h3 id="heading-code-after-refactoring">Code After Refactoring:</h3>
<table><colgroup></colgroup><tbody><tr><td><p><span>#REFACTORED code</span><span><br></span><span><br></span><span>def</span><span> </span><span>input_positive_integer</span><span>(prompt)</span><span>:</span><span><br></span><span>&nbsp; </span><span>while</span><span> </span><span>True</span><span>:</span><span><br></span><span>&nbsp; &nbsp; input_value = int(input(prompt))</span><span><br></span><span>&nbsp; &nbsp; </span><span>if</span><span> input_value &gt; </span><span>0</span><span>:</span><span><br></span><span>&nbsp; &nbsp; &nbsp; </span><span>return</span><span> input_value</span><span><br></span><span>mass = input_positive_integer(</span><span>"Enter the mass: "</span><span>)</span><span><br></span><span>acceleration = input_positive_integer(</span><span>"Enter the acceleration: "</span><span>)</span><span><br></span><span>print(</span><span>"The Force is"</span><span>, mass * acceleration)</span><span><br><br></span></p></td></tr></tbody></table>

<p>Now that our code is simpler to understand, we can start to save some lines of code. What's more, in the future, we'll be able to effortlessly insert our function into another script by just calling out the function, saving us time.</p>
<p>Here are some other tips to keep in mind while refactoring your Python code:</p>
<h3 id="heading-use-list-comprehension-instead-of-a-for-loop">Use List Comprehension Instead of a For Loop</h3>
<p>One of the most well-known Python constructs is list comprehension. Compared to several basic for loops, list comprehension provides a cleaner, quicker solution.</p>
<p>The code will run more quickly, existing data won't be altered, and list comprehensions are simpler to understand at a glance once you become used to them. </p>
<p>Below is a simple expression of using list comprehension instead of a For Loop:</p>
<p>Initial code:</p>
<table><colgroup></colgroup><tbody><tr><td><p><span>#List Comprehension instead of For Loop</span><span><br></span><span><br></span><span>numbers = [1,2,3,4,5,6,7,8]</span><span><br></span><span>odd_numbers = []</span><span><br></span><span>for</span><span> item </span><span>in</span><span> numbers:</span><span><br></span><span>&nbsp; </span><span>if</span><span> item % </span><span>2</span><span> == </span><span>1</span><span>:</span><span><br></span><span>&nbsp; &nbsp; odd_numbers.append(item)</span><span><br></span><span>print(odd_numbers)</span><span><br></span><span><br></span><span>#Outcome will be [1,3,5,7]</span></p></td></tr></tbody></table>

<p>In the above example, we loop through a list of integers (numbers) to see if we should enter each one into the odd numbers list.</p>
<p>Refactored code:</p>
<table><colgroup></colgroup><tbody><tr><td><p><span>numbers = [</span><span>1</span><span>,</span><span>2</span><span>,</span><span>3</span><span>,</span><span>4</span><span>,</span><span>5</span><span>,</span><span>6</span><span>,</span><span>7</span><span>,</span><span>8</span><span>]</span><span><br></span><span>odd_numbers = [item </span><span>for</span><span> item </span><span>in</span><span> numbers </span><span>if</span><span> item % </span><span>2</span><span> == </span><span>1</span><span>]</span><span><br></span><span>print</span><span> (odd_numbers)</span></p></td></tr></tbody></table>

<p>In the above-refactored code, the list comprehension is an assignment into the odd numbers. Then we iterate over the "numbers" and set the return (the first item). Then, we can include an if-statement to filter.</p>
<h3 id="heading-use-def-functions-for-repetitive-tasks">Use Def Functions for Repetitive Tasks</h3>
<p>Defining functions for repetitive tasks is the first and perhaps most significant refactoring we can perform. </p>
<p>Although functions provide many advantages, from the standpoint of refactoring, we will shorten our code and increase readability. In addition, you only have to update the job once, in the declaration of the function (the example given below).</p>
<p>Initial code:</p>
<table><colgroup></colgroup><tbody><tr><td><p><span>#Using the Work done expression W=f<em>d</em></span><span><br></span><span><br></span><span>while</span><span> </span><span>True</span><span>:</span><span><br></span><span>&nbsp; force = int(input(</span><span>"Enter the force value:&nbsp; "</span><span>))</span><span><br></span><span>&nbsp; </span><span>if</span><span> force &gt; </span><span>0</span><span>:</span><span><br></span><span>&nbsp; &nbsp; </span><span>break</span><span><br></span><span>while</span><span> </span><span>True</span><span>:</span><span><br></span><span>&nbsp; distance = int(input(</span><span>"Enter the distance value:&nbsp; "</span><span>))</span><span><br></span><span>&nbsp; </span><span>if</span><span> distance &gt; </span><span>0</span><span>:</span><span><br></span><span>&nbsp; &nbsp; </span><span>break</span><span><br></span><span>print(</span><span>"The Workdone is"</span><span>, force  distance)</span></p></td></tr></tbody></table>

<p>Refactored code:</p>
<table><colgroup></colgroup><tbody><tr><td><p><span>#REFACTORED code</span><span><br></span><span><br></span><span>def</span><span> </span><span>input_positive_integer</span><span>(prompt)</span><span>:</span><span><br></span><span>&nbsp; </span><span>while</span><span> </span><span>True</span><span>:</span><span><br></span><span>&nbsp; &nbsp; input_value = int(input(prompt))</span><span><br></span><span>&nbsp; &nbsp; </span><span>if</span><span> input_value &gt; </span><span>0</span><span>:</span><span><br></span><span>&nbsp; &nbsp; &nbsp; </span><span>return</span><span> input_value</span><span><br></span><span>force = input_positive_integer(</span><span>"Enter the force value:&nbsp; "</span><span>)</span><span><br></span><span>distance = input_positive_integer(</span><span>"Enter the distance:&nbsp; "</span><span>)</span><span><br></span><span>print(</span><span>"The Workdone is"</span><span>, force * distance, </span><span>"J"</span><span>)</span><span><br><br></span></p></td></tr></tbody></table>

<h3 id="heading-use-ternary-operators-instead-of-if-else-statements">Use Ternary Operators Instead of if-else Statements:</h3>
<p>You can use an easy if-statement like this to find if an input integer is an "even" or "odd" number instead of an if-else statement:</p>
<table><colgroup></colgroup><tbody><tr><td><p><span># USING TERNARY OPERATOR</span><span><br></span><span><br></span><span>to_check = int(input(</span><span>"Enter a whole number :"</span><span>))</span><span><br></span><span>msg = </span><span>"Even"</span><span> </span><span>if</span><span> to_check%</span><span>2</span><span> == </span><span>0</span><span> </span><span>else</span><span> </span><span>"Odd"</span><span><br></span><span>print(</span><span>"The number you entered is"</span><span>, msg, </span><span>"number"</span><span>)</span><span><br></span><span><br></span><span><br></span><span># USING USUAL IF-ELSE</span><span><br></span><span><br></span><span>to_check = int(input(</span><span>"Enter a whole number :"</span><span>))</span><span><br></span><span>msg = </span><span>""</span><span><br></span><span>if</span><span>(to_check%</span><span>2</span><span> == </span><span>0</span><span>):</span><span><br></span><span>&nbsp; msg = </span><span>"Even"</span><span><br></span><span>else</span><span>:</span><span><br></span><span>&nbsp; msg = </span><span>"Odd"</span><span><br></span><span>print(</span><span>"The number you entered is"</span><span>, msg, </span><span>"number"</span><span>)</span><span><br></span><span><br><br></span></p></td></tr></tbody></table>

<p>The ternary operator is used in the code above to determine if an integer is even or odd.</p>
<p><code>msg</code> will be assigned <code>Even</code> if the condition (to_check% 2 == 0) is true. <code>msg</code> will also be assign <code>Odd</code> if the condition (to check% 2 == 0) is not true.</p>
<p>Note: you can't use the ternary operator to replace any code that uses an if-else expression. For example, if-else statements involving more than two cases.</p>
<h3 id="heading-use-the-enumerate-function-instead-of-range">Use the Enumerate Function Instead of Range():</h3>
<p>Python for loops can be complicated, especially if you are coming from another programming language. </p>
<p>Initially, you might have thought to make an index variable and use it to loop through your list. As a result, people often find and utilize the range() method. But it's not a best practice to use this method. Below is an example:</p>
<p>Code Before Refactoring:</p>
<table><colgroup></colgroup><tbody><tr><td><p><span>colors = [</span><span>"white"</span><span>, </span><span>"black"</span><span>, </span><span>"brown"</span><span>]</span><span><br></span><span>for</span><span> i </span><span>in</span><span> range(len(colors)):</span><span><br></span><span>&nbsp; print(i+</span><span>1</span><span>, colors[i])</span><span><br></span><span># 1 white</span><span><br></span><span># 2 black</span><span><br></span><span># 3 brown</span></p></td></tr></tbody></table>

<p>Using the index on the initial list makes it more difficult to understand and encourages poor habits like changing original values. Particularly in complex nested data structures. </p>
<p>Additionally, we need to manually change the index's values into "counting numbers."</p>
<p>Code After Refactoring:</p>
<table><colgroup></colgroup><tbody><tr><td><p><span>colors = [</span><span>"white"</span><span>, </span><span>"black"</span><span>, </span><span>"brown"</span><span>]</span><span><br></span><span>for</span><span> i, colors </span><span>in</span><span> enumerate(colors, </span><span>1</span><span>):</span><span><br></span><span>&nbsp; print(i, colors)</span><span><br></span><span>&nbsp; </span><span><br></span><span># 1 white</span><span><br></span><span># 2 black</span><span><br></span><span># 3 brown</span><span><br><br></span></p></td></tr></tbody></table>

<p>The enumerate() method returns the current counter value and each item in the iterable and requires two arguments: the iterable and the initial count value.</p>
<p>Note: It is a typical error to order the temporary count variable first in the for loop and the initial count value second in the function call.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Hopefully, you now understand what code refactoring is along with the importance and reasons for refactoring your code.</p>
<p>Based on the simple Python code above, you should understand that refactoring does not necessarily mean your code needs to be shorter after refactoring – but just somehow simpler and more understandable.  </p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
