avatarAmit Chauhan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4270

Abstract

lass="hljs-meta prompt_">>>></span> <span class="language-python">y=<span class="hljs-number">0o47</span> <span class="hljs-comment">#octal literal</span></span> <span class="hljs-meta prompt_">>>></span> <span class="language-python">z=<span class="hljs-number">0x1f</span> <span class="hljs-comment">#hexadecimal literal</span></span> <span class="hljs-meta prompt_">>>></span> <span class="language-python">w=<span class="hljs-number">0b10101</span> <span class="hljs-comment">#binary literal</span></span></pre></div><ul><li>By default, the constructor int() returns value 0.</li></ul><blockquote id="6f10"><p><b><i>The float Class</i></b></p></blockquote><ul><li>The float class is the main floating-point type using a fixed-precision representation.</li><li>The float() form of constructor returns 0.0.</li><li>One other form of literal for floating-point value uses scientific notation.</li></ul><p id="9328">Sequence Types of data structures- The list, tuple and str Classes</p><blockquote id="c8dd"><p><b><i>The list Classes</i></b></p></blockquote><ul><li>An ordered collection of one or more data items can be of the same or different types, enclosed in square brackets.</li></ul><div id="1a27"><pre>>>>myLIst <span class="hljs-punctuation">=</span> <span class="hljs-punctuation">[</span><span class="hljs-string">"kajal"</span>,<span class="hljs-number">2021</span>,<span class="hljs-number">20</span>,<span class="hljs-number">5.5</span>,<span class="hljs-literal">True</span><span class="hljs-punctuation">]</span> >>>myList <span class="hljs-punctuation">[</span><span class="hljs-string">"kajal"</span>,<span class="hljs-number">2021</span>,<span class="hljs-number">20</span>,<span class="hljs-number">5.5</span>,<span class="hljs-literal">True</span><span class="hljs-punctuation">]</span></pre></div><ul><li>Just like the arrays, declared in other languages, indexed from 0 to n-1 where n is the size of the list. List datatype is quite flexible as the elements don’t need to be of the same type.</li><li>By default, the constructor list() returns an empty list. This constructor can accept any parameter of iterable type. E.g.,</li></ul><figure id="d7af"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*t4F71riysODK1R-J_Mv5Zw.png"><figcaption>Example of List classes. A photo by Author</figcaption></figure><blockquote id="aa3d"><p><b><i>The tuple Class</i></b></p></blockquote><ul><li>Same as a list, however, immutable.</li><li>Immutable because one can’t add and remove elements from the container.</li><li>In-place sorting also can’t be done.</li><li>In python, [] character is used to delimit a list, similarly, parenthesis () delimits a tuple.</li></ul><figure id="6dc0"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*enfG-oRpiIJ_PMhA6nW5iQ.png"><figcaption>Example of Tuple. A photo by Author</figcaption></figure><p id="ad87">Note: For a one-element tuple, the expression (element) won’t work as this expression considered as a simple parenthesized expression rather than a tuple element. Therefore, the element should be followed by a comma like this: (element,)</p><p id="b394">e.g,</p><figure id="c713"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*cX1i9YiXMXXkvFlTIQrzkg.png"><figcaption>Example of Tuple. A photo by Author</figcaption></figure><blockquote id="076e"><p><b><i>The str Class</i></b></p></blockquote><ul><li>Strings are collections of letters, numbers and other symbols, aka characters in a sequence. Literal string values are differentiated from identifiers by using single or double-quotes.</li></ul><div id="c901"><pre><span class="hljs-meta prompt_">></span><span class="language-bash">>>name=<span class="hljs-string">"Kajal"</span></span> <span class="hljs-meta prompt_">></span><span class="language-bash">>>newName=<span class="hljs-string">'Aman'</span></span></pre></div><ul><li>A major difference between lists and strings is that lists can be modified while strings cannot, which means that str class is immutable i.e., list elements can be changed using assignment and indexing whereas string’s element can’t be changed.</li></ul><figure id="47ce"><img src="https://cdn-images-1.readmedium.

Options

com/v2/resize:fit:800/1*IXGqf8SS1J0rhC-_UEm8Zw.png"><figcaption>Example of List. A photo by Author</figcaption></figure><ul><li>Triple-quotation is used for multiline strings in the code. It is because the newline character is embedded naturally rather than escaped as \n.</li></ul><figure id="fc9d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*ZfLK08tszg59givsVJIqOw.png"><figcaption>Example of String. A photo by Author</figcaption></figure><blockquote id="533a"><p><b><i>The set and frozenset Classes</i></b></p></blockquote><ul><li>Python uses {} as a delimiter for sets.</li><li>This set class represents the mathematical notion of a set, i.e., a collection of distinct elements (without duplicates).</li><li>There are two important restrictions :</li></ul><ol><li>The set doesn’t maintain the elements in any particular order.</li></ol><p id="9a2b">2. Only instances of immutable type can be added to the python’s set.</p><ul><li>The frozenset class is the set only, but immutable one.</li><li>The hash table is used based on a data structure.</li></ul><figure id="5663"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*K5PiXmVV7jxDFe-trNMz4w.png"><figcaption>Example of Set. A photo by Author</figcaption></figure><blockquote id="3779"><p><b><i>The dict Class</i></b></p></blockquote><p id="d349">Python’s dict is the same as the dictionary. It is mapping from a set of distinct keys to associated values.</p><ul><li>A dictionary literal also uses curly braces {}, this literal form {} produces an empty dictionary. A non-empty dictionary is represented using a comma-separated series of key-value pairs.</li></ul><figure id="96c2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*8NLOR0hjfIoRlu3Sd7PK4w.png"><figcaption>Dictionay Examples. A photo by Author</figcaption></figure><blockquote id="f7b1"><p><b><i>Conclusion:</i></b></p></blockquote><p id="4a91">The Data Structures in python is very easy to learn and understand. They are the backbone of programming language.</p><p id="86bc">I hope you like the article. Reach me on my <a href="https://www.linkedin.com/in/data-scientist-95040a1ab/">LinkedIn</a> and <a href="https://twitter.com/amitprius">twitter</a>.</p><h1 id="986a">Recommended Articles</h1><p id="ae53">1. <a href="https://pub.towardsai.net/15-most-usable-numpy-methods-with-python-4d20eb93e149?sk=911d2bebf042b148be8f366b907af158">15 Most Usable NumPy Methods with Python</a> 2. <a href="https://pub.towardsai.net/numpy-linear-algebra-on-images-ed3180978cdb?source=friends_link&amp;sk=d9afa4a1206971f9b1f64862f6291ac0">NumPy: Linear Algebra on Images</a> 3. <a href="https://pub.towardsai.net/exception-handling-concepts-in-python-4d5116decac3?source=friends_link&amp;sk=a0ed49d9fdeaa67925eac34ecb55ea30">Exception Handling Concepts in Python</a> 4. <a href="https://pub.towardsai.net/pandas-dealing-with-categorical-data-7547305582ff?source=friends_link&amp;sk=11c6809f6623dd4f6dd74d43727297cf">Pandas: Dealing with Categorical Data</a> 5. <a href="https://pub.towardsai.net/hyper-parameters-randomseachcv-and-gridsearchcv-in-machine-learning-b7d091cf56f4?source=friends_link&amp;sk=cab337083fb09601114a6e466ec59689">Hyper-parameters: RandomSeachCV and GridSearchCV in Machine Learning</a> 6. <a href="https://readmedium.com/fully-explained-linear-regression-with-python-fe2b313f32f3?source=friends_link&amp;sk=53c91a2a51347ec2d93f8222c0e06402">Fully Explained Linear Regression with Python</a> 7. <a href="https://readmedium.com/fully-explained-logistic-regression-with-python-f4a16413ddcd?source=friends_link&amp;sk=528181f15a44e48ea38fdd9579241a78">Fully Explained Logistic Regression with Python</a> 8. <a href="https://pub.towardsai.net/data-distribution-using-numpy-with-python-3b64aae6f9d6?source=friends_link&amp;sk=809e75802cbd25ddceb5f0f6496c9803">Data Distribution using Numpy with Python</a> 9. <a href="https://readmedium.com/40-most-insanely-usable-methods-in-python-a983c78f5bfd?sk=07df9058ea3e8c2fce4318a73cd8fce9">40 Most Insanely Usable Methods in Python</a> 10. <a href="https://readmedium.com/20-most-usable-pandas-shortcut-methods-in-python-c9bc065ce11e?sk=1faf673d0cdfb46234975cbdeed12beb">20 Most Usable Pandas Shortcut Methods in Python</a></p></article></body>

Programming

Python Data Structures Data-types and Objects

Handy concepts on class objects in python

Photo by Clint Adair on Unsplash

Python is an object-oriented language and the basis of all data types are formed by classes. Its variable assignment is different from c, c++, and java. The variable does not have a declaration, it is just an assignment statement.

>>>marks=79

Python Objects

Python is a dynamically typed language. It has no knowledge about the variable’s datatype until the code executes. Hence, a declaration is of no use. The value is stored at some memory location, which is bound up with the identifier and makes the contents of the container accessible through that identifier. So the data type does not matter and the type of the particular identifier will be known during runtime.

Therefore, an identifier can be associated with any object type and can be later assigned to another object of the same or different type.

Example of Variable Assignment. A photo by Author

How to create objects?

The process of creating a new object or a new instance of a class is known as instantiation. In general, we do this by invoking the constructor of a class.

Example of making class and object. A photo by Author

Many of the python’s built-in classes support literal form for designating new instances. For example,

>>> Age = 20

Creates a new instance of the int class, the term 20 in the expression is a literal form.

Python’s Built-in Classes

Before moving on, we should know what exactly mutable and immutable classes are. The Immutability of a class means each object of that class points to a fixed value on instantiation, and a unique object ID is assigned to it. The type of the object is defined at the runtime and it can’t be changed later on. Whereas, in the case of mutable, it’s the state can be changed.

The table to differentiate type of Data Structure

The bool Class

  • Used to manipulate logical values.
  • The only two instances of the class are expressed as literals True and False.
  • By default, the constructor bool () returns False.
  • Any expression evaluated to zero is False and the rest is True.
>>> result=bool()
False
>>>answer=True
>>>print(answer)
True

The int Class

  • Designed to represent integer value with arbitrary magnitude.
  • In some contexts, it is convenient to express an integral value using binary, octal and hexadecimal.
>>> x=20 #decimal literal
>>> y=0o47 #octal literal
>>> z=0x1f #hexadecimal literal
>>> w=0b10101 #binary literal
  • By default, the constructor int() returns value 0.

The float Class

  • The float class is the main floating-point type using a fixed-precision representation.
  • The float() form of constructor returns 0.0.
  • One other form of literal for floating-point value uses scientific notation.

Sequence Types of data structures- The list, tuple and str Classes

The list Classes

  • An ordered collection of one or more data items can be of the same or different types, enclosed in square brackets.
>>>myLIst = ["kajal",2021,20,5.5,True]
>>>myList
["kajal",2021,20,5.5,True]
  • Just like the arrays, declared in other languages, indexed from 0 to n-1 where n is the size of the list. List datatype is quite flexible as the elements don’t need to be of the same type.
  • By default, the constructor list() returns an empty list. This constructor can accept any parameter of iterable type. E.g.,
Example of List classes. A photo by Author

The tuple Class

  • Same as a list, however, immutable.
  • Immutable because one can’t add and remove elements from the container.
  • In-place sorting also can’t be done.
  • In python, [] character is used to delimit a list, similarly, parenthesis () delimits a tuple.
Example of Tuple. A photo by Author

Note: For a one-element tuple, the expression (element) won’t work as this expression considered as a simple parenthesized expression rather than a tuple element. Therefore, the element should be followed by a comma like this: (element,)

e.g,

Example of Tuple. A photo by Author

The str Class

  • Strings are collections of letters, numbers and other symbols, aka characters in a sequence. Literal string values are differentiated from identifiers by using single or double-quotes.
>>>name="Kajal"
>>>newName='Aman'
  • A major difference between lists and strings is that lists can be modified while strings cannot, which means that str class is immutable i.e., list elements can be changed using assignment and indexing whereas string’s element can’t be changed.
Example of List. A photo by Author
  • Triple-quotation is used for multiline strings in the code. It is because the newline character is embedded naturally rather than escaped as \n.
Example of String. A photo by Author

The set and frozenset Classes

  • Python uses {} as a delimiter for sets.
  • This set class represents the mathematical notion of a set, i.e., a collection of distinct elements (without duplicates).
  • There are two important restrictions :
  1. The set doesn’t maintain the elements in any particular order.

2. Only instances of immutable type can be added to the python’s set.

  • The frozenset class is the set only, but immutable one.
  • The hash table is used based on a data structure.
Example of Set. A photo by Author

The dict Class

Python’s dict is the same as the dictionary. It is mapping from a set of distinct keys to associated values.

  • A dictionary literal also uses curly braces {}, this literal form {} produces an empty dictionary. A non-empty dictionary is represented using a comma-separated series of key-value pairs.
Dictionay Examples. A photo by Author

Conclusion:

The Data Structures in python is very easy to learn and understand. They are the backbone of programming language.

I hope you like the article. Reach me on my LinkedIn and twitter.

Recommended Articles

1. 15 Most Usable NumPy Methods with Python 2. NumPy: Linear Algebra on Images 3. Exception Handling Concepts in Python 4. Pandas: Dealing with Categorical Data 5. Hyper-parameters: RandomSeachCV and GridSearchCV in Machine Learning 6. Fully Explained Linear Regression with Python 7. Fully Explained Logistic Regression with Python 8. Data Distribution using Numpy with Python 9. 40 Most Insanely Usable Methods in Python 10. 20 Most Usable Pandas Shortcut Methods in Python

Python
Programming
Python3
Machine Learning
Data Visualization
Recommended from ReadMedium
avatarAbhay Kumar
OOPs in Python

An easy guide

10 min read