Shravani Thirunagari
Shravani Roy

Follow

Shravani Roy

Follow

Basic Programming in Python - Part 3

Shravani Thirunagari's photo
Shravani Thirunagari
·Jul 11, 2022·

3 min read

Functions:

A set of instructions that you are aware of that could be required to be executed multiple times in the program for different conditions or so. instead of writing the same code / same set of instructions multiple times in the program, we can put these sets of instructions into a block and call wherever required. These blocks of codes are called functions and can be declared by the keyword 'def' in python. Let us look at an example below:

Screenshot 2022-07-12 at 10.56.18 PM.png

From the above code, you see that we are passing an argument 'name', and the rest of the code is reusable. So next time you want to print another name you could just call this function and pass the name you want to print. This example might seem nonrealistic. However, this is just to give you an example of the syntax and to give you an overview of the concept. Functions are widely used and are the core of any program to perform basic computations to integrate APIs in the program.

Let me give you a real-time example. Say from a real estate application where people can buy and sell real estate properties, we want to display the price of the property, wherein you have access to only property size and price per square foot. In this case, we property price is the product of property size and price per square foot. Check the code below:

Screenshot 2022-07-12 at 11.22.49 PM.png

Dictionaries and Tuples:

In simple terms, a dictionary is like an object, having key-value pairs. Values of dictionaries can be accessed by keys

example1 = {
   "platform": "Hashnode",
   "rank": 1
}

A Tuple is similar to a List, yet immutable. Values of Tuple can be accessed by position, just like that of lists. Let us check an example:

tuple_example = (1, 3, 5, 7, 11)
print(tuple_example[2]) //prints the element at position 2

tuple_example[1] = 13 //this doesn't replace any element from the tuple

Let us create a dictionary with Indian States and capitals and then play with it :)

Screenshot 2022-07-13 at 10.55.26 AM.png

From the above code, I have created a dictionary and a function that allows the user to enter a key from the menu ['print', 'add', 'remove', 'find']. So, assuming the user is aware of the menu, I started by assigning each case with an if block and proceeding with the respective computation. You could use different functions for each case to understand the functions better and a bit of practice. However, I have used a single function to achieve all the cases for reusability purposes. I Printed all the items of the dictionary when the user input is 'print'. Adding a new state and its capital when the user input is 'add', provided the state isn't in the dictionary already. Removing the state if it is in the dictionary when the user input is 'remove'. And finally, look for the state and return its capital, when the user input is 'find'.

Let us call the function by passing other menu items as arguments and find out the result.

Screenshot 2022-07-13 at 10.59.41 AM.png

Screenshot 2022-07-13 at 11.01.27 AM.png

Screenshot 2022-07-13 at 11.01.52 AM.png

Screenshot 2022-07-13 at 11.02.52 AM.png

Screenshot 2022-07-13 at 11.03.19 AM.png

It was so cool playing with dictionaries. One thing before ending the blog that I wanted to share is that, yourDictionary.item() helps you iterate the dictionary. Thanks for reading so far. I hope this was helpful. Happy Learning!

 
Share this