<?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>DEV Community: Siddhesh Agarwal</title>
    <description>The latest articles on DEV Community by Siddhesh Agarwal (@siddhesh_agarwal).</description>
    <link>https://dev.to/siddhesh_agarwal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F700017%2F1e385636-f19b-483f-98ee-be1a20b0a01a.jpg</url>
      <title>DEV Community: Siddhesh Agarwal</title>
      <link>https://dev.to/siddhesh_agarwal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siddhesh_agarwal"/>
    <language>en</language>
    <item>
      <title>Formatter for Jupyter notebooks</title>
      <dc:creator>Siddhesh Agarwal</dc:creator>
      <pubDate>Sat, 18 Feb 2023 06:14:56 +0000</pubDate>
      <link>https://dev.to/siddhesh_agarwal/formatting-jupyter-notebooks-from-the-terminal-18d6</link>
      <guid>https://dev.to/siddhesh_agarwal/formatting-jupyter-notebooks-from-the-terminal-18d6</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;If you are into Data Science or Machine Learning, you have probably come across jupyter notebooks (.ipynb files). The problem I faced when using jupyter notebooks was that the black formatter didn't work on them. I had tried using the&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ black notebook.ipynb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;command many times. This article is meant to help with code formatting in Python Notebooks.&lt;/p&gt;




&lt;h1&gt;
  
  
  nbQA
&lt;/h1&gt;

&lt;p&gt;So, we will be using a python library called nbQA along with code formatters like Black and isort.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Install the library using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install nbqa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;You can use various formatters along with nqba and I will demonstrate how to use a few of them. before trying the formatters, make sure you have installed them already.&lt;/p&gt;

&lt;h3&gt;
  
  
  black
&lt;/h3&gt;

&lt;p&gt;Format the notebook using black as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nbqa black notebook.ipynb
reformatted notebook.ipynb
All done! ✨ 🍰 ✨
1 files reformatted.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  isort
&lt;/h3&gt;

&lt;p&gt;Similarly, format the notebook using isort:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nbqa isort notebook.ipynb
Fixing notebook.ipynb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  yapf
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nbqa yapf --in-place notebook.ipynb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  autopep8
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nbqa autopep8 -i notebook.ipynb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  mdformat
&lt;/h3&gt;

&lt;p&gt;To format the markdown cells in your notebook, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nbqa mdformat notebook.ipynb --nbqa-md --nbqa-diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  doctest
&lt;/h3&gt;

&lt;p&gt;To run tests for iPython notebooks using doctypes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nbqa doctest notebook.ipynb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;I hope you liked it. That's all for this time.&lt;br&gt;
&lt;a href="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%2Fi1zik6jzq4s0bprhi46c.gif" class="article-body-image-wrapper"&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%2Fi1zik6jzq4s0bprhi46c.gif" alt="The end" width="499" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>welcome</category>
      <category>community</category>
    </item>
    <item>
      <title>Packaging and Publishing on PyPI</title>
      <dc:creator>Siddhesh Agarwal</dc:creator>
      <pubDate>Tue, 19 Apr 2022 03:17:07 +0000</pubDate>
      <link>https://dev.to/siddhesh_agarwal/packaging-and-publishing-on-pypi-1lbd</link>
      <guid>https://dev.to/siddhesh_agarwal/packaging-and-publishing-on-pypi-1lbd</guid>
      <description>&lt;p&gt;This post is on "Packaging and Publishing a python library on &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPI&lt;/a&gt;".&lt;/p&gt;

&lt;p&gt;There is a tutorial on &lt;a href="https://packaging.python.org/en/latest/tutorials/packaging-projects/" rel="noopener noreferrer"&gt;Packaging Python Projects&lt;/a&gt; in the official PyPI website, but these docs are... outdated.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is PyPI?
&lt;/h2&gt;

&lt;p&gt;As the website says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Python Package Index (PyPI) is a repository of software for the Python programming language.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means that PyPI is the official &lt;strong&gt;third-party software&lt;/strong&gt; repository for Python. Anyone (with a PyPI account) can publish a Python Package for the use of the other developers. PyPI hosts these Python packages in the form of sdists (source distributions) or precompiled "wheels".&lt;/p&gt;




&lt;h2&gt;
  
  
  File structure
&lt;/h2&gt;

&lt;p&gt;You have to maintain a certain file structure before packaging your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package_name/
├── src/
|    ├── __init__.py
|    ├── example.py
|    └── py.typed
├── LICENSE
├── pyproject.toml
├── README.md
├── setup.py (or) setup.cfg
└── tests/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So here is what each file does:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;src/__init__.py&lt;/code&gt;: This file contains all the import statements. We add statements that look like &lt;code&gt;from .example import &amp;lt;function names&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;src/example.py&lt;/code&gt;: This file contains all the function and class definitions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;src/py.typed&lt;/code&gt;: An empty file. &lt;a href="https://peps.python.org/pep-0561/#packaging-type-information" rel="noopener noreferrer"&gt;Read more here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LICENSE&lt;/code&gt;: This file contains the license for the code you are publishing. 

&lt;ul&gt;
&lt;li&gt;You can choose a license from &lt;a href="https://choosealicense.com/" rel="noopener noreferrer"&gt;choosealicense.com&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;pyproject.toml&lt;/code&gt;: This file tells build tools (like build and pip) what is required to build your package. For now, this will do:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[build-system]
requires = [
    "setuptools&amp;gt;=42",
    "wheel"
]
build-backend = "setuptools.build_meta"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;README.md&lt;/code&gt;: This file acts as a guide. It gives developers a detailed description of your project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;setup.py&lt;/code&gt; (dynamic) or &lt;code&gt;setup.cfg&lt;/code&gt; (static) are files that give "setuptools" information about your package (such as the name, version, author etc.). They also inform &lt;code&gt;setuptools&lt;/code&gt; which code files to include.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://setuptools.pypa.io/en/latest/userguide/declarative_config.html" rel="noopener noreferrer"&gt;Writing setup.cfg/setup.py&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;tests/&lt;/code&gt;: This folder is a placeholder for test files.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Packaging Project
&lt;/h2&gt;

&lt;p&gt;First, make sure that the &lt;code&gt;pip&lt;/code&gt; installed on the device is of the latest version. To do that, run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install --upgrade pip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After this, install &lt;code&gt;build&lt;/code&gt; using:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install --upgrade build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, let's package the project. To package the project, run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ python -m build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This command will output lots of text and create a &lt;code&gt;build/&lt;/code&gt; folder and a &lt;code&gt;dist/&lt;/code&gt; folder. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The project has been packaged!&lt;/strong&gt; Now we have to publish it on PyPI.&lt;/p&gt;




&lt;h2&gt;
  
  
  Publishing on PyPI
&lt;/h2&gt;

&lt;p&gt;The first step to publishing on PyPI is &lt;strong&gt;creating an account there&lt;/strong&gt;. Head over to &lt;a href="https://pypi.org/" rel="noopener noreferrer"&gt;PyPI&lt;/a&gt; and click on register (the text at the top right corner,  that I highlighted in green):&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Fb6t6t6pvtw94ww092n0e.png" class="article-body-image-wrapper"&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%2Fb6t6t6pvtw94ww092n0e.png" alt="PyPI Register Button" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will get a form like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2F6z13ieluyfs9idklhy5c.png" class="article-body-image-wrapper"&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%2F6z13ieluyfs9idklhy5c.png" alt="PyPI Registration Form" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fill out the form and &lt;strong&gt;remember the username and password&lt;/strong&gt;. You will need it while publishing your project.&lt;/p&gt;

&lt;p&gt;Now head back to the terminal and run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install --upgrade twine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This command will upgrade twine to the latest version if twine is already present. If it isn't present, then it will install the latest version. Now, it is time to upload the package!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ twine upload dist/*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You will be prompted to enter your PyPI username and password for authentication purposes. After entering them, you will see a URL in the last line of the output. the URL will be of the format &lt;code&gt;https://pypi.org/project/&amp;lt;package_name&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Congratulations, the python package has been published on PyPI!!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Fi4z5w1j8fori572fhm0v.gif" class="article-body-image-wrapper"&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%2Fi4z5w1j8fori572fhm0v.gif" alt="Congrats" width="498" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's pip install the library!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pip install &amp;lt;package_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;&lt;a href="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%2Fen4ls4vpepbcofv2dd3e.gif" class="article-body-image-wrapper"&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%2Fen4ls4vpepbcofv2dd3e.gif" alt="That's all Folks!" width="499" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>pypi</category>
      <category>packaging</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>print() in python</title>
      <dc:creator>Siddhesh Agarwal</dc:creator>
      <pubDate>Sun, 16 Jan 2022 06:26:10 +0000</pubDate>
      <link>https://dev.to/siddhesh_agarwal/print-in-python-556p</link>
      <guid>https://dev.to/siddhesh_agarwal/print-in-python-556p</guid>
      <description>&lt;p&gt;In this post, I will talk about about the &lt;code&gt;print()&lt;/code&gt; statement and it's parameters.&lt;/p&gt;

&lt;p&gt;So, I was experimenting with the &lt;code&gt;help()&lt;/code&gt; function in python and tried the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;me, after seeing &lt;code&gt;file&lt;/code&gt; and &lt;code&gt;flush&lt;/code&gt; arguments in the output:&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2F0xymgfuiarnxkbpl2l3c.gif" class="article-body-image-wrapper"&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%2F0xymgfuiarnxkbpl2l3c.gif" alt="Haha, Meme based on my pain" width="290" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I knew about the &lt;code&gt;sep&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; parameters but not about the other 2 parameters. But what are these &lt;strong&gt;unpopular-parameters-that-online-tutorials-do-not-talk-about&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;Today, I am going to share what they are. But before that, let me explain what the first 2 parameters are.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;sep&lt;/code&gt; is the string that is inserted between 2 values. For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sep&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-&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="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# The default value of "sep" is a space
&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;end&lt;/code&gt; is the string that is appended after the last value. For example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="sh"&gt;'&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# The default value of "end" is a newline
&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;Hello&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;Will print:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 2 3-4 5 6
Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Now that I have explained what &lt;code&gt;sep&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; are, let's talk about the 3rd parameter. The 3rd parameter is &lt;code&gt;file&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;It is a &lt;strong&gt;file-like object&lt;/strong&gt; (stream). The default value of &lt;code&gt;file&lt;/code&gt; is &lt;code&gt;sys.stdout&lt;/code&gt; (sys is a built-in module). If you don't pass this argument, it will default to &lt;code&gt;stdout&lt;/code&gt; and the output will be printed to the &lt;strong&gt;standard output&lt;/strong&gt;. It is the terminal where you execute your code. Standard output is full for &lt;code&gt;stdout&lt;/code&gt; (Python is implemented in C that's the reason we get to see &lt;code&gt;stdout&lt;/code&gt; in Python).  If you specify a value for &lt;code&gt;file&lt;/code&gt;, the output will be &lt;strong&gt;printed to that file&lt;/strong&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sep&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;output.txt&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;w+&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;the &lt;code&gt;open("output.txt", "w+")&lt;/code&gt; will create a file called &lt;code&gt;output.txt&lt;/code&gt; (if it doesn't exist already) and write the output to it. So our &lt;code&gt;output.txt&lt;/code&gt; file will look 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;1
2
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;strong&gt;allows us to write to a file directly without having to convert to a string&lt;/strong&gt;. It also allows us to use the &lt;code&gt;sep&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; parameters of the &lt;code&gt;print()&lt;/code&gt; function without worrying about how to implement them.&lt;/p&gt;




&lt;p&gt;Finally, let's talk about the &lt;code&gt;flush&lt;/code&gt; parameter. The &lt;code&gt;flush&lt;/code&gt; parameter is a boolean value. It "flushes" the internal buffer/stream. Let's see a small example for better understanding:&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;from&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sleep&lt;/span&gt;

&lt;span class="c1"&gt;# output is not flushed here
&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;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;sleep&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="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;world!&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;would result in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, world!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output looks perfect but there is the problem: &lt;strong&gt;the 5-second pause&lt;/strong&gt; that was supposed to happen between the 2 words! It is not there. Now we run the same code but this time, we clear the output stream:&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;from&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sleep&lt;/span&gt;

&lt;span class="c1"&gt;# output is flushed this time
&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;Hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;sleep&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="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;world!&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;Now, when you run the program "Hello, " will be printed first and then after 5 seconds "world!" will be printed.&lt;/p&gt;




&lt;p&gt;That's all for this time.&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Fen4ls4vpepbcofv2dd3e.gif" class="article-body-image-wrapper"&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%2Fen4ls4vpepbcofv2dd3e.gif" alt="That's all folks!" width="499" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>id() in python</title>
      <dc:creator>Siddhesh Agarwal</dc:creator>
      <pubDate>Sat, 18 Dec 2021 04:28:59 +0000</pubDate>
      <link>https://dev.to/siddhesh_agarwal/id-in-python-27b5</link>
      <guid>https://dev.to/siddhesh_agarwal/id-in-python-27b5</guid>
      <description>&lt;p&gt;In this post, I will try to improve your idea about memory in python using the in-built &lt;code&gt;id()&lt;/code&gt; function. For those of you who don't know what &lt;code&gt;id()&lt;/code&gt; is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;id()&lt;/code&gt; function returns a unique ID of the object. All objects in python have a unique ID and no 2 different values correspond to the same ID.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So let us begin with 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="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&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;id&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="o"&gt;==&lt;/span&gt; &lt;span class="nf"&gt;id&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Fun fact:&lt;/strong&gt; In python, &lt;code&gt;id(a) == id(b)&lt;/code&gt; is analogous to &lt;code&gt;a is b&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The above code prints &lt;code&gt;True&lt;/code&gt; because python creates the variable &lt;code&gt;b&lt;/code&gt; with the value &lt;code&gt;500&lt;/code&gt; and then creates a variable &lt;code&gt;a&lt;/code&gt; pointing to the value of &lt;code&gt;b&lt;/code&gt;. This implies that &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are pointing towards the same memory location and hence the same ID.&lt;/p&gt;




&lt;p&gt;Now let us raise the bar:&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&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;id&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="o"&gt;==&lt;/span&gt; &lt;span class="nf"&gt;id&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code prints &lt;code&gt;False&lt;/code&gt; because: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python creates a variable &lt;code&gt;a&lt;/code&gt; pointing to the value &lt;code&gt;500&lt;/code&gt; in the memory. &lt;/li&gt;
&lt;li&gt;Then it creates another variable &lt;code&gt;b&lt;/code&gt; pointing to another value &lt;code&gt;500&lt;/code&gt; (yeah, both 500 are different).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hence, both have different IDs because both point towards different memory locations.&lt;/p&gt;




&lt;p&gt;I hope this isn't confusing because there is more to come. Guess the output for this:&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&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;id&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="o"&gt;==&lt;/span&gt; &lt;span class="nf"&gt;id&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some of you may think &lt;em&gt;"This is the previous question with different values. I know the answer is &lt;code&gt;False&lt;/code&gt;"&lt;/em&gt; but not so fast.&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Fjyyp8ukxbxtpvk0oaeyh.gif" class="article-body-image-wrapper"&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%2Fjyyp8ukxbxtpvk0oaeyh.gif" alt="Not so fast" width="498" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For small integers (The CPython range is -5 to 256, both inclusive), then integer objects (&lt;code&gt;&amp;lt;class 'int'&amp;gt;&lt;/code&gt;) are shared. This is done entirely to save space. The memory imprint of the console would be significantly larger if these objects weren’t sharing their memory.&lt;/p&gt;

&lt;p&gt;So the correct answer is &lt;code&gt;True&lt;/code&gt;&lt;/p&gt;




&lt;p&gt;Okay, okay. Just one more to go. The last one:&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;
&lt;span class="n"&gt;id1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;id&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;
&lt;span class="n"&gt;id2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;id&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;id2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, even though I am re-declaring the same variable with the same value, the answer is &lt;strong&gt;most likely&lt;/strong&gt; to be &lt;code&gt;False&lt;/code&gt;. I'll explain to you why. When you re-declare a variable in python, the interpreter works in the same way as a declaration. i.e. It entirely deletes the before existing value and creates a variable with the new value. So when we give &lt;code&gt;a = 500&lt;/code&gt;, the second time the interpreter deletes the previously existing value of &lt;code&gt;a&lt;/code&gt; and create a new memory location for &lt;code&gt;500&lt;/code&gt; where &lt;code&gt;a&lt;/code&gt;  would point towards. Both these IDs are most likely to be different.&lt;/p&gt;

&lt;p&gt;SO, the answer is &lt;code&gt;False&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; If the above example had a number belonging to the inclusive range -5 to 256, the answer would have been &lt;code&gt;True&lt;/code&gt;. this is because number belonging to the inclusive range have a fixed memory location.&lt;/p&gt;

</description>
      <category>python</category>
      <category>memorymanagement</category>
    </item>
  </channel>
</rss>
