<?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: Fueler - Your Developer Portfolio</title>
    <description>The latest articles on Forem by Fueler - Your Developer Portfolio (@fueler).</description>
    <link>https://forem.com/fueler</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%2Forganization%2Fprofile_image%2F6766%2F64036005-77d6-40cd-bfcd-56351cd3ca63.png</url>
      <title>Forem: Fueler - Your Developer Portfolio</title>
      <link>https://forem.com/fueler</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fueler"/>
    <language>en</language>
    <item>
      <title>Git Reset vs. Git Revert: When to Use Each Command</title>
      <dc:creator>Rahul</dc:creator>
      <pubDate>Wed, 05 Apr 2023 15:46:34 +0000</pubDate>
      <link>https://forem.com/fueler/git-reset-vs-git-revert-when-to-use-each-command-501a</link>
      <guid>https://forem.com/fueler/git-reset-vs-git-revert-when-to-use-each-command-501a</guid>
      <description>&lt;p&gt;Create your &lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;Dev Portfolio&lt;/a&gt; today at &lt;a href="//bit.ly/rahulinvitesdevelopers"&gt;Fueler&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbzcqe0o1936d9alwwwb6.png" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Have you ever committed changes to a repository, only to realize that it wasn't what you meant to do?&lt;/p&gt;

&lt;p&gt;Don't worry—we've all been there.&lt;/p&gt;

&lt;p&gt;The good news is that there are smarter ways of undoing commits than simply rolling back the entire repository.&lt;/p&gt;

&lt;p&gt;At its core, Git is a version-control system that lets you track and manage changes in your projects.&lt;/p&gt;

&lt;p&gt;It offers several powerful commands which can help you undo commits and restore a clean state for your project.&lt;/p&gt;

&lt;p&gt;These commands are Git Reset and Git Revert.&lt;/p&gt;

&lt;p&gt;But what’s the difference between them? When should you use each command?&lt;/p&gt;

&lt;p&gt;In this article, we'll look at both Git Reset and Git Revert in detail and explain when it’s best to use each one. &lt;/p&gt;

&lt;p&gt;That way, you’ll never have to worry about messing up your commits again! Let's start by learning about the basics of Git Reset vs Revert.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Git Reset?
&lt;/h2&gt;

&lt;p&gt;Git reset is one of the most powerful commands in git. It can help you reset your local branch to any commit in the branch's history, going back in time as far as you need.&lt;/p&gt;

&lt;p&gt;There are three types of Git reset:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Soft&lt;/strong&gt;: This just resets the pointer that references your commits to whatever you specify (for example, a previous commit). Your working directory and staging area aren't changed at all.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mixed&lt;/strong&gt;: The mixed option moves your HEAD pointer back but leaves everything else untouched. In other words, any committed changes are still saved, but the staging index and working directory are updated with the contents of HEAD at that commit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hard&lt;/strong&gt;: If you want to erase all commits after a certain point, the hard option is your best bet. Once applied, any changes committed after the target commit are permanently discarded. It’s important to note that this resets both your current branch and staging index and makes them look exactly like they did when you set it back.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Git reset is best used when you need to undo changes made in a single file (or fewer) and don’t want to discard any of your other work or commits. But be careful—a hard reset can cause serious headaches if used improperly!&lt;/p&gt;

&lt;h3&gt;
  
  
  Code example: how to perform a soft reset using Git command line
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, open your terminal and navigate to the repository you want to work with.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To perform a soft reset, use &lt;code&gt;git reset --soft HEAD~1&lt;/code&gt; command&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This will undo the last commit, but keep the changes in the staging area so that you can modify them and commit again.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;To check the status of your repository after the reset, use the &lt;code&gt;git status&lt;/code&gt; command. This will show you the files that were changed in the last commit, which are now in the staging area.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To modify the changes and commit again, use:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

    &lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;commit&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;New commit message&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will add the modified changes to the staging area and create a new commit with a new message.&lt;/p&gt;

&lt;p&gt;Note: It's important to be cautious when using git reset as it can delete commits and modify the history of your repository. &lt;/p&gt;

&lt;p&gt;Always make sure to backup your work before using this command, and consider using git revert instead if you want to undo a commit without modifying the history.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Git Revert?
&lt;/h2&gt;

&lt;p&gt;Git revert is a command that allows you to undo a specified commit while preserving the rest of your history in the repository. It creates a new commit that undoes some or all changes of the specified commit, making Git revert an ideal way to undo a specific change to your repository.&lt;/p&gt;

&lt;p&gt;When should you use Git revert?&lt;/p&gt;

&lt;p&gt;Generally speaking, it's an effective way to undo changes in certain scenarios.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For example, if you've pushed code that breaks the build, has unforeseen bugs, or violates another team member’s work, Git revert should be your go-to command.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, there are some risks and precautions to consider when using Git revert.&lt;/p&gt;

&lt;p&gt;Like for example, always double-check that you're reverting the correct commit – if not, it could introduce its own bugs and issues.&lt;/p&gt;

&lt;p&gt;It's also important to make sure there aren't any conflicts between branches with similar commits; this could cause problems when trying to merge branches in the future.&lt;/p&gt;

&lt;p&gt;Overall, Git revert is a great tool for discreetly undoing changes and keeping your repository clean and organized. When used properly and with caution though, it can save you from any major blunders or missteps down the road.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to revert a commit using Git command line
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;First, use the &lt;code&gt;git log&lt;/code&gt; command to find the commit hash that you want to revert. The commit hash is a long string of characters that uniquely identifies the commit.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;commit&lt;/span&gt; &lt;span class="mi"&gt;946763&lt;/span&gt;&lt;span class="n"&gt;c97874be293696ba2c119c97ff1b7cb3&lt;/span&gt;
&lt;span class="n"&gt;Author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rahuldotbiz&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;gamerrahul1122&lt;/span&gt;&lt;span class="nd"&gt;@gmail.com&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;   &lt;span class="n"&gt;Thu&lt;/span&gt; &lt;span class="n"&gt;Mar&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;02&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;46&lt;/span&gt; &lt;span class="mi"&gt;2023&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;0530&lt;/span&gt;

    &lt;span class="n"&gt;analytics&lt;/span&gt; &lt;span class="n"&gt;removed&lt;/span&gt;
&lt;span class="bp"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Once you have identified the commit hash, use the &lt;code&gt;git revert&lt;/code&gt; command followed by the commit hash to create a new commit that undoes the changes made in the specified commit.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;revert&lt;/span&gt; &lt;span class="mi"&gt;946763&lt;/span&gt;&lt;span class="n"&gt;c97874be293696ba2c119c97ff1b7cb3&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;master&lt;/span&gt; &lt;span class="mi"&gt;4387&lt;/span&gt;&lt;span class="n"&gt;c34&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;Revert&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Add new feature&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
 &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="n"&gt;changed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="nf"&gt;deletion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Git will open up a text editor where you can enter a commit message describing the revert. Once you save and close the editor, Git will create a new commit that undoes the changes made in the specified commit.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;    &lt;span class="n"&gt;Revert&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Add new feature&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

    &lt;span class="n"&gt;This&lt;/span&gt; &lt;span class="n"&gt;reverts&lt;/span&gt; &lt;span class="n"&gt;commit&lt;/span&gt; &lt;span class="mi"&gt;946763&lt;/span&gt;&lt;span class="n"&gt;c97874be293696ba2c119c97ff1b7cb3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;

    &lt;span class="n"&gt;Reason&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;revert&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;This&lt;/span&gt; &lt;span class="n"&gt;feature&lt;/span&gt; &lt;span class="n"&gt;caused&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;regression&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;needs&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;be&lt;/span&gt; &lt;span class="n"&gt;reworked&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Finally, use the &lt;code&gt;\&lt;/code&gt;&lt;code&gt;git push\&lt;/code&gt; &lt;code&gt;\&lt;/code&gt; command to push the new revert commit to the remote repository.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Comparing Git Reset and Git Revert
&lt;/h2&gt;

&lt;p&gt;When undoing a commit, it's important to know the differences between &lt;code&gt;git reset&lt;/code&gt; and &lt;code&gt;git revert&lt;/code&gt;. They seemingly do the same thing, but their impacts are very different.&lt;/p&gt;

&lt;h3&gt;
  
  
  Git Reset
&lt;/h3&gt;

&lt;p&gt;Git reset is a powerful command that should be used with caution. It moves the HEAD and the current branch pointer to the desired commit.&lt;/p&gt;

&lt;p&gt;One of its benefits is that it affects only your local repository, while changes can be pushed to remote repositories with &lt;code&gt;git push&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, using this command has certain drawbacks as well—if you want to go back further than what you've already committed, it can only be done from the staging area.&lt;/p&gt;

&lt;h3&gt;
  
  
  Git Revert
&lt;/h3&gt;

&lt;p&gt;Unlike &lt;code&gt;git reset&lt;/code&gt;, &lt;code&gt;git revert&lt;/code&gt; does not move any of your branches or change anything about them—it just creates a new commit that is the opposite of the one you specify.&lt;/p&gt;

&lt;p&gt;In other words, if you have a commit that adds some lines of code, running &lt;code&gt;git revert&lt;/code&gt; on that commit adds another commit that removes those lines again.&lt;/p&gt;

&lt;p&gt;This allows for more flexibility in terms of going back further in your history and making changes to existing commits.&lt;/p&gt;

&lt;p&gt;When it comes to making changes in your repository, it's important to understand when it's appropriate to use one command over another — otherwise, you could end up with unexpected results.&lt;/p&gt;

&lt;p&gt;Of course, both commands can be used on local or remote repositories but there are certain circumstances in which one is better than the other.&lt;/p&gt;

&lt;p&gt;Knowing this will help make sure you're making the right choices when using either command.&lt;/p&gt;

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

&lt;p&gt;To recap, if you want to make an old commit disappear from Git history, use git reset. If you just want to leave a commit intact but undo the changes it introduced, use git revert.&lt;/p&gt;

&lt;p&gt;As a general rule of thumb, try to use git revert first. If you’re dealing with a messy merge conflict, though, you may want to use git reset to go back to a known good state before trying the merge again.&lt;/p&gt;

&lt;p&gt;Just remember that using git reset to undo commits is a bit more risky and should be used with caution. &lt;/p&gt;

&lt;p&gt;Understanding the differences between “git reset” vs “git revert” can save you a lot of time and headaches in the long run.&lt;/p&gt;

&lt;p&gt;But in the next, we'll learn more about how to undo multiple commits using Git interactive rebase.&lt;/p&gt;




&lt;p&gt;Create your &lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;Dev Portfolio&lt;/a&gt; today at &lt;a href="//bit.ly/rahulinvitesdevelopers"&gt;Fueler&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbzcqe0o1936d9alwwwb6.png" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Learn how to create Python Dictionary | Beginners Guide</title>
      <dc:creator>Rahul</dc:creator>
      <pubDate>Wed, 05 Apr 2023 12:48:04 +0000</pubDate>
      <link>https://forem.com/fueler/learn-how-to-create-python-dictionary-beginners-guide-137o</link>
      <guid>https://forem.com/fueler/learn-how-to-create-python-dictionary-beginners-guide-137o</guid>
      <description>&lt;p&gt;Create your &lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;Dev Portfolio&lt;/a&gt; today at &lt;a href="//bit.ly/rahulinvitesdevelopers"&gt;Fueler&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbzcqe0o1936d9alwwwb6.png" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Welcome to the world of Python dictionaries! Whether you're a beginner looking to get your feet wet or an experienced programmer taking a refresher course, understanding dictionaries is an essential part of learning the language.&lt;/p&gt;

&lt;p&gt;So what exactly is a dictionary?&lt;/p&gt;

&lt;p&gt;Simply put, a Python dictionary is an unordered collection of key-value pairs. This means that each piece of data has associated with it a key that can be used to access it.&lt;/p&gt;

&lt;p&gt;As such, this makes dictionaries incredibly useful for organizing large sets of data in an efficient manner. &lt;/p&gt;

&lt;p&gt;In Python, dictionaries are used to store data in a way that’s both organized and accessible.&lt;/p&gt;

&lt;p&gt;Since data stored in dictionaries is organized into key-value pairs, retrieving or updating this information can be done quickly and easily—without having to remember exact locations or indices within the structure itself.&lt;/p&gt;

&lt;p&gt;This makes them easy to use and master for any level of programmer learning Python.&lt;/p&gt;

&lt;p&gt;In short, whether you’re dealing with small sets of data or large ones, Python dictionaries will make your life (and working with Python) much easier!&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Dictionary
&lt;/h2&gt;

&lt;p&gt;Creating a dictionary in Python is a simple and effective way to start making your data accessible and usable in your code.&lt;/p&gt;

&lt;p&gt;There are a few different ways that you can use to create a dictionary, each one has its strengths and weaknesses.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Using the dictionary constructor to create an empty dictionary:&lt;/em&gt; The simplest way to create an empty dictionary is to use the &lt;code&gt;dict()&lt;/code&gt; constructor. This method provides you with an empty structure that you can populate with keys and values.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Create an empty dictionary using the dict() constructor
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Add key-value pairs to the dictionary
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Rahul&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;age&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;
&lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Amsterdam&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

&lt;span class="c1"&gt;# Print the dictionary
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;returns&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Rahul&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;age&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Amsterdam&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Using curly braces ({}) to create a dictionary with initial values:&lt;/em&gt; If you know some of the initial key-value pairs you want in your dictionary, you can use curly braces {} to quickly set up the structure. This method is ideal for quickly creating dictionaries with an initial set of data.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# create a dictionary with initial values using curly braces
&lt;/span&gt;&lt;span class="n"&gt;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;make&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Jaguar&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;model&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Ftype&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;year&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2023&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;color&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;maroon&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# print the dictionary
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;Creating a dictionary with nested dictionaries:&lt;/em&gt; If you need more complex structures than simple key-value pairs, then this method will come in handy. It allows you to make nested dictionaries, which is useful when creating complex data structures like &lt;a href="https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/" rel="noopener noreferrer"&gt;graphs&lt;/a&gt; or &lt;a href="https://www.geeksforgeeks.org/binary-tree-data-structure/" rel="noopener noreferrer"&gt;trees&lt;/a&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Creating a dictionary with nested dictionaries
&lt;/span&gt;&lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;John&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;position&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Manager&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;salary&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Mary&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;position&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Developer&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;salary&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;70000&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Bob&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;position&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Developer&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;salary&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;65000&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;position&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Director&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;salary&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;150000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Sarah&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;position&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Manager&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;salary&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;120000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Tom&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;position&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Developer&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;salary&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;80000&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Accessing nested dictionary values
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;John&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Mary&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;position&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;# Output: Developer
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Sarah&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Tom&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;salary&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;# Output: 80000
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we have a dictionary called &lt;code&gt;employees&lt;/code&gt; that contains nested dictionaries.&lt;/p&gt;

&lt;p&gt;The first level of the dictionary contains the names of the employees as keys, and their information (position, salary, and team) as values. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;team&lt;/code&gt; key of each employee also contains a nested dictionary that contains the information of the employees in their team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing Dictionary Items
&lt;/h2&gt;

&lt;p&gt;Now that you've created a dictionary, let's look at how you can access the data inside it. To do this, you'll need to use dictionary keys and values.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retrieving Values Via Keys
&lt;/h3&gt;

&lt;p&gt;The simplest way to access a value in a Python Dictionary is to use the key associated with it. Let's see from an example:&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="c1"&gt;# Create a dictionary
&lt;/span&gt;&lt;span class="n"&gt;phonebook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Rahul&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;999999999&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sunaina&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;123456789&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;SunainaFather&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;456789123&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Retrieve a value using a key
&lt;/span&gt;&lt;span class="n"&gt;rahul_phone&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;phonebook&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Rahul&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Print the retrieved value
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rahul_phone&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we create a dictionary called &lt;code&gt;phonebook&lt;/code&gt; with three key-value pairs. We then use the key &lt;code&gt;"Rahul"&lt;/code&gt; to retrieve the associated value (999999999) from the &lt;code&gt;phonebook&lt;/code&gt; dictionary and assign it to a variable called &lt;code&gt;rahul_phone&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, we print the value of &lt;code&gt;rahul_phone&lt;/code&gt;, which should output 999999999.&lt;/p&gt;




&lt;p&gt;Read More - &lt;a href="https://fueler.io/blog/a-step-by-step-guide-to-comment-blocking-in-python" rel="noopener noreferrer"&gt;A Step-by-Step Guide to Comment Blocking in Python&lt;/a&gt; &lt;/p&gt;




&lt;h3&gt;
  
  
  Using The Get() Method To Access Dictionary Items Safely
&lt;/h3&gt;

&lt;p&gt;If you're not sure that the item exists in your Python Dictionary, then another option is to use the &lt;code&gt;.get()&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;This allows you to safely retrieve items from your dictionary — or, if that item doesn't exist, then it will return &lt;code&gt;None&lt;/code&gt; instead of throwing a &lt;code&gt;KeyError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&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="c1"&gt;# create a dictionary
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;banana&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;orange&lt;/span&gt;&lt;span class="sh"&gt;'&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="c1"&gt;# using the get() method
&lt;/span&gt;&lt;span class="n"&gt;apple_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pear_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;pear&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# print the results
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;apple_count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: 2
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pear_count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# Output: None
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we have a dictionary &lt;code&gt;my_dict&lt;/code&gt; with the key-value pairs &lt;code&gt;{'apple': 2, 'banana': 3, 'orange': 4}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We then use the &lt;code&gt;get()&lt;/code&gt; method to safely retrieve the value associated with the key &lt;code&gt;'apple'&lt;/code&gt; and &lt;code&gt;'pear'&lt;/code&gt;. The value associated with &lt;code&gt;'apple'&lt;/code&gt; is &lt;code&gt;2&lt;/code&gt;, so &lt;code&gt;apple_count&lt;/code&gt; is assigned the value &lt;code&gt;2&lt;/code&gt;. The key &lt;code&gt;'pear'&lt;/code&gt; is not in the dictionary, so &lt;code&gt;pear_count&lt;/code&gt; is assigned the value &lt;code&gt;None&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling KeyError Exceptions
&lt;/h3&gt;

&lt;p&gt;When accessing dictionary items in Python, it's important to handle the possibility of a KeyError exception. This occurs when a key is not found in the dictionary. Let's understand this from a short example.&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="c1"&gt;# Creating a dictionary with key-value pairs
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;banana&lt;/span&gt;&lt;span class="sh"&gt;"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orange&lt;/span&gt;&lt;span class="sh"&gt;"&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="c1"&gt;# Retrieving a value using a non-existent key
&lt;/span&gt;&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;grape&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;KeyError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The key &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;grape&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; does not exist in the dictionary&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we are trying to retrieve a value from the &lt;code&gt;my_dict&lt;/code&gt; dictionary using the key &lt;code&gt;'grape'&lt;/code&gt;. However, since this key doesn't exist in the dictionary, a &lt;code&gt;KeyError&lt;/code&gt; exception will be raised.&lt;/p&gt;

&lt;p&gt;To handle this exception, we can use a &lt;code&gt;try-except&lt;/code&gt; block. The &lt;code&gt;try&lt;/code&gt; block contains the code that may raise an &lt;strong&gt;exception&lt;/strong&gt;, while the &lt;code&gt;except&lt;/code&gt; block specifies &lt;strong&gt;how to handle&lt;/strong&gt; the exception.&lt;/p&gt;

&lt;p&gt;In this case, we are catching the &lt;code&gt;KeyError&lt;/code&gt; exception and printing a message to inform the user that the key doesn't exist in the dictionary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Modifying Dictionary Items
&lt;/h2&gt;

&lt;p&gt;Another important part of working with dictionaries is knowing how to modify existing items in them. &lt;/p&gt;

&lt;p&gt;On the surface, this might seem pretty simple—you just add it back in with a new value, right? Well, it's a bit more complex than that.&lt;/p&gt;

&lt;p&gt;A great way to get started is by first learning the syntax: it requires brackets and quotation marks at certain points.&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="c1"&gt;# Creating a dictionary with some initial values
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;age&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;New York&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Modifying an existing item in the dictionary
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;age&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;

&lt;span class="c1"&gt;# Printing the updated dictionary
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we first create a dictionary called &lt;code&gt;my_dict&lt;/code&gt; with some initial key-value pairs. We then modify the value of the 'age' key to 26 by accessing it using the brackets and assigning the new value to it.&lt;/p&gt;

&lt;p&gt;It's important to note that if the key you're trying to modify doesn't already exist in the dictionary, the above approach will create a new key-value pair. &lt;/p&gt;

&lt;p&gt;If you don't want that to happen, you can use the &lt;code&gt;update()&lt;/code&gt; method to modify existing items or add new ones.&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="c1"&gt;# Creating a dictionary with some initial values
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;age&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;New York&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Modifying an existing item or adding a new one using the update() method
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;age&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;gender&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;female&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="c1"&gt;# Printing the updated dictionary
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we use the &lt;code&gt;update()&lt;/code&gt; method to modify the value of the 'age' key to 26 and add a new key-value pair for 'gender'. Note that the update() method takes a dictionary as an argument, and any matching keys in the dictionary will be updated with their corresponding values.&lt;/p&gt;

&lt;p&gt;Modifying items in an existing dictionary requires some code literacy and trial and error, but once you have the syntax down, you'll be able to edit your dictionaries like a pro!&lt;/p&gt;

&lt;h2&gt;
  
  
  Dictionary Methods and Operations
&lt;/h2&gt;

&lt;p&gt;Let's now look at some useful dictionary methods and operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;len()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;len()&lt;/code&gt; function is great for finding out the length of a dictionary, that is, the number of items in a dictionary. You can use this to see how many items your dictionary contains. &lt;/p&gt;

&lt;p&gt;In short, a simple &lt;code&gt;len()&lt;/code&gt; statement will display how many keys and values are stored within your dictionary!&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;my_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;banana&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;orange&lt;/span&gt;&lt;span class="sh"&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;pear&lt;/span&gt;&lt;span class="sh"&gt;'&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="c1"&gt;# Finding the length of the dictionary
&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The dictionary contains&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;items.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So here the &lt;code&gt;len()&lt;/code&gt; function will return the number of key-value pairs in the dictionary, which is 4 in this case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Checking if a key exists using the &lt;code&gt;in&lt;/code&gt; keyword
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;in&lt;/code&gt; keyword can be used to check if a key exists in a given Python Dictionary. Let's see a small example:&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="c1"&gt;# Create a dictionary
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;banana&lt;/span&gt;&lt;span class="sh"&gt;"&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;orange&lt;/span&gt;&lt;span class="sh"&gt;"&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="c1"&gt;# Check if a key exists using the in keyword
&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The key &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; exists in the dictionary!&lt;/span&gt;&lt;span class="sh"&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The key &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt; does not exist in the dictionary.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we create a dictionary called my_dict with three key-value pairs. We then use the in keyword to check if the key "apple" exists in the dictionary. Since "apple" is a key in the dictionary, the output will be: The key 'apple' exists in the dictionary!.&lt;/p&gt;

&lt;p&gt;If the key you are looking for does exist, then anything within the if statement will execute; otherwise, nothing happens—it's as simple as that!&lt;/p&gt;

&lt;h3&gt;
  
  
  Retrieving all Keys and Values of a Dictionary
&lt;/h3&gt;

&lt;p&gt;You can retrieve all keys and values of a dictionary with two built-in functions: &lt;code&gt;dict.keys()&lt;/code&gt;, which shows the keys to your dictionary; and &lt;code&gt;dict.values()&lt;/code&gt;, which returns all values of the given Python Dictionary.&lt;/p&gt;

&lt;p&gt;What's more, you can find out both keys and values by using the &lt;code&gt;dict.items()&lt;/code&gt; method!&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="c1"&gt;# Create a dictionary with some key-value pairs
&lt;/span&gt;&lt;span class="n"&gt;my_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;banana&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;orange&lt;/span&gt;&lt;span class="sh"&gt;'&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="c1"&gt;# Retrieve all keys and values of the dictionary using the items() method
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we create a dictionary &lt;code&gt;my_dict&lt;/code&gt; with some key-value pairs. We then use the &lt;code&gt;items()&lt;/code&gt; method to retrieve all the keys and values of the dictionary, and iterate through them using a &lt;code&gt;for&lt;/code&gt; loop. &lt;/p&gt;

&lt;p&gt;Inside the loop, we print out each key and its corresponding value using string formatting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating and Merging Dictionaries
&lt;/h3&gt;

&lt;p&gt;Updating an existing Python Dictionary with new data is easy—simply use the &lt;code&gt;update()&lt;/code&gt; method, which takes two parameters: one that contains all keys and values from another dictionary (the source) and another that points to an existing dictionary (the target).&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="c1"&gt;# Updating a dictionary
&lt;/span&gt;&lt;span class="n"&gt;dict1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;John&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;age&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;dict2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;age&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;New York&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;dict1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dict2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dict1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Output: {'name': 'John', 'age': 35, 'city': 'New York'}
&lt;/span&gt;
&lt;span class="c1"&gt;# Merging two dictionaries
&lt;/span&gt;&lt;span class="n"&gt;dict3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;gender&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Female&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;dict4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;age&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;city&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Chicago&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;merged_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;dict3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;dict4&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;merged_dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Output: {'name': 'Alice', 'gender': 'Female', 'age': 25, 'city': 'Chicago'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we have two dictionaries &lt;code&gt;dict1&lt;/code&gt; and &lt;code&gt;dict2&lt;/code&gt;. We update &lt;code&gt;dict1&lt;/code&gt; with the contents of &lt;code&gt;dict2&lt;/code&gt; using the &lt;code&gt;update()&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;This changes the value of the 'age' key in &lt;code&gt;dict1&lt;/code&gt; to 35 and adds the 'city' key with the value 'New York' to &lt;code&gt;dict1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we have two dictionaries &lt;code&gt;dict3&lt;/code&gt; and &lt;code&gt;dict4&lt;/code&gt;. We merge these dictionaries into a new dictionary &lt;code&gt;merged_dict&lt;/code&gt; using the &lt;code&gt;{**dict1, **dict2}&lt;/code&gt; syntax, which combines the key-value pairs from both dictionaries into a single dictionary.&lt;/p&gt;

&lt;p&gt;You're getting it right? Just take time to understand, it can be a bit confusing but when you get it, it's easy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterating Through a Dictionary
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Using a for loop to iterate through a dictionary
&lt;/h3&gt;

&lt;p&gt;You can also use a for loop to iterate through a dictionary. A for loop is another way of repeating an action or set of actions over and over again. It's like a robotic machine, where you give it instructions and it will simply follow them.&lt;/p&gt;

&lt;p&gt;Basically, what you do is tell the loop to go through each value in your dictionary, one at a time, and then execute the instructions that you have given it. &lt;/p&gt;

&lt;p&gt;It will stop when it reaches the end of your dictionary. Here's an example:&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="c1"&gt;# Create a dictionary of fruits and their colors
&lt;/span&gt;
&lt;span class="n"&gt;fruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;apple&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;red&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;banana&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yellow&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;grape&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;purple&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Loop through the keys and values of the dictionary
&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;fruits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;fruit&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Output:
# The apple is red
# The banana is yellow
# The grape is purple
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we first create a dictionary called &lt;code&gt;fruits&lt;/code&gt; that contains the names of different fruits as keys and their corresponding colors as values.&lt;/p&gt;

&lt;p&gt;Then, we use a &lt;code&gt;for&lt;/code&gt; loop to iterate through the keys and values of the &lt;code&gt;fruits&lt;/code&gt; dictionary using the &lt;code&gt;items()&lt;/code&gt; method. Inside the loop, we assign the key to the variable &lt;code&gt;fruit&lt;/code&gt; and the value to the variable &lt;code&gt;color&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, we use the &lt;code&gt;print()&lt;/code&gt; function to display a message for each fruit and its corresponding color. &lt;/p&gt;

&lt;p&gt;The loop will stop when it reaches the end of the &lt;code&gt;fruits&lt;/code&gt; dictionary.&lt;/p&gt;

&lt;p&gt;Pretty cool right? Iterating with a for loop is just one of many ways to work with dictionaries in Python—there's plenty of other methods that you can explore!&lt;/p&gt;

&lt;h3&gt;
  
  
  items() method
&lt;/h3&gt;

&lt;p&gt;item() method is used to retrieving keys and values. Let's see a simple example for this:&lt;/p&gt;

&lt;p&gt;Suppose we have a dictionary that contains the names and ages of some individuals as key-value pairs. We can use the items() method to iterate through the dictionary and retrieve the name and age of each person as a pair.&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="c1"&gt;# Creating a dictionary with names and ages of individuals
&lt;/span&gt;&lt;span class="n"&gt;ages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Alice&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Bob&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Charlie&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Iterating through the dictionary using the items() method
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;ages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;is&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;years old&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#The output
&lt;/span&gt;&lt;span class="n"&gt;Alice&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;
&lt;span class="n"&gt;Bob&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;
&lt;span class="n"&gt;Charlie&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt; &lt;span class="n"&gt;years&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In conclusion, Python dictionaries make it easy to manage complex data. They're a powerful tool for data manipulation, and a great way to store and access information. With this beginner's guide, you now have the information you need to start using Python dictionaries for yourself.&lt;/p&gt;

&lt;p&gt;You'll be able to create, modify, and iterate over dictionaries with ease. Go ahead and get started now—you'll be glad you did.&lt;/p&gt;




&lt;p&gt;Create your &lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;Dev Portfolio&lt;/a&gt; today at &lt;a href="//bit.ly/rahulinvitesdevelopers"&gt;Fueler&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbzcqe0o1936d9alwwwb6.png" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>A Step-by-Step Guide to Comment Blocking in Python</title>
      <dc:creator>Rahul</dc:creator>
      <pubDate>Wed, 05 Apr 2023 12:27:30 +0000</pubDate>
      <link>https://forem.com/fueler/a-step-by-step-guide-to-comment-blocking-in-python-gih</link>
      <guid>https://forem.com/fueler/a-step-by-step-guide-to-comment-blocking-in-python-gih</guid>
      <description>&lt;p&gt;If you're new to coding in Python, you may be wondering what comment blocking is and how it can help you. &lt;/p&gt;

&lt;p&gt;Well, comment blocking is a process that helps you organize your code, which is important for readability, maintainability, and debugging.&lt;/p&gt;

&lt;p&gt;By using comment blocks, you can separate different parts of your code into logical sections. &lt;/p&gt;

&lt;p&gt;This makes it easy to quickly find parts of a program when making changes or troubleshooting issues. &lt;/p&gt;

&lt;p&gt;The block comments also serve as helpful reminders for yourself or other coders who need to make changes in the future.&lt;/p&gt;

&lt;p&gt;Comment blocks help ensure that your code remains organized and easy to work with. It also allows you to quickly jump between different functional parts of a program without having to manually search through the codebase.&lt;/p&gt;

&lt;p&gt;Create your &lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;Dev Portfolio&lt;/a&gt; today at &lt;a href="//bit.ly/rahulinvitesdevelopers"&gt;Fueler&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbzcqe0o1936d9alwwwb6.png" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Write Single Line Comments in Python
&lt;/h2&gt;

&lt;p&gt;Writing comments in your code is a great way to organize it, keep track of your progress, and make sure that anyone else who might use your code can understand it more easily. In Python, you can create single line comments using a pound (#) symbol at the beginning of a line.&lt;/p&gt;

&lt;p&gt;When you use this symbol in Python, anything that follows the # is considered part of the comment-in other words, it won't be read by the interpreter at runtime. &lt;/p&gt;

&lt;p&gt;This is useful for adding explanations and context to your code without changing its behavior.&lt;/p&gt;

&lt;p&gt;For instance, if you’re writing a function that will calculate the tax on a purchase, you might include an explanatory comment like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# calculate tax on a purchase
tax_amount = purchase_amt * 0.085
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Including helpful comments like this allows anyone looking at your code to understand what each line does and why it’s there. It also creates an easier path for debugging later down the road.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Write Multi Line Comments in Python
&lt;/h2&gt;

&lt;p&gt;If you want to write a multi-line comment in Python, then you can use the triple quotation marks. This is useful when you are writing long comments that need to be broken into multiple lines.&lt;/p&gt;

&lt;p&gt;Here's an example of what a multi-line comment would look like in Python:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;""" This is a

multi-line comment

in Python """
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use the multi-line comment in your code. For example, if you are writing a large block of code and you want to add some explanatory comments, then you can use the triple quotation marks to do this.&lt;/p&gt;

&lt;p&gt;The comments will appear after the code block, which makes them easier to read and understand.&lt;/p&gt;

&lt;p&gt;Another great thing about using the triple quotation marks is that they allow you to add more detailed comments than a single line of text would allow. &lt;/p&gt;

&lt;p&gt;This makes it easier to explain what is happening in your code and provide helpful notes for other developers who might be reading your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Comment Blocking for Python Programming
&lt;/h2&gt;

&lt;p&gt;You might be wondering what the benefits are that come with comment blocking in Python; &lt;/p&gt;

&lt;p&gt;after all, why bother when it can feel like an extra step in your programming workflow? &lt;/p&gt;

&lt;p&gt;As it turns out, there are some great reasons to get on board with comment blocking.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Testing made easier
&lt;/h3&gt;

&lt;p&gt;Comment blocking helps you to quickly test snippets of code within larger projects, and means you don’t have to keep cutting out desired portions from within a program and pasting them into a new file.&lt;/p&gt;

&lt;p&gt;Because comment blocks exist outside of the main code, you can easily pull them out for testing purposes without worry.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Improved clarity
&lt;/h3&gt;

&lt;p&gt;Comment blocking helps both yourself and other readers of your code understand your logic quickly and easily; &lt;/p&gt;

&lt;p&gt;each block is divided from the main program, so at a glance it’s easy to understand which pieces of code are related.&lt;/p&gt;

&lt;p&gt;This makes project troubleshooting a breeze for other programmers who may be collaborating on your project.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. More efficient coding
&lt;/h3&gt;

&lt;p&gt;Comment blocking allows you to keep all your related functions in the same area and significantly reduces the amount of scrolling required when searching through longer programs.&lt;/p&gt;

&lt;p&gt;Having the ability to quickly move between related functions can save huge amounts of time in development.&lt;/p&gt;

&lt;h2&gt;
  
  
  Commonly Asked Questions About Python Comment Blocking
&lt;/h2&gt;

&lt;p&gt;Comment blocking can seem like an intimidating task for the beginner Python coder, but don’t worry—it’s actually not so hard, once you understand the fundamentals.&lt;/p&gt;

&lt;p&gt;To help you get started, here are some of the most commonly asked questions about comment blocking in Python.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Is it possible to block comments in Python?
&lt;/h3&gt;

&lt;p&gt;Yes! Comment blocking is actually a straightforward process in Python. All you need to do is use the “#” symbol before any text that you want to be blocked, and it will be ignored by the compiler.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. How will comment blocking affect code execution?
&lt;/h3&gt;

&lt;p&gt;Comment blocking won't affect code execution—in fact, it's completely invisible to the compiler. &lt;/p&gt;

&lt;p&gt;You can use comment blocking to create notes or reminders for yourself and other coders without affecting the program's functionality.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Is everything between /* and */ ignored by the program in Python?
&lt;/h3&gt;

&lt;p&gt;No, everything between &lt;code&gt;/*&lt;/code&gt; and &lt;code&gt;*/&lt;/code&gt; is not ignored in Python because this is not the syntax used for comments in Python. In Python, single-line comments are denoted by a hash symbol (#) and multi-line comments can be created using triple quotes (''' or """).&lt;/p&gt;

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

&lt;p&gt;Commenting in Python is a great way to organize and simplify your code. With the help of this guide, you've learnt how to block comments to make your code more readable and easier to debug.&lt;/p&gt;

&lt;p&gt;By following the steps outlined in this tutorial, you can now confidently add comments to your Python code and know that it will be blocked properly. &lt;/p&gt;

&lt;p&gt;So be sure to practice this skill and enable yourself to comment with ease in the future!&lt;/p&gt;

&lt;p&gt;Create your &lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;Dev Portfolio&lt;/a&gt; today at &lt;a href="//bit.ly/rahulinvitesdevelopers"&gt;Fueler&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbzcqe0o1936d9alwwwb6.png" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>python</category>
    </item>
    <item>
      <title>Ultimate Guide to Big O Notation in 2023 | A Comprehensive Cheatsheet</title>
      <dc:creator>Rahul</dc:creator>
      <pubDate>Tue, 04 Apr 2023 13:00:46 +0000</pubDate>
      <link>https://forem.com/fueler/ultimate-guide-to-big-o-notation-in-2023-a-comprehensive-cheatsheet-589o</link>
      <guid>https://forem.com/fueler/ultimate-guide-to-big-o-notation-in-2023-a-comprehensive-cheatsheet-589o</guid>
      <description>&lt;p&gt;If you're feeling a bit overwhelmed by the concept of Big O Notation and need a quick cheat sheet to help you understand it, then you've come to the right place! &lt;/p&gt;

&lt;p&gt;In this comprehensive guide, we'll give you all the tips and tricks you need to learn Big O Notation quickly and easily.&lt;/p&gt;

&lt;p&gt;From the basics of Big O to some of its most common symbols, we'll cover what you need to know to help you in exams or just for coding at home.&lt;/p&gt;

&lt;p&gt;Whether you're new to computer science or already have some coding skills, this cheat sheet will give you all the information you need about Big O Notation in one easy-to-follow guide.&lt;/p&gt;

&lt;p&gt;So let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Big O Notation
&lt;/h2&gt;

&lt;p&gt;If you're a programmer or studying computer science, you've probably heard of Big O notation. It's an important tool used in the analysis of algorithms and helps to determine the time complexity of a given software or program.&lt;/p&gt;

&lt;p&gt;It's also essential for optimizing code and understanding how efficient your software performs.&lt;/p&gt;

&lt;p&gt;An easy-to-follow Big O cheatsheet can help make understanding this complex concept simpler.&lt;/p&gt;

&lt;p&gt;Big O notation is used to describe the &lt;em&gt;runtime&lt;/em&gt; or &lt;em&gt;space complexity&lt;/em&gt; of an algorithm.&lt;/p&gt;

&lt;p&gt;A high-level description can be provided quickly for a simple algorithm without diving into every detail about how it works.&lt;/p&gt;

&lt;p&gt;In Big O notation, two variables are used - &lt;strong&gt;the input size (n)&lt;/strong&gt;, and the amount of time it takes to &lt;strong&gt;execute (t)&lt;/strong&gt;. The notation gives us a way to compare algorithms based on their execution times - that is, to find out which algorithm is more efficient when dealing with different input sizes.&lt;/p&gt;

&lt;p&gt;The purpose of our cheatsheet is to provide an overview of Big O notation, including its symbols, examples, and rules for determining time complexity.&lt;/p&gt;

&lt;p&gt;To make things easier for those just starting out with Big O notation we'll provide examples using simple algorithms so you can better understand the concepts at hand.&lt;/p&gt;

&lt;p&gt;Also included are resources to deepen your understanding should you want to explore further into this topic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Concepts
&lt;/h2&gt;

&lt;p&gt;If you’ve been brushing up on algorithms and data structures, you may have heard of something called Big O Notation. But what is Big O Notation and why is it so important? Let's dive into this topic with a refresher on the basics to get you up to speed.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Does "O" Stand For?
&lt;/h3&gt;

&lt;p&gt;The "O" stands for the Greek letter "Omega," which essentially means infinite or unbounded. So when someone says something has an "O(n)" time complexity, they're talking about the runtime of an algorithm being relative to the size or quantity of input data (n).&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Time Complexity?
&lt;/h3&gt;

&lt;p&gt;Time complexity is a measure of how much time it takes for a computer algorithm to run and is usually expressed using Big O notation. It's a useful tool to help you understand how efficient certain algorithms are and how they scale with extra data.&lt;/p&gt;

&lt;p&gt;Basically, the time complexity of an algorithm will determine how fast it can complete a task and how much memory it will require for larger datasets.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Space Complexity?
&lt;/h3&gt;

&lt;p&gt;When it comes to understanding Big O Notation, one of the key elements that can define the performance of an algorithm is space complexity. Space complexity measures the amount of memory that an algorithm requires for it to execute, and is expressed in terms of the size of the input.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding Asymptotic Behavior
&lt;/h3&gt;

&lt;p&gt;Understanding Big O Notation requires understanding asymptotic behavior, which is when a function grows &lt;strong&gt;increasingly&lt;/strong&gt; larger or smaller than some base value.&lt;/p&gt;

&lt;p&gt;That increase can be taken at different rates—&lt;code&gt;linear (O(n))&lt;/code&gt;, &lt;code&gt;logarithmic (O(log n))&lt;/code&gt;, &lt;code&gt;quadratic (O(n^2))&lt;/code&gt;, &lt;code&gt;cubic (O(n^3))&lt;/code&gt;—and so on.&lt;/p&gt;

&lt;p&gt;There are also common functions in Big O Notation that include &lt;strong&gt;constant functions&lt;/strong&gt; (all runtimes are equal), &lt;strong&gt;exponential functions&lt;/strong&gt; (runtime increases exponentially), and &lt;strong&gt;loglinear functions&lt;/strong&gt; (when the runtime increases linearly with each additional input).&lt;/p&gt;

&lt;p&gt;Now that you have a better understanding of the basic concepts behind Big O Notation, you're ready to explore this mathematical tool further!&lt;/p&gt;

&lt;p&gt;Below this section, you will look at the key time and space complexities, explanations, and examples.&lt;/p&gt;




&lt;h2&gt;
  
  
  Big O Notation Cheatsheet
&lt;/h2&gt;

&lt;p&gt;Learning Big O notation can be a challenge, and you may not have time to become an expert. That's why we've created this Big O Notation cheat sheet for you to bookmark and refer to as needed.&lt;/p&gt;

&lt;p&gt;Let's quickly break it down:&lt;/p&gt;

&lt;h3&gt;
  
  
  Constant Time - O(1)
&lt;/h3&gt;

&lt;p&gt;This is the fastest time complexity class — it means an algorithm takes the same amount of time regardless of the input size. A good example would be when you add two numbers together — it takes exactly the same amount of time whether you are adding &lt;code&gt;1 + 1&lt;/code&gt; or &lt;code&gt;10^256 + 10^256&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's understand this from a simple example:&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;add_numbers&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="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the function &lt;code&gt;add_numbers&lt;/code&gt; takes two integer inputs &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; and returns their sum. No matter what the values of &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are, the function will always take the same amount of time to execute as it only performs a single operation of adding the two inputs together.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logarithmic Time - O(log n)
&lt;/h3&gt;

&lt;p&gt;This is a bit slower than constant time, but still pretty fast. Put simply, it means that if your input size doubles, the algorithm will only take one more step to finish which is amazing compared to other complexity classes.&lt;/p&gt;

&lt;p&gt;An example would be searching a sorted array — as you iterate through it, the search space gets halved after each check since “binary search” divides and conquers approach is used.&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;binary_search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;right&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the binary search algorithm takes a sorted array "arr" and a target value "target" as input. It initializes two pointers "left" and "right" to the beginning and end of the array respectively, and then repeatedly divides the search space in half by computing the midpoint "mid".&lt;/p&gt;

&lt;p&gt;If the target value is found at the midpoint, the function returns the index of the midpoint. If the target value is less than the midpoint, the function continues the search on the left half of the array by updating the "right" pointer.&lt;/p&gt;

&lt;p&gt;Otherwise, the function continues the search on the right half of the array by updating the "left" pointer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linear Time - O(n)
&lt;/h3&gt;

&lt;p&gt;This complexity type is actually quite slow compared to the previous two, because here your time for computation grows proportionally with your input size — i.e., double your inputs and the algorithm will take twice as much time.&lt;/p&gt;

&lt;p&gt;An example would be finding something in an unsorted array — each iteration takes longer until you find what you’re looking for.&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;find_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;

&lt;span class="n"&gt;my_list&lt;/span&gt; &lt;span class="o"&gt;=&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;7&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;9&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;find_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# Output: True
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;find_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_list&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="c1"&gt;# Output: False
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the function &lt;code&gt;find_element&lt;/code&gt; takes a list &lt;code&gt;my_list&lt;/code&gt; and an element &lt;code&gt;element&lt;/code&gt; as input. It iterates through the list and checks each element to see if it matches the desired element. If it finds a match, it returns &lt;code&gt;True&lt;/code&gt;. If it reaches the end of the list without finding a match, it returns &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The time complexity of this algorithm is O(n), as the number of iterations (that is the time it takes to find the element) grows proportionally with the size of the list.&lt;/p&gt;

&lt;h3&gt;
  
  
  Linearithmic Time - O(n log n)
&lt;/h3&gt;

&lt;p&gt;Linearithmic time is an algorithm that takes a certain amount of time to run but scales logarithmically with the size of the data input. This means that when you double your data set, the algorithm only takes twice as long to run.&lt;/p&gt;

&lt;p&gt;Let's say you have an unsorted list of numbers and you want to sort them in ascending order using the Merge Sort algorithm.&lt;/p&gt;

&lt;p&gt;Merge Sort has a time complexity of O(n log n) in the worst-case scenario, meaning that as the size of the input list (n) increases, the time taken to sort the list increases logarithmically.&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;merge_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;

    &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&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="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;

    &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;merge_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;merge_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&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;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;j&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;while&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
            &lt;span class="n"&gt;j&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;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, when we call the &lt;code&gt;merge_sort&lt;/code&gt; function on an unsorted list, it will divide the list into smaller sublists, sort them recursively using &lt;code&gt;merge_sort&lt;/code&gt;, and then merge them back together using the &lt;code&gt;merge&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;The time complexity of Merge Sort is O(n log n) because the algorithm divides the input list into halves repeatedly until each sublist contains only one element (which takes &lt;code&gt;O(log n) time&lt;/code&gt;), and then merges the sublists together in a linear time of O(n) for each level of the recursion, resulting in a total time complexity of O(n log n).&lt;/p&gt;

&lt;p&gt;As the size of the input list (n) increases, the time taken to sort the list increases logarithmically.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exponential time - &lt;code&gt;O(2^n)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Exponential Time (O(2^n)) is when the time required to complete an algorithm doubles with each increase in input size.&lt;/p&gt;

&lt;p&gt;This means that the growth rate of this type of algorithm is exponential—for every extra item in its input, the time it needs more than doubles!&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;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;n&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;return&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fibonacci&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we have a recursive function that calculates the nth Fibonacci number. The function makes two recursive calls for each value of n. As a result, the time required to calculate the nth Fibonacci number doubles with each increase in n.&lt;/p&gt;

&lt;h3&gt;
  
  
  Factorial time - &lt;code&gt;O(n!)&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Factorial Time (O(n!)) refers to a type of algorithm which requires processing all possible combinations of a set.&lt;/p&gt;

&lt;p&gt;This means that the input size increases exponentially—for every extra item added, there are more unique combinations possible, and thus more calculation required.&lt;/p&gt;

&lt;p&gt;Let's say you have a list of numbers &lt;code&gt;[1, 2, 3]&lt;/code&gt;, and you want to find all possible permutations of the 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="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;itertools&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;find_permutations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lst&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;permutations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;permutation&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;itertools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;permutations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lst&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;permutations&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;permutation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;permutations&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we call this function with the list &lt;code&gt;[1, 2, 3]&lt;/code&gt;, it will return a list of all possible permutations of that list: &lt;code&gt;[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Data Structure Complexity Chart
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Access means accessing an element at a given index&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Search means finding an element with a given key&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Insertion means adding a new element to the data structure&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Deletion means removing an element from the data structure&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;n is the number of elements in the data structure&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;m is the length of the key being searched in the Trie&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"-" means the operation is not applicable or not defined for that data structure.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Data Structure&lt;/th&gt;
&lt;th&gt;Access&lt;/th&gt;
&lt;th&gt;Search&lt;/th&gt;
&lt;th&gt;Insertion&lt;/th&gt;
&lt;th&gt;Deletion&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Array&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linked List&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)/O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stack&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Queue&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hash Table&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Binary Search Tree&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O( log n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B-Tree&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O( log n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Algorithm Complexity Chart
&lt;/h3&gt;

&lt;p&gt;When talking about Big O Notation, it's important to differentiate between best-case, average-case and worst-case scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Case Scenario&lt;/strong&gt;: The best-case scenario is when a given algorithm takes the least amount of time to complete it’s task. This means it has the most optimal performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Average Case Scenario&lt;/strong&gt;: The average-case scenario is when an algorithm completes its task in an expected amount of time – neither too fast nor too slow. It's neither the best nor worst case, but rather a combination of different scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Worst Case Scenario&lt;/strong&gt;: The worst-case scenario is when a given algorithms takes the most amount of time to complete its task. This means that it has the least optimal performance. This is important to consider because algorithms are typically designed with worst-case scenarios in mind, so they run efficiently even under unfavorable conditions.&lt;/p&gt;

&lt;p&gt;When talking about Big O Notation, it's important to understand each scenario and determine which one best suits your needs – as this can make a huge difference in terms of performance!&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Algorithm&lt;/th&gt;
&lt;th&gt;Best case&lt;/th&gt;
&lt;th&gt;Average Case&lt;/th&gt;
&lt;th&gt;Worst Case&lt;/th&gt;
&lt;th&gt;Space Complexity&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bubble Sort&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n^2)&lt;/td&gt;
&lt;td&gt;O(n^2)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insertion Sort&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n^2)&lt;/td&gt;
&lt;td&gt;O(n^2)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Selection Sort&lt;/td&gt;
&lt;td&gt;O(n^2)&lt;/td&gt;
&lt;td&gt;O(n^2)&lt;/td&gt;
&lt;td&gt;O(n^2)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Merge Sort&lt;/td&gt;
&lt;td&gt;O(n log n)&lt;/td&gt;
&lt;td&gt;O(n log n)&lt;/td&gt;
&lt;td&gt;O(n log n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Quick Sort&lt;/td&gt;
&lt;td&gt;O(n log n)&lt;/td&gt;
&lt;td&gt;O(n log n)&lt;/td&gt;
&lt;td&gt;O(n^2)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Heap Sort&lt;/td&gt;
&lt;td&gt;O(n log n)&lt;/td&gt;
&lt;td&gt;O(n log n)&lt;/td&gt;
&lt;td&gt;O(n log n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Binary Search&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  Which one to focus on? Time Complexity or Space Complexity?
&lt;/h3&gt;

&lt;p&gt;The answer depends on your use case. If you're dealing with a real-time system like a video game with strict time constraints then it's important to focus on time complexity; if you're concerned about memory usage then focus on space complexity.&lt;/p&gt;

&lt;p&gt;Ultimately, it depends on your underlying needs — knowing when to focus on each type of Big O notation can help you create better algorithms for your use cases and optimize your codebase.&lt;/p&gt;

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

&lt;p&gt;In summary, Big O notation is an essential tool for any developer or data scientist. It's a simple way of identifying the time complexity of an algorithm, and is incredibly useful when analyzing algorithms to see which perform best. It can also be used to evaluate data structures and determine the best one to use for a particular application.&lt;/p&gt;

&lt;p&gt;Having a solid understanding of Big O notation will help you become a better programmer and data scientist. With the cheatsheet in this article, you can look up the complexity of any algorithm or data structure, and improve your coding.&lt;/p&gt;

&lt;p&gt;So, don't miss the chance to become a pro and build algorithms for complex data structures with the help of Big O notation cheatsheets.&lt;/p&gt;




&lt;p&gt;Create your &lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;Dev Portfolio&lt;/a&gt; today at &lt;a href="https://fueler.io/" rel="noopener noreferrer"&gt;Fueler&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://fueler.io/use-case/developer" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbzcqe0o1936d9alwwwb6.png" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
