What is Data Structure?
- Data Structures helps you to arrange the data in such a way that you can store, relate and run data collections accordingly.
- Organizing, managing and storing data is important, as it allows easier access and productive modifications.
Primitive Data Structures
These are the most primitive data structures, or basic data structures.They are the building blocks for manipulating data, and contain plain, pure data values.
Python has four different types of primitive variables
Strings are sequences of alphabets, words, etc. You can construct strings in Python by enclosing a series of characters within a pair of single or double quotes. For instance: table, chair etc.
Lets have a quick look at operations on Strings in Python.
a)Concatenation - You can use ‘+’ operations to concatenate two or more strings.
NOTE :- Strings can also be alpha-numeric characters, but the ’+’ operation still is used to concatenate strings.
b) Repeat - You can use * to repeat the string certain number of times.
C) Range Slicing - You can also slice strings, which means that you select parts of strings:
D) Capitalize strings -
2) Boolean -
A built -in category of data that can take the values: True and False, which often makes them compatible with the 1 and 0 integers.Booleans are useful in terms of conditional expression and contrast, justas in the following examples:
3) Float -
- To check the type of an object in Python, use the built-in type() function.
- Float() method is also used to return a floating point number from a number or a string.
4) Integer -
Python integers are nothing but whole numbers, whose range dependent on the hardware on which python is run.Integers can be of different types such as positive, negative,zero, and long.
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.
1) Array -
An array is essentially a configuration of the data that can contain more than one value at a time. It is a set, or ordered, of the same sort of elements.Basic array operations: A lot of operations can be performed on arrays that are as follows –.
Adding elements to an Array:
We can add value to an array by using the append(), extend() and the insert (i,x) functions.The append() function is used when we need to add a single element at the end of the array.
Removing Array Elements :
You can use the pop() method to remove an element from the array.
Looping Array Elements :
You can use the for in loop to loop through all the elements of an array.
Tuples are a more standard type of data set. The difference between tuples and list is that tuples are immutable plus you can't delete, addr change
any values inside it once you have defined them.
Python's Dictionary is an unordered set of data values used to store data values like a map that, unlike other Data Types that only carry a single value as an element, Dictionary holds key: value pair.
Sets are an array of defined (unique) objects. These are useful for constructing lists that contain unique values only in the dataset. It's an unordered list, but it's a mutable collection, which is quite useful to go through a huge dataset.
5) Files -
Traditionally, files are a part of data structures.
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 your system, the filename is the name of the file to be opened;
- read() to read entire files;
- redline() to read one line at a time;
- write() to write a string to a file, and return the number of characters written; And
- close() to close the 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+).