LEARN
DATA STRUCTURE
LINKED LIST
ARRAY
STACK
QUEUE
USING
PYTHON
IN 5 MINUTES
Copyright 2021 S Basu
All rights reserved.
Disclaimer:
The information and materials presented here are for educational purposes only. Every effort has been made to make this book as complete and as accurate as possible but no warranty or fitness is implied. The information provided is on an "as is" basis. The ideas and opinions expressed are of the author's own imagination and the author is not affiliated to any organization, school or educational discipline and will not be held accountable or liable for any inadvertent misrepresentation.
Contents
Chapter 1 : Few Important Python Basics
Before we start learning about data structure related topics like linked list, array, stack and queue, we first need to get an understanding about a few python basics like how to create a python module, run a python code and what is a python function or a python class.
So let us first review these python topics.
1.1 : What is a Python module?
Lets create a simple Python file or module,
Inside hello_world.py file, write one line of code:
What is print( ) function in Python?
print( ) function displays the output to the screen
Now lets run the above piece of code.
Open command prompt -> Go to the hello_world.py file location and type the following command:
python file_name .py
The output displays
In test.py file, simply import the hello_world module created above.
Now run this file.
Open command prompt -> Go to the test.py file location and write the following command:
python test .py
We see that the test module prints out Hello World output from hello_world module.
What is import statement in Python?
import statement in Python allows a Python file or module to access all the codes present in another Python file or module.
1.2 : What is a Python function?
Python Functions are just like Java methods. It is a block of codes performing certain task.
Syntax:
def function_name ( ) :
..
Example:
Code Explanation:
Output
Difference between parameter and argument
Parameter is the variable passed to a function . Example:
function function_name ( variable1 , variable2 ) :
Argument is the value assigned to the variables. Example:
function_name ( 123,Hello_World )
1.3 : What is a Python class?
Python is an object oriented programming language .
A class is a blueprint for creating an object and like any other programming language Python class contains attributes and methods.
Python object contains the copy of all attributes and methods present within its class. To access those properties dot operator ( . ) is used.
class class_name :
def __init__(self) :
.
def method1 :
.
The syntax for creating an object of a class is:
class Hello :
def __init__(self) :
h = Hello( ) , where h is the object of class Hello .
What is def __init__ (self)?
"__init__" is a reserved method in Python classes initializing the object s state. In Object Oriented concepts, it refers to the constructor of the class . This method initializes the attributes of a class when an object is created.
self keyword helps to access the attributes of the class.
Example:
Output
Important points to note from the above piece of code are:
A Python Class name should start with a capital letter.
Within the __init__ method we declared different attributes of our class Employee and passed those values to self . id_num , self . name and so on.
In our validation ( ) method we pass the argument self which helps to access all the attributes of the class Employee .
str( ) function helps to convert an integer value to a string value.
and keyword is the logical and operator in Python.
In line 15 of the above code, we created an object e and passed values into it.
Chapter 2 : Array
An Array is a collection of items or elements all having the same datatypes. An element from an array can only be accessed from its index value. Example: Lets create an array of cars.
cars = [Kia, Toyota, Ford, Tesla]
In order to get the value Ford as output, we need to write print ( cars [ ] )
List_name [ index_num ]