Data Structures in Python
Data structures are simply the ways we process, design and store the data in programming language. In computer programming data structures are the specialized format using what we can store the data for the purpose of working on it using various algorithms. Choosing the right data structure makes one a good programmer. So what are the specific characteristics of a good programmer? well, a good programmer should have the following qualities:
- Picking right data structures
- Ability to choose right algorithms
- Writing good program which is readable and scalable
- Should know the three pillars of good code: a)Readable. b)Memory(Space Complexity). c)Speed(Time complexity)
Let’s talk about the three pillars of a good code in a nutshell:
Readability: When we code in any specific programming language we should concentrate on the readability of the program we are writing. We should not be much clever in using programming language, as for example we should avoid using too much conditional statements in our coding which won’t be readable to other programmers and moreover these types of coding will lead us to worst case state in Big O notation. Rather we can simply make our program much easier to read and understand to other programmers for further uses in future.
Memory(Space Complexity): It’s space of the machine. To solve a problem using programming language we need specific space in memory, we don’t want any unexpected code that fills up our memory space extra. For any algorithm memory can be used for: variables, Instructions and the execution.
Speed(Time Complexity): It’s the minimum execution time of a program. Nobody wants any program that requires much time for the execution. As a good programmer we’ve to concentrate on this topic.
The common data structures in any programming language are as follows:
- Arrays
- Stacks
- Queues
- Linked list
- Trees
- Graphs
- Hash Tables
- Tries
In Python we have also a collection of data structures they are:
- Int(Integer)
- Bool(Boolean)
- List(Arrays)
- Float
- str(String)
- Tuple
- Set
- Dictionary(Hash Tables)
Python Data Types numbers:
Integers : They are referred as “int” with no decimal point. Integers can be a positive or negative whole numbers.
Floating point numbers : They are integers with decimal points or fractional parts. They are referred as “float”.
Complex numbers : They are of the form a + bj where and b are floats and j is an imaginary number. Complex numbers are not used much in python programming
PYTHON DATA TYPES STRINGS:
Strings are sequence of characters using the syntax of either single quotes or double quotes.
•’hello’ “hello” “Today’s date”
•print (“Hello python”)
•print (“Hello \npython”)
•print (“Hello \t python”)
•len (“Hello”)
- len (“Today’s date”)
Python 4 built-in data types:
There are four built-in data structures in python — list, tuple, dictionary and set.
- List : List is a collection of items which is ordered and changeable. Allows for duplicate members. Example:
thislist = [“apple”, “banana”, “cherry”]
print(thislist)
- Tuple : Tuple is a collection of items which is ordered and unchangeable. Allows for duplicate members. Example:
thistuple = (“apple”, “banana”, “cherry”)
print(thistuple)
- Set : Set is a collection of items which is unordered and unindexed. Does not allow for duplicate members. Example:
thisset = {“apple”, “banana”, “cherry”}
print(thisset)
Dictionary: Dictionaries are used to store data values in key: value pairs. A dictionary is a collection which is ordered*, changeable and does not allow duplicates. Example:
thisdict = {
“brand”: “Ford”,
“model”: “Mustang”,
“year”: 1964
}
print(thisdict)
Python Boolean data type:
Booleans represent one of two values: True
or False
.
print(10 > 9)
print(10 == 9)
print(10 < 9)
print(bool(“Hello”))
print(bool(15))
Output will be only True or False.
These are the all data structures in Python programming language. Exercising these data structures we will be more capable to know which data type to use depending on the project or the purposes of our program. Thank you.