<commit_before><!DOCTYPE html>
<html lang="en-US">
    <head>
        <meta charset="utf-8"> 
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="author" content="<NAME>" />
        <meta name="copyright" content="Amit Kushwaha" />

        <meta name="twitter:creator" content="@imYardstick17">
        <meta property="og:type" content="article" />
        <meta name="twitter:card" content="summary">

<meta name="keywords" content=", Data Analytics with Python, " />

<meta property="og:title" content="Basic Data-Types in Python "/>
<meta property="og:url" content="https://amitkushwaha.co.in/basic-data-types-in-python.html" />
<meta property="og:description" content="Every variable has a type in Python. As everything in Python is an object. Datatypes are the classes, and the variables are instances of these classes. type is used to determine the class an object belongs to and isinstance is used to check if an object belongs to a particular …" />
<meta property="og:site_name" content="Connecting Dots" />
<meta property="og:article:author" content="<NAME>" />
<meta property="og:article:published_time" content="2019-01-22T00:00:00+05:30" />
<meta name="twitter:title" content="Basic Data-Types in Python ">
<meta name="twitter:description" content="Every variable has a type in Python. As everything in Python is an object. Datatypes are the classes, and the variables are instances of these classes. type is used to determine the class an object belongs to and isinstance is used to check if an object belongs to a particular …">

        <title>Basic Data-Types in Python  · Connecting Dots
</title>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
        <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.1/css/font-awesome.css" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="https://amitkushwaha.co.in/theme/css/pygments.css" media="screen">
        <link rel="stylesheet" type="text/css" href="https://amitkushwaha.co.in/theme/tipuesearch/tipuesearch.css" media="screen">
        <link rel="stylesheet" type="text/css" href="https://amitkushwaha.co.in/theme/css/elegant.css" media="screen">
        <link rel="stylesheet" type="text/css" href="https://amitkushwaha.co.in/theme/css/custom.css" media="screen">
<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
     (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
     m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
     })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-98482392-1', 'auto');
    ga('send', 'pageview');
</script>
    </head>
    <body>
        <div id="content-sans-footer">
        <div class="navbar navbar-static-top">
            <div class="navbar-inner">
                <div class="container-fluid">
                    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </a>
                    <a class="brand" href="https://amitkushwaha.co.in/"><span class=site-name>Connecting Dots</span></a>
                    <div class="nav-collapse collapse">
                        <ul class="nav pull-right top-menu">
                            <li ><a href="https://amitkushwaha.co.in">Home</a></li>
                            <li ><a href="https://amitkushwaha.co.in/pages/about-me.html">About me</a></li>
                            <li ><a href="https://amitkushwaha.co.in/categories.html">Categories</a></li>
                            <li ><a href="https://amitkushwaha.co.in/tags.html">Tags</a></li>
                            <li ><a href="https://amitkushwaha.co.in/archives.html">Archives</a></li>
                            <li><form class="navbar-search" action="https://amitkushwaha.co.in/search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span1"></div>
                <div class="span10">
<article>
<div class="row-fluid">
    <header class="page-header span10 offset2">
    <h1><a href="https://amitkushwaha.co.in/basic-data-types-in-python.html"> Basic Data-Types in Python  </a></h1>
    </header>
</div>

<div class="row-fluid">
        <div class="span8 offset2 article-content">

            
            <p>Every variable has a type in Python. As everything in Python is an object. Datatypes are the classes, and the variables
are instances of these classes.</p>
<p><code>type</code> is used to determine the class an object belongs to and
<code>isinstance</code> is used to check if an object belongs to a particular class.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">4</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="o">&lt;</span><span class="nb">type</span> <span class="s1">&#39;int&#39;</span><span class="o">&gt;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>
<span class="bp">False</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
<span class="bp">True</span>
</pre></div>


<p>Among many various data types in Python, these are the important types are listed below.</p>
<ul>
<li>integer, floats: 3, 4.5</li>
<li>string: 'hello world'</li>
<li>list: [4, 're', 1.4]</li>
<li>tuple: (4, 5)</li>
<li>set: {2, 4}</li>
<li>dictionary: {1: 'one', 2: 'two'}</li>
</ul>
<h3><strong>INTEGERS, FLOATS</strong></h3>
<p>These are for storinng numerical data. Numerical data can be of three types: Integer, Float and Complex. Python offers
all these three. But let's focus on Integer and Float only.</p>
<p>Python interprets the decimal digits without any prefix. For example: </p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">4</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="mi">4</span>
</pre></div>


<p>For <strong>binary numbers</strong> append <code>0b</code> or <code>0B</code> before the number representation.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mb">0b111</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="mi">7</span>
</pre></div>


<p>For <strong>octal numbers</strong> append <code>0o</code> or <code>0O</code> before the number representation.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="n">o101</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="mi">65</span>
</pre></div>


<p>For <strong>hexadecimal numbers</strong> append <code>0x</code> or <code>0X</code> before the number representation.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mh">0x101</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="mi">257</span>
</pre></div>


<p>Floating points are accurate upto 15 decimal places. In representation, numbers are separated by a dot(.).</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mf">1.12345678910111213</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="mf">1.1234567891011122</span>
</pre></div>


<h3><strong>STRINGS</strong></h3>
<p>Strings are the sequence of unicode characters. These sequence must be enclosed by single quotes, double quotes or triple quotes.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;hello world&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="s1">&#39;hello world&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="s2">&quot;hello world&quot;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="s1">&#39;hello world&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;&#39;&#39;hello world&#39;&#39;&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="s1">&#39;hello world&#39;</span>
</pre></div>


<p>Slicing operations are allowed only to read values from strings like:</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;&#39;&#39;hello world&#39;&#39;&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">6</span><span class="p">:</span><span class="mi">12</span><span class="p">]</span>
<span class="s1">&#39;world&#39;</span>
</pre></div>


<p>Value assignment at any index is not possible in string.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;&#39;&#39;hello world&#39;&#39;&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;W&#39;</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  <span class="n">File</span> <span class="s2">&quot;&lt;stdin&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="ne">TypeError</span><span class="p">:</span> <span class="s1">&#39;str&#39;</span> <span class="nb">object</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">support</span> <span class="n">item</span> <span class="n">assignment</span>
</pre></div>


<p>This means once a string objects is created, it cannot be changed which makes them <code>immutable</code> data type.</p>
<h3><strong>LIST</strong></h3>
<p>A list is a ordered sequence of items. The items can be of any data types.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">]</span>
</pre></div>


<p>Slicing operations and Value assignment are allowed in list.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;world&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;world&#39;</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">]</span>
</pre></div>


<h3><strong>TUPLE</strong></h3>
<p>A tuple is a sequence of items like list but with a difference that once created, it cannot be
changed later. It means value assignment is not allowed, just like strings. It is defined by putting
parenthesis around comma separated items.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;world&#39;</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  <span class="n">File</span> <span class="s2">&quot;&lt;stdin&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="ne">TypeError</span><span class="p">:</span> <span class="s1">&#39;tuple&#39;</span> <span class="nb">object</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">support</span> <span class="n">item</span> <span class="n">assignment</span>
</pre></div>


<p>Tuple is also an <code>immutable</code> data-type.</p>
<h3><strong>SET</strong></h3>
<p>A set is an unordered collection of unique items.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">}</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="nb">set</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
</pre></div>


<p>As sets are unordered, index access is not allowed on them.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">a</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">}</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  <span class="n">File</span> <span class="s2">&quot;&lt;string&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">301</span><span class="p">,</span> <span class="ow">in</span> <span class="n">runcode</span>
  <span class="n">File</span> <span class="s2">&quot;&lt;interactive input&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="ne">TypeError</span><span class="p">:</span> <span class="s1">&#39;set&#39;</span> <span class="nb">object</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">support</span> <span class="n">indexing</span>
</pre></div>


<h3><strong>DICTIONARY</strong></h3>
<p>A dictionary is an collection of key-values pairs. They are meant to be used for accessing random key data.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span> <span class="p">:</span> <span class="s1">&#39;one&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span> <span class="s1">&#39;two&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">:</span> <span class="s1">&#39;three&#39;</span><span class="p">}</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="s1">&#39;one&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="s1">&#39;three&#39;</span>
</pre></div>


<p>One important thing to remember about dictionary is that only <code>immutable</code> data-types can be keys.</p>
<p>This pretty much covers the basic types of data-types in python. There are more specialized
data types like dates and times, queues, fixed-type arrays etc. Just like the other data types
they also have some special property which comes very handy while working with real data. We will be soon
exploring them.</p>
<p>Goodbye until then.</p>
            
            <section>
<div class="accordion" id="accordion2">
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle disqus-comment-count" data-toggle="collapse" data-parent="#accordion2"
                href="https://amitkushwaha.co.in/basic-data-types-in-python.html#disqus_thread">
                Comments
            </a>
        </div>
        <div id="disqus_thread" class="accordion-body collapse">
            <div class="accordion-inner">
                <div class="comments">
                    <div id="disqus_thread"></div>
<script type="text/javascript">
    var disqus_shortname = 'amitkushwaha';
        var disqus_identifier = 'https://amitkushwaha.co.in/basic-data-types-in-python.html';
    var disqus_url = 'https://amitkushwaha.co.in/basic-data-types-in-python.html';

    (function() {
         var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
         dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
         (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
     })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

                </div>
            </div>
        </div>
    </div>
</div>
</section>

            <hr/>
        </div>
        <section>
        <div class="span2" style="float:right;font-size:0.9em;">
            <h4>Published</h4>
            <time pubdate="pubdate" datetime="2019-01-22T00:00:00+05:30">Jan 22, 2019</time>
            <h4>Category</h4>
            <a class="category-link" href="https://amitkushwaha.co.in/categories.html#data-analytics-with-python-ref">Data Analytics with Python</a>
<h4>Contact</h4>
    <a href="https://www.linkedin.com/in/yardstick17" title="My linkedin Profile" class="sidebar-social-links" target="_blank">
    <i class="fa fa-linkedin sidebar-social-links"></i></a>
    <a href="https://github.com/yardstick17" title="My github Profile" class="sidebar-social-links" target="_blank">
    <i class="fa fa-github sidebar-social-links"></i></a>
    <a href="https://twitter.com/imYardstick17" title="My twitter Profile" class="sidebar-social-links" target="_blank">
    <i class="fa fa-twitter sidebar-social-links"></i></a>
        </div>
        </section>
</div>
</article>
                </div>
                <div class="span1"></div>
            </div>
        </div>
        <div id="push"></div>
    </div>
<footer>
<div id="footer">
    <ul class="footer-content">
        <li class="elegant-power">Powered by <a href="http://getpelican.com/" title="Pelican Home Page">Pelican</a>. Theme: <a href="http://oncrashreboot.com/pelican-elegant" title="Theme Elegant Home Page">Elegant</a> by <a href="http://oncrashreboot.com" title="<NAME>ansoor Home Page"><NAME></a></li>
    </ul>
</div>
</footer>            <script src="http://code.jquery.com/jquery.min.js"></script>
        <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
        <script>
            function validateForm(query)
            {
                return (query.length > 0);
            }
        </script>

            <script type="text/javascript">
var disqus_shortname = 'amitkushwaha';
(function () {
    var s = document.createElement('script'); s.async = true;
    s.type = 'text/javascript';
    s.src = '//' + disqus_shortname + '.disqus.com/count.js';
    (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<script  language="javascript" type="text/javascript">
function uncollapse() {
    if (window.location.hash.match(/^#comment-\d+$/)) {
        $('#disqus_thread').collapse('show');
    }
}
</script>
<script type="text/javascript" language="JavaScript">
uncollapse();
window.onhashchange=function(){
    if (window.location.hash.match(/^#comment-\d+$/))
        window.location.reload(true);
}
</script>
<script>
$('#disqus_thread').on('shown', function () {
    var link = document.getElementsByClassName('accordion-toggle');
    var old_innerHTML = link[0].innerHTML;
    $(link[0]).fadeOut(500, function() {
        $(this).text('Click here to hide comments').fadeIn(500);
    });
    $('#disqus_thread').on('hidden', function () {
        $(link[0]).fadeOut(500, function() {
            $(this).text(old_innerHTML).fadeIn(500);
        });
    })
})
</script>


    </body>
    <!-- Theme: Elegant built for Pelican
    License : http://oncrashreboot.com/pelican-elegant -->
</html><commit_msg>Case fix<commit_after><!DOCTYPE html>
<html lang="en-US">
    <head>
        <meta charset="utf-8"> 
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="author" content="<NAME>" />
        <meta name="copyright" content="Amit Kushwaha" />

        <meta name="twitter:creator" content="@imYardstick17">
        <meta property="og:type" content="article" />
        <meta name="twitter:card" content="summary">

<meta name="keywords" content=", Data Analytics with Python, " />

<meta property="og:title" content="Basic Data-Types in Python "/>
<meta property="og:url" content="https://amitkushwaha.co.in/basic-data-types-in-python.html" />
<meta property="og:description" content="Every variable has a type in Python. As everything in Python is an object. Datatypes are the classes, and the variables are instances of these classes. type is used to determine the class an object belongs to and isinstance is used to check if an object belongs to a particular …" />
<meta property="og:site_name" content="Connecting Dots" />
<meta property="og:article:author" content="<NAME>" />
<meta property="og:article:published_time" content="2019-01-22T00:00:00+05:30" />
<meta name="twitter:title" content="Basic Data-Types in Python ">
<meta name="twitter:description" content="Every variable has a type in Python. As everything in Python is an object. Datatypes are the classes, and the variables are instances of these classes. type is used to determine the class an object belongs to and isinstance is used to check if an object belongs to a particular …">

        <title>Basic Data-Types in Python  · Connecting Dots
</title>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
        <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.1/css/font-awesome.css" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="https://amitkushwaha.co.in/theme/css/pygments.css" media="screen">
        <link rel="stylesheet" type="text/css" href="https://amitkushwaha.co.in/theme/tipuesearch/tipuesearch.css" media="screen">
        <link rel="stylesheet" type="text/css" href="https://amitkushwaha.co.in/theme/css/elegant.css" media="screen">
        <link rel="stylesheet" type="text/css" href="https://amitkushwaha.co.in/theme/css/custom.css" media="screen">
<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
     (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
     m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
     })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-98482392-1', 'auto');
    ga('send', 'pageview');
</script>
    </head>
    <body>
        <div id="content-sans-footer">
        <div class="navbar navbar-static-top">
            <div class="navbar-inner">
                <div class="container-fluid">
                    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </a>
                    <a class="brand" href="https://amitkushwaha.co.in/"><span class=site-name>Connecting Dots</span></a>
                    <div class="nav-collapse collapse">
                        <ul class="nav pull-right top-menu">
                            <li ><a href="https://amitkushwaha.co.in">Home</a></li>
                            <li ><a href="https://amitkushwaha.co.in/pages/about-me.html">About me</a></li>
                            <li ><a href="https://amitkushwaha.co.in/categories.html">Categories</a></li>
                            <li ><a href="https://amitkushwaha.co.in/tags.html">Tags</a></li>
                            <li ><a href="https://amitkushwaha.co.in/archives.html">Archives</a></li>
                            <li><form class="navbar-search" action="https://amitkushwaha.co.in/search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="container-fluid">
            <div class="row-fluid">
                <div class="span1"></div>
                <div class="span10">
<article>
<div class="row-fluid">
    <header class="page-header span10 offset2">
    <h1><a href="https://amitkushwaha.co.in/basic-data-types-in-python.html"> Basic Data-Types in Python  </a></h1>
    </header>
</div>

<div class="row-fluid">
        <div class="span8 offset2 article-content">

            
            <p>Every variable has a type in Python. As everything in Python is an object. Datatypes are the classes, and the variables
are instances of these classes.</p>
<p><code>type</code> is used to determine the class an object belongs to and
<code>isinstance</code> is used to check if an object belongs to a particular class.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">4</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="o">&lt;</span><span class="nb">type</span> <span class="s1">&#39;int&#39;</span><span class="o">&gt;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>
<span class="bp">False</span>
<span class="o">&gt;&gt;&gt;</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="nb">int</span><span class="p">)</span>
<span class="bp">True</span>
</pre></div>


<p>Among many various data types in Python, these are the important types are listed below.</p>
<ul>
<li>integer, floats: 3, 4.5</li>
<li>string: 'hello world'</li>
<li>list: [4, 're', 1.4]</li>
<li>tuple: (4, 5)</li>
<li>set: {2, 4}</li>
<li>dictionary: {1: 'one', 2: 'two'}</li>
</ul>
<h3><strong>Integers, Floats</strong></h3>
<p>These are for storinng numerical data. Numerical data can be of three types: Integer, Float and Complex. Python offers
all these three. But let's focus on Integer and Float only.</p>
<p>Python interprets the decimal digits without any prefix. For example: </p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">4</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="mi">4</span>
</pre></div>


<p>For <strong>binary numbers</strong> append <code>0b</code> or <code>0B</code> before the number representation.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mb">0b111</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="mi">7</span>
</pre></div>


<p>For <strong>octal numbers</strong> append <code>0o</code> or <code>0O</code> before the number representation.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="n">o101</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="mi">65</span>
</pre></div>


<p>For <strong>hexadecimal numbers</strong> append <code>0x</code> or <code>0X</code> before the number representation.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mh">0x101</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="mi">257</span>
</pre></div>


<p>Floating points are accurate upto 15 decimal places. In representation, numbers are separated by a dot(.).</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="mf">1.12345678910111213</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="mf">1.1234567891011122</span>
</pre></div>


<h3><strong>Strings</strong></h3>
<p>Strings are the sequence of unicode characters. These sequence must be enclosed by single quotes, double quotes or triple quotes.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;hello world&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="s1">&#39;hello world&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="s2">&quot;hello world&quot;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="s1">&#39;hello world&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;&#39;&#39;hello world&#39;&#39;&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="s1">&#39;hello world&#39;</span>
</pre></div>


<p>Slicing operations are allowed only to read values from strings like:</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;&#39;&#39;hello world&#39;&#39;&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">6</span><span class="p">:</span><span class="mi">12</span><span class="p">]</span>
<span class="s1">&#39;world&#39;</span>
</pre></div>


<p>Value assignment at any index is not possible in string.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;&#39;&#39;hello world&#39;&#39;&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;W&#39;</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  <span class="n">File</span> <span class="s2">&quot;&lt;stdin&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="ne">TypeError</span><span class="p">:</span> <span class="s1">&#39;str&#39;</span> <span class="nb">object</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">support</span> <span class="n">item</span> <span class="n">assignment</span>
</pre></div>


<p>This means once a string objects is created, it cannot be changed which makes them <code>immutable</code> data type.</p>
<h3><strong>List</strong></h3>
<p>A list is a ordered sequence of items. The items can be of any data types.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">]</span>
</pre></div>


<p>Slicing operations and Value assignment are allowed in list.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">]</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;world&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;world&#39;</span><span class="p">,</span> <span class="mf">1.2</span><span class="p">]</span>
</pre></div>


<h3><strong>Tuple</strong></h3>
<p>A tuple is a sequence of items like list but with a difference that once created, it cannot be
changed later. It means value assignment is not allowed, just like strings. It is defined by putting
parenthesis around comma separated items.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;hello world&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;world&#39;</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  <span class="n">File</span> <span class="s2">&quot;&lt;stdin&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="ne">TypeError</span><span class="p">:</span> <span class="s1">&#39;tuple&#39;</span> <span class="nb">object</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">support</span> <span class="n">item</span> <span class="n">assignment</span>
</pre></div>


<p>Tuple is also an <code>immutable</code> data-type.</p>
<h3><strong>Set</strong></h3>
<p>A set is an unordered collection of unique items.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">}</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span>
<span class="nb">set</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">])</span>
</pre></div>


<p>As sets are unordered, index access is not allowed on them.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">a</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">}</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
  <span class="n">File</span> <span class="s2">&quot;&lt;string&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">301</span><span class="p">,</span> <span class="ow">in</span> <span class="n">runcode</span>
  <span class="n">File</span> <span class="s2">&quot;&lt;interactive input&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span>
<span class="ne">TypeError</span><span class="p">:</span> <span class="s1">&#39;set&#39;</span> <span class="nb">object</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">support</span> <span class="n">indexing</span>
</pre></div>


<h3><strong>Dictionary</strong></h3>
<p>A dictionary is an collection of key-values pairs. They are meant to be used for accessing random key data.</p>
<div class="highlight"><pre><span></span><span class="o">&gt;&gt;&gt;</span> <span class="n">x</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span> <span class="p">:</span> <span class="s1">&#39;one&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span> <span class="s1">&#39;two&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">:</span> <span class="s1">&#39;three&#39;</span><span class="p">}</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="s1">&#39;one&#39;</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">x</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="s1">&#39;three&#39;</span>
</pre></div>


<p>One important thing to remember about dictionary is that only <code>immutable</code> data-types can be keys.</p>
<p>This pretty much covers the basic types of data-types in python. There are more specialized
data types like dates and times, queues, fixed-type arrays etc. Just like the other data types
they also have some special property which comes very handy while working with real data. We will be soon
exploring them.</p>
<p>Goodbye until then.</p>
            
            <section>
<div class="accordion" id="accordion2">
    <div class="accordion-group">
        <div class="accordion-heading">
            <a class="accordion-toggle disqus-comment-count" data-toggle="collapse" data-parent="#accordion2"
                href="https://amitkushwaha.co.in/basic-data-types-in-python.html#disqus_thread">
                Comments
            </a>
        </div>
        <div id="disqus_thread" class="accordion-body collapse">
            <div class="accordion-inner">
                <div class="comments">
                    <div id="disqus_thread"></div>
<script type="text/javascript">
    var disqus_shortname = 'amitkushwaha';
        var disqus_identifier = 'https://amitkushwaha.co.in/basic-data-types-in-python.html';
    var disqus_url = 'https://amitkushwaha.co.in/basic-data-types-in-python.html';

    (function() {
         var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
         dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
         (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
     })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

                </div>
            </div>
        </div>
    </div>
</div>
</section>

            <hr/>
        </div>
        <section>
        <div class="span2" style="float:right;font-size:0.9em;">
            <h4>Published</h4>
            <time pubdate="pubdate" datetime="2019-01-22T00:00:00+05:30">Jan 22, 2019</time>
            <h4>Category</h4>
            <a class="category-link" href="https://amitkushwaha.co.in/categories.html#data-analytics-with-python-ref">Data Analytics with Python</a>
<h4>Contact</h4>
    <a href="https://www.linkedin.com/in/yardstick17" title="My linkedin Profile" class="sidebar-social-links" target="_blank">
    <i class="fa fa-linkedin sidebar-social-links"></i></a>
    <a href="https://github.com/yardstick17" title="My github Profile" class="sidebar-social-links" target="_blank">
    <i class="fa fa-github sidebar-social-links"></i></a>
    <a href="https://twitter.com/imYardstick17" title="My twitter Profile" class="sidebar-social-links" target="_blank">
    <i class="fa fa-twitter sidebar-social-links"></i></a>
        </div>
        </section>
</div>
</article>
                </div>
                <div class="span1"></div>
            </div>
        </div>
        <div id="push"></div>
    </div>
<footer>
<div id="footer">
    <ul class="footer-content">
        <li class="elegant-power">Powered by <a href="http://getpelican.com/" title="Pelican Home Page">Pelican</a>. Theme: <a href="http://oncrashreboot.com/pelican-elegant" title="Theme Elegant Home Page">Elegant</a> by <a href="http://oncrashreboot.com" title="Talha Mansoor Home Page">Tal<NAME></a></li>
    </ul>
</div>
</footer>            <script src="http://code.jquery.com/jquery.min.js"></script>
        <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
        <script>
            function validateForm(query)
            {
                return (query.length > 0);
            }
        </script>

            <script type="text/javascript">
var disqus_shortname = 'amitkushwaha';
(function () {
    var s = document.createElement('script'); s.async = true;
    s.type = 'text/javascript';
    s.src = '//' + disqus_shortname + '.disqus.com/count.js';
    (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());
</script>
<script  language="javascript" type="text/javascript">
function uncollapse() {
    if (window.location.hash.match(/^#comment-\d+$/)) {
        $('#disqus_thread').collapse('show');
    }
}
</script>
<script type="text/javascript" language="JavaScript">
uncollapse();
window.onhashchange=function(){
    if (window.location.hash.match(/^#comment-\d+$/))
        window.location.reload(true);
}
</script>
<script>
$('#disqus_thread').on('shown', function () {
    var link = document.getElementsByClassName('accordion-toggle');
    var old_innerHTML = link[0].innerHTML;
    $(link[0]).fadeOut(500, function() {
        $(this).text('Click here to hide comments').fadeIn(500);
    });
    $('#disqus_thread').on('hidden', function () {
        $(link[0]).fadeOut(500, function() {
            $(this).text(old_innerHTML).fadeIn(500);
        });
    })
})
</script>


    </body>
    <!-- Theme: Elegant built for Pelican
    License : http://oncrashreboot.com/pelican-elegant -->
</html>
{'fraction_non_alphanumeric': 0.24231333914930742, 'fraction_numerical': 0.017490478115624403, 'mean_word_length': 4.764503863608487, 'pattern_counts': {'":': 0, '<': 2152, '<?xml version=': 0, '>': 2154, 'https://': 40, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6791478', 'n_tokens_mistral': 16633, 'n_tokens_neox': 16059, 'n_words': 3199}