<?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[ 100Days100Projects - 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[ 100Days100Projects - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Tue, 19 May 2026 10:29:31 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/100days100projects/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ The Ultimate Guide to Python: How to Go From Beginner to Pro ]]>
                </title>
                <description>
                    <![CDATA[ If you have an interest in Data Science, Web Development, Robotics, or IoT you must learn Python. Python has become the fastest-growing programming language due to its heavy usage and wide range of applications. For a beginner or a person from a non-... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/the-ultimate-guide-to-python-from-beginner-to-intermediate-to-pro/</link>
                <guid isPermaLink="false">66d460fb8812486a37369d60</guid>
                
                    <category>
                        <![CDATA[ 100Days100Projects ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python 3 ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Sharvin Shah ]]>
                </dc:creator>
                <pubDate>Fri, 01 May 2020 16:47:40 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/05/The-Ultimate-Guide-To-Python-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If you have an interest in Data Science, Web Development, Robotics, or IoT you must learn Python. Python has become the fastest-growing programming language due to its heavy usage and wide range of applications.</p>
<p>For a beginner or a person from a non-tech background, learning Python is a good choice. The syntax is like talking and writing plain English. For example, consider this syntax which shows its resemblance to the English language.</p>
<pre><code class="lang-python">print(<span class="hljs-string">"Hello folks"</span>)
</code></pre>
<p>We will use <code>Python3</code> in this tutorial as it is widely used. Most of Python's frameworks and libraries support this version.</p>
<blockquote>
<p><strong>Note:</strong> Any version above 3.5.2 supports most of the libraries and frameworks.</p>
</blockquote>
<h2 id="heading-index">Index:</h2>
<ol>
<li><p><a class="post-section-overview" href="#heading-introduction">Introduction</a></p>
</li>
<li><p><a class="post-section-overview" href="#installation-">Installation</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-python-shell">Python shell</a></p>
</li>
<li><p><a class="post-section-overview" href="#comment-">Comment</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-print">Print</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-indentation">Indentation</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-variables">Variables</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-operators">Operators</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conditional-statements">Conditional Statements</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-for-loops">For Loops</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-while-loops">While loops</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-user-input">User Input</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-typecasting">Typecasting</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-dictionaries">Dictionaries</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-lists">Lists</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-tuples">Tuples</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-sets">Sets</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-functions-and-arguments">Functions and Arguments</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-args">Args</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-keyword-arguments">keyword Arguments</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-default-argument">Default Arguments</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-kwargs">kwargs</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-scope">Scope</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-return-statement">Return Statement</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-lambda-expression">Lambda Expression</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-list-comprehension">List comprehension</a></p>
</li>
<li><p><a class="post-section-overview" href="#oops-concepts-">OOPS concepts</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-classes">Classes</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-methods">Methods</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-objects">Objects</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-constructor">Constructor</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-instance-attributes">Instance attribute</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-class-attributes">Class attributes</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-self">Self</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-inheritance">Inheritance</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-super">Super</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-multiple-inheritance">Multiple Inheritance</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-polymorphism">Polymorphism</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-encapsulation">Encapsulation</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-decorator">Decorators</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-exceptions">Exceptions</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-package-import">Package Import</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-json-handling">JSON Handling</a></p>
</li>
</ol>
<p><strong>Note:</strong> The beginning of this guide is geared towards beginners. If you have intermediate experience in Python, feel free to skip ahead using the links above.</p>
<h2 id="heading-introduction"><strong>Introduction</strong>:</h2>
<p>As per Github's <a target="_blank" href="https://octoverse.github.com/#top-languages">octoverse</a>, Python is the second most used language by developers in 2019.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/04/Screenshot-2020-04-29-at-6.53.10-PM.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Octoverse graph of how languages have evolved</em></p>
<p>Before learning any language, it's helpful to know how that language came into existence. Well, Python was developed by <a target="_blank" href="https://en.wikipedia.org/wiki/Guido_van_Rossum">Guido van Rossum</a>, a Dutch programmer, and was released in 1991.</p>
<p>Python is an Interpreted language. It uses the <a target="_blank" href="https://en.wikipedia.org/wiki/CPython">CPython</a> Interpreter to compile the Python code to byte code. For a beginner, you don't need to know much about CPython, but you must be aware of how Python works internally.</p>
<p>The philosophy behind Python is that code must be readable. It achieves this with the help of indentation. It supports many programming paradigms like Functional and Object Oriented programming. You will understand more about these as you read through the article.</p>
<p>The basic question that most beginners have in mind is what a language can do. Here are some of the use-cases of Python:</p>
<ul>
<li><p>Server-side development ( Django, Flask )</p>
</li>
<li><p>Data Science ( Pytorch, Tensor-flow )</p>
</li>
<li><p>Data analysis / Visualisation ( Matplotlib )</p>
</li>
<li><p>Scripting ( Beautiful Soup )</p>
</li>
<li><p>Embedded development</p>
</li>
</ul>
<blockquote>
<p><strong>Note:</strong> I do not endorse any of the above-mentioned libraries or frameworks in particular. They are popular and broadly used in their respective domains.</p>
</blockquote>
<h2 id="heading-installation">Installation</h2>
<p>The first step of learning any programming language is installing it. Python comes bundled with most operating systems nowadays. Use the following command in your terminal to check if Python is available:</p>
<pre><code class="lang-shell">python3 --version
</code></pre>
<p>You'll see the following output:</p>
<pre><code class="lang-shell">Python 3.7.0
</code></pre>
<p>Note that your version of Python might be different. If you have Python installed and the version is above 3.5.2 then you can skip this section.</p>
<p>For those who don't have Python installed, follow the steps below:</p>
<ul>
<li><p><a class="post-section-overview" href="#heading-windows-user">Windows User</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-mac-user">Mac User</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-linux-user">Linux User</a></p>
</li>
</ul>
<h3 id="heading-windows-user">Windows User:</h3>
<ul>
<li><p>Go to <a target="_blank" href="https://www.python.org/downloads/">Python's official website</a>.</p>
</li>
<li><p>Click on the download button ( Download Python 3.8.2 ) [ <strong>Note:</strong> The version may differ based on when you are reading this article ]</p>
</li>
<li><p>Go to the path where the package is downloaded and double-click the installer.</p>
</li>
<li><p>Check the box indicating to "Add Python 3.x to PATH" and then click on "Install Now".</p>
</li>
<li><p>Once done you'll get a prompt that "Setup was successful". Check again if python is configured properly using the above command.</p>
</li>
<li><p>To confirm if Python is installed and configured properly, use the command <code>python3 --version</code>.</p>
</li>
</ul>
<h3 id="heading-mac-user">Mac User:</h3>
<ul>
<li><p>First install <a target="_blank" href="https://apps.apple.com/in/app/xcode/id497799835?mt=12">xcode</a> from the app store.</p>
</li>
<li><p>If you want to install Xcode using the terminal then use the following command:</p>
</li>
</ul>
<pre><code class="lang-shell">xcode-select --install
</code></pre>
<ul>
<li>After that, we will use the brew package manager to install Python. To install and configure <a target="_blank" href="https://brew.sh/">brew</a>, use the following command:</li>
</ul>
<pre><code class="lang-shell">/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
</code></pre>
<ul>
<li>Once brew setup is done, use the following command to update any outdated packages:</li>
</ul>
<pre><code class="lang-shell">brew update
</code></pre>
<ul>
<li>Use the following command to install Python:</li>
</ul>
<pre><code class="lang-shell">brew install python3
</code></pre>
<ul>
<li>To confirm if Python is installed and configured properly, use the command <code>python3 --version</code>.</li>
</ul>
<h3 id="heading-linux-user">Linux User:</h3>
<ul>
<li>To install Python using <code>apt</code>, use the following command:</li>
</ul>
<pre><code class="lang-shell">sudo apt install python3
</code></pre>
<ul>
<li>To install the Python using <code>yum</code>, use the following command:</li>
</ul>
<pre><code class="lang-shell">sudo yum install python3
</code></pre>
<ul>
<li>To confirm if Python is installed and configured properly, use the command <code>python3 --version</code>.</li>
</ul>
<h2 id="heading-python-shell">Python shell:</h2>
<p>The shell is one of the most useful tools you'll come across. The Python shell gives us the power to quickly test any concept before integrating it into our application.</p>
<p>Go to the terminal or command line prompt. Enter <code>python3</code> command and you'll get the following output:</p>
<pre><code class="lang-shell">➜ python3.7
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt;
</code></pre>
<p>In this tutorial, we will learn some concepts with the help of the python3 shell which you can see above. From now on, whenever I mention <strong>go to the Python shell</strong>, it means that you have to use the <code>python3</code> command.</p>
<p>To learn the remaining concepts we will create a file called "testing" with the extension <code>.py</code>. To run this file we will use the following command:</p>
<pre><code class="lang-shell">python3 testing.py
</code></pre>
<p>Let's go to the Python shell. Type <code>10 + 12</code> after the <code>&gt;&gt;&gt;</code> mark. You'll get the output 22:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-number">10</span> + <span class="hljs-number">12</span>
<span class="hljs-number">22</span>
</code></pre>
<h2 id="heading-commenting">Commenting:</h2>
<p>Comments make it easy to write code as they help us (and others) understand why a particular piece of code was written. Another awesome thing about comments is that they help improve the readability of the code.</p>
<pre><code class="lang-python"><span class="hljs-comment"># Stay Safe</span>
</code></pre>
<p>When you add the above syntax, the Python interpreter understands that it is a comment. Everything after <code>#</code> is not executed.</p>
<p>You may be wondering why you should use comments. Imagine you are a developer and you have been assigned to a huge project. The project has more than a thousand lines of code. To understand how everything works you'll need to go line by line and read through all the code.</p>
<p>What's a better solution than that? Ah-ha! Comments. Comments help us understand why a particular piece of code was written and what it returns or does. Consider it as documentation for every piece of code.</p>
<h2 id="heading-print">Print:</h2>
<p>Other than debugging tools from the editor, the thing which helps developers solve problems most often is a print statement. The print statement is one of the most underrated pieces of syntax in all of programming.</p>
<p>So how does it help in debugging an issue? Well, consider that you have a module and you want to check the flow of execution to understand or debug it. There are two options. Either you can use a debugger or add a print statement.</p>
<p>It's not always possible to use a debugger. For example, if you are using the Python shell, then a debugger is not available. In such a scenario, print helps us. Another scenario is when your application is running. You can add a print statement that will display in the logs of your application and monitor them in runtime.</p>
<p>Python provides a inbuilt print method with the following syntax:</p>
<pre><code class="lang-python">print(<span class="hljs-string">"Stay safe..."</span>)
</code></pre>
<h2 id="heading-indentation">Indentation:</h2>
<p>Another interesting part of this language is indentation. Why? Well, the answer is simple: It makes the code readable and well-formatted. It is compulsory in Python to follow the rules of indentation. If proper indentation is not followed you'll get the following error:</p>
<pre><code class="lang-python">IndentationError: unexpected indent
</code></pre>
<p>See, even the errors in Python are so readable and easy to understand. At the start, you may be annoyed by the compulsion of indentation. But with the time you'll understand that indentation is a developer's friend.</p>
<h2 id="heading-variables"><strong>Variables:</strong></h2>
<p>As the name implies, a variable is something that can change. A variable is a way of referring to a memory location used by a computer program.</p>
<p>Well in most programming languages you need to assign the type to a variable. But in Python, you don’t need to. For example, to declare an integer in C, the following syntax is used: <code>int num = 5;</code>. In Python it's <code>num = 5</code> .</p>
<p>Go to the Python shell and perform the operation step by step:</p>
<ul>
<li><code>Integer</code>: Numerical values that can be positive, negative, or zero without a decimal point.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>num = <span class="hljs-number">5</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print(num)
<span class="hljs-number">5</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>type(num)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">int</span>'&gt;</span>
</code></pre>
<p>As you can see here we have declared a <code>num</code> variable and assigned 5 as a value. Python's inbuilt <code>type</code> method can be used to check the type of variable. When we check the type of <code>num</code> we see the output <code>&lt;class 'int'&gt;</code>. For now, just focus on the <code>int</code> in that output. <code>int</code> represents an integer.</p>
<ul>
<li><code>Float</code>: Similar an integer but with one slight difference – floats are a numerical value with a decimal place.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>num = <span class="hljs-number">5.0</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print(num)
<span class="hljs-number">5.0</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>type(num)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">float</span>'&gt;</span>
</code></pre>
<p>Here we have assigned a number with a single decimal to the <code>num</code>. When we check the type of <code>num</code> we can see it is <code>float</code>.</p>
<ul>
<li><code>String</code>: A formation of characters or integers. They can be represented using double or single quotes.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>greet = <span class="hljs-string">"Hello user"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print(greet)
Hello user
<span class="hljs-meta">&gt;&gt;&gt; </span>type(greet)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">str</span>'&gt;</span>
</code></pre>
<p>Here we have assigned a string to <code>greet</code>. The type of greet is a string as you can see from the output.</p>
<ul>
<li><code>Boolean</code>: A binary operator with a True or False value.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>is_available = <span class="hljs-literal">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print(is_available)
<span class="hljs-literal">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>type(is_available)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">bool</span>'&gt;</span>
</code></pre>
<p>Here we have assigned a True value to <code>is_available</code>. The type of this variable is boolean. You can only assign <strong>True</strong> or <strong>False</strong>. Remember <strong>T</strong> and <strong>F</strong> should be capital or it will give an error as follows:</p>
<pre><code class="lang-shell">&gt;&gt;&gt; is_available = true
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
NameError: name 'true' is not defined
</code></pre>
<ul>
<li><code>NoneType</code>: This is used when we don't have the value of the variable.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>num = <span class="hljs-literal">None</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print(num)
<span class="hljs-literal">None</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>type(num)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">NoneType</span>'&gt;</span>
</code></pre>
<h2 id="heading-operators">Operators:</h2>
<p>Take a look at the image below to see all the arithmetic operators available in Python:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/04/Screenshot-2020-04-30-at-12.28.55-PM.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Operators table</em></p>
<p>Let's go over the operators one by one.</p>
<h3 id="heading-arithmetic-operators">Arithmetic operators</h3>
<p>These include addition, subtraction, deletion, exponentiation, modulus, and floor division. Also the shorthand syntax for some operators.</p>
<p>First, we will declare two variables, <code>a</code> and <code>b</code>.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-number">6</span> <span class="hljs-comment"># Assignment</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b = <span class="hljs-number">2</span>
</code></pre>
<p>Let's try our basic arithmetic operations:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a + b <span class="hljs-comment"># Addition</span>
<span class="hljs-number">8</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a - b <span class="hljs-comment"># Subtraction</span>
<span class="hljs-number">4</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a * b <span class="hljs-comment"># Multiplication</span>
<span class="hljs-number">12</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a / b <span class="hljs-comment"># Division</span>
<span class="hljs-number">3.0</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a ** b <span class="hljs-comment"># Exponentiation</span>
<span class="hljs-number">36</span>
</code></pre>
<p>To test for other arithmetic operations let's change the value of <code>a</code> and <code>b</code>.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-number">7</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b = <span class="hljs-number">3</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a % b <span class="hljs-comment"># Modulus</span>
<span class="hljs-number">1</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a // b <span class="hljs-comment"># Floor division</span>
<span class="hljs-number">2</span>
</code></pre>
<p>Shorthand arithmetic operations are also available in Python. Refer back to the image above to test them out. To print the output of the shorthand operations use the <code>print</code> statement.</p>
<h3 id="heading-comparison-operators">Comparison operators</h3>
<p>These include equal to, greater than, and less than.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-number">5</span> <span class="hljs-comment"># Assign</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b = <span class="hljs-number">2</span> <span class="hljs-comment"># Assign</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a &gt; b <span class="hljs-comment"># Greater than</span>
<span class="hljs-literal">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a &lt; b <span class="hljs-comment"># less then</span>
<span class="hljs-literal">False</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a == b <span class="hljs-comment"># Equal to</span>
<span class="hljs-literal">False</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a &gt;= <span class="hljs-number">5</span> <span class="hljs-comment"># Greater than or equal to</span>
<span class="hljs-literal">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b &lt;= <span class="hljs-number">1</span> <span class="hljs-comment"># Less than or equal to</span>
<span class="hljs-literal">False</span>
</code></pre>
<h3 id="heading-logical-operators">Logical operators</h3>
<p>These operators include not, and, &amp; or.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-number">10</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b = <span class="hljs-number">2</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a == <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> b == <span class="hljs-number">10</span> <span class="hljs-comment"># and</span>
<span class="hljs-literal">False</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a == <span class="hljs-number">10</span> <span class="hljs-keyword">or</span> b == <span class="hljs-number">10</span> <span class="hljs-comment"># or</span>
<span class="hljs-literal">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">not</span>(a == <span class="hljs-number">10</span>) <span class="hljs-comment"># not</span>
<span class="hljs-literal">False</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">not</span>(a == <span class="hljs-number">2</span>)
<span class="hljs-literal">True</span>
</code></pre>
<h2 id="heading-conditional-statements">Conditional Statements:</h2>
<p>As the name suggests, conditional statements are used to evaluate if a condition is true or false.</p>
<p>Many times when you are developing an application you need to check a certain condition and do different things depending on the outcome. In such scenarios conditional statements are useful. If, elif and else are the conditional statements used in Python.</p>
<p>We can compare variables, check if the variable has any value or if it's a boolean, then check if it's true or false. Go to the Python shell and perform the operation step by step:</p>
<p><strong>Condition Number 1:</strong> We have an integer and 3 conditions here. The first one is the <code>if</code> condition. It checks if the number is equal to 10.</p>
<p>The second one is the <code>elif</code> condition. Here we are checking if the number is less than 10.</p>
<p>The last condition is <code>else</code>. This condition executes when none of the above conditions match.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>number = <span class="hljs-number">5</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> number == <span class="hljs-number">10</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Number is 10"</span>)
<span class="hljs-meta">... </span><span class="hljs-keyword">elif</span> number &lt; <span class="hljs-number">10</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Number is less than 10"</span>)
<span class="hljs-meta">... </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Number is more than 10"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Number is less than 10
</code></pre>
<p><strong>Note:</strong> It is not compulsory to check that two conditions are equal in the <code>if</code> condition. You can do it in the <code>elif</code> also.</p>
<p><strong>Condition Number 2:</strong> We have a boolean and 2 conditions here. Have you noticed how we are checking if the condition is true? If <code>is_available</code>, then print "Yes it is available", else print "Not available".</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>is_available = <span class="hljs-literal">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> is_available:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Yes it is available"</span>)
<span class="hljs-meta">... </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Not available"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Yes it is available
</code></pre>
<p><strong>Condition Number 3:</strong> Here we have reversed condition number 2 with the help of the not operator.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>is_available = <span class="hljs-literal">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> is_available:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Not available"</span>)
<span class="hljs-meta">... </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Yes it is available"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Yes it is available
</code></pre>
<p><strong>Condition Number 4:</strong> Here we are declaring the data as None and checking if the data is available or not.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data = <span class="hljs-literal">None</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> data:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"data is not none"</span>)
<span class="hljs-meta">... </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"data is none"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">data is none
</code></pre>
<p><strong>Condition Number 5:</strong> You can also use an inline if in Python. The syntax to achieve this is the following:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>num_a = <span class="hljs-number">10</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>num_b = <span class="hljs-number">5</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> num_a &gt; num_b: print(<span class="hljs-string">"num_a is greater than num_b"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">num_a is greater than num_b
</code></pre>
<p><strong>Condition Number 6:</strong> You can also use an inline if else in Python. The syntax to achieve this is the following:</p>
<pre><code class="lang-python">expression_if_true <span class="hljs-keyword">if</span> condition <span class="hljs-keyword">else</span> expression_if_false
</code></pre>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>num = <span class="hljs-number">5</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print(<span class="hljs-string">"Number is five"</span>) <span class="hljs-keyword">if</span> num == <span class="hljs-number">5</span> <span class="hljs-keyword">else</span> print(<span class="hljs-string">"Number is not five"</span>)
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Number is five
</code></pre>
<p><strong>Conditional Number 7:</strong> You can also use nested if-else statements. The syntax to achieve this is the following:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>num = <span class="hljs-number">25</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> num &gt; <span class="hljs-number">10</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Number is greater than 10"</span>)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">if</span> num &gt; <span class="hljs-number">20</span>:
<span class="hljs-meta">... </span>            print(<span class="hljs-string">"Number is greater than 20"</span>)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">if</span> num &gt; <span class="hljs-number">30</span>:
<span class="hljs-meta">... </span>            print(<span class="hljs-string">"Number is greater than 30"</span>)
<span class="hljs-meta">... </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Number is smaller than 10"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Number is greater than 10
Number is greater than 20
</code></pre>
<p><strong>Condition Number 8:</strong> You can also use the <code>and</code> operator in a conditional statement. It states if condition1 and condition2 both are true then execute it.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>num = <span class="hljs-number">10</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> num &gt; <span class="hljs-number">5</span> <span class="hljs-keyword">and</span> num &lt; <span class="hljs-number">15</span>:
<span class="hljs-meta">... </span>    print(num)
<span class="hljs-meta">... </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Number may be small than 5 or larger than 15"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">10
</code></pre>
<p>As our number is between 5 and 15 we get the output of 10.</p>
<p><strong>Condition Number 9:</strong> You can also use the <code>or</code> operator in a conditional statement. It states that if either condition1 or condition2 is true then execute it.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>num = <span class="hljs-number">10</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> num &gt; <span class="hljs-number">5</span> <span class="hljs-keyword">or</span> num &lt; <span class="hljs-number">7</span>:
<span class="hljs-meta">... </span>    print(num)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">10
</code></pre>
<p>Are you confused because the value of <code>num</code> is 10 and our second condition states that <code>num</code> is less than 7? So why do we get the output as 10? It's because of the <code>or</code> condition. As one of the conditions matches, it will execute it.</p>
<h2 id="heading-for-loops">For Loops:</h2>
<p>Another useful method in any programming language is an iterator. If you have to implement something multiple times, what will you do?</p>
<pre><code class="lang-python">print(<span class="hljs-string">"Hello"</span>)
print(<span class="hljs-string">"Hello"</span>)
print(<span class="hljs-string">"Hello"</span>)
</code></pre>
<p>Well, that's one way to do it. But imagine you have to do it a hundred or a thousand times. Well, that's a lot of print statements we have to write. There's a better way called iterators or loops. We can either use a <code>for</code> or <code>while</code> loop.</p>
<p>Here we are using the range method. It specifies the range until which the loop should be repeated. By default, the starting point is 0.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">3</span>):
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Hello"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Hello
Hello
Hello
</code></pre>
<p>You can also specify the range in this way <code>range(1,3)</code>.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">1</span>,<span class="hljs-number">3</span>):
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Hello"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Hello
Hello
</code></pre>
<p>"Hello" is only printed two times as we have specified the range here. Think of the range as <code>Number on right - Number on left</code>.</p>
<p>Well, you can also add an else statement in the for loop.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">3</span>):
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Hello"</span>)
<span class="hljs-meta">... </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Finished"</span>)
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Hello
Hello
Hello
Finished
</code></pre>
<p>See our loop iterated 3 times ( 3 - 0 ) and once that is done it executed the else statement.</p>
<p>We can also nest a for loop inside another for loop.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">3</span>):
<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(<span class="hljs-number">2</span>):
<span class="hljs-meta">... </span>            print(<span class="hljs-string">"Inner loop"</span>)
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Outer loop"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Inner loop
Inner loop
Outer loop
Inner loop
Inner loop
Outer loop
Inner loop
Inner loop
Outer loop
</code></pre>
<p>As you can see the inner loop print statement executed two times. After that outer loop print statement executed. Again the inner loop executed two times. So what is happening here? If you are confused then consider this to solve it:</p>
<ul>
<li><p>Our Interpreter comes and sees that there is a <code>for</code> loop. It goes down again and checks there is another <code>for</code> loop.</p>
</li>
<li><p>So now it will execute the inner <code>for</code> loop two times and exit. Once it's finished it knows that outer for loop has instructed it to repeat two more times.</p>
</li>
<li><p>It starts again and sees the inner for loop and repeats.</p>
</li>
</ul>
<p>Well, you can also choose to pass a certain <code>for</code> loop condition. What does pass mean here? Well whenever that for loop will occur and the Interpreter sees the <code>pass</code> statement it won't execute it and will move to the next line.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">3</span>):
<span class="hljs-meta">... </span>    <span class="hljs-keyword">pass</span>
...
</code></pre>
<p>You will not get any output on the shell.</p>
<h2 id="heading-while-loops">While loops:</h2>
<p>Another loop or iterator available in Python is the <code>while</code> loop. We can achieve some of the same results with the help of a <code>while</code> loop as we achieved with the <code>for</code> loop.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>i = <span class="hljs-number">0</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">while</span> i &lt; <span class="hljs-number">5</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Number"</span>, i)
<span class="hljs-meta">... </span>    i += <span class="hljs-number">1</span>
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Number 0
Number 1
Number 2
Number 3
Number 4
</code></pre>
<p>Remember whenever you use a while loop it's important that you add an increment statement or a statement that will end the while loop at some point. If not then the while loop will execute forever.</p>
<p>Another option is to add a <code>break</code> statement in a <code>while</code> loop. This will break the loop.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>i = <span class="hljs-number">0</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">while</span> i &lt; <span class="hljs-number">5</span>:
<span class="hljs-meta">... </span>    <span class="hljs-keyword">if</span> i == <span class="hljs-number">4</span>:
<span class="hljs-meta">... </span>            <span class="hljs-keyword">break</span>
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Number"</span>, i)
<span class="hljs-meta">... </span>    i += <span class="hljs-number">1</span>
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Number 0
Number 1
Number 2
Number 3
</code></pre>
<p>Here we are breaking the <code>while</code> loop if we find the value of <code>i</code> to be 4.</p>
<p>Another option is to add an <code>else</code> statement in <code>while</code> loop. The statement will be executed after the while loop is completed.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>i = <span class="hljs-number">0</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">while</span> i &lt; <span class="hljs-number">5</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Number"</span>, i)
<span class="hljs-meta">... </span>    i += <span class="hljs-number">1</span>
<span class="hljs-meta">... </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Number is greater than 4"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Number 0
Number 1
Number 2
Number 3
Number 4
Number is greater than 4
</code></pre>
<p>The <code>continue</code> statement can be used to skip the current execution and to proceed to the next.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>i = <span class="hljs-number">0</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">while</span> i &lt; <span class="hljs-number">6</span>:
<span class="hljs-meta">... </span>    i += <span class="hljs-number">1</span>
<span class="hljs-meta">... </span>    <span class="hljs-keyword">if</span> i == <span class="hljs-number">2</span>:
<span class="hljs-meta">... </span>            <span class="hljs-keyword">continue</span>
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"number"</span>, i)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">number 1
number 3
number 4
number 5
number 6
</code></pre>
<h2 id="heading-user-input">User Input:</h2>
<p>Imagine you are building a command-line application. Now you have to take the user input and act accordingly. To do that you can use Python's inbuilt <code>input</code> method.</p>
<p>The syntax to achieve this is as follows:</p>
<pre><code class="lang-python">variable = input(<span class="hljs-string">"....."</span>)
</code></pre>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>name = input(<span class="hljs-string">"Enter your name: "</span>)
Enter your name: Sharvin
</code></pre>
<p>When you use the <code>input</code> method and press enter, you'll be prompted with the text that you enter in the <code>input</code> method. Let's check if our assignment is working or not:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>print(name)
Sharvin
</code></pre>
<p>There it is! It is working perfectly. Here <code>Sharvin</code> is of the type string.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>type(name)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">str</span>'&gt;</span>
</code></pre>
<p>Let's try one more example where we will assign an integer rather than a string and check the type.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>date = input(<span class="hljs-string">"Today's date: "</span>)
Today<span class="hljs-string">'s date: 12
&gt;&gt;&gt; type(date)
&lt;class '</span>st<span class="hljs-string">r'&gt;</span>
</code></pre>
<p>Are you confused? We entered an integer 12 and it's still giving us its type as a string. It's not a bug. It's how input is intended to work. To convert the string to integer we will use typecasting.</p>
<h2 id="heading-typecasting">Typecasting:</h2>
<p>We saw that the <code>input</code> method returns a string for the integer also. Now if we want to compare this output with another integer then we need a way to convert it back to an integer.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>date_to_int = int(date)
<span class="hljs-meta">&gt;&gt;&gt; </span>type(date_to_int)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">int</span>'&gt;</span>
</code></pre>
<p>Here we took the date that we have declared above in the User input section and converted it into the integer using the Python's inbuilt <code>int</code> method. This is called typecasting.</p>
<p>Basically you can do the following conversion with the help of typecasting:</p>
<ul>
<li><p>integer to string: <code>str()</code></p>
</li>
<li><p>string to integer: <code>int()</code></p>
</li>
<li><p>integer to float: <code>float()</code></p>
</li>
</ul>
<blockquote>
<p>Note: Conversion from float to integer is also possible.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>type(date)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">str</span>'&gt;

# <span class="hljs-title">Converting</span> <span class="hljs-title">from</span> <span class="hljs-title">string</span> <span class="hljs-title">to</span> <span class="hljs-title">float</span>
&gt;&gt;&gt; <span class="hljs-title">date_to_float</span> = <span class="hljs-title">float</span>(<span class="hljs-params">date</span>)
&gt;&gt;&gt; <span class="hljs-title">type</span>(<span class="hljs-params">date_to_float</span>)
&lt;<span class="hljs-title">class</span> '<span class="hljs-title">float</span>'&gt;

# <span class="hljs-title">Converting</span> <span class="hljs-title">from</span> <span class="hljs-title">float</span> <span class="hljs-title">to</span> <span class="hljs-title">string</span>
&gt;&gt;&gt; <span class="hljs-title">date_to_string</span> = <span class="hljs-title">str</span>(<span class="hljs-params">date_to_float</span>)
&gt;&gt;&gt; <span class="hljs-title">type</span>(<span class="hljs-params">date_to_string</span>)
&lt;<span class="hljs-title">class</span> '<span class="hljs-title">str</span>'&gt;

# <span class="hljs-title">Converting</span> <span class="hljs-title">from</span> <span class="hljs-title">float</span> <span class="hljs-title">to</span> <span class="hljs-title">integer</span>
&gt;&gt;&gt; <span class="hljs-title">date_to_int</span> = <span class="hljs-title">int</span>(<span class="hljs-params">date_to_float</span>)
&gt;&gt;&gt; <span class="hljs-title">type</span>(<span class="hljs-params">date_to_int</span>)
&lt;<span class="hljs-title">class</span> '<span class="hljs-title">int</span>'&gt;</span>
</code></pre>
<h2 id="heading-dictionaries">Dictionaries:</h2>
<p>Imagine you want to store some user details. So how can you store these details? Yes, we can use variable to store them as follows:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>fname = <span class="hljs-string">"Sharvin"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>lname = <span class="hljs-string">"Shah"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>profession = <span class="hljs-string">"Developer"</span>
</code></pre>
<p>To access this value we can do the following:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>print(fname)
Sharvin
</code></pre>
<p>But is this an elegant and optimized way to access it? The answer is no. To make it more friendly, let's store the data in a key-value dictionary.</p>
<p>What is a dictionary? A dictionary is a collection that is unordered and mutable ( i.e. it can be updated ).</p>
<p>Following is the format of the dictionary:</p>
<pre><code class="lang-json">data = {
    <span class="hljs-attr">"key"</span> : <span class="hljs-string">"value"</span>
}
</code></pre>
<p>Let's understand the dictionary further by an example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details = {
<span class="hljs-meta">... </span>    <span class="hljs-string">"fname"</span>: <span class="hljs-string">"Sharvin"</span>,
<span class="hljs-meta">... </span>    <span class="hljs-string">"lname"</span>: <span class="hljs-string">"Shah"</span>,
<span class="hljs-meta">... </span>    <span class="hljs-string">"profession"</span>: <span class="hljs-string">"Developer"</span>
<span class="hljs-meta">... </span>}
</code></pre>
<h3 id="heading-how-to-access-a-value-in-a-dictionary">How to access a value in a dictionary</h3>
<p>We can access the value inside a dictionary in two ways. We will take a look at both and then debug them to find out which is better.</p>
<p>Method 1: To access the value of <code>fname</code> key from <code>user_details</code> dictionary we can use the following syntax:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details[<span class="hljs-string">"fname"</span>]
<span class="hljs-string">'Sharvin'</span>
</code></pre>
<p>Method 2: We can also access the value of <code>fname</code> key from <code>user_details</code> dictionary using <code>get</code>.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details.get(<span class="hljs-string">"fname"</span>)
<span class="hljs-string">'Sharvin'</span>
</code></pre>
<p>I know method 1 looks easier to understand. The problem with it occurs when we try to access the data that is not available in our dictionary.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details[<span class="hljs-string">"age"</span>]
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
KeyError: <span class="hljs-string">'age'</span>
</code></pre>
<p>We get a KeyError which indicates that the key is not available. Let's try the same scenario with method 2.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details.get(<span class="hljs-string">"age"</span>)
</code></pre>
<p>We do not get anything printed in our console. Let's debug it further to know why this happened. Assign a variable age to our <code>get</code> operation and we will print it in our console.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>age = user_details.get(<span class="hljs-string">"age"</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>print(age)
<span class="hljs-literal">None</span>
</code></pre>
<p>So when <code>get</code> doesn't find the key it sets the value to None. Because of this, we do not get any error. Now you may be wondering which one is right. Most of the time using method 2 makes more sense, but for some strict checking conditions, we need to use method 1.</p>
<h3 id="heading-how-to-check-if-a-key-exists">How to check if a key exists</h3>
<p>You may be wondering how to check if the dictionary has a particular key or not in it. Python provides the built-in method <code>keys()</code> to solve this issue.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> <span class="hljs-string">"age"</span> <span class="hljs-keyword">in</span> user_details.keys():
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Yes it is present"</span>)
<span class="hljs-meta">... </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Not present"</span>)
...
</code></pre>
<p>We will get the following output:</p>
<pre><code class="lang-shell">Not present
</code></pre>
<p>What if we want to check if the dictionary is empty or not? To understand this let's declare an empty dictionary as follows:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details = {}
</code></pre>
<p>When we use if-else on a dictionary directly it either returns true if data is present or false if empty.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> user_details:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Not empty"</span>)
<span class="hljs-meta">... </span><span class="hljs-keyword">else</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Empty"</span>)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Empty
</code></pre>
<p>We can also use Python's inbuilt method <code>bool</code> to check if the dictionary is empty or not. Remember bool returns False if the dictionary is empty and True if it is filled.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>bool(user_details)
<span class="hljs-literal">False</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>user_details = {
<span class="hljs-meta">... </span>    <span class="hljs-string">"fname"</span> : <span class="hljs-string">"Sharvin"</span>
<span class="hljs-meta">... </span>}
<span class="hljs-meta">&gt;&gt;&gt; </span>bool(user_details)
<span class="hljs-literal">True</span>
</code></pre>
<h3 id="heading-how-to-update-the-value-of-an-existing-key">How to update the value of an existing key</h3>
<p>So now we know how to get a particular key and find if it exists – but how do you update it in the dictionary?</p>
<p>Declare a dictionary as follows:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details = {
<span class="hljs-meta">... </span>    <span class="hljs-string">"fname"</span>:<span class="hljs-string">"Sharvin"</span>,
<span class="hljs-meta">... </span>    <span class="hljs-string">"lname"</span>: <span class="hljs-string">"Shah"</span>,
<span class="hljs-meta">... </span>    <span class="hljs-string">"profession"</span>: <span class="hljs-string">"Developer"</span>
<span class="hljs-meta">... </span>}
</code></pre>
<p>To update the value use the following syntax:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details[<span class="hljs-string">"profession"</span>] = <span class="hljs-string">"Software Developer"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print(user_details)
{<span class="hljs-string">'fname'</span>: <span class="hljs-string">'Sharvin'</span>, <span class="hljs-string">'lname'</span>: <span class="hljs-string">'Shah'</span>, <span class="hljs-string">'profession'</span>: <span class="hljs-string">'Software Developer'</span>}
</code></pre>
<p>Updating a value of key in dictionary is same as assigning a value to the variable.</p>
<h3 id="heading-how-to-add-a-key-value-pair">How to add a key-value pair</h3>
<p>The next question is how to add a new value to the dictionary? Let's add an <code>age</code> key with a value of 100.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details[<span class="hljs-string">"age"</span>] = <span class="hljs-string">"100"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>print(user_details)
{<span class="hljs-string">'fname'</span>: <span class="hljs-string">'Sharvin'</span>, <span class="hljs-string">'lname'</span>: <span class="hljs-string">'Shah'</span>, <span class="hljs-string">'profession'</span>: <span class="hljs-string">'Software Developer'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-string">'100'</span>}
</code></pre>
<p>As you can see a new key-value is added in our dictionary.</p>
<h3 id="heading-how-to-remove-a-key-value-pair">How to remove a key-value pair</h3>
<p>To remove a key-value from the dictionary, Python provides an inbuilt method called <code>pop</code>.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details.pop(<span class="hljs-string">"age"</span>)
<span class="hljs-string">'100'</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>print(user_details)
{<span class="hljs-string">'fname'</span>: <span class="hljs-string">'Sharvin'</span>, <span class="hljs-string">'lname'</span>: <span class="hljs-string">'Shah'</span>, <span class="hljs-string">'profession'</span>: <span class="hljs-string">'Software Developer'</span>}
</code></pre>
<p>This removes the <code>age</code> key-value pair from the <code>user_details</code> dictionary. We can also use a <code>del</code> operator to delete the value.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">del</span> user_details[<span class="hljs-string">"age"</span>]

<span class="hljs-meta">&gt;&gt;&gt; </span>print(user_details)
{<span class="hljs-string">'fname'</span>: <span class="hljs-string">'Sharvin'</span>, <span class="hljs-string">'lname'</span>: <span class="hljs-string">'Shah'</span>, <span class="hljs-string">'profession'</span>: <span class="hljs-string">'Software Developer'</span>}
</code></pre>
<p>The <code>del</code> method can also be used to <strong>delete complete dictionary</strong>. Use the following syntax to delete complete dictionary <code>del user_details</code>.</p>
<h3 id="heading-how-to-copy-a-dictionary">How to copy a dictionary</h3>
<p>A dictionary cannot be copied in a traditional way. For example, you cannot copy value of <code>dictA</code> to <code>dictB</code> as follows:</p>
<pre><code class="lang-python">dictA = dictB
</code></pre>
<p>To copy the values you need to use the <code>copy</code> method.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>dictB = user_details.copy()

<span class="hljs-meta">&gt;&gt;&gt; </span>print(dictB)
{<span class="hljs-string">'fname'</span>: <span class="hljs-string">'Sharvin'</span>, <span class="hljs-string">'lname'</span>: <span class="hljs-string">'Shah'</span>, <span class="hljs-string">'profession'</span>: <span class="hljs-string">'Software Developer'</span>}
</code></pre>
<h2 id="heading-lists">Lists:</h2>
<p>Imagine you have a bunch of data that is not labeled. In other words, each piece of data doesn't have a key that defines it. So how will you store it? Lists to the rescue. They are defined as follows:</p>
<pre><code class="lang-python">data = [ <span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-string">"xyz"</span>, <span class="hljs-literal">True</span> ]
</code></pre>
<p>A list is a collection of random, ordered, and mutable data (i.e., it can be updated).</p>
<h3 id="heading-how-to-access-list-elements">How to access list elements</h3>
<p>Let's try to access the first element:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data[<span class="hljs-number">1</span>]
<span class="hljs-number">5</span>
</code></pre>
<p>Wait what happened here? We are trying to access the first element but we are getting the second element. Why?</p>
<p>Indexing of the list begins from zero. So what do I mean by this? The indexing of the position of the elements begins from zero. The syntax to access an element is as follows:</p>
<pre><code class="lang-python">list[position_in_list]
</code></pre>
<p>To access the first element we need to access it as follows:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data[<span class="hljs-number">0</span>]
<span class="hljs-number">1</span>
</code></pre>
<p>You can also specify a range to access the element between those positions.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data[<span class="hljs-number">2</span>:<span class="hljs-number">4</span>]
[<span class="hljs-string">'xyz'</span>, <span class="hljs-literal">True</span>]
</code></pre>
<p>Here, the first value represents the start while the last value represents the position until which we want the value.</p>
<h3 id="heading-how-to-add-an-item-to-a-list">How to add an item to a list</h3>
<p>To add an item in the list we need to use the append method provided by python.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data.append(<span class="hljs-string">"Hello"</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>data
[<span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-string">'abc'</span>, <span class="hljs-literal">True</span>, <span class="hljs-string">'Hello'</span>]
</code></pre>
<h3 id="heading-how-to-change-the-value-of-an-item">How to change the value of an item</h3>
<p>To change the value of an item, use the following syntax:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data[<span class="hljs-number">2</span>] = <span class="hljs-string">"abc"</span>

<span class="hljs-meta">&gt;&gt;&gt; </span>data
[<span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-string">'abc'</span>, <span class="hljs-literal">True</span>]
</code></pre>
<h3 id="heading-how-to-remove-an-item-from-a-list">How to remove an item from a list</h3>
<p>To remove an item from a list we can use the Python's inbuilt <code>remove</code> method.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data.remove(<span class="hljs-string">"Hello"</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>data
[<span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-string">'abc'</span>, <span class="hljs-literal">True</span>]
</code></pre>
<h3 id="heading-how-to-loop-through-a-list">How to loop through a list</h3>
<p>We can also loop through the list to find a certain element and operate on it.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> data:
<span class="hljs-meta">... </span>    print(i)
...
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">1
5
abc
True
</code></pre>
<h3 id="heading-how-to-check-if-an-item-exists-or-not">How to check if an item exists or not</h3>
<p>To check if a particular item exists or not in list we can use if loop as follows:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> <span class="hljs-string">'abc'</span> <span class="hljs-keyword">in</span> data:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"yess.."</span>)
...
yess..
</code></pre>
<h3 id="heading-how-to-copy-list-data">How to copy list data</h3>
<p>To copy list data from one list to another we need to use <code>copy</code> method.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>List2 = data.copy()
<span class="hljs-meta">&gt;&gt;&gt; </span>List2
[<span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-string">'abc'</span>, <span class="hljs-literal">True</span>]
</code></pre>
<h3 id="heading-how-to-check-the-length-of-a-list">How to check the length of a list</h3>
<p>We can also check the length of list using Python's inbuilt <code>len</code> method.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>len(data)
<span class="hljs-number">4</span>
</code></pre>
<h3 id="heading-how-to-join-two-lists">How to join two lists</h3>
<p>To join two list we can use the <code>+</code> operator.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>list1 = [<span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-string">"hello"</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>list2 = [<span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-string">"bye"</span>]
<span class="hljs-meta">&gt;&gt;&gt; </span>list1 + list2
[<span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-string">'hello'</span>, <span class="hljs-number">2</span>, <span class="hljs-number">8</span>, <span class="hljs-string">'bye'</span>]
</code></pre>
<p>What happens if we try to access a element position which is not available in the list? We get a <code>list index out of range error</code> in such a condition.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>list1[<span class="hljs-number">6</span>]
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
IndexError: list index out of range
</code></pre>
<h2 id="heading-tuples">Tuples:</h2>
<p>The tuple is a data type which is ordered and immutable (i.e. data cannot be changed).</p>
<p>Let's create a tuple:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data = ( <span class="hljs-number">1</span>, <span class="hljs-number">3</span> , <span class="hljs-number">5</span>, <span class="hljs-string">"bye"</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>data
(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>, <span class="hljs-string">'bye'</span>)
</code></pre>
<h3 id="heading-how-to-access-a-tuple-element">How to access a tuple element</h3>
<p>We can access elements in the tuple the same way as we access them in a list:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data[<span class="hljs-number">3</span>]
<span class="hljs-string">'bye'</span>
</code></pre>
<p>We can access the index range as follows:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data[<span class="hljs-number">2</span>:<span class="hljs-number">4</span>]
(<span class="hljs-number">5</span>, <span class="hljs-string">'bye'</span>)
</code></pre>
<h3 id="heading-how-to-change-a-tuples-value">How to change a tuple's value</h3>
<p>If you are thinking wait – how can we change the value of tuple, then you are right my friend. We cannot change the value of tuple as it is immutable. We get the following error if we try to change the value of a tuple:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data[<span class="hljs-number">1</span>] = <span class="hljs-number">8</span>
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
TypeError: <span class="hljs-string">'tuple'</span> object does <span class="hljs-keyword">not</span> support item assignment
</code></pre>
<p>There's a workaround available to change the value of a tuple:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data = ( <span class="hljs-number">1</span>, <span class="hljs-number">3</span> , <span class="hljs-number">5</span>, <span class="hljs-string">"bye"</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>data_two = list(data) <span class="hljs-comment"># Convert data to list</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>data_two[<span class="hljs-number">1</span>] = <span class="hljs-number">8</span> <span class="hljs-comment"># Update value as list is mutable</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>data = tuple(data_two) <span class="hljs-comment"># Convert again to tuple</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>data
(<span class="hljs-number">1</span>, <span class="hljs-number">8</span>, <span class="hljs-number">5</span>, <span class="hljs-string">'bye'</span>)
</code></pre>
<p>All other methods that we have seen in the list are applicable for the tuple also.</p>
<p><strong>[ Note: Once a tuple is created a new value cannot be added in it. ]</strong>.</p>
<h2 id="heading-sets">Sets:</h2>
<p>Sets are another data type in Python which are unordered and unindexed. Sets are declared as follows:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data = { <span class="hljs-string">"hello"</span>, <span class="hljs-string">"bye"</span>, <span class="hljs-number">10</span>, <span class="hljs-number">15</span> }
<span class="hljs-meta">&gt;&gt;&gt; </span>data
{<span class="hljs-number">10</span>, <span class="hljs-number">15</span>, <span class="hljs-string">'hello'</span>, <span class="hljs-string">'bye'</span>}
</code></pre>
<h3 id="heading-how-to-access-a-value">How to access a value</h3>
<p>As sets are unindexed we cannot directly access the value in a set. Thus to access the value in the set you need to use a for loop.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> data:
<span class="hljs-meta">... </span>    print(i)
...

<span class="hljs-number">10</span>
<span class="hljs-number">15</span>
hello
bye
</code></pre>
<h3 id="heading-how-to-change-a-value">How to change a value</h3>
<p>Once the set is created, values cannot be changed.</p>
<h3 id="heading-how-to-add-an-item">How to add an item</h3>
<p>To add an item to the set python provides an inbuilt method called <code>add</code>.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data.add(<span class="hljs-string">"test"</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>data
{<span class="hljs-number">10</span>, <span class="hljs-string">'bye'</span>, <span class="hljs-string">'hello'</span>, <span class="hljs-number">15</span>, <span class="hljs-string">'test'</span>}
</code></pre>
<h3 id="heading-how-to-check-length">How to check length</h3>
<p>To check the length of the set we use the <code>len</code> method.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>len(data)
<span class="hljs-number">5</span>
</code></pre>
<h3 id="heading-how-to-remove-an-item">How to remove an item</h3>
<p>To remove an item use the <code>remove</code> method:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>data.remove(<span class="hljs-string">"test"</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>data
{<span class="hljs-number">10</span>, <span class="hljs-string">'bye'</span>, <span class="hljs-string">'hello'</span>, <span class="hljs-number">15</span>}
</code></pre>
<h2 id="heading-functions-and-arguments">Functions and Arguments:</h2>
<p>Functions are a handy way to declare an operation that we want to perform. With the help of functions, you can separate logic according to the operation.</p>
<p>Functions are a block of code that helps us in the reusability of the repetitive logic. Functions can be both inbuilt and user-defined.</p>
<p>To declare a function we use the <code>def</code> keyword. Following is the syntax of the functions:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">hello_world</span>():</span>
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Hello world"</span>)
...
</code></pre>
<p>Here we are declaring a function which, when called, prints a "Hello world" statement. To call a function we use the following syntax:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>hello_world()
</code></pre>
<p>We will get the following output:</p>
<pre><code class="lang-shell">Hello world
</code></pre>
<p>Remember that the <code>()</code> brackets in a function call means to execute it. Remove those round brackets and try the call again.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>hello_world
</code></pre>
<p>You'll get the following output:</p>
<pre><code class="lang-shell">&lt;function hello_world at 0x1083eb510&gt;
</code></pre>
<p>When we remove the round brackets from the function call then it gives us a function reference. Here above as you can see the reference of <code>function hello_world</code> points to this memory address <code>0x1083eb510</code>.</p>
<p>Consider you have to perform an addition operation. You can do it by declaring <code>a</code> and <code>b</code> and then performing the addition.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-number">5</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b = <span class="hljs-number">10</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a + b
<span class="hljs-number">15</span>
</code></pre>
<p>This is one way to go. But now consider that the value of <code>a</code> and <code>b</code> have changed and you need to do it again.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-number">5</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b = <span class="hljs-number">10</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a + b
<span class="hljs-number">15</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a = <span class="hljs-number">2</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>b = <span class="hljs-number">11</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>a + b
<span class="hljs-number">13</span>
</code></pre>
<p>This still looks doable. Now imagine we need to add a set of two numbers a hundred times. The numbers within the set are different for every calculation. That's a lot to do. Don't worry – we have a function at our disposal to solve this issue.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">a,b</span>):</span>
<span class="hljs-meta">... </span>    print(a+b)
...
</code></pre>
<p>Here we are adding <code>a</code> and <code>b</code> as a compulsory argument to the <code>add</code> function. To call this function we will use the following syntax:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>add(<span class="hljs-number">10</span>,<span class="hljs-number">5</span>)
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">15
</code></pre>
<p>See how easy it is to define a function and use it? So what happens if we don't pass an argument?</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>add()
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
TypeError: add() missing <span class="hljs-number">2</span> required positional arguments: <span class="hljs-string">'a'</span> <span class="hljs-keyword">and</span> <span class="hljs-string">'b'</span>
</code></pre>
<p>Python throws a TypeError and informs us that the function requires two arguments.</p>
<p>Can you guess what will happen if we pass a third argument?</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>add(<span class="hljs-number">10</span>,<span class="hljs-number">5</span>,<span class="hljs-number">1</span>)
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
TypeError: add() takes <span class="hljs-number">2</span> positional arguments but <span class="hljs-number">3</span> were given
</code></pre>
<p>Well, Python will inform us that we have passed 3 arguments but there are only 2 positional arguments.</p>
<p>So what can we do when we don't know how many arguments a function can take? To solve this issue we use args and kwargs.</p>
<h2 id="heading-args">Args:</h2>
<p>When you don't know how many arguments will be passed to the function, use args and kwargs (kwargs are discussed below).</p>
<p>To pass n number of arguments to a function we use args. We add a <code>*</code> in front of the argument.</p>
<blockquote>
<p>Remember when you attach a <code>*</code> in front, you will be receiving a tuple of arguments.</p>
</blockquote>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">*num</span>):</span>
<span class="hljs-meta">... </span>    print(num)
...
</code></pre>
<p>Here <code>*num</code> is an instance of args. Now when we call the function <code>add</code> we can pass in n number of arguments and it won't throw a <code>TypeError</code>.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>add(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)
(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)

<span class="hljs-meta">&gt;&gt;&gt; </span>add(<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">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
</code></pre>
<p>Now to perform the addition operation we will use the Python's builtin function <code>sum</code></p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">*num</span>):</span>
<span class="hljs-meta">... </span>    print(sum(num))
...
</code></pre>
<p>Now when we call the add function we will get the following output:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>add(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>) <span class="hljs-comment"># Function call</span>
<span class="hljs-number">6</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>add(<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-comment"># Function call</span>
<span class="hljs-number">10</span>
</code></pre>
<h2 id="heading-keyword-arguments">Keyword Arguments:</h2>
<p>There are times when we don't know the order of the arguments that will be passed to our function when it's called. In such a scenario we use keyword arguments because you can pass them in any order in your call and our function will know the value. Take a look at this example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">user_details</span>(<span class="hljs-params">username, age</span>):</span>
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Username is"</span>, username)
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Age is"</span>, age)
...
</code></pre>
<p>Let's call this function as follows:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details(<span class="hljs-string">"Sharvin"</span>, <span class="hljs-number">100</span>)
</code></pre>
<p>We will get the following output:</p>
<pre><code class="lang-shell">Username is Sharvin
Age is 100
</code></pre>
<p>Well this looks correct, but imagine if we called our function in this way:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details(<span class="hljs-number">100</span>, <span class="hljs-string">"Sharvin"</span>)
</code></pre>
<p>We will get the following output:</p>
<pre><code class="lang-shell">Username is 100
Age is Sharvin
</code></pre>
<p>This does not look right. What happened is <code>username</code> took the value of 100 while <code>age</code> took the value of "Sharvin". In scenarios like this where we don't know the order of arguments we can use keyword arguments when calling the function:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details(age=<span class="hljs-number">100</span>, username=<span class="hljs-string">"Sharvin"</span>)
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Username is Sharvin
Age is 100
</code></pre>
<h2 id="heading-default-argument">Default Argument:</h2>
<p>Suppose there is a condition where we are not sure if a particular argument will get a value or not when the function is called. In such a scenario we can use Default arguments as follows:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">user_details</span>(<span class="hljs-params">username, age = None</span>):</span>
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Username is"</span>, username)
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Age is"</span>, age)
...
</code></pre>
<p>Here we are assigning a <code>None</code> to our age argument. If we don't pass a second argument while calling the function it will take None as a default value.</p>
<p>Let's call the function:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details(<span class="hljs-string">"Sharvin"</span>)
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Username is Sharvin
Age is None
</code></pre>
<p>If we pass in the second argument it will override None and use it as the value.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user_details(<span class="hljs-string">"Sharvin"</span>, <span class="hljs-number">200</span>)
Username <span class="hljs-keyword">is</span> Sharvin
Age <span class="hljs-keyword">is</span> <span class="hljs-number">200</span>
</code></pre>
<p>But what will happen if we assign the first argument in our function as default and the second as a compulsory argument? Go to the Python shell and try this out:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">user_details</span>(<span class="hljs-params">username=None, age</span>):</span>
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Username is"</span>, username)
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Age is"</span>, age)
...
</code></pre>
<p>You'll get the following error:</p>
<pre><code class="lang-shell">  File "&lt;stdin&gt;", line 1
SyntaxError: non-default argument follows default argument
</code></pre>
<blockquote>
<p><strong>Remember:</strong> All compulsory arguments must be declared first and then the default argument must be declared.</p>
</blockquote>
<h2 id="heading-kwargs">kwargs:</h2>
<p>There can be a situation where you don't know how many keyword arguments will be passed into the function. In such a scenario we can use Kwargs.</p>
<p>To use kwargs we put <code>**</code> in front of the argument.</p>
<blockquote>
<p><strong>Remember:</strong> When you attach a <code>**</code> in front, you will be receiving a dictionary of arguments.</p>
</blockquote>
<p>Let's understand this by example. We will declare a function which accepts username as it's argument with <code>**</code> in front of it.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">user</span>(<span class="hljs-params">**username</span>):</span>
<span class="hljs-meta">... </span>    print(username)
...
</code></pre>
<p>When we call the <code>user</code> function as follows we will receive a dictionary.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user(username1=<span class="hljs-string">"xyz"</span>,username2=<span class="hljs-string">"abc"</span>)
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">{'username1': 'xyz', 'username2': 'abc'}
</code></pre>
<p>So what's happening here? It looks the same as args, right?</p>
<p>No, it's not. In args, you cannot access a particular value by its name as it is in the form of a tuple. Here we get the data in the form of a dictionary so we can easily access the value.</p>
<p>Consider this example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">user</span>(<span class="hljs-params">**user_details</span>):</span>
<span class="hljs-meta">... </span>    print(user_details[<span class="hljs-string">'username'</span>])
...
</code></pre>
<p>Let's call our function:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>user(username=<span class="hljs-string">"Sharvin"</span>,age=<span class="hljs-string">"1000"</span>)
</code></pre>
<p>And you'll get the following output:</p>
<pre><code class="lang-shell">Sharvin
</code></pre>
<h2 id="heading-scope">Scope:</h2>
<p>A scope defines where a variable or function is available. There are two types of scope in Python: Global and Local.</p>
<h3 id="heading-global-scope">Global Scope</h3>
<p>A variable or function created in the main body of Python code is called a global variable or function and is part of the global scope. For example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>greet = <span class="hljs-string">"Hello world"</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">testing</span>():</span>
<span class="hljs-meta">... </span>    print(greet)
...
<span class="hljs-meta">&gt;&gt;&gt; </span>testing()
Hello world
</code></pre>
<p>Here the variable <code>greet</code> is available globally because it is declared in the body of the program.</p>
<h3 id="heading-local-scope">Local Scope</h3>
<p>A variable or function created inside a function is called a local variable or function and is part of the local scope:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">testing</span>():</span>
<span class="hljs-meta">... </span>    greet = <span class="hljs-string">"Hello world"</span>
<span class="hljs-meta">... </span>    print(greet)
...
<span class="hljs-meta">&gt;&gt;&gt; </span>testing()
Hello world
</code></pre>
<p>Here <code>greet</code> is created inside the testing function and is only available there. Let's try to access it in our main body and see what happens:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>print(greet)
Traceback (most recent call last):
  File <span class="hljs-string">"&lt;stdin&gt;"</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
NameError: name <span class="hljs-string">'greet'</span> <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> defined
</code></pre>
<p><strong>Remember:</strong> Restart the Python console by pressing ctrl + d and starting the shell again by using the <code>python3</code> command before testing the code above. The first example has you declare the <code>greet</code> variable in the global scope meaning it will still be available in memory when you run the second example.</p>
<p>As <code>greet</code> is not available globally we get the error that it is not defined.</p>
<h2 id="heading-return-statement">Return Statement:</h2>
<p>Until now our functions are pretty simple. They are receiving data, processing it, and printing them. But in the real world, you need a function to return output so that it can be used in different operations.</p>
<p>To achieve this, return statements are used. Remember, return statements are only part of functions and methods. The syntax for the return statement is quite easy.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">add</span>(<span class="hljs-params">a, b</span>):</span>
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> a + b
...
<span class="hljs-meta">&gt;&gt;&gt; </span>add(<span class="hljs-number">1</span>,<span class="hljs-number">3</span>)
<span class="hljs-number">4</span>
</code></pre>
<p>Instead of printing our addition, we are returning the output. The value of the returned output can also be stored in a variable.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>sum = add(<span class="hljs-number">5</span>,<span class="hljs-number">10</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>print(sum)
<span class="hljs-number">15</span>
</code></pre>
<h2 id="heading-lambda-expression">Lambda Expression:</h2>
<p>Consider a situation where you don't want to perform much computation in a function. In such a situation writing a full-fledged function doesn't make sense. To solve this we use a lambda expression or lambda function.</p>
<p>So what is a lambda expression? It is an anonymous function and they are restricted to a single expression. The lambda expression can take n number of arguments.</p>
<p>The syntax for lambda expression is:</p>
<pre><code class="lang-python">variable = <span class="hljs-keyword">lambda</span> arguments: operation
</code></pre>
<p>Let's understand it more by example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>sum = <span class="hljs-keyword">lambda</span> a: a + <span class="hljs-number">10</span>
</code></pre>
<p>Here we have declared a variable <code>sum</code> which we are using to call the lambda function. <code>a</code> represents the argument that is passed to that function.</p>
<p>Let's call our function:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>x(<span class="hljs-number">5</span>)
<span class="hljs-number">15</span>
</code></pre>
<h2 id="heading-list-comprehension">List comprehension:</h2>
<p>Consider a situation where you want a list of squares. Normally you'll declare a <code>squares</code> list and then in a for loop you'll square out the numbers.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>squares = []
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>):
<span class="hljs-meta">... </span>    squares.append(x**<span class="hljs-number">2</span>)
...
<span class="hljs-meta">&gt;&gt;&gt; </span>squares
[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">9</span>, <span class="hljs-number">16</span>, <span class="hljs-number">25</span>, <span class="hljs-number">36</span>, <span class="hljs-number">49</span>, <span class="hljs-number">64</span>, <span class="hljs-number">81</span>]
</code></pre>
<p>Well this is doable, but we can achieve this in a single line with the help of list comprehension.</p>
<p>There are two ways to achieve this. Let's understand both of them.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>squares = list(map(<span class="hljs-keyword">lambda</span> x: x**<span class="hljs-number">2</span>, range(<span class="hljs-number">10</span>)))
<span class="hljs-meta">&gt;&gt;&gt; </span>squares
[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">9</span>, <span class="hljs-number">16</span>, <span class="hljs-number">25</span>, <span class="hljs-number">36</span>, <span class="hljs-number">49</span>, <span class="hljs-number">64</span>, <span class="hljs-number">81</span>]
</code></pre>
<p>Here we are using <code>list</code> constructor to build a list and inside that lambda function which squares out the number. Another way to achieve the same result is as follows:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>squares = list(x**<span class="hljs-number">2</span> <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>))
<span class="hljs-meta">&gt;&gt;&gt; </span>squares
[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">9</span>, <span class="hljs-number">16</span>, <span class="hljs-number">25</span>, <span class="hljs-number">36</span>, <span class="hljs-number">49</span>, <span class="hljs-number">64</span>, <span class="hljs-number">81</span>]
</code></pre>
<p>I prefer this way because it is easier to more concise and easier to understand.</p>
<p>What about when we have a condition where we want a set of two numbers that are the same? Well, we need to write two for loops and one if loop.</p>
<p>Let's see how that will look:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>num_list = []
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>):
<span class="hljs-meta">... </span>    <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>):
<span class="hljs-meta">... </span>            <span class="hljs-keyword">if</span> i == j:
<span class="hljs-meta">... </span>                    num_list.append((i,j))
...
<span class="hljs-meta">&gt;&gt;&gt; </span>num_list
[(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">1</span>, <span class="hljs-number">1</span>), (<span class="hljs-number">2</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">3</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">4</span>, <span class="hljs-number">4</span>), (<span class="hljs-number">5</span>, <span class="hljs-number">5</span>), (<span class="hljs-number">6</span>, <span class="hljs-number">6</span>), (<span class="hljs-number">7</span>, <span class="hljs-number">7</span>), (<span class="hljs-number">8</span>, <span class="hljs-number">8</span>), (<span class="hljs-number">9</span>, <span class="hljs-number">9</span>)]
</code></pre>
<p>That's a lot of work. And in terms of readability it's hard to understand.</p>
<p>Let's use list comprehension to achieve the same result.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>num_list = list((i,j) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>) <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>) <span class="hljs-keyword">if</span> i == j)

<span class="hljs-meta">&gt;&gt;&gt; </span>num_list
[(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">1</span>, <span class="hljs-number">1</span>), (<span class="hljs-number">2</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">3</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">4</span>, <span class="hljs-number">4</span>), (<span class="hljs-number">5</span>, <span class="hljs-number">5</span>), (<span class="hljs-number">6</span>, <span class="hljs-number">6</span>), (<span class="hljs-number">7</span>, <span class="hljs-number">7</span>), (<span class="hljs-number">8</span>, <span class="hljs-number">8</span>), (<span class="hljs-number">9</span>, <span class="hljs-number">9</span>)]
</code></pre>
<p>See how easy it is to get the same output in a single expression? Well, that's the power of list comprehension.</p>
<h2 id="heading-oop-concepts">OOP concepts:</h2>
<p>Python is a multi-paradigm programming language. It means Python can use different approaches to solve a problem. One of the paradigms is procedural or functional programming. It structures the code like a recipe – a set of steps in the form of functions and code blocks.</p>
<p>Another approach to solving the problem is by creating classes and objects. This is known as object-oriented oriented programming. An object is a collection of data (variables) and methods that act on those data. And classes are a blueprint for each object.</p>
<p>The important thing to understand in object-oriented programming is that objects are at the center of the paradigm – they not only represent the data but also the structure of the program.</p>
<p>You can choose the paradigm that best suits the problem at hand, mix different paradigms in one program, and/or switch from one paradigm to another as your program evolves.</p>
<h3 id="heading-advantages-of-object-oriented-programming">Advantages of object oriented programming</h3>
<ul>
<li><p><strong>Inheritance:</strong> This is one of the most useful concepts in OOP. It specifies that the child object will have all the properties and behavior of the parent object. Thus Inheritance allows us to define a class that inherits all the methods and properties from another class.</p>
</li>
<li><p><strong>Polymorphism:</strong> To understand polymorphism let’s divide the word into two parts. The first part "poly" means many and "morph" means to form or shape. Thus polymorphism means one task can be performed in many different ways.</p>
</li>
</ul>
<p>For example, you have a class <code>animal</code>, and all animals speak. But they speak differently. Here, the “speak” behavior is polymorphic and depends on the animal. So, the abstract “animal” concept does not actually “speak”, but specific animals (like dogs and cats) have a concrete implementation of the action “speak”.</p>
<p>Polymorphism means the same function name or method name being used for different types.</p>
<ul>
<li><strong>Encapsulation:</strong> In object-oriented programming you can restrict access to methods and variables – we can make the methods and variables private. This can prevent the data from being modified by accident and is known as encapsulation.</li>
</ul>
<p>First, we will understand classes, objects, and constructors. Then after that, we will look into the above properties again. If you already know about classes, objects, and constructors, feel free to skip ahead.</p>
<h2 id="heading-classes">Classes:</h2>
<p>There are primitive data structures available in Python, for example, numbers, strings, and lists. These can all be used for simple representations like name, place, cost, and so on.</p>
<p>But what if we have more complex data? If there is a pattern in the repetition of the properties of that data, what can we do?</p>
<p>Suppose we have 100 different animals. Every animal has a name, age, legs, etc. What if we want to add other properties to each animal, or one more animal gets added to that list? To manage such a complex scenario we need classes.</p>
<p>According to the official <a target="_blank" href="https://docs.python.org/3/tutorial/classes.html">Python documentation</a>:</p>
<blockquote>
<p>Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made.</p>
</blockquote>
<p>Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.</p>
<p>Syntax of class:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ClassName</span>:</span>

    &lt;expression<span class="hljs-number">-1</span>&gt;
    .
    .
    .
    &lt;expression-N&gt;
</code></pre>
<p>We use <code>class</code> keyword to define a class. We will define a <code>class Car</code>.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>
    <span class="hljs-keyword">pass</span>
</code></pre>
<h2 id="heading-methods">Methods:</h2>
<p>Methods look the same as functions. The only difference is that methods are dependent on an object. A function can be invoked by name while methods need to be invoked by using their class reference. They are defined inside the class.</p>
<p>In our example, let's create two methods. One is an engine and another is a wheel. These two methods define the parts available in our car.</p>
<p>The below program will give us a better idea of classes:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>
<span class="hljs-meta">... </span>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">engine</span>(<span class="hljs-params">self</span>):</span>
<span class="hljs-meta">... </span>            print(<span class="hljs-string">"Engine"</span>)
...

<span class="hljs-meta">&gt;&gt;&gt; </span>Car().engine()
Engine
</code></pre>
<p>Here we are calling the <code>engine</code> method by using the <code>Car()</code> reference.</p>
<p>To summarize, the class provides a blueprint of what should be defined but it does not provide any real content. The <code>Car</code> class above defines the engine but it will not state what a specific car’s engine is. It is specified by the object.</p>
<h2 id="heading-objects">Objects:</h2>
<p>The object is an instance of the class. Let’s consider the above example of a car. Here Car is our <code>class</code> and <code>toyota</code> is the <code>object</code> of the car. We can create multiple copies of the object. Every object must be defined using the class.</p>
<p>The syntax for creating an object is:</p>
<pre><code class="lang-python">toyota = Car()
</code></pre>
<p>Let’s consider our <code>Car</code> example to understand objects a bit better:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">engine</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Engine"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wheel</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Wheel"</span>)

toyota = Car()
</code></pre>
<p>The above <code>toyota = Car()</code> is a <strong>class object</strong>. Class objects support two kinds of operations: attribute references and instantiation.</p>
<p>Class instantiation uses function notation. The instantiation operation (“calling” a class object) creates an empty object.</p>
<p>Now we can call different methods from our class <code>Car</code> using the object <code>toyota</code> that we have created. let’s call the method <code>engine</code> and <code>wheel</code>.</p>
<p>Open your editor and create a file named <code>mycar.py</code>. In that file copy the code below:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">engine</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Engine"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wheel</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Wheel"</span>)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    toyota = Car()
    toyota.engine()
    toyota.wheel()
</code></pre>
<p>Save the above code. Now let's take a closer look at our program.</p>
<p>Here we are creating a <code>toyota</code> object with the help of <code>Car</code> class. The <code>toyota.engine()</code> is a method object. What exactly happens when a method object is called?</p>
<p>In the call <code>toyota.engine()</code> doesn't take any argument but if you see the method declaration we can see that it takes a <code>self</code> argument.</p>
<p>You may be confused about why it is not throwing an error. Well whenever we use a method object, the call <code>toyota.engine()</code> is converted to <code>Car.engine(toyota)</code>. We will understand more about the self in the upcoming section.</p>
<p>Run the program using the following command.</p>
<pre><code class="lang-shell">python mycar.py
</code></pre>
<p>You'll get the following output:</p>
<pre><code class="lang-shell">Engine
Wheel
</code></pre>
<h2 id="heading-constructor">Constructor:</h2>
<p>The <code>__init__</code> method is the constructor method in Python. The constructor method is used to initialize the data.</p>
<p>Go to the Python shell and enter this example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>():</span>
<span class="hljs-meta">... </span>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self</span>):</span>
<span class="hljs-meta">... </span>            print(<span class="hljs-string">"Hello I am the constructor method."</span>)
...
</code></pre>
<p>When we will call our class we will get the following output:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>toyota = Car()
Hello I am the constructor method.
</code></pre>
<p><strong>Note:</strong> You will never have to call the <strong>init</strong>() method – it gets called automatically when you create a class instance.</p>
<h2 id="heading-instance-attributes">Instance attributes:</h2>
<p>All the classes have objects and all the objects have attributes. Attributes are the properties. We use <code>__init__()</code> method to specify an object’s initial attribute.</p>
<p>Let’s consider our car example:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>():</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, model</span>):</span> 
        self.model = model  <span class="hljs-comment">#instance attribute</span>
</code></pre>
<p>In our example, each <code>Car()</code> has a specific model. Thus instance attributes are unique data to each instance.</p>
<h2 id="heading-class-attributes">Class attributes:</h2>
<p>We saw that instance attributes are specific to each object but class attributes are the same for all the instances. Let us look at the example of the car with the help of class attributes.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>():</span>

    no_of_wheels = <span class="hljs-number">4</span> <span class="hljs-comment">#class attribute</span>
</code></pre>
<p>So each car can have different models but all the cars will have only 4 wheels.</p>
<h2 id="heading-self">Self:</h2>
<p>Now let’s understand what <code>self</code> means and how we use it in object-oriented programming. <code>self</code> represents the instance of a class. By using the <code>self</code> keyword we can access the data initialized in the constructor and methods of a class.</p>
<p>Let's look at an example of how <code>self</code> can be used. Let’s create a method named <code>brand</code> under our class <code>Car</code>.</p>
<p>Inside that <code>__init__</code> method, we will pass a model by passing our car’s model name when we are instantiating our object. This name can be accessed anywhere in the class, for example <code>self.model</code> in our case.</p>
<p>Go to the file named <code>mycar.py</code> and replace old code with this code:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>():</span> 

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, model</span>):</span> 
    self.model = model

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">brand</span>(<span class="hljs-params">self</span>):</span> 
    print(<span class="hljs-string">"The brand is"</span>, self.model)  

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
  car = Car(<span class="hljs-string">"Bmw"</span>)
  car.brand()
</code></pre>
<p>Now when we run our above program using the following command:</p>
<pre><code class="lang-shell">python mycar.py
</code></pre>
<p>We will get the following output:</p>
<pre><code class="lang-shell">The brand is Bmw
</code></pre>
<p><strong>Note:</strong> <code>self</code> is a convention and not a real Python keyword. <code>self</code> is an argument in a method and we can use another name in place of it. But it is recommended to use <code>self</code> because it increases the readability of your code.</p>
<h2 id="heading-inheritance">Inheritance:</h2>
<p>Inheritance refers to when a class inherits the property of another class.</p>
<p>The class from which properties are inherited is called the base class. The class which inherits the property of another class is called the derived class.</p>
<p>Inheritance can be defined as a parent and child relationship. The child inherits the properties of the parent. Thus making the child a derived class while parent is a base class. Here the term property refers to attributes and methods.</p>
<p>The syntax for a derived class definition looks like this:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DerivedClassName</span>(<span class="hljs-params">BaseClassName</span>):</span>
    &lt;statement<span class="hljs-number">-1</span>&gt;
    .
    .
    .
    &lt;statement-N&gt;
</code></pre>
<p>It’s important to note that child classes override or extend the attributes and behaviors of parent class methods. This is to say that child classes inherit all of the the attributes and behaviors of their parents – but they're also able to specify different behavior to follow.</p>
<p>The most basic type of class is an object, which generally all other classes inherit as their parent. Let’s modify our previous example to understand how inheritance works.</p>
<p>We will create a base class named <code>vehicle</code>:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name</span>):</span>
        self.name = name

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getName</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.name
</code></pre>
<p>We have created a class <code>Vehicle</code> and instantiated a constructor with <code>self.name</code> which we are using in <code>getName</code> method. Whenever this method will be called, it will return the <code>name</code> that has been passed when an object is instantiated for that class.</p>
<p>Now let’s create a child class <code>Car</code>:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name</span>):</span>
        self.name = name

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">getName</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.name

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>(<span class="hljs-params">Vehicle</span>):</span>
  <span class="hljs-keyword">pass</span>
</code></pre>
<p><code>Car</code> is a child class of <code>Vehicle</code>. It inherits all the method and attributes of parent class.</p>
<p>Now let’s use methods and attribute from the <code>Vehicle</code> class in our child class <code>Car</code>.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name, color=<span class="hljs-string">'silver'</span></span>):</span>
        self.name = name
        self.color = color

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_name</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.name

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_color</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.color

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>(<span class="hljs-params">Vehicle</span>):</span>
  <span class="hljs-keyword">pass</span>

audi = Car(<span class="hljs-string">"Audi r8"</span>)
print(<span class="hljs-string">"The name of our car is"</span>, audi.get_name(), <span class="hljs-string">"and color is"</span>, audi.get_color())
</code></pre>
<p>Let's understand what we have done here.</p>
<p>We have declared a class named <code>Vehicle</code> with a constructor that takes name as an argument while color has a default argument.</p>
<p>We have two methods inside it. <code>get_name</code> returns name while <code>get_color</code> returns the color. We have instantiated an object and passed the car name.</p>
<p>One thing you'll notice here that we are using base class methods in our child class declaration.</p>
<p>Run the above program using the following command:</p>
<pre><code class="lang-shell">python mycar.py
</code></pre>
<p>Output:</p>
<pre><code class="lang-python">The name of our car <span class="hljs-keyword">is</span> Audi r8 <span class="hljs-keyword">and</span> color <span class="hljs-keyword">is</span> silver
</code></pre>
<p>We can also override a parent method or attribute. In the above example, we have defined our vehicle color has silver. But what if the color of our car is black?</p>
<p>Now for every child class, we can’t make changes in the parent class. There comes the overriding functionality.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name, color=<span class="hljs-string">'silver'</span></span>):</span>
        self.name = name
        self.color = color

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_name</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.name

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_color</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.color

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>(<span class="hljs-params">Vehicle</span>):</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_color</span>(<span class="hljs-params">self</span>):</span>
        self.color = <span class="hljs-string">'black'</span>
        <span class="hljs-keyword">return</span> self.color

audi = Car(<span class="hljs-string">"Audi r8"</span>)
print(<span class="hljs-string">"The name of our car is"</span>, audi.get_name(), <span class="hljs-string">"and color is"</span>, audi.get_color()
</code></pre>
<p>As you can see in the above program, I have not instantiated a constructor. The reason behind this is that our child class <code>Car</code> is only using attributes from the <code>Vehicle</code> class and it is already inheriting them. So in such a scenario, there is no need to re-instantiate these attributes.</p>
<p>Now when we run the above program we will get the following output:</p>
<pre><code class="lang-shell">The name of our car is Audi r8 and color is black
</code></pre>
<h2 id="heading-super">Super:</h2>
<p><code>super()</code> returns a temporary object of the superclass that then allows us to call that superclass’s methods.</p>
<p>Calling the previously built methods with <code>super()</code> saves us from needing to rewrite those methods in our subclass, and allows us to swap out superclasses with minimal code changes. Thus <code>super</code> extends the functionality of the inherited method.</p>
<p>Let’s extend our car example using <code>super()</code>. We will instantiate a constructor with <code>brand_name</code> and <code>color</code> in the parent class, <code>Vehicle</code>. Now we will call this constructor from our child class (<code>Car</code>) using <code>super</code>. We will create a <code>get_description</code> method which is returning <code>self.model</code> from <code>Car</code> class and <code>self.brand_name</code>, <code>self.color</code> from the <code>Vehicle</code> class.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, brand_name, color</span>):</span>
        self.brand_name = brand_name
        self.color = color

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_brand_name</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.brand_name

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>(<span class="hljs-params">Vehicle</span>):</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, brand_name, model, color</span>):</span>  
        super().__init__(brand_name, color)       
        self.model = model

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_description</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"Car Name: "</span> + self.get_brand_name() + self.model + <span class="hljs-string">" Color:"</span> + self.color

c = Car(<span class="hljs-string">"Audi "</span>,  <span class="hljs-string">"r8"</span>, <span class="hljs-string">" Red"</span>)
print(<span class="hljs-string">"Car description:"</span>, c.get_description())
print(<span class="hljs-string">"Brand name:"</span>, c.get_brand_name())
</code></pre>
<p>When we run the above program we get following output:</p>
<pre><code class="lang-shell">Car description: Car Name: Audi r8 Color: Red
Brand name: Audi
</code></pre>
<h2 id="heading-multiple-inheritance">Multiple Inheritance:</h2>
<p>When a class inherits the method and attributes from multiple parent class then it is called multiple inheritance. This allows us to use the property from multiple base classes or parent classes in a derived or child class.</p>
<p>The general syntax of multiple Inheritance is as follows:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DerivedClassName</span>(<span class="hljs-params">Base1, Base2, Base3</span>):</span>
    &lt;statement<span class="hljs-number">-1</span>&gt;
    .
    .
    .
    &lt;statement-N&gt;
</code></pre>
<p>Let’s extend our vehicle example using the multiple inheritance property. Here in this example, we will create 3 classes i.e. <code>Vehicle</code>, <code>Cost</code> and <code>Car</code></p>
<p>The classes <code>Vehicle</code> and <code>Cost</code> will be the Parent class. A <code>Vehicle</code> class represents the general property while the <code>Cost</code> class represents its pricing.</p>
<p>As <code>Car</code> has a general property and cost will have two parent classes. Thus we will inherit multiple parent classes.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, brand_name</span>):</span>
        self.brand_name = brand_name

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_brand_name</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.brand_name


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cost</span>:</span>        

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, cost</span>):</span>
        self.cost = cost

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_cost</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.cost


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>(<span class="hljs-params">Vehicle, Cost</span>):</span>    

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, brand_name, model, cost</span>):</span> 
        self.model = model 
        Vehicle.__init__(self, brand_name) 
        Cost.__init__(self, cost) 

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_description</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.get_brand_name() + self.model + <span class="hljs-string">" is the car "</span> + <span class="hljs-string">"and it's cost is "</span> + self.get_cost()

c = Car(<span class="hljs-string">"Audi "</span>,  <span class="hljs-string">"r8"</span>, <span class="hljs-string">"2 cr"</span>)
print(<span class="hljs-string">"Car description:"</span>, c.get_description())
</code></pre>
<p>Here you will find one thing in the above program that is different from all the other programs in this tutorial. I have used <code>Vehicle.__init__(self, brand_name)</code> in the constructor of <code>Car</code> class. This is one way of calling attributes from the parent class. Another was is <code>super</code> which I have explained above.</p>
<p>When we run the above program we will get the following output:</p>
<pre><code class="lang-python">Car description: Audi r8 <span class="hljs-keyword">is</span> the car <span class="hljs-keyword">and</span> it<span class="hljs-string">'s cost is 2 cr</span>
</code></pre>
<p>Though it can be used effectively, multiple inheritance should be done with care so that our programs do not become ambiguous and difficult for other programmers to understand.</p>
<h2 id="heading-polymorphism">Polymorphism:</h2>
<p>The word polymorphism means having many forms. In programming, polymorphism means same function name (but different signatures) being uses for different types.</p>
<p>Let’s extend our car program using polymorphism. We will create two classes, <code>Car</code> and <code>Bike</code>. Both the classes have common method or function, but they are printing different data. The program is pretty self-explanatory:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span> 

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">company</span>(<span class="hljs-params">self</span>):</span> 
        print(<span class="hljs-string">"Car belongs to Audi company."</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">model</span>(<span class="hljs-params">self</span>):</span> 
        print(<span class="hljs-string">"The Model is R8."</span>) 

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">color</span>(<span class="hljs-params">self</span>):</span> 
        print(<span class="hljs-string">"The color is silver."</span>) 

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bike</span>:</span> 

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">company</span>(<span class="hljs-params">self</span>):</span> 
        print(<span class="hljs-string">"Bike belongs to pulsar company."</span>) 

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">model</span>(<span class="hljs-params">self</span>):</span> 
        print(<span class="hljs-string">"The Model is dominar."</span>) 

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">color</span>(<span class="hljs-params">self</span>):</span> 
        print(<span class="hljs-string">"The color is black."</span>) 

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span>(<span class="hljs-params">obj</span>):</span> 
    obj.company() 
    obj.model() 
    obj.color() 

car = Car() 
bike = Bike() 

func(car) 
func(bike)
</code></pre>
<p>When we run the above code we will get the following output:</p>
<pre><code class="lang-shell">Car belongs to Audi company.
The Model is R8.
The color is silver.
Bike belongs to pulsar company.
The Model is dominar.
The color is black.
</code></pre>
<h2 id="heading-encapsulation">Encapsulation:</h2>
<p>In most object-oriented programming, we can restrict access to methods and variables. This can prevent the data from being modified by accident and is known as encapsulation.</p>
<p>Let’s use encapsulation in our car example. Now imagine we have a super-secret engine. In the first example, we will hide our engine using a <strong>private variable</strong>. In the second example, we will hide our engine using a <strong>private method</strong>.</p>
<p><strong>Example 1:</strong></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self</span>):</span> 
    self.brand_name = <span class="hljs-string">'Audi '</span>
    self.model = <span class="hljs-string">'r8'</span>
    self.__engine = <span class="hljs-string">'5.2 L V10'</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_description</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self.brand_name + self.model + <span class="hljs-string">" is the car"</span>

c = Car()
print(c.get_description)
print(c.__engine)
</code></pre>
<p>In this example <code>self.__engine</code> is a private attribute. When we run this program we will get the following output.</p>
<pre><code class="lang-shell">Audi r8 is the car
AttributeError: 'Car' object has no attribute '__engine'
</code></pre>
<p>We get an error that <code>Car</code> object doesn't have _engine because it is a private object.</p>
<p><strong>Example 2:</strong></p>
<p>We can also define a private method by adding <code>__</code> in front of the method name. Following is the example of how we can define a private method.</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self</span>):</span>
      self.brand_name = <span class="hljs-string">'Audi '</span>
      self.model = <span class="hljs-string">'r8'</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__engine</span>(<span class="hljs-params">self</span>):</span>
      <span class="hljs-keyword">return</span> <span class="hljs-string">'5.2 L V10'</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_description</span>(<span class="hljs-params">self</span>):</span>
      <span class="hljs-keyword">return</span> self.brand_name + self.model + <span class="hljs-string">" is the car"</span>


c = Car()
print(c.get_description())
print(c.__engine())
</code></pre>
<p>In this example <code>def __engine(self)</code> is a private method. When we run this program we will get the following output.</p>
<pre><code class="lang-shell">Audi r8 is the car
AttributeError: 'Car' object has no attribute '__engine'
</code></pre>
<p>Now suppose we want to access the private attribute or method we can do it in the following way:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self</span>):</span>
      self.brand_name = <span class="hljs-string">'Audi '</span>
      self.model = <span class="hljs-string">'r8'</span>
      self.__engine_name = <span class="hljs-string">'5.2 L V10'</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__engine</span>(<span class="hljs-params">self</span>):</span>
      <span class="hljs-keyword">return</span> <span class="hljs-string">'5.2 L V10'</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_description</span>(<span class="hljs-params">self</span>):</span>
      <span class="hljs-keyword">return</span> self.brand_name + self.model + <span class="hljs-string">" is the car"</span>


c = Car()
print(c.get_description())
print(<span class="hljs-string">"Accessing Private Method: "</span>, c._Car__engine()) 
print(<span class="hljs-string">"Accessing Private variable: "</span>, c._Car__engine_name)
</code></pre>
<p>The output of the following program is:</p>
<pre><code class="lang-shell">Audi r8 is the car
Accessing Private Method:  5.2 L V10
Accessing Private variable:  5.2 L V10
</code></pre>
<p>Encapsulation gives you more control over the degree of coupling in your code. It allows a class to change its implementation without affecting other parts of the code.</p>
<h2 id="heading-decorator">Decorator:</h2>
<p>Imagine you have to extend the functionality of multiple functions. How will you do that?</p>
<p>Well, one way is you can make functional calls and in that function, you can handle it. Making changes in 30 to 40 function calls and remembering where to place the call is a messy task. But the more elegant way provided by Python is with decorators.</p>
<p>What is a decorator? A decorator is a function that takes a function and extends its functionality without modifying it explicitly. Well, I understand if you are still confused about what decorators are. Don't worry – we have a tool named example to explain it.</p>
<p>Let's try an example to understand the decorator. There are two ways to write a decorator.</p>
<h3 id="heading-method-1">Method 1</h3>
<p>We declare a decorator function and in the arguments of the function we expect the function to be passed as an argument. Inside that, we write a wrapper function where operations are carried out and it is returned.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_decorator</span>(<span class="hljs-params">func</span>):</span>
<span class="hljs-meta">... </span>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrapper</span>():</span>
<span class="hljs-meta">... </span>            print(<span class="hljs-string">"Line Number 1"</span>)
<span class="hljs-meta">... </span>            func()
<span class="hljs-meta">... </span>            print(<span class="hljs-string">"Line Number 3"</span>)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> wrapper
...
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say_hello</span>():</span>
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Hello I am line Number 2"</span>)
...
</code></pre>
<p>To call the function we assign the decorator with <code>say_hello</code> as an argument.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>say_hello = my_decorator(say_hello)
</code></pre>
<p>We can also check the reference using <code>say_hello</code>. We will get the output that tells us it has been wrapped by the <code>my_decorator</code> function.</p>
<pre><code class="lang-python">&lt;function my_decorator.&lt;locals&gt;.wrapper at <span class="hljs-number">0x10dc84598</span>&gt;
</code></pre>
<p>Let's call our <code>say_hello</code> function:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>say_hello()
Line Number <span class="hljs-number">1</span>
Hello I am line Number <span class="hljs-number">2</span>
Line Number <span class="hljs-number">3</span>
</code></pre>
<p>See the magic the line "Hello I am line Number 2" gets printed in between Line Number 1 and 3 because the function call gets executed there.</p>
<p>Method 1 is clunky, and because of that many people prefer a different approach.</p>
<h3 id="heading-method-2">Method 2</h3>
<p>Here our decorator declaration remains same but we change how the call is assigned to that decorator. Whichever function requires that decorator wraps itself with <code>@decorator_name</code>.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">my_decorator</span>(<span class="hljs-params">func</span>):</span>
<span class="hljs-meta">... </span>    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wrapper</span>():</span>
<span class="hljs-meta">... </span>            print(<span class="hljs-string">"Line Number 1"</span>)
<span class="hljs-meta">... </span>            func()
<span class="hljs-meta">... </span>            print(<span class="hljs-string">"Line Number 3"</span>)
<span class="hljs-meta">... </span>    <span class="hljs-keyword">return</span> wrapper
...
<span class="hljs-meta">&gt;&gt;&gt; </span>@my_decorator
<span class="hljs-meta">... </span><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say_hello</span>():</span>
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Hello I am line Number 2"</span>)
...
<span class="hljs-meta">&gt;&gt;&gt; </span>say_hello()
</code></pre>
<p>Output is the same:</p>
<pre><code class="lang-shell">Line Number 1
Hello I am line Number 2
Line Number 3
</code></pre>
<p>A decorator is a powerful tool and it is used in the following development scenarios of an application:</p>
<ul>
<li><p>Setup logger</p>
</li>
<li><p>Setup configuration</p>
</li>
<li><p>Setup Error catching</p>
</li>
<li><p>Extending common functionality for all function and classes</p>
</li>
</ul>
<h2 id="heading-exceptions">Exceptions:</h2>
<p>When we were learning various syntax we came across various errors. Those errors occurred because of the syntax. But in a real-world application, errors (or commonly known as bugs) not only occur due to syntax issues but also because of network errors or some other cause.</p>
<p>To handle these issues we use Try - Except. In <code>try</code> block, we write the expression that we want to be executed, while in <code>except</code> block we catch the error. The Try-Except block looks as follows:</p>
<pre><code class="lang-python"><span class="hljs-keyword">try</span>:
    expression
<span class="hljs-keyword">except</span>:
    catch error
</code></pre>
<p>Let's understand this by an example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">try</span>:
<span class="hljs-meta">... </span>    print(value)
<span class="hljs-meta">... </span><span class="hljs-keyword">except</span>:
<span class="hljs-meta">... </span>    print(<span class="hljs-string">"Something went wrong"</span>)
...
</code></pre>
<p>Here we are trying to print the value variable but it is not defined. So we get the following output:</p>
<pre><code class="lang-shell">Something went wrong
</code></pre>
<p>You may be thinking that the line "something went wrong" is not that helpful. So how can we know what went wrong here?</p>
<p>We can print the exception and use it to find out what went wrong. Let's test this in our example:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">try</span>:
<span class="hljs-meta">... </span>    print(value)
<span class="hljs-meta">... </span><span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-meta">... </span>    print(e)
...
</code></pre>
<p>And the result is:</p>
<pre><code class="lang-shell">name 'value' is not defined
</code></pre>
<p>Whoa! That's magic. It is notifying me that 'value' is not defined.</p>
<p>Python also provides a tool named <code>raise</code>. Suppose you don't want a certain condition to occur and if it occurs you want to raise it. In such condition you can use <code>raise</code>. Consider the example below:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>i = <span class="hljs-number">5</span>
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">if</span> i &lt; <span class="hljs-number">6</span>:
<span class="hljs-meta">... </span>    <span class="hljs-keyword">raise</span> Exception(<span class="hljs-string">"Number below 6 are not allowed"</span>)
...
</code></pre>
<p>The output we get is as follows:</p>
<pre><code class="lang-shell">Traceback (most recent call last):
  File "&lt;stdin&gt;", line 2, in &lt;module&gt;
Exception: Number below 6 are not allowed
</code></pre>
<p>There are many sub-types of Exceptions, so I recommend that you go through the <a target="_blank" href="https://docs.python.org/3/tutorial/errors.html#errors-and-exceptions">Python Documentation</a> to understand them.</p>
<h2 id="heading-package-import">Package Import:</h2>
<p>You have learned the basics of Python and now you are all ready to build awesome applications. But hold on – we are still missing some important topics.</p>
<p>Without package import, you will be forced to write everything in one single file. Imagine what a mess it will be.</p>
<p>Create two files named <code>main.py</code> and <code>hello.py</code>. Remember both file needs to be in the same directory.</p>
<p>Under <code>hello.py</code> copy paste the following code:</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">say_hello</span>():</span>
    print(<span class="hljs-string">"Hello world"</span>)
</code></pre>
<p>Under <code>main.py</code> copy paste the following code:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> hello

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    hello.say_hello()
</code></pre>
<p>In <code>hello.py</code> we have declared a <code>say_hello()</code> function which prints "Hello world". In <code>main.py</code> you'll see an import statement. We are importing the hello module and calling the <code>say_hello()</code> function from that module.</p>
<p>Run our program using the following command:</p>
<pre><code class="lang-shell">➜ python main.py
</code></pre>
<p>Output:</p>
<pre><code class="lang-shell">Hello world
</code></pre>
<p>Now let's understand how to import a module which is in another directory.</p>
<p>Let's create a directory named "data" and move our <code>hello.py</code> inside that directory.</p>
<p>Go to the <code>main.py</code> and change the previous import statement.</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> data <span class="hljs-keyword">import</span> hello

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    hello.say_hello()
</code></pre>
<p>There are two ways to import from a directory.</p>
<ul>
<li><p>Method 1: <code>from data import hello</code></p>
</li>
<li><p>Method 2: <code>import data.hello</code></p>
</li>
</ul>
<p>I prefer method 1 because of its readability. You can choose whichever method looks better to you.</p>
<p>Let's run our application using the following command:</p>
<pre><code class="lang-shell">➜ python main.py
</code></pre>
<p>And an error occurs. Wait why did this happen? We did everything right. Let's go through the error:</p>
<pre><code class="lang-shell">Traceback (most recent call last):
  File "main.py", line 1, in &lt;module&gt;
    from data import hello
ImportError: No module named data
</code></pre>
<p>Well Python is telling us that it doesn't recognize a module named data. To solve this issue create a <code>__init__.py</code> inside data directory. Leave the file blank and run the program again and you'll get the following output:</p>
<pre><code class="lang-shell">Hello world
</code></pre>
<p>Well python by default does not treat a directory as a module. To inform Python to treat a directory as a module, <code>__init__.py</code> is required.</p>
<h2 id="heading-json-handling">JSON Handling:</h2>
<p>If you have worked previously with web development or app development you may be aware that all the API calls take place in JSON format. While JSON looks similar to a dictionary in Python, remember that it's very different.</p>
<p>To handle JSON, Python provides an inbuilt <code>json</code> package. To use this package we need to import it as follows:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json
</code></pre>
<p>This library provides two methods which help us in handling the JSON. Let's understand them one by one.</p>
<h3 id="heading-json-loads">JSON loads:</h3>
<p>If you have a JSON string and want to convert it back to a dictionary you need to use the <code>loads</code> method. Go to the Python shell and copy-paste the following code:</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">import</span> json
<span class="hljs-meta">&gt;&gt;&gt; </span>json_string = <span class="hljs-string">'{ "user_name":"Sharvin", "age":1000}'</span> <span class="hljs-comment">#JSON String</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>type(json_string)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">str</span>'&gt;
&gt;&gt;&gt; <span class="hljs-title">data</span> = <span class="hljs-title">json</span>.<span class="hljs-title">loads</span>(<span class="hljs-params">json_string</span>)
&gt;&gt;&gt; <span class="hljs-title">type</span>(<span class="hljs-params">data</span>)
&lt;<span class="hljs-title">class</span> '<span class="hljs-title">dict</span>'&gt;
&gt;&gt;&gt; <span class="hljs-title">data</span>
{'<span class="hljs-title">user_name</span>':</span> <span class="hljs-string">'Sharvin'</span>, <span class="hljs-string">'age'</span>: <span class="hljs-number">1000</span>}
</code></pre>
<h3 id="heading-json-dumps">JSON dumps:</h3>
<p>Now let's convert our data back to the JSON string format using the <code>dumps</code> method.</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>jsonString = json.dumps(data)
<span class="hljs-meta">&gt;&gt;&gt; </span>type(jsonString)
&lt;<span class="hljs-class"><span class="hljs-keyword">class</span> '<span class="hljs-title">str</span>'&gt;
&gt;&gt;&gt; <span class="hljs-title">jsonString</span>
'{"<span class="hljs-title">user_name</span>":</span> <span class="hljs-string">"Sharvin"</span>, <span class="hljs-string">"age"</span>: <span class="hljs-number">1000</span>}<span class="hljs-string">'</span>
</code></pre>
<p>To learn more about JSON Manipulation, go through the <a target="_blank" href="https://docs.python.org/3/library/json.html">Python Documentation</a>.</p>
<h2 id="heading-thats-it">That's it!</h2>
<p>And we're done! I hope you now understand the basics of Python. Congratulations! That's a huge achievement.</p>
<p>Feedback is welcomed. Also if you want to learn about any other topic you can tweet the topic name on Twitter and include my Twitter handle. [ <strong>@sharvinshah26</strong> ]</p>
<blockquote>
<p>Feel free to connect with me on <a target="_blank" href="https://twitter.com/sharvinshah26">Twitter</a> and <a target="_blank" href="https://github.com/Sharvin26">Github</a>.</p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Set Your Future Self Up for Success with Good Coding Habits ]]>
                </title>
                <description>
                    <![CDATA[ Think before you code. You have the power to make your future self's life Heaven on Earth or a living hell. In this article we'll explore what kinds of things you can do to make it a little easier on your future self. Revisiting "prior art" We've all... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/set-future-you-up-for-success-with-good-coding-habits/</link>
                <guid isPermaLink="false">66bee9536769d2581da6f951</guid>
                
                    <category>
                        <![CDATA[ Self-awareness ]]>
                    </category>
                
                    <category>
                        <![CDATA[ 100Days100Projects ]]>
                    </category>
                
                    <category>
                        <![CDATA[ clean code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Code Quality ]]>
                    </category>
                
                    <category>
                        <![CDATA[ code review ]]>
                    </category>
                
                    <category>
                        <![CDATA[ fundamentals ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ learn to code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Self Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ self-improvement  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Colby Fayock ]]>
                </dc:creator>
                <pubDate>Thu, 16 Apr 2020 14:45:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/04/set-up-for-success.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Think before you code. You have the power to make your future self's life Heaven on Earth or a living hell.</p>
<p>In this article we'll explore what kinds of things you can do to make it a little easier on your future self.</p>
<h2 id="heading-revisiting-prior-art">Revisiting "prior art"</h2>
<p>We've all been there. Six months into a project, you're trying to squash a bug, and what you find is shocking. You might be asking yourself, "who would write this kind of code?"</p>
<p>So you dig through your <a target="_blank" href="https://git-scm.com/">git</a> commit history using <code>git log -p filename.js</code> showing changes for a specific file, trying to see who would come up with something like that. And then your heart drops – you're the one who wrote it!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/04/tituss-burgess-shocked.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Tituss Burgess shocked</em></p>
<p>This is a common scenario for any developer, experienced or new. If you haven't hit that scenario, I promise if you stick with coding long enough, you will.</p>
<h2 id="heading-becoming-more-conscious-about-our-coding-habits">Becoming more conscious about our coding habits</h2>
<p>That six month reflection point is inevitable. That's a lot of time which you've most likely been using to work on other parts of the project or another project completely. Chances are, you've leveled up which has changed the way you write code.</p>
<p>On the other hand, sometimes it takes stepping outside of the code to see the bigger picture and get a better look at how all the pieces fit together. We can naturally dig ourselves too deep into a solution and can become a little narrowly focused as we work to solve those challenges.</p>
<p>But either way, while part of the code journey will simply be gaining more experience and learning more about your craft, there are other small habits you can get used to early on that will help you down the road.</p>
<p>So let's jump in.</p>
<h2 id="heading-improving-the-readability-of-your-code">Improving the readability of your code</h2>
<h3 id="heading-what-is-the-challenge">What is the challenge?</h3>
<p>Part of the fun about our craft is there are a ton of ways you can do the same thing. Think an <code>if</code> statement is too many lines? Well we can write it <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator">ternary</a> style!</p>
<pre><code class="lang-js"><span class="hljs-comment">// Example ternary statement</span>
<span class="hljs-keyword">const</span> isFreezing = temperature &lt;= <span class="hljs-number">32</span> ? <span class="hljs-literal">true</span> : <span class="hljs-literal">false</span>;
</code></pre>
<p>But sometimes this takes a toll on the readability of your code. While it might seem like it looks nice and super clean on one line, imagine that as that ternary gets more complex, someone will have to spend more time understanding what it means.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> minutes = <span class="hljs-number">30</span>;
<span class="hljs-keyword">const</span> cookie = {
  <span class="hljs-attr">color</span>: <span class="hljs-string">'black'</span>
};

<span class="hljs-keyword">const</span> cookieStatus = minutes &gt; <span class="hljs-number">20</span> ? cookie.color === <span class="hljs-string">'black'</span> ? <span class="hljs-string">'burned'</span> : <span class="hljs-string">'done'</span> : <span class="hljs-string">'not done'</span>;
</code></pre>
<h3 id="heading-what-can-we-do-better">What can we do better?</h3>
<p>Now I would imagine most of us can figure out what <code>cookieStatus</code> is in this example (spoilers: <code>burned</code>). But think about the time you spent figuring it out. Whether an extra 1s or 2s, it forces you to spend additional cognitive energy to read through the code.</p>
<p>On the other hand:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> minutes = <span class="hljs-number">30</span>;
<span class="hljs-keyword">const</span> cookie = {
  <span class="hljs-attr">color</span>: <span class="hljs-string">'black'</span>
};
<span class="hljs-keyword">let</span> cookieStatus;

<span class="hljs-keyword">if</span> ( minutes &lt;= <span class="hljs-number">20</span> ) {
  cookieStatus = <span class="hljs-string">'not done'</span>;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ( cookie.color === <span class="hljs-string">'black'</span> ) {
  cookieStatus = <span class="hljs-string">'burned'</span>;
} <span class="hljs-keyword">else</span> {
  cookieStatus = <span class="hljs-string">'done'</span>;
}
</code></pre>
<p>No, it may not be as clean or clever as the 1-line ternary statement, but the next time you visit it, the less you'll have to think about what the answer is. </p>
<p>It's also going to make it that much easier for bugs to creep up and get past your code reviewers when all of your code changes are in a 1-line git diff.</p>
<p>And yes, this is a simple example. But imagine this in a real world scenario with important business logic where you could run into these situations frequently.  </p>
<p>Say you need to add another condition – that ternary is just going to keep getting more complex! You're just making it more difficult to debug or extend, where the <code>if</code> statements can continue on in an easily readable way.</p>
<p>For what it's worth, ternaries and other shortcuts can be simple and effective in code, but don't abuse that effectiveness and end up making things more difficult.</p>
<h2 id="heading-keeping-things-consistent">Keeping things consistent</h2>
<h3 id="heading-what-is-the-challenge-1">What is the challenge?</h3>
<p>We all have our favorite way to code. Though I'd argue not including a semicolon at the end of your JavaScript is just completely wrong, you might prefer to write your code the wrong way without them.</p>
<pre><code class="lang-jsx"><span class="hljs-comment">// Jim's code style</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyComponent</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleOnClick</span>(<span class="hljs-params"></span>) </span>{
    alert(<span class="hljs-string">'Click!'</span>)
  }
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleOnClick}</span>&gt;</span>My Button<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>
  )
}

<span class="hljs-comment">// Creed's code style</span>

<span class="hljs-keyword">const</span> MyComponent = <span class="hljs-function">() =&gt;</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> alert('Click!')}&gt;My Button<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>;
</code></pre>
<p>But it's not always about what <em>you</em> prefer. When working with a team, chances are, everyone's opinion on how code should look is slightly different. You might agree about the semi-colon, but disagree about whitespace.</p>
<p>And no one is wrong (except for the non-semi-coloners)! There are valid arguments to most code styles, whether for or against, but the solution isn't for everyone to write their code their own way.</p>
<h3 id="heading-what-can-we-do-better-1">What can we do better?</h3>
<p>Keeping code consistent is important to maintain code health. A typical goal is to "make the codebase look like one person wrote it."</p>
<p>The point isn't that one person gets their way, it's that the team came to a conclusion about a set of rules they would use that everyone would follow. Having this consistency provides less cognitive overhead as people work through the code. It gives everyone the ability to know what to expect when reading the code.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/04/linting-code.jpg" alt="Image" width="600" height="400" loading="lazy">
<em>Errors from linting code</em></p>
<p>And achieving this doesn't need to be hard. There are tools that can simply <a target="_blank" href="https://www.colbyfayock.com/2019/10/what-is-linting-and-how-can-it-save-you-time">check for these inconsistencies</a> like <a target="_blank" href="https://eslint.org/">Eslint</a> for Javascript. And even better, there is another level of tools like <a target="_blank" href="https://prettier.io/">Prettier</a> that <a target="_blank" href="https://www.colbyfayock.com/2019/11/dont-just-lint-your-code-fix-it-with-prettier">will fix it for you</a>!</p>
<h2 id="heading-commenting-your-code">Commenting your code</h2>
<h3 id="heading-what-is-the-challenge-2">What is the challenge?</h3>
<p>Keeping up with commenting your code is an important way to put context to complex logic. As much as we all want our code to be self-documenting, that's rarely the case.</p>
<p>Too often we find ourselves dealing with a block of code that just doesn't make sense. And even if it makes sense on its own, we might not be able to figure out how it fits in to the rest of the application.</p>
<h3 id="heading-what-can-we-do-better-2">What can we do better?</h3>
<p>By providing a good set of comments, you're setting up the next person who touches that code to have a better understanding of what the code is doing before they make a change.</p>
<pre><code class="lang-js"><span class="hljs-comment">// DONT CHANGE - WILL STOP MAKING MONEY</span>

<span class="hljs-keyword">const</span> shouldMakeMoney = <span class="hljs-literal">true</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">makeMoney</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">if</span> ( shouldMakeMoney ) {
    <span class="hljs-keyword">return</span> noMoney;
  }
  <span class="hljs-keyword">return</span> moreMoney;
}
</code></pre>
<p>While this is a silly example, it brings up a real world case. Businesses are increasingly dependent on being able to maintain a reliable website to make money. Whether this is as an ecommerce business or an ad giant, these websites rely on business logic that determine things like cost, taxes, discounts, and other math related things we tend to not want to think about, but could make or break a business on the internet.</p>
<p>But it's not all about the company you work for. Touching old code can be scary. It's even scarier when no one on your team was around when it was written, so no one knows what it does!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/04/patton-oswalt-hands-over-mouth.gif" alt="Image" width="600" height="400" loading="lazy">
<em>Patton Oswalt hands over mouth</em></p>
<p>While you might not be the next person who touches that code, try to help out your future friend who's tackling the next ticket involving that code. Because there's also a good chance you will be that person and you'll wish you remembered how it worked.</p>
<h2 id="heading-documenting-your-solutions">Documenting your solutions</h2>
<h3 id="heading-what-is-the-challenge-3">What is the challenge?</h3>
<p>Documentation is similar to commenting your code, but from a different perspective. Documentation and commenting are both about finding ways to describe a solution in a human-readable way that will ultimately give more context. But documentation is more about the overall solution rather than the implementation details.</p>
<p>Having a high performing application is everyone's goal. But how did we get there? There's a realistic chance that someone will have to be working on the same project you are like onboarding a new team member. How will they be able to maintain that high performance if they don't know how it works?</p>
<h3 id="heading-what-can-we-do-better-3">What can we do better?</h3>
<p>Whether it's introducing that new team member to the project or trying to share knowledge with another project team, documentation is an important part of maintaining a project. It helps keep everyone on the same page so we all confidently know what we're working towards.</p>
<p>For example, if we're still working with our ecommerce project with our business logic, there will be rules that the code needs to implement. While commenting might give details inline about how the rules were implemented, the documentation would define those rules.</p>
<pre><code class="lang-js"><span class="hljs-comment">/**
 * DOCUMENTATION
 * Order Total &gt;= 25: Discount %10
 * Order Total &gt;= 50: Discount %15
 * Order Total &gt;= 100: Discount %20
 * Order Total &gt;= 75: Free Shipping
 */</span>

<span class="hljs-keyword">const</span> orderSubTotal = <span class="hljs-number">84.00</span>;
<span class="hljs-keyword">let</span> orderTotal = orderSubTotal;

<span class="hljs-comment">// If the order total is under 75, apply shipping discount</span>

<span class="hljs-keyword">if</span> ( orderTotal &lt; <span class="hljs-number">75</span> ) {
  orderTotal = addShipping(orderTotal);
}

<span class="hljs-comment">// If the order total reaches a threshold, apply given discount</span>

<span class="hljs-keyword">if</span> ( orderTotal &gt;= <span class="hljs-number">100</span>) {
  orderTotal = applyDiscount(orderTotal, <span class="hljs-number">.2</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ( orderTotal &gt;= <span class="hljs-number">50</span> ) {
  orderTotal = applyDiscount(orderTotal, <span class="hljs-number">.15</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ( orderTotal &gt;= <span class="hljs-number">25</span> ) {
  orderTotal = applyDiscount(orderTotal, <span class="hljs-number">.1</span>);
}
</code></pre>
<p>This is a minimal example, but we can see the difference between the rules at the top and how we apply them. The documentation should clearly explain what the rules are, but it shouldn't care about how those rules were implemented.</p>
<p>On the other hand, the comments might not care about what the rules are, but need to implement them in an efficient and logical way. We should be able to update the code with the business rules, such as changing the top level discount tier from $100 to $80, without having to rework the code.</p>
<p>But documentation is much more than business rules – it's providing a way for anyone to understand your work from a higher level. This could include anything from architectural diagrams to the theory behind your core algorithm.</p>
<p>While maybe code isn't the best place for details like this to live, it's really important information that can help instill confidence in your project and give others an opportunity to understand more about the work.</p>
<h2 id="heading-creating-effective-pull-requests">Creating effective Pull Requests</h2>
<h3 id="heading-what-is-the-challenge-4">What is the challenge?</h3>
<p>Pull requests (or merge requests) are a core part of any development team's project lifecycle. It provides a way to package and present your code in a consumable way for your peers to review and understand your work.</p>
<p>There's a lot that can go into a pull request from a single commit to the entirety of the next version of your website. That's a lot of context to expect someone to understand by reading through the commits alone.</p>
<h3 id="heading-what-can-we-do-better-4">What can we do better?</h3>
<p>Pull requests don't need to be an art. There should be one primary goal of the preparation you put into it – providing context into your changes. At a minimum, it should answer the questions of "what" and "why".</p>
<p>We can even use tools like pull request templates to push us in the right direction. <a target="_blank" href="https://www.freecodecamp.org/news/why-you-should-write-merge-requests-like-youre-posting-to-instagram-765e32a3ec9c/">Define an outline</a> of what you want explained and chances are, people will follow that outline. This helps avoid the 1-line "closes [ticket]" description or even worse, an empty description.</p>
<p>With my projects, I hope to have a few questions answered before I dive into a code review:</p>
<ul>
<li>What is the change?</li>
<li>What does it impact?</li>
<li>How do I reproduce or test the change?</li>
</ul>
<p>Just a few details around the change set can provide much needed context for those reviewing your code. It's easy to look at code, but it's harder to understand it without knowing how it fits into the bigger picture.</p>
<h2 id="heading-hardening-your-code-with-tests">Hardening your code with tests</h2>
<h3 id="heading-what-is-the-challenge-5">What is the challenge?</h3>
<p>Tests are a way to ensure your code runs the same way each time. Being able to prove that the same input will always have the same output will help provide you and your team with a higher level of confidence that your application won't come crashing down with the next small change.</p>
<p>Without them, we're left with human error, where no matter how good your QA Engineer is (shoutout to my testers out there), something will always slip through. And that's not to say your tests will always catch every problem, but we can use the tools available to help prevent it.</p>
<h3 id="heading-what-can-we-do-better-5">What can we do better?</h3>
<p>Where comments are a way of providing the context of how something works, test are a way to ensure they work. Providing test cases that are repeatable helps enforce that.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">applyDiscount</span>(<span class="hljs-params">value, discount</span>) </span>{
  <span class="hljs-keyword">const</span> discountAmount = value * discount;
  <span class="hljs-keyword">return</span> value - discountAmount;
}

expect(applyDiscount(<span class="hljs-number">10</span>, <span class="hljs-number">.1</span>)).toEqual(<span class="hljs-number">.9</span>);
expect(applyDiscount(<span class="hljs-number">532151235</span>, <span class="hljs-number">.1054</span>)).toEqual(<span class="hljs-number">476062494.831</span>);
</code></pre>
<p>If I fudge the math on our <code>applyDiscount</code> function above, there's a high probability that the test would fail (never say never).</p>
<p>But testing doesn't need to be hard. There are many tools out there that help from different perspectives. For example, you might use <a target="_blank" href="https://jestjs.io/">Jest</a> to run your unit tests or add <a target="_blank" href="https://enzymejs.github.io/enzyme/">Enzyme</a> on top to test your React components. But you can also bring in <a target="_blank" href="https://www.cypress.io/">Cypress</a> as an integration test solution that will work like a robot clicking through your application to make sure all the components actually work together.</p>
<p>There's also different methodologies of testing. While you probably see most teams write their tests after they have a working solution, some people swear by <a target="_blank" href="https://en.wikipedia.org/wiki/Test-driven_development">test-driven development</a>. They might write their tests first where the code has to pass the tests rather than the other way around. This is a great way to define the requirements of the code before diving right in.</p>
<p>Whatever the method, capture the points that are most likely to break or the functions that add the most business value. You'll be helping to prevent a potential business loss or even simpler, a headache.</p>
<h2 id="heading-what-can-we-learn-from-this">What can we learn from this?</h2>
<p>That might be a lot to digest, but they're important points to consider as you grow as a developer. Starting these habits early in your career will help you naturally build these skills and work that way by default.</p>
<p>And if you're late in your career, it's never too late to start. We should all want to strive to be the best developer we can be and do our best to help make our teammate's lives easier, as we're all in this together.</p>
<h2 id="heading-looking-for-more-to-learn">Looking for more to learn?</h2>
<ul>
<li><a target="_blank" href="https://www.colbyfayock.com/2019/08/put-down-the-javascript-learn-html-css">Put Down the Javascript - Learn HTML &amp; CSS</a></li>
<li><a target="_blank" href="https://www.colbyfayock.com/2020/02/how-to-become-a-full-stack-web-developer-in-2020">How to Become a Full Stack Web Developer in 2020</a></li>
<li><a target="_blank" href="https://www.colbyfayock.com/2020/02/what-is-the-jamstack-and-how-do-i-get-started">What is the JAMstack and how do I get started?</a></li>
<li><a target="_blank" href="https://www.colbyfayock.com/2019/10/what-is-linting-and-how-can-it-save-you-time">What is linting and how can it save you time?</a></li>
<li><a target="_blank" href="https://www.freecodecamp.org/news/why-you-should-write-merge-requests-like-youre-posting-to-instagram-765e32a3ec9c/">Why you should write merge requests like you’re posting to Instagram</a></li>
</ul>
<h2 id="heading-whats-your-advice-for-growing-as-a-developer">What’s your advice for growing as a developer?</h2>
<p><a target="_blank" href="https://twitter.com/colbyfayock">Share with me on Twitter!</a></p>
<div id="colbyfayock-author-card">
  <p>
    <a href="https://twitter.com/colbyfayock">
      <img src="https://res.cloudinary.com/fay/image/upload/w_2000,h_400,c_fill,q_auto,f_auto/w_1020,c_fit,co_rgb:007079,g_north_west,x_635,y_70,l_text:Source%20Sans%20Pro_64_line_spacing_-10_bold:Colby%20Fayock/w_1020,c_fit,co_rgb:383f43,g_west,x_635,y_6,l_text:Source%20Sans%20Pro_44_line_spacing_0_normal:Follow%20me%20for%20more%20JavaScript%252c%20UX%252c%20and%20other%20interesting%20things!/w_1020,c_fit,co_rgb:007079,g_south_west,x_635,y_70,l_text:Source%20Sans%20Pro_40_line_spacing_-10_semibold:colbyfayock.com/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_68,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_145,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_222,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/w_300,c_fit,co_rgb:7c848a,g_north_west,x_1725,y_295,l_text:Source%20Sans%20Pro_40_line_spacing_-10_normal:colbyfayock/v1/social-footer-card" alt="Follow me for more Javascript, UX, and other interesting things!" width="2000" height="400" loading="lazy">
    </a>
  </p>
  <ul>
    <li>
      <a href="https://twitter.com/colbyfayock">? Follow Me On Twitter</a>
    </li>
    <li>
      <a href="https://youtube.com/colbyfayock">?️ Subscribe To My Youtube</a>
    </li>
    <li>
      <a href="https://www.colbyfayock.com/newsletter/">✉️ Sign Up For My Newsletter</a>
    </li>
  </ul>
</div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How I built 100 Projects in 100 Days ]]>
                </title>
                <description>
                    <![CDATA[ By Florin Pop I finished the #100Days100Projects challenge ? In this article I’m going to cover my experience doing this challenge and I’m also going to answer some of the questions that I received on Twitter about this. It will be an “ask and answer... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-i-built-100-projects-in-100-days/</link>
                <guid isPermaLink="false">66d45eec264384a65d5a951e</guid>
                
                    <category>
                        <![CDATA[ 100Days100Projects ]]>
                    </category>
                
                    <category>
                        <![CDATA[ 100DaysOfCode ]]>
                    </category>
                
                    <category>
                        <![CDATA[ coding challenge ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 27 Dec 2019 11:17:27 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/12/Overview--100Days100Projects.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Florin Pop</p>
<p>I finished the <a target="_blank" href="https://www.florin-pop.com/blog/2019/09/100-days-100-projects">#100Days100Projects</a> challenge ? In this article I’m going to cover my experience doing this challenge and I’m also going to answer some of the questions that I received on <a target="_blank" href="https://twitter.com/florinpop1705">Twitter</a> about this.</p>
<p>It will be an “ask and answer” type of article, so I hope you enjoy it! ?</p>
<h2 id="heading-what-is-this-challenge-about">What is this challenge about?</h2>
<p>The main purpose of this challenge is to build a project every single day for 100 days in a row. You can read more about the rules <a target="_blank" href="https://www.florin-pop.com/blog/2019/09/100-days-100-projects">here</a>. (Note: there is also an easier variant: #30Days30Projects, in case you are interested for something lighter ?)</p>
<p>I started this challenge on <strong>17 September 2019</strong> and finished on <strong>25 December 2019</strong>.</p>
<h2 id="heading-what-projects-have-you-built-during-the-challenge">What projects have you built during the challenge?</h2>
<p>In the last project (#100) I added all the projects that I’ve built during the challenge, so if you want to check them out click <a target="_blank" href="https://codepen.io/FlorinPop17/full/VwYWMOa">here</a>.</p>
<p>All of the projects were web based - frontend mostly (HTML, CSS, JavaScript), and some of them had a little bit of NodeJS for the backend.</p>
<p>I used all sorts of APIs, some JS libraries (ReactJS, p5js - for working with the canvas), but most of the projects were done with Vanilla JS and CSS.</p>
<h2 id="heading-how-do-you-come-up-with-so-many-ideas">How do you come up with so many ideas?</h2>
<p>Apparently a lot of people have asked me this question over these 100 days.</p>
<p>It’s simple: I am a <strong>genius</strong>. ?</p>
<p>Just kidding ?... I’m far from being that.</p>
<p>The truth is that I gathered a lot of resources over the past year because I needed inspiration for my <a target="_blank" href="https://www.florin-pop.com/blog">articles</a>, <a target="_blank" href="https://www.youtube.com/channel/UCeU-1X402kT-JlLdAitxSMA">videos</a>, etc, and I’ve used them to get inspiration for these projects also.</p>
<p>Also, doing this creative process over and over and over, you end up eventually getting ideas from all the things around you. There are some projects that I did because I saw a need in my day-to-day life (Drink Water app, Random Picker Visualizer, Countdown, Password Generator, etc…).</p>
<p>Let’s not forget also that some of the projects were suggested by my <a target="_blank" href="https://twitter.com/florinpop1705">followers</a>. (Thank you guys ?)</p>
<p><strong>The resources that I’ve used mostly for inspiration</strong>:</p>
<ul>
<li><a target="_blank" href="https://dribbble.com">Dribbble</a></li>
<li><a target="_blank" href="https://codepen.io">CodePen</a></li>
<li><a target="_blank" href="http://collectui.com/designs">CollectUI</a></li>
</ul>
<h2 id="heading-did-you-have-a-plan-for-a-longer-period-of-time-or-did-you-do-it-spontaneously">Did you have a plan for a longer period of time or did you do it spontaneously?</h2>
<p>Out of the 100 projects, I think that only 2 or 3 were “planned” beforehand - like a day before. ?</p>
<p>The rest of them were just done spontaneously.</p>
<p>But I wouldn’t recommend doing that. It’s much better to have things planned out - it gets rid of the headache you might get when you have to come up with an idea right away.</p>
<h2 id="heading-why-did-you-do-it">Why did you do it?</h2>
<p><strong>I LOVE CHALLENGES</strong>! ?</p>
<p>As simple as that.</p>
<p>I love pushing my limits and this challenge looked like one that could do that (and it did!). Also, I’ve noticed that if I publicly announce that I’m doing something, I end up actually doing it - maybe because of the shame that I would feel if I fail. I don’t know. Haven’t failed a challenge yet. ?</p>
<p>The first challenge I did was a 365 days of running challenge. I publicly declared on Facebook that I was going to do it and I posted a picture daily with me after my runs. This gave me the strength to finish it (actually ended up doing 422 days). I couldn’t fail my followers, right? ?</p>
<p>And on the other hand, I knew that if I was going to post daily I would inspire others. I wanted to show people that even ordinary people like us can do stuff if we set our minds to it. We have a powerful mind, we just don’t use it at its full capacity!</p>
<p>Yes. I am an ordinary guy. Nothing special here. So if I did it, you can do it too!</p>
<h2 id="heading-how-much-work-did-it-take-to-create-a-project">How much work did it take to create a project?</h2>
<p>Some of the projects I did in under 30 minutes, a couple took me 6-8 hours and 1 or 2 I had to “postpone” to the 2nd day because I couldn’t finish them. Although I postponed these projects to the next day, I recreated them from scratch - only using the gained knowledge. This turned out to be a good way to get "unstuck". Just redo it from scratch.</p>
<p>I would say that I needed around 1-1.5 hours on average per day. This includes: idea + execution + social media.</p>
<p>Keep in mind that I’ve been coding since 2013, so maybe someone with less experience would need a little more time, but it really depends on the project difficulty.</p>
<h2 id="heading-what-motivated-you-to-keep-going">What motivated you to keep going?</h2>
<p>As I mentioned above, I think that the shame of failing kept me moving, considering that I publicly committed to the challenge. I can’t fail my audience!</p>
<p>I had a \$25 “punishment fee” which I would have to pay for the failing days, but I don’t think that I ever thought about it after I started.</p>
<p>I added the money aspect because I knew that it would be engaging for people to have it and some of them would want me to fail (sneaky you! ?) so they would check in daily - which ended up giving me a small boost of views on my posts ?.</p>
<h2 id="heading-what-have-you-learned-during-the-challenge">What have you learned during the challenge?</h2>
<p>I’ve learned a couple of things:</p>
<ul>
<li>Learned how to “sketch” and plan a project structure</li>
<li>Learned and played around with some technology and APIs that I haven’t used before</li>
<li>Learned more about CSS properties (positioning, animation, etc)</li>
<li>Learned more on how to work with the JS DOM API</li>
</ul>
<p>It ended up helping me solidify my vanilla JS and CSS skills!</p>
<h2 id="heading-what-would-you-do-differently">What would you do differently?</h2>
<p>One thing that I’ve noticed is that there were more <em>simpler</em> projects that I initially planned. I expected to have more apps with multiple functionalities, but considering the fact that this challenge wasn’t the only thing that I had going, I ended up doing more simple projects (like just a design implementation with HTML/CSS, no JavaScript).</p>
<p>If I would ever do this challenge again, I would either:</p>
<ul>
<li>Do fewer days (30 or 50) or</li>
<li>Focus 100% on the challenge (no more client work or other projects, etc)</li>
</ul>
<p>This way I could do more <em>“sophisticated”</em> apps.</p>
<p>Nevertheless, I’m happy with the projects that I’ve done considering the given situation.</p>
<p>Also, I ended up spending a lot of time posting on social media (Twitter, LinkedIn, Facebook, Instagram - for which I created a new image every time).</p>
<p>Maybe a good system to “share everywhere” would help to speed up the process.</p>
<p>Don’t get me wrong, posting on SM is important and I wouldn’t drop it, but I would speed it up if possible.</p>
<h2 id="heading-should-people-do-this-challenge">Should people do this challenge?</h2>
<p>If you want a good challenge. Yes.<br>
If you want to learn new things. Yes.<br>
If you want to create engagement with your audience. Yes.<br></p>
<p>Should you do 100 days? Depends.<br>
Do you have the time? Then do it. Otherwise I would only do the 30 days variant.</p>
<h2 id="heading-whats-next-for-you">What’s next for you?</h2>
<p>Another challenge. ?</p>
<p>For 2020 I’m planning to go all-in on <a target="_blank" href="https://www.youtube.com/channel/UCeU-1X402kT-JlLdAitxSMA">YouTube</a> so I’m going to start the year with a 31 days 31 videos challenge (more about it coming soon). I want to have a solid backlog of videos for my YouTube channel, so this challenge goes very well for that.</p>
<p>Who knows… maybe I’ll keep doing it if everything goes well.</p>
<p><a target="_blank" href="https://www.youtube.com/channel/UCeU-1X402kT-JlLdAitxSMA">Subscribe</a> if you want to follow my progress! ?</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>If you have any other questions, let me know and I’ll be happy to answer and update the article!</p>
<p>I hope this was helpful for you!</p>
<p>Thank you for reading! ?</p>
<p>Read more of my articles on my <a target="_blank" href="https://www.florin-pop.com/blog/built-100-projects-in-100-days/">blog</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Why You Should Use SVG Images: How to Animate Your SVGs and Make Them Lightning Fast ]]>
                </title>
                <description>
                    <![CDATA[ Why Are We Using SVG? The web development sector is growing at a rapid pace, and SVG (scalable vector graphics) images are becoming more popular. As vector images, SVGs are composed of mathematical equations that define the positioning and color of l... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/a-fresh-perspective-at-why-when-and-how-to-use-svg/</link>
                <guid isPermaLink="false">66be14ac2969640c348b568d</guid>
                
                    <category>
                        <![CDATA[ 100Days100Projects ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ SVG ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Oleh Romanyuk ]]>
                </dc:creator>
                <pubDate>Tue, 26 Nov 2019 14:51:07 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/11/svg.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <h2 id="heading-why-are-we-using-svg"><strong>Why Are We Using SVG?</strong></h2>
<p>The web development sector is growing at a rapid pace, and SVG (scalable vector graphics) images are becoming more popular. As vector images, SVGs are composed of mathematical equations that define the positioning and color of lines and curves which make up graphic shapes and text in XML format. SVGs are used for icons, logos, graphic designs, and fonts.</p>
<p>Using SVGs is an easy choice once you consider the advantages they offer. For a client, you get superb quality on any device. For us as developers, there are even more reasons to use SVG. </p>
<p>Let's discuss some of the benefits of SVG now.</p>
<h3 id="heading-1-text-based-format"><strong>1. Text-based format</strong></h3>
<p>SVG elements contain text, which greatly improves the accessibility of a website. But the main advantage is that this text is indexed by search engines. And a user can find an SVG file via Google.</p>
<h3 id="heading-2-scalability"><strong>2. Scalability</strong></h3>
<p>The quality of SVG images does not depend on the resolution. Unlike images of other formats or icon fonts, SVGs look perfectly sharp on any device with any screen size. Scalability also means that if you use the same image throughout the website but in different sizes, you use a single SVG. You do not have to create multiple copies of it as in the case of PNG. Instead, you embed the same image and define the size of it directly in SVG code.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/u6zLw9HQVb8DpDR1rhvHp/a5e491c95b5261e47e1c28098b2a2422/Copy_of_2.7_billion_people_use_smartphones__1_-min.png?fm=png&amp;q=85&amp;w=1000" alt="Scalability" width="600" height="400" loading="lazy"></p>
<h3 id="heading-3-high-performance"><strong>3. High performance</strong></h3>
<p>If you prioritize performance, you should use SVG. With SVG, there is no need for an HTTP request to load in an image file. The page loads faster as it has no files to download. Faster loading time translates into better webpage performance and higher search engine ranking. In turn, it improves user experience.</p>
<h3 id="heading-4-small-file-size"><strong>4. Small file size</strong></h3>
<p>The size of simple SVG files is defined by the colors, layers, gradients, effects, and masks that it contains. The size of a PNG or any other raster graphics file is defined by the number of pixels that it consists of. The larger a PNG image is, the heavier it gets in size. This is not the case for SVG icons, though. Also, SVGs can be optimized, and I will tell how later in this article.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/6TEDo4rR558facWlrHZjlT/ab71bf067cf14098d65c602eac3d5735/Copy_of_2.7_billion_people_use_smartphones__2_-min.png?fm=png&amp;q=85&amp;w=1000" alt="SVG file size" width="600" height="400" loading="lazy"></p>
<h3 id="heading-5-numerous-editing-and-animating-opportunities"><strong>5. Numerous editing and animating opportunities</strong></h3>
<p>Unlike raster images, vector images can be edited both in special vector drawing programs and directly in a text editor. You can also edit colors or sizes of SVG icons directly via CSS. As for animating SVGs, it can be done with the help of SMIL, Web Animations API, ​WebGL, or CSS animation. Scroll down to learn more about CSS animation of SVG images.</p>
<h3 id="heading-6-integration-with-html-xhtml-and-css"><strong>6. Integration with HTML, XHTML, and CSS</strong></h3>
<p>SVG was designed “to integrate with and extend other prominent open Web platform technologies, such as X/HTML, CSS, and Javascript”, according to <a target="_blank" href="https://dev.w3.org/SVG/proposals/svg-html/svg-html-proposal.html">W3C</a>. So, unlike different image formats, this format can be easily integrated with other documents and technologies.</p>
<h3 id="heading-7-w3c-document-object-model-support"><strong>7. W3C Document Object Model support</strong></h3>
<p>There is growing community support for SVG. The <a target="_blank" href="https://www.w3.org/">World Wide Web Consortium</a> (W3C) has always claimed that the Internet cannot do without vector images. This organization basically <a target="_blank" href="https://www.w3.org/2002/04/svg11-pressrelease">created the SVG format</a>, and they actively support it nowadays.</p>
<h2 id="heading-what-are-the-inconveniences-of-svg"><strong>What Are the Inconveniences of SVG?</strong></h2>
<p>The large number of small parts makes the use of the SVG format irrational. The more parts an image consists of, the heavier it grows in size.</p>
<p>For example, <a target="_blank" href="https://www.amcharts.com/svg-maps/?map=usa">here</a> are two SVG maps of the United States. The second one is slightly more detailed than the first one. But the higher level of detail cost almost a fivefold increase in file size – 33 KB compared to 147 KB. If this map was not monochromatic, the increase would be much greater.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/4xg76fiYHZzxpSxDXW9uFp/4f5161cd3efc5dd2ea0d9578388498f0/Copy_of_2.7_billion_people_use_smartphones__3_-min.png?fm=png&amp;q=85&amp;w=1000" alt="SVG maps" width="600" height="400" loading="lazy"></p>
<p>If the picture is linear and contains a few colors – SVG is a solution. However, if the details matter and there are a lot of them, PNG or JPEG may be more suitable.</p>
<p>Also note that SVG cannot be used for photographs. If you use a photograph on your website, SVG is not the best option. You definitely should go with a raster image format.</p>
<h2 id="heading-how-to-optimize-svg-images"><strong>How to Optimize SVG Images</strong></h2>
<p>When rendering a vector format, we have to write some extra SVG code. The end result should be optimized using different services. Most often, for optimizing SVG, I use a <a target="_blank" href="https://keenethics.com/services-web-development-node">Node.js</a> tool <a target="_blank" href="https://github.com/svg/svgo">SVGO</a>. It is quite easy to use, and there is no need to upload the images to other websites.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/7m9UpCpPW7GNLR9tXAvTca/4c804519c3dd110b650977f72785453a/0_8YcO63_4ajXq0qEb." alt="Example of SVG optimization using SVGO" width="600" height="400" loading="lazy">
<em>Example of SVG optimization using SVGO</em></p>
<h2 id="heading-how-to-animate-svg"><strong>How to Animate SVG</strong></h2>
<p>SVG graphics on the web can be animated in a number of ways:</p>
<ol>
<li>SMIL, which is the native SVG animation specification</li>
<li>Web Animations API, which is a native JavaScript API allowing you to create more complex sequential animations without loading any external scripts</li>
<li>​WebGL</li>
<li>CSS animation</li>
</ol>
<p>Let’s consider the last one.</p>
<p><strong>CSS animation</strong> is used in order to avoid overloading your service with big libraries for animating icons and loaders.</p>
<p>To see the example of SVG check the <a target="_blank" href="http://jsfiddle.net/yd3c81bg/9/embedded/html,css,result">animated yolk</a>, the graphic design of which was initially drawn in Sketch.</p>
<p><img src="https://images.ctfassets.net/6xhdtf1foerq/7JTBd4pwJJwKYmqgTSwScv/525b6d4e42c53c35961706390802bc5e/ezgif.com-crop.gif" alt="SVG gif" width="600" height="400" loading="lazy"></p>
<p>As you can see here, I use Keyframe Animation Syntax for animation. It is implemented by setting the initial position of an element by id (0%), transition (50%) and final position (100%). To achieve smooth animation, initial and final values are equal.</p>
<p>Here are some <strong>benefits</strong> of using the CSS approach to SVG animation:</p>
<ol>
<li>You do not need an external library.</li>
<li>Preprocessors (like Sass or Less) allow you to create variables.</li>
<li>You can use onAnimationEnd and some other animation hooks with native JavaScript.</li>
<li>This approach is easy to use for responsive web design development because you can modify your animation with media queries.</li>
</ol>
<p>The <strong>downsides</strong> of using CSS animation are the following:</p>
<ol>
<li>You cannot produce some complex physics effects, which would make the animation more realistic.</li>
<li>A lot of recalculation needs to be done if you adjust timing.</li>
<li>CSS and SVG graphics on mobile sometimes require strange hacks.</li>
</ol>
<h2 id="heading-for-example"><strong>For Example</strong></h2>
<p>Still, we can make some interesting projects with the help of simple and trivial CSS animation. For example, I have made a simple game-video using HTML, CSS, and a little bit of JavaScript. All the SVGs were drawn in Sketch. The objective of this game is to save the princess. In any situation, just click. You can find the project at my <a target="_blank" href="https://github.com/maryna-yanul/duck-the-princess/">GitHub</a>.</p>
<h2 id="heading-to-wrap-up"><strong>To Wrap Up</strong></h2>
<p>SVGs are a great image format to go with. They are scalable, lightweight, text-based, and efficient. They are easy to edit, animate, and integrate. Importantly, they are supported by almost any browser except Internet Explorer 8 and Android 2.3.</p>
<p>While learning to work with scalable vector graphics images may take you some time, it is an investment that will pay off considering the benefits of SVG.</p>
<h2 id="heading-do-you-have-an-idea-for-a-software-project">Do you have an idea for a software project?</h2>
<p>My company KeenEthics is a team of experienced <a target="_blank" href="https://keenethics.com/services-web-development">web application developers</a>. In case you need a free estimate of a similar project, feel free to <a target="_blank" href="https://keenethics.com/contacts?activeForm=estimate">get in touch</a><em>.</em></p>
<p>You can read more of similar articles on my Keen Blog. Allow me to suggest you read <a target="_blank" href="https://keenethics.com/blog/the-value-of-user-testing">The Value of User Testing</a> or <a target="_blank" href="https://www.freecodecamp.org/news/7-cases-when-not-to-use-docker/">7 Cases When You Should Not Use Docker</a>. </p>
<h2 id="heading-ps">P.S.</h2>
<p>Also, I would like to say "thank you" to <a target="_blank" href="https://www.linkedin.com/in/yanul/">Maryna Yanul</a> for coauthoring this article as well as the readers for making it to the end!</p>
<p>The original article posted on KeenEthics blog can be found here: <a target="_blank" href="https://keenethics.com/blog/1478674800000-svg-animation-scalable-vector-graphics">A Fresh Perspective at Why, When, and How to Use SVG</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create a Random Meal Generator ]]>
                </title>
                <description>
                    <![CDATA[ By Florin Pop Last week I decided to take on a new challenge. I called it: The #100Days100Projects Challenge. The purpose of the challenge is to create one project every single day. Think of it as a next step for the #100DaysOfCode challenge. A proje... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/creating-a-random-meal-generator/</link>
                <guid isPermaLink="false">66d45ee0b6b7f664236cbdc2</guid>
                
                    <category>
                        <![CDATA[ 100Days100Projects ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sun, 22 Sep 2019 22:00:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/09/random-meal-generator-1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Florin Pop</p>
<p>Last week I decided to take on a new challenge. I called it: The <a target="_blank" href="https://www.florin-pop.com/blog/2019/09/100-days-100-projects">#100Days100Projects</a> Challenge.</p>
<p>The purpose of the challenge is to create one project every single day. Think of it as a next step for the #100DaysOfCode challenge.</p>
<p>A project can be either:</p>
<ul>
<li>an app</li>
<li>a component</li>
<li>a website</li>
<li>a game</li>
<li>a library
and so on...</li>
</ul>
<p>The programming language used is also not important, but I need to complete the project by 11:59 PM (my time), otherwise I'm "punishing" myself by giving away $5 for 5 people ($25 total) - first 5 people who point it out on Twitter that I missed the deadline. ?</p>
<p>If you want to join, you can read more about this challenge and the other variants it has <a target="_blank" href="https://www.florin-pop.com/blog/2019/09/100-days-100-projects">here</a>. </p>
<p><strong>Note</strong>: you don't have to give away $5 if you fail, just set some other "punishment" for yourself. Also, there are other variants with less days (<strong>7Days7Projects</strong> and <strong>30Days30Projects</strong>) if you don't feel like taking on the 100Days challenge.</p>
<hr>
<p>For the first project in the <a target="_blank" href="https://florin-pop.com/blog/2019/09/100-days-100-projects">#100Days100Projects</a> I thought about working with a public API in order to get some data that would be displayed in a webpage - an usual thing to do with an API.</p>
<p>For that I chose to use <a target="_blank" href="https://www.themealdb.com">TheMealDB</a>'s public API in order to get some random meals by pressing a button. Something straightforward! ?</p>
<p>Check out the live version of what we're going to build in this article over on <a target="_blank" href="https://codepen.io/FlorinPop17/full/WNeggor">CodePen</a>:</p>
<div class="embed-wrapper">
        <iframe width="100%" height="350" src="https://codepen.io/FlorinPop17/embed/WNeggor" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="CodePen embed" scrolling="no" allowtransparency="true" allowfullscreen="true" loading="lazy"></iframe></div>
<p>As always let's start at the beginning:</p>
<h2 id="heading-the-html">The HTML</h2>
<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">"container"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row text-center"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>
            Feeling hungry?
        <span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h5</span>&gt;</span>Get a random meal by clicking below<span class="hljs-tag">&lt;/<span class="hljs-name">h5</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"button-primary"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"get_meal"</span>&gt;</span>Get Meal ?<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">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"meal"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row meal"</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>
</code></pre>
<p>We have a little text, but the two most important parts are:</p>
<ul>
<li>the <code>#get_meal</code> button and</li>
<li>the <code>#meal</code> div</li>
</ul>
<p>We're going to use the <code>button</code> to make a request to the API. This will send back some data that we're going to put into the <code>#meal</code> div which acts as a container - in this case.</p>
<p>Usually after the HTML I'll go right into the CSS. But we don't yet have the entire markup as it will be populated in the <strong>JavaScript</strong> section, so that's what we're going to do next.</p>
<h2 id="heading-the-javascript">The JavaScript</h2>
<p>As mentioned above, we need the <code>button</code> and that container <code>div</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> get_meal_btn = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'get_meal'</span>);
<span class="hljs-keyword">const</span> meal_container = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'meal'</span>);
</code></pre>
<p>Next, before we dive more into the code, let's see what the API is going to return. For that please open the following URL: <a target="_blank" href="https://www.themealdb.com/api/json/v1/1/random.php">https://www.themealdb.com/api/json/v1/1/random.php</a>.</p>
<p>As you can see from the URL, we are getting a <strong>random</strong> meal from this API (refresh to see the <em>randomness</em>). When we're making a <strong>GET</strong> request to that endpoint (like accessing it from the browser), it sends back a JSON response, which we can parse in order to retrieve the data we want.</p>
<p>The data looks something like this:</p>
<pre><code class="lang-js">{
    <span class="hljs-attr">meals</span>: [
        {
            <span class="hljs-attr">idMeal</span>: <span class="hljs-string">'52873'</span>,
            <span class="hljs-attr">strMeal</span>: <span class="hljs-string">'Beef Dumpling Stew'</span>,
            <span class="hljs-attr">strDrinkAlternate</span>: <span class="hljs-literal">null</span>,
            <span class="hljs-attr">strCategory</span>: <span class="hljs-string">'Beef'</span>,
            <span class="hljs-attr">strArea</span>: <span class="hljs-string">'British'</span>,
            <span class="hljs-attr">strInstructions</span>: <span class="hljs-string">'Long description'</span>,
            <span class="hljs-attr">strMealThumb</span>:
                <span class="hljs-string">'https://www.themealdb.com/images/media/meals/uyqrrv1511553350.jpg'</span>,
            <span class="hljs-attr">strTags</span>: <span class="hljs-string">'Stew,Baking'</span>,
            <span class="hljs-attr">strYoutube</span>: <span class="hljs-string">'https://www.youtube.com/watch?v=6NgheY-r5t0'</span>,
            <span class="hljs-attr">strIngredient1</span>: <span class="hljs-string">'Olive Oil'</span>,
            <span class="hljs-attr">strIngredient2</span>: <span class="hljs-string">'Butter'</span>,
            <span class="hljs-attr">strIngredient3</span>: <span class="hljs-string">'Beef'</span>,
            <span class="hljs-attr">strIngredient4</span>: <span class="hljs-string">'Plain Flour'</span>,
            <span class="hljs-attr">strIngredient5</span>: <span class="hljs-string">'Garlic'</span>,
            <span class="hljs-attr">strIngredient6</span>: <span class="hljs-string">'Onions'</span>,
            <span class="hljs-attr">strIngredient7</span>: <span class="hljs-string">'Celery'</span>,
            <span class="hljs-attr">strIngredient8</span>: <span class="hljs-string">'Carrots'</span>,
            <span class="hljs-attr">strIngredient9</span>: <span class="hljs-string">'Leek'</span>,
            <span class="hljs-attr">strIngredient10</span>: <span class="hljs-string">'Swede'</span>,
            <span class="hljs-attr">strIngredient11</span>: <span class="hljs-string">'Red Wine'</span>,
            <span class="hljs-attr">strIngredient12</span>: <span class="hljs-string">'Beef Stock'</span>,
            <span class="hljs-attr">strIngredient13</span>: <span class="hljs-string">'Bay Leaf'</span>,
            <span class="hljs-attr">strIngredient14</span>: <span class="hljs-string">'Thyme'</span>,
            <span class="hljs-attr">strIngredient15</span>: <span class="hljs-string">'Parsley'</span>,
            <span class="hljs-attr">strIngredient16</span>: <span class="hljs-string">'Plain Flour'</span>,
            <span class="hljs-attr">strIngredient17</span>: <span class="hljs-string">'Baking Powder'</span>,
            <span class="hljs-attr">strIngredient18</span>: <span class="hljs-string">'Suet'</span>,
            <span class="hljs-attr">strIngredient19</span>: <span class="hljs-string">'Water'</span>,
            <span class="hljs-attr">strIngredient20</span>: <span class="hljs-string">''</span>,
            <span class="hljs-attr">strMeasure1</span>: <span class="hljs-string">'2 tbs'</span>,
            <span class="hljs-attr">strMeasure2</span>: <span class="hljs-string">'25g'</span>,
            <span class="hljs-attr">strMeasure3</span>: <span class="hljs-string">'750g'</span>,
            <span class="hljs-attr">strMeasure4</span>: <span class="hljs-string">'2 tblsp '</span>,
            <span class="hljs-attr">strMeasure5</span>: <span class="hljs-string">'2 cloves minced'</span>,
            <span class="hljs-attr">strMeasure6</span>: <span class="hljs-string">'175g'</span>,
            <span class="hljs-attr">strMeasure7</span>: <span class="hljs-string">'150g'</span>,
            <span class="hljs-attr">strMeasure8</span>: <span class="hljs-string">'150g'</span>,
            <span class="hljs-attr">strMeasure9</span>: <span class="hljs-string">'2 chopped'</span>,
            <span class="hljs-attr">strMeasure10</span>: <span class="hljs-string">'200g'</span>,
            <span class="hljs-attr">strMeasure11</span>: <span class="hljs-string">'150ml'</span>,
            <span class="hljs-attr">strMeasure12</span>: <span class="hljs-string">'500g'</span>,
            <span class="hljs-attr">strMeasure13</span>: <span class="hljs-string">'2'</span>,
            <span class="hljs-attr">strMeasure14</span>: <span class="hljs-string">'3 tbs'</span>,
            <span class="hljs-attr">strMeasure15</span>: <span class="hljs-string">'3 tblsp chopped'</span>,
            <span class="hljs-attr">strMeasure16</span>: <span class="hljs-string">'125g'</span>,
            <span class="hljs-attr">strMeasure17</span>: <span class="hljs-string">'1 tsp '</span>,
            <span class="hljs-attr">strMeasure18</span>: <span class="hljs-string">'60g'</span>,
            <span class="hljs-attr">strMeasure19</span>: <span class="hljs-string">'Splash'</span>,
            <span class="hljs-attr">strMeasure20</span>: <span class="hljs-string">''</span>,
            <span class="hljs-attr">strSource</span>:
                <span class="hljs-string">'https://www.bbc.co.uk/food/recipes/beefstewwithdumpling_87333'</span>,
            <span class="hljs-attr">dateModified</span>: <span class="hljs-literal">null</span>
        }
    ];
}
</code></pre>
<p>Basically we get back an array of <code>meals</code>, but with only one item in it - the randomly generated one. And this item has all the data we want to showcase in our little application. Things like:</p>
<ul>
<li>meal name (under <code>strMeal</code>)</li>
<li>meal caterogy (under <code>strCategory</code>)</li>
<li>meal image (under <code>strMealThumb</code>)</li>
<li>a youtube video with the recipe (under <code>strYoutube</code>)</li>
<li>the ingredients and the measures (under <code>strIngredientsX</code> and <code>strMeasureX</code> - X representing the nth ingredient and it's measure).This is a little bit awkward as I would expect here to have an array with this information, but they choose to add it as object props. On well... ? The important thing to note is that there are a maximum of 20 ingredients / measures, although they aren't all filled in - some of them might be empty so we need to account for that.</li>
</ul>
<p>Now that we have the button we're going to add an event listener for the <code>click</code> event. Inside we're going to make a request to the API:</p>
<pre><code class="lang-js">get_meal_btn.addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function">() =&gt;</span> {
    fetch(<span class="hljs-string">'https://www.themealdb.com/api/json/v1/1/random.php'</span>)
        .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.json())
        .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> {
            createMeal(res.meals[<span class="hljs-number">0</span>]);
        })
        .catch(<span class="hljs-function"><span class="hljs-params">e</span> =&gt;</span> {
            <span class="hljs-built_in">console</span>.warn(e);
        });
});
</code></pre>
<p>We're using the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">fetch</a> API to do the request. We just have to pass in the url of the API we want to make a <strong>GET</strong> request to, and we're going to get back a promise. </p>
<p>Once this is resolved we have a response (<code>res</code>). This <code>res</code> isn't yet in the state we want it to be, so we're going to call the <code>.json()</code> method on it. Then finally we have the beautiful object. Yay! ?</p>
<p>As mentioned above, the API returns the <code>meals</code> array but only with an item in it. So we're going to pass that item (at index <code>0</code>) into our <code>createMeal</code> function, which we'll define next.</p>
<p>I'm going to paste the entire block of code below and we're going to go into detail afterwards, so hold on for a second. ?</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> createMeal = <span class="hljs-function"><span class="hljs-params">meal</span> =&gt;</span> {
    <span class="hljs-keyword">const</span> ingredients = [];

    <span class="hljs-comment">// Get all ingredients from the object. Up to 20</span>
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">1</span>; i &lt;= <span class="hljs-number">20</span>; i++) {
        <span class="hljs-keyword">if</span> (meal[<span class="hljs-string">`strIngredient<span class="hljs-subst">${i}</span>`</span>]) {
            ingredients.push(
                <span class="hljs-string">`<span class="hljs-subst">${meal[<span class="hljs-string">`strIngredient<span class="hljs-subst">${i}</span>`</span>]}</span> - <span class="hljs-subst">${meal[<span class="hljs-string">`strMeasure<span class="hljs-subst">${i}</span>`</span>]}</span>`</span>
            );
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-comment">// Stop if there are no more ingredients</span>
            <span class="hljs-keyword">break</span>;
        }
    }

    <span class="hljs-keyword">const</span> newInnerHTML = <span class="hljs-string">`
        &lt;div class="row"&gt;
            &lt;div class="columns five"&gt;
                &lt;img src="<span class="hljs-subst">${meal.strMealThumb}</span>" alt="Meal Image"&gt;
                <span class="hljs-subst">${
                    meal.strCategory
                        ? <span class="hljs-string">`&lt;p&gt;&lt;strong&gt;Category:&lt;/strong&gt; <span class="hljs-subst">${meal.strCategory}</span>&lt;/p&gt;`</span>
                        : <span class="hljs-string">''</span>
                }</span>
                <span class="hljs-subst">${meal.strArea ? <span class="hljs-string">`&lt;p&gt;&lt;strong&gt;Area:&lt;/strong&gt; <span class="hljs-subst">${meal.strArea}</span>&lt;/p&gt;`</span> : <span class="hljs-string">''</span>}</span>
                <span class="hljs-subst">${
                    meal.strTags
                        ? <span class="hljs-string">`&lt;p&gt;&lt;strong&gt;Tags:&lt;/strong&gt; <span class="hljs-subst">${meal.strTags
                                .split(<span class="hljs-string">','</span>)
                                .join(<span class="hljs-string">', '</span>)}</span>&lt;/p&gt;`</span>
                        : <span class="hljs-string">''</span>
                }</span>
                &lt;h5&gt;Ingredients:&lt;/h5&gt;
                &lt;ul&gt;
                    <span class="hljs-subst">${ingredients.map(ingredient =&gt; <span class="hljs-string">`&lt;li&gt;<span class="hljs-subst">${ingredient}</span>&lt;/li&gt;`</span>).join(<span class="hljs-string">''</span>)}</span>
                &lt;/ul&gt;
            &lt;/div&gt;
            &lt;div class="columns seven"&gt;
                &lt;h4&gt;<span class="hljs-subst">${meal.strMeal}</span>&lt;/h4&gt;
                &lt;p&gt;<span class="hljs-subst">${meal.strInstructions}</span>&lt;/p&gt;
            &lt;/div&gt;
        &lt;/div&gt;
        <span class="hljs-subst">${
            meal.strYoutube
                ? <span class="hljs-string">`
        &lt;div class="row"&gt;
            &lt;h5&gt;Video Recipe&lt;/h5&gt;
            &lt;div class="videoWrapper"&gt;
                &lt;iframe width="420" height="315"
                src="https://www.youtube.com/embed/<span class="hljs-subst">${meal.strYoutube.slice(<span class="hljs-number">-11</span>)}</span>"&gt;
                &lt;/iframe&gt;
            &lt;/div&gt;
        &lt;/div&gt;`</span>
                : <span class="hljs-string">''</span>
        }</span>
    `</span>;

    meal_container.innerHTML = newInnerHTML;
};
</code></pre>
<p>Basically the entire function's purpose is to get the JSON response, parse it, and transform it into an HTML component. For that we need to do a couple of things, as the data is not yet formated exactly the way we want it to be.</p>
<p>First, we're getting all the <strong>ingredients</strong> and their <strong>measures</strong>. As mentioned above there are a maximum of 20 ingredients, but they are separated into their own properties in the object like: <code>strIngredient1</code>, <code>strIngredient2</code>, etc... (I still don't know why they did that, but... ?).</p>
<p>So, we're creating a <code>for</code> loop which goes from <code>1</code> to <code>20</code> and checks if the <code>meal</code> has that corresponding <code>ingredient</code>-<code>measure</code> pair. If it does, we're putting it into the <code>ingredients</code> array. If there aren't any more ingredients we're stopping the for loop with a <code>break</code> condition.</p>
<p>Next, we're creating the <code>newInnerHTML</code> string which is going to hold the entire HTML markup. In it we are parsing the remaining properties that we want to be displayed.</p>
<p><strong>Note</strong> that some of the properties might not be available. So for that we're using the <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator">ternary operator</a> to check if we have the data to display the corresponding tag. If we don't have it then we're returning an empty string and nothing will be displayed on the page. The <code>category</code> and the <code>area</code> are examples of these type of properties.</p>
<p>The tags are coming in a string divided by a comma like: <code>'tag1,tag2,tag3'</code>. So we need to <code>split</code> it by that comma, and <code>join</code> it back by a comma and a space as it looks nicer (<code>'tag1, tag2, tag3'</code> ❤️). Or at least for me does. ?</p>
<p>To show the <code>ingredients</code>, we're mapping over the array and we're creating an <code>&lt;li&gt;</code> for each ingredient/measure pair. At the end we're joining the array back to form a string. (This is something you would do in ReactJS but without the <code>join</code>ing part ?).</p>
<p>There is also a Youtube video <em>string</em> (maybe) which is returning the URL of the video. But in order for us to embed the video in the page we need to extract the video ID only. For that we're using <code>.slice(-11)</code> to get the last 11 characters of the string as this is where the ID is hiding ?.</p>
<p>And finally, we're setting this entire <code>newInnerHTML</code> to be the <code>meal_container</code>'s <code>innerHTML</code> -&gt; this will populate that div with all this information!</p>
<p>This entire process will repeat every time we're pressing the <code>Get Meal</code> button.</p>
<h2 id="heading-the-css">The CSS</h2>
<p>The last part is to style it a little bit, right? ?</p>
<p>For the <strong>CSS</strong> I wanted to use something new so I tried out the <a target="_blank" href="http://getskeleton.com/">SkeletonCSS</a> library. It's useful if you have a small project and don't want to get overwhelmed with all those classes, as it only has a couple of them that take care of some basic styling (the button for example) and the responsive part.</p>
<pre><code class="lang-css"><span class="hljs-keyword">@import</span> url(<span class="hljs-string">'https://fonts.googleapis.com/css?family=Muli&amp;display=swap'</span>);

* {
    <span class="hljs-attribute">box-sizing</span>: border-box;
}

<span class="hljs-selector-tag">body</span> {
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">flex-direction</span>: column;
    <span class="hljs-attribute">justify-content</span>: center;
    <span class="hljs-attribute">align-items</span>: center;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">30px</span> <span class="hljs-number">0</span>;
    <span class="hljs-attribute">min-height</span>: <span class="hljs-built_in">calc</span>(<span class="hljs-number">100vh</span> - <span class="hljs-number">60px</span>);
}

<span class="hljs-selector-tag">img</span> {
    <span class="hljs-attribute">max-width</span>: <span class="hljs-number">100%</span>;
}

<span class="hljs-selector-tag">p</span> {
    <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">5px</span>;
}

<span class="hljs-selector-tag">h3</span> {
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-tag">h5</span> {
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span> <span class="hljs-number">0</span>;
}

<span class="hljs-selector-tag">li</span> {
    <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-class">.meal</span> {
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span> <span class="hljs-number">0</span>;
}

<span class="hljs-selector-class">.text-center</span> {
    <span class="hljs-attribute">text-align</span>: center;
}

<span class="hljs-selector-class">.videoWrapper</span> {
    <span class="hljs-attribute">position</span>: relative;
    <span class="hljs-attribute">padding-bottom</span>: <span class="hljs-number">56.25%</span>;
    <span class="hljs-attribute">padding-top</span>: <span class="hljs-number">25px</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-class">.videoWrapper</span> <span class="hljs-selector-tag">iframe</span> {
    <span class="hljs-attribute">position</span>: absolute;
    <span class="hljs-attribute">top</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">left</span>: <span class="hljs-number">0</span>;
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
}
</code></pre>
<p>You can see that the CSS is pretty simple. The only part that's worth mentioning is the <code>.videoWrapper</code> CSS declaration. This makes sure that the YouTube embed is responsive. (Got this from <a target="_blank" href="https://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php">CSS-Tricks</a> - thanks guys! ?)</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>And voilà! We're done! ?</p>
<p>You should now know how to use a public API to get some data which you can then insert on the page easily! Well done! ?</p>
<p><em>This is the first project I did for the <a target="_blank" href="https://florin-pop.com/blog/2019/09/100-days-100-projects">#100Days100Projects</a> challenge. You can check out what other projects I've built and what are the rules of the challenge (if you might want to join) by clicking <a target="_blank" href="https://florin-pop.com/blog/2019/09/100-days-100-projects">here</a>.</em></p>
<p>You can read more of my articles on <a target="_blank" href="https://florin-pop.com">www.florin-pop.com</a>.</p>
<p>Happy Coding! ?</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to use video as a background in React Native ]]>
                </title>
                <description>
                    <![CDATA[ By Said Hayani In this post, we are going to create a **backgroundVideo** in React Native. If you have just started with React Native check out my article What you need to know to start building mobile apps with React Native. Demo: Peleton Home Scre... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-a-background-video-in-react-native-cb53304ee4f6/</link>
                <guid isPermaLink="false">66d460d43a8352b6c5a2aaf3</guid>
                
                    <category>
                        <![CDATA[ 100Days100Projects ]]>
                    </category>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ iOS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ mobile app development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React Native ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                    <category>
                        <![CDATA[ user experience ]]>
                    </category>
                
                    <category>
                        <![CDATA[ User Interface ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 15 Apr 2019 21:02:58 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/0*Y-yUIvkbgkWU7Tlm" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Said Hayani</p>
<p>In this post, we are going to create a <code>**backgroundVideo**</code> in React Native. If you have just started with React Native check out my article <a target="_blank" href="https://medium.freecodecamp.org/what-you-need-to-know-to-start-building-mobile-apps-in-react-native-dded951277b7">What you need to know to start building mobile apps with React Nativ</a>e.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*mOSwlhVAq7PTzOuc1J-Hpg.gif" alt="Image" width="374" height="768" loading="lazy">
<em>Demo: Peleton Home Screen</em></p>
<p>Background video can add a nice effect to the UI of an app. They may be helpful also if you want to display, for example, ads or send a message to the user, like we’ll do here.</p>
<p>You will need some basic requirements. To get started, you must have the react-native environment setup. That means you have:</p>
<ul>
<li><a target="_blank" href="https://github.com/react-native-community/react-native-cli">react-native-cli</a> installed</li>
<li>Android SDK; if you have a mac you won’t need that, just Xcode</li>
</ul>
<h3 id="heading-getting-started">Getting started</h3>
<p>First things first, let’s bootstrap a new React Native app. In my case I’m using react-native-cli. So in your terminal run:</p>
<pre><code>react-native init myapp
</code></pre><p>This should install all the dependencies and packages to run your React Native app.</p>
<p>Next step is to run and install the app on the simulator.</p>
<p>For iOS:</p>
<pre><code>react-native run-ios
</code></pre><p>This should open up the iOS simulator.</p>
<p>On Android:</p>
<pre><code>react-native run-android
</code></pre><p>You may have some trouble with Android. I recommend that you use <a target="_blank" href="https://www.genymotion.com/">Genymotion</a> and the Android emulator or check out <a target="_blank" href="https://medium.com/@sunilk/react-native-development-getting-started-with-android-and-ios-ada22e3d00b1">this friendly guide</a> to set up the environment.</p>
<p>First what we are going to do is clone the Peleton app’s home screen. We are using <code>[**react-native-video**](https://github.com/react-native-community/react-native-video)</code> for video streaming, and <code>[**styled-component**](https://www.styled-components.com/docs/basics#react-native)</code> for styling. So you have to install them:</p>
<ul>
<li>Yarn:</li>
</ul>
<pre><code>yarn add react-native-video styled-components
</code></pre><ul>
<li>NPM</li>
</ul>
<pre><code>npm -i react-native-video styled-components --save
</code></pre><p>Then you need to link react-native-video because it contains native code — and for <code>**styled-components**</code> we don’t need that. So simply run:</p>
<pre><code>react-native link
</code></pre><p>You don’t have to worry about the other things, just focus on the <code>Video</code> Component. First, import Video from react-native-video and start using it.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> <span class="hljs-keyword">import</span> Video <span class="hljs-keyword">from</span> <span class="hljs-string">"react-native-video"</span>;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Video</span>
<span class="hljs-attr">source</span>=<span class="hljs-string">{require(</span>"<span class="hljs-attr">.</span>/<span class="hljs-attr">..</span>/<span class="hljs-attr">assets</span>/<span class="hljs-attr">video1.mp4</span>")}
<span class="hljs-attr">style</span>=<span class="hljs-string">{styles.backgroundVideo}</span>
<span class="hljs-attr">muted</span>=<span class="hljs-string">{true}</span>
<span class="hljs-attr">repeat</span>=<span class="hljs-string">{true}</span>
<span class="hljs-attr">resizeMode</span>=<span class="hljs-string">{</span>"<span class="hljs-attr">cover</span>"}
<span class="hljs-attr">rate</span>=<span class="hljs-string">{1.0}</span>
<span class="hljs-attr">ignoreSilentSwitch</span>=<span class="hljs-string">{</span>"<span class="hljs-attr">obey</span>"}
/&gt;</span></span>
</code></pre>
<p>Let’s break it down:</p>
<ul>
<li><strong>source</strong>: the path to the source video. You can use the URL instead:</li>
</ul>
<pre><code>source={{<span class="hljs-attr">uri</span>:<span class="hljs-string">"https://youronlineVideo.mp4"</span>}}
</code></pre><ul>
<li><strong>style:</strong> the costume style we want to give to the video, and the key to making the background video</li>
<li>resizeMode: in our case it is <code>cover</code>; you can try also <code>contain or stretch</code> but this won’t give us what we want</li>
</ul>
<p>And other props are optional.</p>
<p>Let’s move to the important part: placing the video in the background position. Let’s define the styles.</p>
<pre><code class="lang-hs">// <span class="hljs-type">We</span> use <span class="hljs-type">StyleSheet</span> from react-native so don't forget to <span class="hljs-keyword">import</span> it
//<span class="hljs-keyword">import</span> {StyleSheet} from "react-native";
<span class="hljs-title">const</span> { height } = <span class="hljs-type">Dimensions</span>.get(<span class="hljs-string">"window"</span>);
<span class="hljs-title">const</span> styles = <span class="hljs-type">StyleSheet</span>.create({
<span class="hljs-title">backgroundVideo</span>: {
<span class="hljs-title">height</span>: height,
<span class="hljs-title">position</span>: <span class="hljs-string">"absolute"</span>,
<span class="hljs-title">top</span>: <span class="hljs-number">0</span>,
<span class="hljs-title">left</span>: <span class="hljs-number">0</span>,
<span class="hljs-title">alignItems</span>: <span class="hljs-string">"stretch"</span>,
<span class="hljs-title">bottom</span>: <span class="hljs-number">0</span>,
<span class="hljs-title">right</span>: <span class="hljs-number">0</span>
}
});
</code></pre>
<p>What did we do here?</p>
<p>We gave the video a <code>position :absolute</code> and we give it the window <code>height</code> of the device. We used the <code>Dimensions</code> from React Native to ensure that the video is taking up the whole hight — <code>top:0, left:0,bottom:0,right:0</code> — so that the video takes up all the space!</p>
<p>The entire code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component, Fragment } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> {
  Text,
  View,
  StyleSheet,
  Dimensions,
  TouchableHighlight
} <span class="hljs-keyword">from</span> <span class="hljs-string">"react-native"</span>;
<span class="hljs-keyword">import</span> styled <span class="hljs-keyword">from</span> <span class="hljs-string">"styled-components/native"</span>;
<span class="hljs-keyword">import</span> Video <span class="hljs-keyword">from</span> <span class="hljs-string">"react-native-video"</span>;
<span class="hljs-keyword">const</span> { width, height } = Dimensions.get(<span class="hljs-string">"window"</span>);
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BackgroundVideo</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
  render() {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">View</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">Video</span>
          <span class="hljs-attr">source</span>=<span class="hljs-string">{require(</span>"<span class="hljs-attr">.</span>/<span class="hljs-attr">..</span>/<span class="hljs-attr">assets</span>/<span class="hljs-attr">video1.mp4</span>")}
          <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.backgroundVideo}</span>
          <span class="hljs-attr">muted</span>=<span class="hljs-string">{true}</span>
          <span class="hljs-attr">repeat</span>=<span class="hljs-string">{true}</span>
          <span class="hljs-attr">resizeMode</span>=<span class="hljs-string">{</span>"<span class="hljs-attr">cover</span>"}
          <span class="hljs-attr">rate</span>=<span class="hljs-string">{1.0}</span>
          <span class="hljs-attr">ignoreSilentSwitch</span>=<span class="hljs-string">{</span>"<span class="hljs-attr">obey</span>"}
        /&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">Wrapper</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Logo</span>
            <span class="hljs-attr">source</span>=<span class="hljs-string">{require(</span>"<span class="hljs-attr">.</span>/<span class="hljs-attr">..</span>/<span class="hljs-attr">assets</span>/<span class="hljs-attr">cadence-logo.png</span>")}
            <span class="hljs-attr">width</span>=<span class="hljs-string">{50}</span>
            <span class="hljs-attr">height</span>=<span class="hljs-string">{50}</span>
            <span class="hljs-attr">resizeMode</span>=<span class="hljs-string">"contain"</span>
          /&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Title</span>&gt;</span>Join Live And on-demand classes<span class="hljs-tag">&lt;/<span class="hljs-name">Title</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">TextDescription</span>&gt;</span>
            With world-class instructions right here, right now
          <span class="hljs-tag">&lt;/<span class="hljs-name">TextDescription</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">ButtonWrapper</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">Fragment</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Create Account"</span> /&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">Button</span> <span class="hljs-attr">transparent</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Login"</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">Fragment</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">ButtonWrapper</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">Wrapper</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">View</span>&gt;</span></span>
    );
  }
}

<span class="hljs-keyword">const</span> styles = StyleSheet.create({
  <span class="hljs-attr">backgroundVideo</span>: {
    <span class="hljs-attr">height</span>: height,
    <span class="hljs-attr">position</span>: <span class="hljs-string">"absolute"</span>,
    <span class="hljs-attr">top</span>: <span class="hljs-number">0</span>,
    <span class="hljs-attr">left</span>: <span class="hljs-number">0</span>,
    <span class="hljs-attr">alignItems</span>: <span class="hljs-string">"stretch"</span>,
    <span class="hljs-attr">bottom</span>: <span class="hljs-number">0</span>,
    <span class="hljs-attr">right</span>: <span class="hljs-number">0</span>
  }
});

<span class="hljs-comment">// styled-component</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> Wrapper = styled.View<span class="hljs-string">`
  justify-content: space-between;
  padding: 20px;
  align-items: center;
  flex-direction: column;
`</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> Logo = styled.Image<span class="hljs-string">`
  max-width: 100px;
  width: 100px;
  height: 100px;
`</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> TextDescription = styled.Text<span class="hljs-string">`
  letter-spacing: 3;
  color: #f4f4f4;
  text-align: center;
  text-transform: uppercase;
`</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> ButtonWrapper = styled.View<span class="hljs-string">`
  justify-content: center;
  flex-direction: column;
  align-items: center;
  margin-top: 100px;
`</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> Title = styled.Text<span class="hljs-string">`
  color: #f4f4f4;
  margin: 50% 0px 20px;
  font-size: 30;
  text-align: center;
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 3;
`</span>;
<span class="hljs-keyword">const</span> StyledButton = styled.TouchableHighlight<span class="hljs-string">`
 width:250px;
 background-color:<span class="hljs-subst">${props =&gt; (props.transparent ? <span class="hljs-string">"transparent"</span> : <span class="hljs-string">"#f3f8ff"</span>)}</span>;
 padding:15px;
border:<span class="hljs-subst">${props =&gt; (props.transparent ? <span class="hljs-string">"1px solid #f3f8ff "</span> : <span class="hljs-number">0</span>)}</span>
 justify-content:center;
 margin-bottom:20px;
 border-radius:24px
`</span>;
StyledTitle = styled.Text<span class="hljs-string">`
  text-transform: uppercase;
  text-align: center;
  font-weight: bold;
  letter-spacing: 3;
  color: <span class="hljs-subst">${props =&gt; (props.transparent ? <span class="hljs-string">"#f3f8ff "</span> : <span class="hljs-string">"#666"</span>)}</span>;
`</span>;

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> Button = <span class="hljs-function">(<span class="hljs-params">{ onPress, color, ...props }</span>) =&gt;</span> {
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">StyledButton</span> {<span class="hljs-attr">...props</span>}&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">StyledTitle</span> {<span class="hljs-attr">...props</span>}&gt;</span>{props.title}<span class="hljs-tag">&lt;/<span class="hljs-name">StyledTitle</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">StyledButton</span>&gt;</span></span>
  );
};
</code></pre>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*mOSwlhVAq7PTzOuc1J-Hpg.gif" alt="Image" width="374" height="768" loading="lazy"></p>
<p>Also, you can make this component reusable by doing the following:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">View</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">Video</span>
<span class="hljs-attr">source</span>=<span class="hljs-string">{require(</span>"<span class="hljs-attr">.</span>/<span class="hljs-attr">..</span>/<span class="hljs-attr">assets</span>/<span class="hljs-attr">video1.mp4</span>")}
<span class="hljs-attr">style</span>=<span class="hljs-string">{styles.backgroundVideo}</span>
<span class="hljs-attr">muted</span>=<span class="hljs-string">{true}</span>
<span class="hljs-attr">repeat</span>=<span class="hljs-string">{true}</span>
<span class="hljs-attr">resizeMode</span>=<span class="hljs-string">{</span>"<span class="hljs-attr">cover</span>"}
<span class="hljs-attr">rate</span>=<span class="hljs-string">{1.0}</span>
<span class="hljs-attr">ignoreSilentSwitch</span>=<span class="hljs-string">{</span>"<span class="hljs-attr">obey</span>"}
/&gt;</span>
{this.props.children}
<span class="hljs-tag">&lt;/<span class="hljs-name">View</span>&gt;</span>
</code></pre>
<p>And you can use it with other components:</p>
<p>That’s pretty much it. Thank you for reading!</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*G8eN5JqWSRzGTXxpp-CICA.jpeg" alt="Image" width="800" height="656" loading="lazy">
_Photo by [Unsplash](https://unsplash.com/photos/wQZSl60DGDM?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" rel="noopener" target="_blank" title=""&gt;David Boca on &lt;a href="https://unsplash.com/search/photos/app?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText" rel="noopener" target="<em>blank" title=")</em></p>
<h4 id="heading-learn-more-about-react-native">Learn more about React Native:</h4>
<ul>
<li><a target="_blank" href="https://medium.freecodecamp.org/what-you-need-to-know-to-start-building-mobile-apps-in-react-native-dded951277b7">What you need to know to start building mobile apps in React Native</a></li>
<li><a target="_blank" href="https://blog.bitsrc.io/styling-in-react-native-c48caddfbe47">Styling in React Native</a></li>
</ul>
<h4 id="heading-other-posts">Other posts:</h4>
<ul>
<li><a target="_blank" href="https://medium.freecodecamp.org/write-less-do-more-with-javascript-es6-5fd4a8e50ee2">JavaScript ES6, write Less — Do more</a></li>
<li><a target="_blank" href="https://medium.freecodecamp.org/how-to-use-routing-in-vue-js-to-create-a-better-user-experience-98d225bbcdd9">How to use routing in Vue.js to create a better user experience</a></li>
<li><a target="_blank" href="https://medium.freecodecamp.org/here-is-the-most-popular-ways-to-make-an-http-request-in-javascript-954ce8c95aaa">Here are the most popular ways to make an HTTP request in JavaScript</a></li>
</ul>
<blockquote>
<p>You can find me <a target="_blank" href="https://twitter.com/SaidHYN">on Twitter</a> ?</p>
</blockquote>
<h4 id="heading-subscribe-to-my-mailing-list-to-stay-tuned-for-upcoming-articles">Subscribe to my Mailing list to stay tuned for upcoming articles.</h4>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
