0% found this document useful (0 votes)
12 views

Python 1

1) Basic data structures in Python include lists, dictionaries, tuples, sets, strings, and linked lists. Lists are ordered collections that allow heterogeneous elements. Dictionaries are unordered collections of key-value pairs that provide fast lookups. Tuples are immutable lists. Sets are unordered collections that do not allow duplicates. 2) Stacks can be implemented using lists and queues in Python. Lists allow efficient push and pop using append() and pop(). Queues provide enqueue() and dequeue() to push and pop elements in FIFO order. 3) The queue module implements queues with methods like get() and put() for insertion and retrieval. Priority queues add elements based on priority number, placing lower priority

Uploaded by

welcomeuser66
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python 1

1) Basic data structures in Python include lists, dictionaries, tuples, sets, strings, and linked lists. Lists are ordered collections that allow heterogeneous elements. Dictionaries are unordered collections of key-value pairs that provide fast lookups. Tuples are immutable lists. Sets are unordered collections that do not allow duplicates. 2) Stacks can be implemented using lists and queues in Python. Lists allow efficient push and pop using append() and pop(). Queues provide enqueue() and dequeue() to push and pop elements in FIFO order. 3) The queue module implements queues with methods like get() and put() for insertion and retrieval. Priority queues add elements based on priority number, placing lower priority

Uploaded by

welcomeuser66
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1) Basic data structures in Python.

 Data Structures are a way of organizing data so that it can be accessed more efficiently
depending upon the situation. Data Structures are fundamentals of any programming
language around which a program is built. Python helps to learn the fundamental of these
data structures in a simpler way as compared to other programming languages.

Lists
Python Lists are just like the arrays, declared in other languages which is an ordered
collection of data. It is very flexible as the items in a list do not need to be of the same type.
List elements can be accessed by the assigned index. In python starting index of the list,
sequence is 0 and the ending index is (if N elements are there) N-1.

Dictionary
Python dictionary is like hash tables in any other language with the time complexity of
O(1). It is an unordered collection of data values, used to store data values like a map,
which, unlike other Data Types that hold only a single value as an element, Dictionary
holds the key:value pair. Key-value is provided in the dictionary to make it more
optimized.
Tuple
Python Tuple is a collection of Python objects much like a list but Tuples are immutable in
nature i.e. the elements in the tuple cannot be added or removed once created. Just like a
List, a Tuple can also contain elements of various types.
In Python, tuples are created by placing a sequence of values separated by ‘comma’ with or
without the use of parentheses for grouping of the data sequence.

Set
Python Set is an unordered collection of data that is mutable and does not allow any
duplicate element. Sets are basically used to include membership testing and eliminating
duplicate entries. The data structure used in this is Hashing, a popular technique to perform
insertion, deletion, and traversal in O(1) on average.

String
Python Strings are arrays of bytes representing Unicode characters. In simpler terms, a
string is an immutable array of characters. Python does not have a character data type, a
single character is simply a string with a length of 1.

Collections Module
Python collection module was introduced to improve the functionality of the built-in
datatypes. It provides various containers let’s see each one of them in detail.

Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations. A linked list is represented by a pointer to the first node of the linked
list. The first node is called the head.
2) Stack (using - list, queue,LifoQueue).

1) Using list
Stack works on the principle of “Last-in, first-out”. Also, the inbuilt functions in Python
make the code short and simple. To add an item to the top of the list, i.e., to push an item,
we use append() function and to pop out an element we use pop() function. These functions
work quiet efficiently and fast in end operations.

2)Queue
Given a Queue data structure that supports standard operations
like enqueue() and dequeue(). The task is to implement a Stack data structure using only
instances of Queue and Queue operations allowed on the instances

Implement Stack using Queues By making push() operation:


The idea is to keep newly entered element at the front of ‘q1’ so that pop operation
dequeues from ‘q1’. ‘q2’ is used to put every new element in front of ‘q1’.

Implement Stack using Queues by making pop() operation


The new element is always enqueued to q1. In pop() operation, if q2 is empty then all the
elements except the last, are moved to q2. Finally, the last element is dequeued.

3)LIFO Queue
A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-
In/Last-Out (FILO) manner. In stack, a new element is added at one end and an element is
removed from that end only. The insert and delete operations are often called push and pop.
The functions associated with stack are:
 empty() – Returns whether the stack is empty – Time Complexity: O(1)
 size() – Returns the size of the stack – Time Complexity: O(1)
 top() / peek() – Returns a reference to the topmost element of the stack – Time
Complexity: O(1)
 push(a) – Inserts the element ‘a’ at the top of the stack – Time Complexity:
O(1)
 pop() – Deletes the topmost element of the stack – Time Complexity: O(1)

3) Queue using queue module.


 A Queue is a data structure where the first element to be inserted is also the first element
popped. It’s just like a real-life queue, where the first in line is also the first one out.
In Python, we can use the queue module to create a queue of objects.
This is a part of the standard Python library, so there’s no need to use pip.
Import the module using: import queue.
We can insert and retrieve values into the Queue using
the queue.get() and queue.put() methods.
We can empty a queue object using q.empty().
This sets the size to 0, and empties the queue.
We’ll use list.append(value) to insert elements into the queue, since insertion happens at the
end, and remove elements using list.pop(0), since the first element is removed.
Priority Queue:-
Priority Queue is a type of queue that adds to the queue on the basis of an item’s priority,
which is typically an integer value.
Items with a lower priority number are given a higher preference and are at the front of the
queue, while others are behind.

4) Linked list.
 A linked list is a sequence of data elements, which are connected together via links. Each
data element contains a connection to another data element in form of a pointer. Python does
not have linked lists in its standard library. We implement the concept of linked lists using the
concept of nodes as discussed in the previous chapter.

We have already seen how we create a node class and how to traverse the elements of a
node.In this chapter we are going to study the types of linked lists known as singly linked
lists. In this type of data structure there is only one link between any two data elements. We
create such a list and create additional methods to insert, update and remove elements from
the list.

Creation of Linked list


A linked list is created by using the node class we studied in the last chapter. We create a
Node object and create another class to use this ode object. We pass the appropriate values
through the node object to point the to the next data elements.

Traversing a Linked List


Singly linked lists can be traversed in only forward direction starting form the first data
element. We simply print the value of the next data element by assigning the pointer of the
next node to the current data element.

Insertion in a Linked List


Inserting element in the linked list involves reassigning the pointers from the existing nodes
to the newly inserted node. Depending on whether the new data element is getting inserted at
the beginning or at the middle or at the end of the linked list.

Removing an Item
We can remove an existing node using the key for that node. In the below program we locate
the previous node of the node which is to be deleted.Then, point the next pointer of this node
to the next node of the node to be deleted.

You might also like