<?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: Vidit Sarkar</title>
    <description>The latest articles on DEV Community by Vidit Sarkar (@vidit1999).</description>
    <link>https://dev.to/vidit1999</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%2F333458%2F92c40a56-f038-4a29-bcc9-71150a1157b6.jpeg</url>
      <title>DEV Community: Vidit Sarkar</title>
      <link>https://dev.to/vidit1999</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vidit1999"/>
    <language>en</language>
    <item>
      <title>Python Basics (Working with Interpreter and writing code in file)</title>
      <dc:creator>Vidit Sarkar</dc:creator>
      <pubDate>Mon, 16 Mar 2020 13:15:59 +0000</pubDate>
      <link>https://dev.to/vidit1999/python-basics-working-with-interpreter-and-writing-code-in-file-40b</link>
      <guid>https://dev.to/vidit1999/python-basics-working-with-interpreter-and-writing-code-in-file-40b</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In my &lt;a href="https://dev.to/vidit1999/python-basics-installation-1pl8"&gt;previous post&lt;/a&gt; I have discussed about how to install Python in your system. In this post I will go through how to work with Python Interpreter and how to write python code in a file and run it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Working with Python interpreter&lt;/li&gt;
&lt;li&gt;Writing code in a file and run it&lt;/li&gt;
&lt;li&gt;What code editor should I use?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Working with Python interpreter
&lt;/h2&gt;

&lt;p&gt;In your terminal type &lt;code&gt;python&lt;/code&gt; and hit enter. You will see something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="se"&gt;\&amp;gt;&lt;/span&gt; python
Python 3.7.4 &lt;span class="o"&gt;(&lt;/span&gt;default, Aug  9 2019, 18:34:13&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;MSC v.1915 64 bit &lt;span class="o"&gt;(&lt;/span&gt;AMD64&lt;span class="o"&gt;)]&lt;/span&gt; :: Anaconda, Inc. on win32
Type &lt;span class="s2"&gt;"help"&lt;/span&gt;, &lt;span class="s2"&gt;"copyright"&lt;/span&gt;, &lt;span class="s2"&gt;"credits"&lt;/span&gt; or &lt;span class="s2"&gt;"license"&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;more information.
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; sign is a indication that you are in python shell. Now type &lt;code&gt;3+2&lt;/code&gt; and hit enter. Output will be &lt;code&gt;5&lt;/code&gt;,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; 3+2
5
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also use &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt; or &lt;code&gt;/&lt;/code&gt; for subtraction, multiplication and division respectively.&lt;br&gt;&lt;br&gt;
Let us write a simple for-loop (I will discuss more about for-loop in a later post).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;i &lt;span class="k"&gt;in &lt;/span&gt;range&lt;span class="o"&gt;(&lt;/span&gt;4&lt;span class="o"&gt;)&lt;/span&gt;:
...     print&lt;span class="o"&gt;(&lt;/span&gt;i&lt;span class="o"&gt;)&lt;/span&gt;
...
0
1
2
3
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you are typing something which will take multiple lines then those &lt;code&gt;...&lt;/code&gt; signs will going to show up. They are indication of continuation lines. Notice there is a colon after &lt;code&gt;range&lt;/code&gt; statement and 4 spaces before &lt;code&gt;print&lt;/code&gt; statement. That &lt;code&gt;:&lt;/code&gt; sign is necessary (you can refer to &lt;a href="https://softwareengineering.stackexchange.com/questions/341035/is-colon-in-python-blocks-technically-necesary"&gt;here&lt;/a&gt; for more info). You can use any number of spaces, but they have to be greater than zero. It is a good practice to use 4 spaces, so I am going to use 4 spaces.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Writing code in a file and run it
&lt;/h2&gt;

&lt;p&gt;Interpreter is good for small code and for testing purposes. But if your code gets bigger, then you should consider using a file. One main benefit is that you can save and use it for later purposes, but the code in interpreter can not be saved using easy methods (At least I think so).Here are the steps,  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a file with &lt;code&gt;.py&lt;/code&gt; extension and avoid using spaces in the name. Example -  &lt;code&gt;app.py&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Copy and paste the code of for-loop in that file and save it. Same rule for spaces are also applicable here and also don't forget to put the &lt;code&gt;:&lt;/code&gt; sign.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&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="k"&gt;print&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In the terminal type &lt;code&gt;python app.py&lt;/code&gt; and you will see output like this.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="se"&gt;\&amp;gt;&lt;/span&gt; python app.py
0
1
2
3

&lt;span class="se"&gt;\&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;By default, Python source files are treated as encoded in UTF-8. To declare an encoding other than the default one, a special comment line should be added as the first line of the file. The syntax is as follows,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# -*- coding: encoding -*-
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For example, to declare that Windows-1252 encoding is to be used, the first line of your source code file should be,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# -*- coding: cp1252 -*-
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can check more about valid encodings &lt;a href="https://docs.python.org/3/library/codecs.html#standard-encodings"&gt;here&lt;/a&gt;. Note that &lt;code&gt;#&lt;/code&gt; sign indicates a comment line in python.&lt;br&gt;
One exception to the first line rule is when the source code starts with a &lt;code&gt;UNIX “shebang”&lt;/code&gt; line (not for Windows users).  In that case, the encoding declaration should be added as the second line of the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env python3
# -*- coding: ascii -*-
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;On BSD’ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env python3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;at the very beginning of the file (assuming that the interpreter is on the user’s PATH). &lt;code&gt;#!&lt;/code&gt; must be the first two characters of the file.&lt;br&gt;&lt;br&gt;
Give the script executable mode, or permission, using &lt;code&gt;chmod&lt;/code&gt; and run it like a script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;chmod&lt;/span&gt; +x app.py
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can check more about this &lt;a href="https://docs.python.org/3/tutorial/appendix.html#tut-scripts"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What code editor should I use?
&lt;/h2&gt;

&lt;p&gt;There are lots of popular code editors available out there like &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt;, &lt;a href="https://atom.io/"&gt;Atom&lt;/a&gt;, &lt;a href="https://notepad-plus-plus.org/"&gt;Notepad++&lt;/a&gt; and more. You can use any editor you prefer. You can even use IDEs. As long as you can write code and run it, any code editor or IDE should be fine. I personally use &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt; or &lt;a href="https://notepad-plus-plus.org/"&gt;Notepad++&lt;/a&gt; to write code and run python files using command prompt (or integrated terminal of VS Code).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Happy coding!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>python</category>
    </item>
    <item>
      <title>Python Basics (Installation)</title>
      <dc:creator>Vidit Sarkar</dc:creator>
      <pubDate>Mon, 16 Mar 2020 10:16:21 +0000</pubDate>
      <link>https://dev.to/vidit1999/python-basics-installation-1pl8</link>
      <guid>https://dev.to/vidit1999/python-basics-installation-1pl8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Python is an interpreted, high-level, general-purpose programming language. It has variety of use cases. You can use it for automating certain tasks and do a lot more with it. Here we will see how to install python in your computer. By the way, we are going to use Python 3 not Python 2, so wherever I mention python that will mean Python 3, if not explicitly mentioned something else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;How to install Python&lt;/li&gt;
&lt;li&gt;Other options&lt;/li&gt;
&lt;li&gt;What I use&lt;/li&gt;
&lt;li&gt;Hello World program&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to install Python
&lt;/h2&gt;

&lt;h4&gt;
  
  
  For Windows users
&lt;/h4&gt;

&lt;p&gt;Check whether you have python in your system or not, by typing &lt;code&gt;python&lt;/code&gt; in terminal or command prompt. If you see something like this, then you don't have python installed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; python
&lt;span class="s1"&gt;'python'&lt;/span&gt; is not recognized as an internal or external &lt;span class="nb"&gt;command&lt;/span&gt;, operable program or batch file.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Head over to &lt;a href="https://www.python.org/"&gt;Python Official Website&lt;/a&gt; and go to the &lt;a href="https://www.python.org/downloads/"&gt;downloads section&lt;/a&gt;. Choose the latest stable python 3 version, download the executable file and install it. Do not forget to &lt;strong&gt;add python to your path&lt;/strong&gt;, so that you can use python from terminal.&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eTIpPhO5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/m27xpr7swqiubpvpcoci.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eTIpPhO5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/m27xpr7swqiubpvpcoci.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Make sure that the box, indicated using red arrow sign, is checked.&lt;/p&gt;
&lt;h4&gt;
  
  
  For Linux users
&lt;/h4&gt;

&lt;p&gt;You should already have python in your system. You can verify it by typing &lt;code&gt;python3&lt;/code&gt; in terminal and then hitting enter. You should see something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;python3
Python 3.6.9 &lt;span class="o"&gt;(&lt;/span&gt;default, Nov  7 2019, 10:44:02&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;GCC 8.3.0] on linux
Type &lt;span class="s2"&gt;"help"&lt;/span&gt;, &lt;span class="s2"&gt;"copyright"&lt;/span&gt;, &lt;span class="s2"&gt;"credits"&lt;/span&gt; or &lt;span class="s2"&gt;"license"&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;more information.
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  For Mac users
&lt;/h4&gt;

&lt;p&gt;You should have python 2 in your system. To install python 3, you may need to use &lt;a href="https://brew.sh/"&gt;Homebrew&lt;/a&gt;. You can check out &lt;a href="https://docs.python.org/3/using/mac.html"&gt;Python official site&lt;/a&gt; for more information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other options
&lt;/h2&gt;

&lt;p&gt;Other popular options include &lt;a href="https://www.anaconda.com/"&gt;Anaconda&lt;/a&gt;, &lt;a href="https://www.activestate.com/products/python/"&gt;ActiveState Python&lt;/a&gt; and more.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I use
&lt;/h2&gt;

&lt;p&gt;I personally use &lt;a href="https://www.anaconda.com/distribution/"&gt;Anaconda Python&lt;/a&gt;. It comes with its own package manager, called &lt;code&gt;conda&lt;/code&gt; and it has has lots of packages pre-installed, so you don't have to install them separately. Head over to their &lt;a href="https://www.anaconda.com/distribution/"&gt;download page&lt;/a&gt; and download the version suitable for you system.&lt;br&gt;&lt;br&gt;
Check out their official installation guides.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.anaconda.com/anaconda/install/windows/"&gt;Installing on Windows&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.anaconda.com/anaconda/install/mac-os/"&gt;Installing on macOS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.anaconda.com/anaconda/install/linux/"&gt;Installing on Linux&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  For Windows users
&lt;/h4&gt;

&lt;p&gt;You may need to add Anaconda to your path to use it from terminal. To do that, make sure the box, indicated using red arrow sign, is checked.&lt;br&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VHd_KOEj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4ghfu8bpduu4z9a0ot11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VHd_KOEj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4ghfu8bpduu4z9a0ot11.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the installation is successful, open a new terminal or command prompt and type &lt;code&gt;python&lt;/code&gt;. You should see something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; python
Python 3.7.4 &lt;span class="o"&gt;(&lt;/span&gt;default, Aug  9 2019, 18:34:13&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;MSC v.1915 64 bit &lt;span class="o"&gt;(&lt;/span&gt;AMD64&lt;span class="o"&gt;)]&lt;/span&gt; :: Anaconda, Inc. on win32
Type &lt;span class="s2"&gt;"help"&lt;/span&gt;, &lt;span class="s2"&gt;"copyright"&lt;/span&gt;, &lt;span class="s2"&gt;"credits"&lt;/span&gt; or &lt;span class="s2"&gt;"license"&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;more information.
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Hello World program
&lt;/h2&gt;

&lt;p&gt;In the python shell after the &lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; sign type &lt;code&gt;print("Hello World")&lt;/code&gt; and hit enter. You will see output like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; python
Python 3.7.4 &lt;span class="o"&gt;(&lt;/span&gt;default, Aug  9 2019, 18:34:13&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;MSC v.1915 64 bit &lt;span class="o"&gt;(&lt;/span&gt;AMD64&lt;span class="o"&gt;)]&lt;/span&gt; :: Anaconda, Inc. on win32
Type &lt;span class="s2"&gt;"help"&lt;/span&gt;, &lt;span class="s2"&gt;"copyright"&lt;/span&gt;, &lt;span class="s2"&gt;"credits"&lt;/span&gt; or &lt;span class="s2"&gt;"license"&lt;/span&gt; &lt;span class="k"&gt;for &lt;/span&gt;more information.
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; print&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello World"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
Hello World
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is your Hello world program in python. Congratulations!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is my first post!&lt;/em&gt;&lt;/p&gt;

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