In Part 4 for our python3 basics for beginners blog, we read about different type of loops and their use. In this blog, we extend this further by introducing concept of dictionary in python.
1. Dictionary
Dictionary in python can be use to input from one source and itself acts as a source for processing in function. Dictionary follow key-pair format (ABC: XYZ) and can be used to call multiple values from list of input.
Code
def coffee_orders(coffee_type, quantity):
"""coffee_type = coffee_type_quantity_dict["coffee_type"]
quantity = int(coffee_type_quantity_dict["quantity"])"""
if coffee_type == "black":
print(f"Your bill for {coffee_type} is {quantity*5} dollars")
elif coffee_type == "latte":
print(f"Your bill for {coffee_type} is {quantity*6} dollars")
elif coffee_type == "americano":
print(f"Your bill for {coffee_type} is {quantity*4} dollars")
else:
print(f"We don't serve it here")
coffee_order=input("Which coffee you like to have and how much?\n")
coffee_type_and_quantity=coffee_order.split(",")
coffee_type_quantity_dict={"coffee_type": coffee_type_and_quantity[0], "quantity": coffee_type_and_quantity[1]}
coffee_type = coffee_type_quantity_dict["coffee_type"]
quantity = int(coffee_type_quantity_dict["quantity"])
coffee_orders(coffee_type, quantity)
In above example, I send input to my code in list seperated with “,” and assigned index[0] or ist value of list to ist key-value pair “coffee_type” and index[1] or 2nd value of list to 2nd key-value pair “quantity” after splitting. These two key-value pairs are then called to my function logic and process result as required.
Output
Which coffee you like to have and how much?
black,3
Your bill for black is 15 dollars
Which coffee you like to have and how much?
latte,5
Your bill for latte is 30 dollars
2. Importing User-Made Modules
Modules can referred as different python codes written in seperate files, called in main program to improve code sanity and clarity. If we take simple example of our previous code where we use dictionary to split and use input from list, I can simply move my function in seperate file and ask my code in my main file to preform processing based on second file.
Code
ubuntu@ubuntu:~$ cat main.py
from back import coffee_orders
coffee_order=input("Which coffee you like to have and how much?\n")
coffee_type_and_quantity=coffee_order.split(",")
coffee_type_quantity_dict={"coffee_type": coffee_type_and_quantity[0], "quantity": coffee_type_and_quantity[1]}
coffee_type = coffee_type_quantity_dict["coffee_type"]
quantity = int(coffee_type_quantity_dict["quantity"])
coffee_orders(coffee_type, quantity)
ubuntu@ubuntu:~$ cat back.py
def coffee_orders(coffee_type, quantity):
"""coffee_type = coffee_type_quantity_dict["coffee_type"]
quantity = int(coffee_type_quantity_dict["quantity"])"""
if coffee_type == "black":
print(f"Your bill for {coffee_type} is {quantity*5} dollars")
elif coffee_type == "latte":
print(f"Your bill for {coffee_type} is {quantity*6} dollars")
elif coffee_type == "americano":
print(f"Your bill for {coffee_type} is {quantity*4} dollars")
else:
print(f"We don't serve it here")
In above example, we divided our code in two files. Inside main file, we asked our code to refer from back and import function name. Now my main file knows which file to refer for performing expected function. Since output will remain same, I am not writing it again.
We may also call all functions other files by putting “import *” instead of import <name of function>.
3. Importing Builtin Modules
Besides importing developer made modules, we can import python’s builtin modules for our different use cases.
Code
import gzip
content = b"Lots of content here"
with gzip.open('/home/ubuntu/file.txt.gz', 'wb') as f:
f.write(content)
In above example, we used gzip builtin module to compress file in commonly used .gz format.
Output
ubuntu@ubuntu:~$ ls -l | grep file
-rw-rw-r-- 1 ubuntu ubuntu 49 Feb 27 12:21 file.txt.gz
Checkout our next blog in this python3 learning series to know more about using python language.