Python Basics
ELEMENTARY DATA ORGANIZATION
Data is a collection of facts, such as numbers, words, measurements, observations or even just descriptions of things. data is collected and processed for some purpose, usually analysis. Data exists in a variety of forms — as numbers or text on pieces of paper, as bits and bytes stored in electronic memory, or as facts stored in a person’s mind.
Forms of Data :
- Qualitative datais descriptive information
- Quantitative datais numerical information (numbers)
- Raw Data : Information that has been collected but not formatted or analysed.
- Structured Data : refers to data that resides in a fixed field within a record or file. It includes data contained in relational databases and spreadsheets.
- Unstructured Data : Information that doesn’t reside in traditional row-column database.
Within a computer’s storage, data is a series of bits (binary digits) that will have the value one or zero. Data is processed by the CPU, which uses logical operations to produce new data output from source data input.
DATA STRUCTURES
A data structure is a specialized format for organizing, processing, retrieving and storing data
“Data may be organized in many ways; the logical or mathematical model of a particular organization of data is called a data structure“.
“A data structure is an arrangement of data in a computer’s memory or even disk storage. It has a different way of storing and organizing data in a computer so that it can used efficiently”
TYPES OF DATA STRUCTURES
Data Structures can be classified into two categories
- Linear Data Structures
- Non Linear Data Structures
Linear Data Structures:- A Data Structures whose elements form a sequence and each element has a unique successor and unique predecessor is known as linear data structures for example: Array, Linked List, Stack, Queue
Non Linear Data Structures:- A Data Structures whose elements do not form a sequence and each element has not a unique successor and unique predecessor is known as non linear data structures for example: Tree, Graph
OPERATIONS ON DATA STRUCTURES
The following operations play a major role in data structures-
- Traversing: Accessing each record exactly once
- Searching: Finding the location of the record with a given key value
- Inserting: Adding a new record
- Deleting: Removing a existing record from the structure
- Sorting: arranging the values in some logical order (by alphabetically, ascending, or descending order)
- Merging: Combining the records in two different sorted files into a single sorted file
CHARACTERISTICS OF DATA STRUCTURES
Linear or non-linear: This characteristic describes whether the data items are arranged in chronological sequence, such as with an array, or in an unordered sequence, such as with a graph.
Homogeneous or non-homogeneous: This characteristic describes whether all data items in a given repository are of the same type or of various types.
Static or dynamic: This characteristic describes how the data structures are compiled. Static data structures have fixed sizes, structures and memory locations at compile time. Dynamic data structures have sizes, structures and memory locations that can shrink or expand depending on the use.
Types of data structures
Data structure types are determined by what types of operations are required or what kinds of algorithms are going to be applied.
Types include:
- Arrays: An array stores a collection of items at adjoining memory locations. Items that are the same type get stored together so that the position of each element can be calculated or retrieved easily. Arrays can be fixed or flexible in length.
- Stacks: A stack stores a collection of items in the linear order that operations are applied. This order could be last in first out (LIFO) or first in first out (FIFO)
- Queues: A queue stores a collection of items similar to a stack; however, the operation order can only be first in first out.
- Linked lists– A linked list stores a collection of items in a linear order. Each element, or node, in a linked list contains a data item as well as a reference, or link, to the next item in the list.
- Trees: A tree stores a collection of items in an abstract, hierarchical way. Each node is linked to other nodes and can have multiple sub-values, also known as children.
- Graphs : A graph stores a collection of items in a non-linear fashion. Graphs are made up of a finite set of nodes, also known as vertices, and lines that connect them, also known as edges. These are useful for representing real-life systems such as computer networks.
- Hash tables- A hash table, or a hash map, stores a collection of items in an associative array that plots keys to values. A hash table uses a hash function to convert an index into an array of buckets that contain the desired data item.
ALGORITHM
“An algorithm is a well defined sequence of steps to solve a particular problem”. “It is a kind of pseudo language used to depict the solution steps.” A set of algorithms are always used to perform operations on data structure. Data may be organized in many ways and there may be more than one algorithm to solve a particular problem. The choice of a particular algorithm depends on the following considerations:
- Performance requirements i.e. time complexity
- Memory requirements i.e. space complexity
Each algorithm is a list of well-defined instructions for completing a task. Starting from an initial state, the instructions describe a computation that proceeds through a well-defined series of successive states, eventually terminating in a final ending state.
ALGORITHM COMPLEXITY
“The complexity of an algorithm is the amount of time and the amount of space that is consumed by algorithm to run to completion”. The complexity is divided into two types
- Space Complexity: – also known as memory requirement
- Time Complexity: – also known as performance requirement
SPACE COMPLEXITY
“The space complexity of an algorithm is the amount of memory it needs to run to completion”. The space needed by a program consists of following components-
- Instructions space- to store executable version of program
- Data space- to store all constants, variables etc.
- Environment space- used in case of recursive program
The total space needed by a program is divided in two parts- A fixed part that is independent of particular problem, and includes instructions space, space for constants, variables and fixed size structure variables. A variable part that includes data structure variables whose size depends on the particular problem being solved dynamically allocated space and the recursion stack space.
TIME COMPLEXITY
“The time complexity of an algorithm is the amount of time it needs to run to completion”. Some of the reasons for studying time complexity are
- To know in advance whether the program will provide a satisfactory real time response.
- To use alternative solutions with different time requirements or with different time complexity
ALGORITHM: Time-Space Trade Off
The best algorithm to solve a given problem is one that requires less space in memory and takes less time to complete its execution. But in practice it is not always possible to achieve these objectives. As we know some programs may take more space but takes less time to complete its execution while the other approach may take less space but takes more time to complete its execution. If space is our constraint, then it may be possible to choose a program that requires less space at the cost of more execution time. On the other hand if time is constraint then it is possible to choose a program that takes less time to complete its execution at the cost of more space.
Sample Python Hello Program: