Book Image

Hands-On Data Structures and Algorithms with Python - Second Edition

By : Dr. Basant Agarwal, Benjamin Baka
Book Image

Hands-On Data Structures and Algorithms with Python - Second Edition

By: Dr. Basant Agarwal, Benjamin Baka

Overview of this book

Data structures allow you to store and organize data efficiently. They are critical to any problem, provide a complete solution, and act like reusable code. Hands-On Data Structures and Algorithms with Python teaches you the essential Python data structures and the most common algorithms for building easy and maintainable applications. This book helps you to understand the power of linked lists, double linked lists, and circular linked lists. You will learn to create complex data structures, such as graphs, stacks, and queues. As you make your way through the chapters, you will explore the application of binary searches and binary search trees, along with learning common techniques and structures used in tasks such as preprocessing, modeling, and transforming data. In the concluding chapters, you will get to grips with organizing your code in a manageable, consistent, and extendable way. You will also study how to bubble sort, selection sort, insertion sort, and merge sort algorithms in detail. By the end of the book, you will have learned how to build components that are easy to understand, debug, and use in different applications. You will get insights into Python implementation of all the important and relevant algorithms.
Table of Contents (16 chapters)

Flow control and iteration

Python programs consist of a sequence of statements. The interpreter executes each statement in order until there are no more statements. This is true if files run as the main program, as well as if they are loaded via import. All statements, including variable assignment, function definitions, class definitions, and module imports, have equal status. There are no special statements that have higher priority than any other, and every statement can be placed anywhere in a program. All the instructions/statements in the program are executed in sequence in general. However, there are two main ways of controlling the flow of program execution—conditional statements and loops.

The if...else and elif statements control the conditional execution of statements. The general format is a series of if and elif statements followed by a final else statement:

x='one' 
if x==0:
print('False')
elif x==1:
print('True')
else: print('Something else')

#prints'Something else'

Note the use of the == operator to compare the two values. This returns True if both the values are equal; it returns False otherwise. Note also that setting x to a string will return Something else rather than generate a type error as may happen in languages that are not dynamically typed. Dynamically typed languages, such as Python, allow flexible assignment of objects with different types.

The other way of controlling program flow is with loops. Python offers two ways of constructing looping, such as the while and for loop statements. A while loop repeats executing statements until a Boolean condition is true. A for loop provides a way of repeating the execution into the loop through a series of elements. Here is an example:

In this example, the while loop executes the statements until the condition x < 3 is true. Let's consider another example that uses a for loop:

>>>words = ['cat', 'dog', 'elephant']
>>> for w in words:
... print(w)
...

cat
dog
elephant

In this example, the for loop executes iterating for all the items over the list.