In python, there is no switch case, unlike other programming languages. So the question comes how do we write a switch case in python?

Basically, there are two ways in which you can try to implement the switch case logic in python where you intend to execute certain statements when a particular case is matched.

- Using
`if-elif-else`

block - Using
`python dictionary`

In this article, we will check both the ways by writing a function that performs arithmetic operations on the numbers passed to the function. The function would take the two numbers and operation as function arguments and return the result based on the operation passed to the function.

`if-elif-else`

The `if-elif-else`

is used in python to write conditional statements as in other programming languages. You can write a switch case by checking every operation passed to function and based on the condition you can return the result.

This is how it is done,

```
def switch_case_function(x, y, op):
if op == "add":
return x + y
elif op == "sub":
return x - y
elif op == "div":
return x/y
elif op == "mul":
return x * y
else:
return "no valid operation"
switch_case_function(4,5,"add")
```

`python dictionary`

Python dictionaries are used to store data in key-value pairs. The key of the dictionary must be an immutable datatype. The dictionary itself is mutable and it does not allow duplicate keys.

You can create a python dictionary in the following ways:

```
# creating an empty dictionary in python
mapping = {}
mapping = dict()
# creating a dictionary with key-value pairs
mapping = {"language" : "python", "creator" : "guido van rossum"}
mapping = dict(language="python", creator="guido van rossum")
```

The main thing to notice here is while you use

`dict()`

and pass the key-value pair, you can directly pass the key as the variable name instead of passing it as strings, as when creating a dictionary using curly braces.

To get a value from the dictionary you can use the built-in `get()`

method on the dictionary object. It returns the value of the key if a key exists else returns the default.

```
def switch_case_function(x,y,op):
operation_mapping = dict(add=x+y, sub=x-y, mul=x*y, div=x/y)
# returns no valid operation if key does not exists.
return operation_mapping.get(op, "no valid operation")
switch_case_function(4,5,"add")
```

Python does not have switch cases unlike other programming languages but you can implement the logic using the two methods shown above. The code is more readable when you write using python dictionaries. You can make your own choice.

Thanks for reading 🙇🏻

]]>In this article, we will take a look at recursion in python. The purpose of this article is not about analyzing the complexity or which approach is better (iterative or recursive). As many searches, sort, and traversal programs use recursion as their solution, it becomes important to understand the basics of recursion prior to getting into the DSA.

Recursion as the name says is a way in which a function recursively calls itself directly or indirectly. While you intend to write recursion, you have to be very careful as the function calls can get into infinite loops. However, there is a default upper limit set for the number of recursion calls, beyond which the program runs into stack overflow or runtime error. A recursion algorithm has two cases, the recursive case, and the base case. Recursion is terminated once the base case is recognized. Generally, the base case output is already known so we can directly return the value when this case is encountered.

With recursion, you can break complex problems into similar smaller ones. Also, the code is more readable than the iterative approach. Recursion is slower than iteration as it has to maintain the stack and uses more memory.

The `greet()`

function calls itself ** recursively**, if you call the function and run this code, it will result in an infinite loop that would print "hello" as output as there is no base case in the function to terminate the recursion. However, in python, you can encounter the

`RecursionError`

once the upper limit of the recursion call is reached.You can get or set the recursion limit in python using the `sys`

module, to do so execute the following code in your python terminal,

```
import sys
# fetch the recursion upper limit
sys.getrecursionlimit()
# ouput: 1000
# if you intend to change the upper limit, you can do so by executing
sys.setrecursionlimit(200)
```

That's it for the basic understanding of recursion.

*Now, let's see an example for finding the factorial of a number using recursion.*

Factorial of a number is a product of all positive numbers smaller than or equal to the number. It is represented by using exclamation after the number. Also, remember that the Factorial of 0 is 1 (0!=1).

```
def factorial(number):
'''
The base case here is when the number becomes 0, we already know 0! = 1
we can directly return 1 once the base case is reached.
'''
if number == 0:
return 1
return number * factorial(number-1)
factorial(4)
```

*Visualize the factorial recursion code below by clicking the next option*

You can observe, the factorial function calls itself with a number decremented by 1, once it reaches the base-case the recursion stops and starts returning the value for each function call until it reaches the actual call `factorial(4)`

and returns the value `24`

.

I hope this article, helps you understand the basics of recursion. As an exercise, you can try implementing the fibonacci series using python and visualize the code on pythontutor.

Thanks for reading 🙇🏻

**references**

https://www.hackerearth.com/practice/basic-programming/recursion/recursion-and-backtracking/tutorial/ https://everythingcomputerscience.com/discrete_mathematics/Recurssion.html

]]>In this article, we will try to understand what are binary trees, as we go along we will be exploring different tree data structures so we must have a basic understanding of the binary tree.

A binary tree is a tree comprising nodes, with each node having at most two other nodes referred to as the left child and the right child of the node.

Each node contains three components data element, pointer to the left child and pointer to the right child, they are recursively defined. If there is no child the pointer points to null.

A data element can be any type of data, while the left and the right pointer point to ** binary trees** on the left and the right side of the node respectively.

The image below represents a binary tree and the node structure if you extract a node element.

```
class BinaryTree:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
root_node = Node(1)
```

- The topmost node is called the root node.
- The nodes that are connected downwards and are next to each other are called children.
- The node without any children is called a leaf node.
- Node with at least one child is called an internal node.
- Node without any child is called an external node.
- Nodes with the same parent are called siblings.

- Full Binary Tree
- Complete Binary Tree
- Perfect Binary Tree
- Degenerate Binary Tree
- Balanced Binary Tree

I hope this article gave you a basic understanding of the binary tree. We will be exploring each type of BT in the later articles.

Thanks for reading 🙇🏻

**references**

https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/tutorial/

]]>