Python From Scratch…
Chapter 4:
In my last post we have seen the syntax structure in python.Now moving forward in this post we will look into Data Structures supported by python.
so,what actually Data Structure is: It is a way of organizing and storing data so that they can be accessed and used efficiently. Data Structures define the relationship between the data and the operations that can be performed on the data.
In Python, you do not have to explicitly state the type of the variable or your data. That is because it is a dynamically typed language.But variable have a type.
Data structures can be divided into two categories in computer science: primitive and non-primitive data structures.
Primitive Data Structures:
- Integer(int):
int holds the signed integer,ranging from -ve infinity to +ve infinity .
2.Float(float):
Float stands for Floating Point Number.It holds floating point real values.
3.String:
A string is a sequence of characters. Python does not have a char data type unlike C++ or Java,it is only having string data type.we can define string using single quotes(‘ ’) or double quotes(“”).
4.Boolean:
This is built-in data type that can take up the values: True and False, which often makes them interchangeable with the integers 1 and 0. Boolean’s are useful in conditional and comparison expressions.
Non-Primitive Data Structures:
Non-primitive types are the sophisticated members of the data structure family. They don’t just store a value, but rather a collection of values in various formats.
- Array:
In Python, arrays are supported by the array module and need to be imported before you start initializing and using them.The data type is specified during the array creation using a type code, which is a single character and the Commonly used type codes are listed as follows:
In following example you can see the use of character I in array which denotes that this array contains unsigned integer:
2.List:
Lists in Python are used to store collection of heterogeneous items. These are mutable, which means that you can change their content without changing their identity. You can recognize lists by their square brackets [ and ] that hold elements, separated by a comma ,. Lists are built into Python: you do not need to invoke them separately.Example of list is as shown below:
Traditionally, the list data structure can be further categorized into Linear and Non-Linear data structures.
Linear :Stacks and Queues
Non-Linear:Graphs and Trees
2.1.Stack:
Get Yashaswini Suryawanshi’s stories in your inbox
Join Medium for free to get updates from this writer.
A stack is a container of objects that are inserted and removed according to the Last-In-First-Out (LIFO) concept.In computer science, this concept is used for evaluating expressions and syntax parsing, scheduling algorithms/routines, etc.Stacks can be implemented using lists in Python. When you add elements to a stack, it is known as a push operation, whereas when you remove or delete an element it is called a pop operation.
2.2.Queue:
A queue is a container of objects that are inserted and removed according to the First-In-First-Out (FIFO) principle. An excellent example of a queue in the real world is the line at a ticket counter where people are catered according to their arrival sequence and hence the person who arrives first is also the first to leave.
2.3.Graph:
A graph in mathematics and computer science are networks consisting of nodes, also called vertices which may or may not be connected to each other. The lines or the path that connects two nodes is called an edge.
If the edge has a particular direction of flow, then it is a directed graph, with the direction edge being called an arc.If no directions are specified, the graph is called an undirected graph.
2.4.Trees:
In computer science, a tree is a data structure that is modeled after nature. Unlike trees in nature, the tree data structure is upside down: the root of the tree is on top. A tree consists of nodes and its connections are called edges. The bottom nodes are also named leaf nodes. A tree may not have a cycle.
3.Tuple:
Tuple is a standard sequence data type. The difference between tuples and list is that tuples are immutable, which means once defined you cannot delete, add or edit any values inside it.A tuple is created by placing all the items (elements) inside parentheses (), separated by commas. The parentheses are optional, however, it is a good practice to use them.
4.Dictionary:
Dictionaries are exactly what you need if you want to implement something similar to a telephone book. None of the data structures that you have seen before are suitable for a telephone book.
This is when a dictionary can come in handy. Dictionaries are made up of key-value pairs. key is used to identify the item and the value holds as the name suggests, the value of the item.
5.Set:
Sets are a collection of distinct (unique) objects. These are useful to create lists that only hold unique values in the data set. It is an unordered collection but a mutable one, this is very helpful when data set is very huge.
6.File:
Files are traditionally a part of data structures. And although big data is commonplace in the data science industry, a programming language without the capability to store and retrieve previously stored information would hardly be useful.
The syntax to read and write files in Python is similar to other programming languages but a lot easier to handle. Here are some of the basic functions that will help you to work with files using Python:
open()to open files in system.- The first argument in the
open()function is name of file. - The second argument in the
open()function is the file mode. It allows you to specify whether you want to read (r), write (w), append (a) or both read and write (r+)(both reading and writing). read()to read entire files;readline()to read one line at a time;write()to write a string to a file, and return the number of characters written;close()to close the file.
SUMMARY: In this chapter we have seen various data structure with their explanatory examples.
In the next chapter we will be looking deep into file data structure.so stay tuned…









