<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sanjay Khati Chhetri</title>
    <description>The latest articles on Forem by Sanjay Khati Chhetri (@jonas_sanjay).</description>
    <link>https://forem.com/jonas_sanjay</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F715115%2F4a0434bf-ffe3-4c00-92cf-a9b0193c4f1a.png</url>
      <title>Forem: Sanjay Khati Chhetri</title>
      <link>https://forem.com/jonas_sanjay</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jonas_sanjay"/>
    <language>en</language>
    <item>
      <title>Introduction to Big O Notation</title>
      <dc:creator>Sanjay Khati Chhetri</dc:creator>
      <pubDate>Mon, 24 Jul 2023 01:07:51 +0000</pubDate>
      <link>https://forem.com/jonas_sanjay/introduction-to-big-o-notation-2lpb</link>
      <guid>https://forem.com/jonas_sanjay/introduction-to-big-o-notation-2lpb</guid>
      <description>&lt;p&gt;&lt;strong&gt;Different Types of Algorithms in Data Structure.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Algorithm?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An Algorithm is a sequence of steps that describe how a problem can be solved. Every computer program that ends with a result is basically based on an Algorithm. These can also be used to solve mathematical problems and on many matters of day-to-day life. Algorithms are generally created independent of underlying languages, i.e. an algorithm can be implemented in more than one programming language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HHiMvoOz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emlsfs5ppc8lehianuoo.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HHiMvoOz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emlsfs5ppc8lehianuoo.jpeg" alt="Image description" width="551" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Algorithm are different Categories which are described as below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Search&lt;/strong&gt; − Algorithm to search an item in a data structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sort&lt;/strong&gt; − Algorithm to sort items in a certain order.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Insert&lt;/strong&gt; − Algorithm to insert item in a data structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update&lt;/strong&gt; − Algorithm to update an existing item in a data structure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delete&lt;/strong&gt; − Algorithm to delete an existing item from a data structure.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are the categories by which every problem become easy and can easily solved.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Write an Algorithm?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;here are no well-defined standards for writing algorithms. Rather, it is problem and resource dependent. Algorithms are never written to support a particular programming code.&lt;/p&gt;

&lt;p&gt;As we know that all programming languages share basic code constructs like loops (do, for, while), flow-control (if-else), etc. These common constructs can be used to write an algorithm.&lt;/p&gt;

&lt;p&gt;We write algorithms in a step-by-step manner, but it is not always the case. Algorithm writing is a process and is executed after the problem domain is well-defined. That is, we should know the problem domain, for which we are designing a solution.&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

&lt;p&gt;Let's try to learn algorithm-writing by using an example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt; − Design an algorithm to add two numbers and display the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**Step 1** − START

**Step 2** − declare three integers **a**, **b** &amp;amp; **c**

**Step 3** − define values of **a** &amp;amp; **b**

**Step 4** − add values of **a** &amp;amp; **b**

**Step 5** − store output of step 4 to **c**

**Step 6** − print **c**

**Step 7** − STOP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Algorithms tell the programmers how to code the program. Alternatively, the algorithm can be written as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**Step 1** − START ADD

**Step 2** − get values of **a** &amp;amp; **b**

**Step 3** − c ← a + b

**Step 4** − display c

**Step 5** − STOP

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In design and analysis of algorithms, usually the second method is used to describe an algorithm. It makes it easy for the analyst to analyze the algorithm ignoring all unwanted definitions. He can observe what operations are being used and how the process is flowing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages and Disadvantages of Algorithms.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages of Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is a step wise representation of a solution for a give problem&lt;/li&gt;
&lt;li&gt;Algorithm use a definite procedure&lt;/li&gt;
&lt;li&gt;It does not depend upon any programming language which make algorithm easily understandable without any programming knowledge.&lt;/li&gt;
&lt;li&gt;Every step in algorithm has logical reason so it can be easily debugged.&lt;/li&gt;
&lt;li&gt;Algorithm divide the problem into smaller problems due to which every problem can easily solved.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages of Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Algorithm is Time Consuming&lt;/li&gt;
&lt;li&gt;Every problem cannot be converted into algorithm which makes it difficult &lt;/li&gt;
&lt;li&gt;Difficult to show looping and branching in Algorithm&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Types of Algorithms&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Based on how they function, we can divide Algorithms into multiple types. Let’s take a look at some of the important ones.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Recursive Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is one of the most interesting Algorithms as it calls itself with a smaller value as inputs which it gets after solving for the current inputs. In more simpler words, It’s an Algorithm that calls itself repeatedly until the problem is solved.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To find factorial using recursion, here is the pseudo code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Fact(x)
        If x is 0      /\*0 is the base value and x is 0 is base case\*/
            return 1
    return (x\*Fact(x-1))  /\* breaks the problem into small problems\*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Divide and Conquer Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is another effective way of solving many problems. In Divide and Conquer algorithms, divide the algorithm into two parts, the first parts divides the problem on hand into smaller subproblems of the same type. Then on the second part, these smaller problems are solved and then added together (combined) to produce the final solution of the problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.thetechplatform.com/post/merge-sort-algorithm"&gt;Merge sort&lt;/a&gt;&lt;/strong&gt; ,&lt;strong&gt;&lt;a href="https://www.thetechplatform.com/post/quick-sort-algorithm"&gt; Quick sort&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Dynamic Programming Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These algorithms work by remembering the results of the past run and using them to find new results. In other words, dynamic programming algorithm solves complex problems by breaking it into multiple simple subproblems and then it solves each of them once and then stores them for future use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fibonacci sequence, here is the pseudo code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Fib(n)
    if n=0
        return 0
    else
        prev\_Fib=0,curr\_Fib=1
    repeat n-1 times  /\*if n=0 it will skip\*/
        next\_Fib=prev\_Fib+curr\_Fib   
        prev\_Fib=curr\_Fib
        curr\_Fib=new\_Fib
    return curr\_Fib
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Greedy Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These algorithms are used for solving optimization problems. In this algorithm, we find a locally optimum solution (without any regard for any consequence in future) and hope to find the optimal solution at the global level.&lt;/p&gt;

&lt;p&gt;The method does not guarantee that we will be able to find an optimal solution.&lt;/p&gt;

&lt;p&gt;The algorithm has 5 components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first one is a candidate set from which we try to find a solution.&lt;/li&gt;
&lt;li&gt;A selection function which helps choose the best possible candidate.&lt;/li&gt;
&lt;li&gt;A feasibility function which helps in deciding if the candidate can be used to find a solution.&lt;/li&gt;
&lt;li&gt;An objective function which assigns value to a possible solution or to a partial solution&lt;/li&gt;
&lt;li&gt;Solution function that tells when we have found a solution to the problem.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Huffman Coding and Dijkstra’s algorithm are two prime examples where Greedy algorithm is used.&lt;/p&gt;

&lt;p&gt;In Huffman coding, The algorithm goes through a message and depending on the frequency of the characters in that message, for each character, it assigns a variable length encoding. To do Huffman coding, we first need to build a Huffman tree from the input characters and then traverse through the tree to assign codes to the characters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.thetechplatform.com/post/huffman-algorithm-coding"&gt;Huffman tree&lt;/a&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;a href="https://www.thetechplatform.com/post/county-money-in-greedy-algorithm"&gt;Counting money&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Brute Force Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is one of the simplest algorithms in the concept. A brute force algorithm blindly iterates all possible solutions to search one or more than one solution that may solve a function. Think of brute force as using all possible combinations of numbers to open a safe.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Exact string&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Matching algorithm&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;6. Backtracking Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Backtracking is a technique to find a solution to a problem in an incremental approach. It solves problems recursively and tries to get to a solution to a problem by solving one piece of the problem at a time. If one of the solutions fail, we remove it and backtrack to find another solution.&lt;/p&gt;

&lt;p&gt;In other words, a backtracking algorithm solves a subproblem and if it fails to solve the problem, it undoes the last step and starts again to find the solution to the problem.&lt;/p&gt;

&lt;p&gt;N Queens problem is one good example to see Backtracking algorithm in action. The N Queen Problem states that there are N pieces of Queens in a chess board and we have to arrange them so that no queen can attack any other queen in the board once organized.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Exploring Variable Scope in Python with Nested Functions</title>
      <dc:creator>Sanjay Khati Chhetri</dc:creator>
      <pubDate>Wed, 21 Jun 2023 00:42:39 +0000</pubDate>
      <link>https://forem.com/jonas_sanjay/exploring-variable-scope-in-python-with-nested-functions-1900</link>
      <guid>https://forem.com/jonas_sanjay/exploring-variable-scope-in-python-with-nested-functions-1900</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Understanding variable scope is key to writing clean, efficient Python code. As variables can have different scopes, knowing how and when to access them is crucial. In this post, we will discuss variable scope in Python using an illustrative example involving nested functions. We will explore how variables in global, enclosed, and local scopes are accessed by different functions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Python Code:
&lt;/h2&gt;

&lt;p&gt;Below is the code snippet we will use to demonstrate variable scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;global_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;enclosed_fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enclosed_var&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;enclosed_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"i am inside the enclosed function, i can access variables in global scope:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;global_var&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"and enclosed scope:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;enclosed_var&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;local_fn&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="n"&gt;local_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"i am inside the local function, i can access variables in global scope:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;global_var&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"enclosed scope:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;enclosed_var&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"and local scope:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;local_var&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;local_fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;enclosed_fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"i am a example of global function called print() , i can access variable declared outside the function or in global scope:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;global_var&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding the Code:
&lt;/h2&gt;

&lt;p&gt;In this code, we define a global variable &lt;code&gt;global_var&lt;/code&gt;, an enclosed function &lt;code&gt;enclosed_fn&lt;/code&gt;, and a local function &lt;code&gt;local_fn&lt;/code&gt; nested within &lt;code&gt;enclosed_fn&lt;/code&gt;. Each function prints a statement demonstrating its ability to access variables from different scopes.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Global Scope:&lt;br&gt;
Variables declared outside of any function or block are considered global variables. Their lifecycle extends throughout the entire runtime of the program. In our example, &lt;code&gt;global_var&lt;/code&gt; is a global variable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enclosed Scope:&lt;br&gt;
When a function is nested within another function, variables declared in the outer function can be accessed within the inner function. These variables are in the enclosing scope. In our example, &lt;code&gt;enclosed_fn&lt;/code&gt; defines the variable &lt;code&gt;enclosed_var&lt;/code&gt;, which is in the enclosing scope for &lt;code&gt;local_fn&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Local Scope:&lt;br&gt;
Variables declared within a function are considered local variables with a lifecycle limited to the execution of that function. In our example, &lt;code&gt;local_fn&lt;/code&gt; defines the variable &lt;code&gt;local_var&lt;/code&gt;, which has local scope.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Built-in Scope:&lt;br&gt;
The scope that includes pre-defined attribute, such as the print(), str(), len(), range() function. These are available in any part of your Python code, without the need for any explicit import or declaration.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Takeaways from the Code:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;enclosed_fn&lt;/code&gt; can access variables in both global and enclosed scopes.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;local_fn&lt;/code&gt; (nested within &lt;code&gt;enclosed_fn&lt;/code&gt;) can access variables in global, enclosed, and local scopes.&lt;/li&gt;
&lt;li&gt;The built-in global function &lt;code&gt;print()&lt;/code&gt; can access &lt;code&gt;global_var&lt;/code&gt; from the global scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;Understanding variable scope in Python is essential for managing the interactions among different parts of your code. By examining this example with nested functions, we have demonstrated how variables in global, enclosed, and local scopes can be accessed by functions on different levels. This knowledge will enable you to write cleaner and more efficient Python programs.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>variablescope</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A Comprehensive Guide to Variable Scope in Python</title>
      <dc:creator>Sanjay Khati Chhetri</dc:creator>
      <pubDate>Wed, 21 Jun 2023 00:07:25 +0000</pubDate>
      <link>https://forem.com/jonas_sanjay/a-comprehensive-guide-to-variable-scope-in-python-2i67</link>
      <guid>https://forem.com/jonas_sanjay/a-comprehensive-guide-to-variable-scope-in-python-2i67</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;Mastering the concept of variable scope is crucial for every Python developer, as it governs the visibility and lifecycle of variables within your code. This in-depth article will discuss the significance of variable scope, explore different types of scopes - including built-in, global, enclosing, and local - with relevant code examples, and provide best practices for managing variable scope effectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Types of Variable Scopes in Python:
&lt;/h2&gt;




&lt;p&gt;In Python, there are four levels of variable scope:&lt;/p&gt;

&lt;h3&gt;
  
  
  a. Built-in Scope
&lt;/h3&gt;

&lt;p&gt;: This scope includes Python's pre-defined built-in functions and attributes like &lt;code&gt;print()&lt;/code&gt;, &lt;code&gt;len()&lt;/code&gt;, &lt;code&gt;range()&lt;/code&gt;, and &lt;code&gt;str()&lt;/code&gt;. These built-in functions are available globally within the program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;example_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This function uses the built-in 'print()' function."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;example_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  b. Global Scope
&lt;/h3&gt;

&lt;p&gt;: Variables declared outside of any function or block are considered global variables. Their lifecycle extends across the entire runtime of the program, and they can be accessed from any part of the code, including functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;global_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I am a global variable"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;example_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;global_var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;example_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;      &lt;span class="c1"&gt;# Output: "I am a global variable"
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;global_var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;       &lt;span class="c1"&gt;# Output: "I am a global variable"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  c. Enclosing Scope
&lt;/h3&gt;

&lt;p&gt;: When a function is nested within another function, variables declared in the outer function are accessible within the inner function. These variables are said to be in the enclosing scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;outer_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;enclosing_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I am in the enclosing scope"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inner_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enclosing_var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;inner_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;outer_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;         &lt;span class="c1"&gt;# Output: "I am in the enclosing scope"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  d. Local Scope
&lt;/h3&gt;

&lt;p&gt;: Variables declared within a function are considered local variables. Their lifecycle is limited to the execution of that function, and they can only be accessed within the function or block in which they are declared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;example_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;local_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"I am a local variable"&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;local_var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;example_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;      &lt;span class="c1"&gt;# Output: "I am a local variable"
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;local_var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# Raises NameError, since local_var is not defined outside the function
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. The &lt;code&gt;global&lt;/code&gt; and &lt;code&gt;nonlocal&lt;/code&gt; Keywords
&lt;/h2&gt;




&lt;h3&gt;
  
  
  a. The &lt;code&gt;global&lt;/code&gt; keyword
&lt;/h3&gt;

&lt;p&gt;: To modify a global variable from within a function, you need to use the &lt;code&gt;global&lt;/code&gt; keyword to explicitly inform Python that you want to reference a global variable, not create a new local variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;increment_counter&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;global&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;
    &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="n"&gt;increment_counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: 1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  b. The &lt;code&gt;nonlocal&lt;/code&gt; keyword
&lt;/h3&gt;

&lt;p&gt;: To modify an enclosing variable from within a nested function, use the &lt;code&gt;nonlocal&lt;/code&gt; keyword. This tells Python that you want to reference a variable from an enclosing scope rather than create a new local variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;outer_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;enclosing_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Original value"&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;inner_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
        &lt;span class="k"&gt;nonlocal&lt;/span&gt; &lt;span class="n"&gt;enclosing_var&lt;/span&gt;
        &lt;span class="n"&gt;enclosing_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Modified value"&lt;/span&gt;

    &lt;span class="n"&gt;inner_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;enclosing_var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;outer_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# Output: "Modified value"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Best Practices for Managing Variable Scope:
&lt;/h2&gt;




&lt;p&gt;Adopting best practices for handling variable scope can help you write cleaner, more maintainable code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Limit usage of global variables: Excessive use of global variables can lead to code that is hard to maintain and prone to conflicts. Instead, use function arguments and return values to pass data between functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Employ function arguments and return values: Rather than rely on global variables, pass necessary data as function arguments and return values to share the results between different parts of your code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Restrict variable scope: Declare variables as local when possible to minimize their exposure and the potential for unintended modifications, thus reducing the risk of bugs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;




&lt;p&gt;In this comprehensive guide, we've discussed the four variable scope levels in Python (built-in, global, enclosing, and local) and explored how to modify variables from different scopes using the &lt;code&gt;global&lt;/code&gt; and &lt;code&gt;nonlocal&lt;/code&gt; keywords. By understanding these concepts and adopting best practices for managing variable scope, you'll be better equipped to write efficient, maintainable, and bug-free Python programs.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>variablescope</category>
      <category>beginners</category>
    </item>
    <item>
      <title>operators in python</title>
      <dc:creator>Sanjay Khati Chhetri</dc:creator>
      <pubDate>Mon, 19 Jun 2023 23:56:04 +0000</pubDate>
      <link>https://forem.com/jonas_sanjay/operators-in-python-10f8</link>
      <guid>https://forem.com/jonas_sanjay/operators-in-python-10f8</guid>
      <description>&lt;h2&gt;
  
  
  Exploring Python Operators: Simplifying Complex Operations Through Code Examples
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Python operators are powerful tools in a programmer’s arsenal that make manipulating data and performing various operations amazingly simple and efficient. In this blog post, we will explore the different types of operators available in Python, including arithmetic, comparison, logical, identity, assignment, bitwise, and membership operators. We will guide you through numerous code examples to help you understand their functionality and application within the context of operators.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arithmetic Operators&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;These operators handle mathematical operations on numbers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Addition &lt;code&gt;+&lt;/code&gt;: Adds two numbers and returns an integer or float value, depending on the input&lt;/li&gt;
&lt;li&gt;Subtraction &lt;code&gt;-&lt;/code&gt;: Subtracts two numbers&lt;/li&gt;
&lt;li&gt;Multiplication &lt;code&gt;*&lt;/code&gt;: Multiplies two numbers&lt;/li&gt;
&lt;li&gt;Division &lt;code&gt;/&lt;/code&gt;: Divides two numbers&lt;/li&gt;
&lt;li&gt;Exponent &lt;code&gt;**&lt;/code&gt;: Calculates the power of a number&lt;/li&gt;
&lt;li&gt;Floor Division &lt;code&gt;//&lt;/code&gt;: Divides two numbers and rounds down to the nearest whole number, returning a float value&lt;/li&gt;
&lt;li&gt;Modulus &lt;code&gt;%&lt;/code&gt;: Determines the remainder of the division of two numbers and returns an integer value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's explore each of these arithmetic operators through code examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Addition
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Subtraction
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Multiplication
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Division
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Exponent
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Floor Division
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Modulus
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Comparison Operators&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Comparison operators compare values and return a Boolean result.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Equal &lt;code&gt;==&lt;/code&gt;: Determines if two values are equal&lt;/li&gt;
&lt;li&gt;Not Equal &lt;code&gt;!=&lt;/code&gt;: Determines if two values are not equal&lt;/li&gt;
&lt;li&gt;Greater Than &lt;code&gt;&amp;gt;&lt;/code&gt;: Checks if the first value is greater than the second value&lt;/li&gt;
&lt;li&gt;Less Than &lt;code&gt;&amp;lt;&lt;/code&gt;: Checks if the first value is less than the second value&lt;/li&gt;
&lt;li&gt;Greater Than or Equal &lt;code&gt;&amp;gt;=&lt;/code&gt;: Checks if the first value is greater than or equal to the second value&lt;/li&gt;
&lt;li&gt;Less Than or Equal &lt;code&gt;&amp;lt;=&lt;/code&gt;: Checks if the first value is less than or equal to the second value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below are code examples for each comparison operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#Equal
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#Not Equal
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#Greater Than
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#Less Than
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Greater Than or Equal
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#Less Than or Equal
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Logical Operators&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;These operators are essential for decision-making and flow control within the code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;And &lt;code&gt;and&lt;/code&gt;: Returns True if both conditions are true; otherwise False&lt;/li&gt;
&lt;li&gt;Or &lt;code&gt;or&lt;/code&gt;: Returns True if at least one condition is true; otherwise False&lt;/li&gt;
&lt;li&gt;Not &lt;code&gt;not&lt;/code&gt;: Reverses the result of the condition – True becomes False and vice versa&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are some code examples using logical operators with if-else constructs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# And
&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"All true!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not all true!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"All true!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not all true!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Or
&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"At least one true!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Both false!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"At least one true!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Both false!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# Not
&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not true!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"True!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Not true!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"True!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Identity Operators&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;These operators test the identity of objects.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is &lt;code&gt;is&lt;/code&gt;: Determines if two variables point to the same object&lt;/li&gt;
&lt;li&gt;Is Not &lt;code&gt;is not&lt;/code&gt;: Determines if two variables do not point to the same object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Code examples for both identity operators are shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# is
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# is not
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Assignment Operators&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Assignment operators assign values to variables.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Equals &lt;code&gt;=&lt;/code&gt;: Assigns a value to a variable&lt;/li&gt;
&lt;li&gt;Addition equals &lt;code&gt;+=&lt;/code&gt;: Adds the specified value to the variable&lt;/li&gt;
&lt;li&gt;Subtraction equals &lt;code&gt;-=&lt;/code&gt;: Subtracts the specified value from the variable&lt;/li&gt;
&lt;li&gt;Multiplication equals &lt;code&gt;*=&lt;/code&gt;: Multiplies the variable by the specified value&lt;/li&gt;
&lt;li&gt;Division equals &lt;code&gt;/=&lt;/code&gt;: Divides the variable by the specified value&lt;/li&gt;
&lt;li&gt;Exponent equals &lt;code&gt;**=&lt;/code&gt;: Raises the variable to the power of the specified value&lt;/li&gt;
&lt;li&gt;Floor Division equals &lt;code&gt;//=&lt;/code&gt;: Performs floor division on the variable by the specified value&lt;/li&gt;
&lt;li&gt;Modulus equals &lt;code&gt;%=&lt;/code&gt;: Performs modulo operation on the variable by the specified value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see these assignment operators in action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;# Addition
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;# Subtraction
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;# Multiplication
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;/=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;# Division
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;**=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;# Exponent
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;//=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;# Floor Division
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;# Modulus
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Bitwise Operators&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Bitwise operators work on bits – the smallest units of data in computers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;And &lt;code&gt;&amp;amp;&lt;/code&gt;: Performs bitwise AND on each bit of the two values&lt;/li&gt;
&lt;li&gt;Or &lt;code&gt;|&lt;/code&gt;: Performs bitwise OR on each bit of the two values&lt;/li&gt;
&lt;li&gt;Xor &lt;code&gt;^&lt;/code&gt;: Performs bitwise XOR on each bit of the two values&lt;/li&gt;
&lt;li&gt;Not &lt;code&gt;~&lt;/code&gt;: Inverts each bit of the value&lt;/li&gt;
&lt;li&gt;Left Shift &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;: Shifts the bits of the value to the left by the specified number of positions&lt;/li&gt;
&lt;li&gt;Right Shift &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;: Shifts the bits of the value to the right by the specified number of positions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example code showcasing bitwise operators:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# And
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Or
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Xor
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Not
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Left Shift
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Right Shift
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Membership Operators&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;These operators test the presence of an element within a sequence (e.g., string, list, or tuple).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;in&lt;/code&gt;: Checks if an element is in the sequence&lt;/li&gt;
&lt;li&gt;Not In &lt;code&gt;not in&lt;/code&gt;: Checks if an element is not in the sequence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Have a look at an example of membership operators using a list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# in
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# not in
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;Python operators provide a simple and efficient way to perform various operations on values and variables. By understanding the functionalities of different types of operators, you can enhance your programming skills, write concise code, and handle complex tasks with ease. Through the exploration of various code examples in this blog post, we hope to provide you clarity on the power and flexibility that comes with Python operators. So go ahead, harness the power of Python operators to make your coding experience smoother and more enjoyable!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
