Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python


Download 1.25 Mb.
Pdf ko'rish
bet24/48
Sana30.01.2023
Hajmi1.25 Mb.
#1140552
1   ...   20   21   22   23   24   25   26   27   ...   48
Bog'liq
Coding in Python A Comprehensive Beginners Guide to Learn the Realms

The sorted() Method


To make your loops more interesting, you can add to them the sorted()
method. A dictionary is not in order, therefore you need to bring it up in the
order you want it to be. You can use the sorted() method to make that happen.
officeitems = {'printer' : 'Produced by HP', 'paper': 'A4 type', 'drafting pads':
'It is blank', 'scanner': 'it is hybrid', 'table': 'made of wood', 'table lights': 'They
are LED'}
for items in sorted(officeitems.keys()):
print(items.title() + " has been purchased at a discount price. I hope it will
help earn a handsome amount from the sale of the office")
= RESTART: C:/Users/saifia computers/Desktop/sample.py
Drafting Pads has been purchased at a discount price. I hope it will help earn
a handsome amount from the sale of the office
Paper has been purchased at a discount price. I hope it will help earn a
handsome amount from the sale of the office
Printer has been purchased at a discount price. I hope it will help earn a
handsome amount from the sale of the office
Scanner has been purchased at a discount price. I hope it will help earn a
handsome amount from the sale of the office
Table has been purchased at a discount price. I hope it will help earn a
handsome amount from the sale of the office
Table Lights have been purchased at a discount price. I hope it will help earn
a handsome amount from the sale of the office
>>>
You can see that the result is in perfect alphabetical order.
Nesting
Ever wondered if you can make a dictionary more complex than it already is.
You can nest a long dictionary inside another dictionary. The process is
dubbed as nesting. You also can nest more than one dictionaries in a list. You
can diversify the process of nesting by several methods.
I am going to pack up multiple dictionaries inside a list. Coming to the back,
I will create three different dictionaries about different items of an office and


then cram them all inside a list.
officeitem1 = {'printer' : 'Produced by HP', 'scanner': 'it is hybrid', 'laptop':
'dell'}
officeitem2 = {'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker'}
officeitem3 = {'table': 'made of wood', 'table lights': 'They are LED', 'office
chair': 'boss'}
officeitems = [officeitem1, officeitem2, officeitem3]
for officeitem in officeitems:
print(officeitem)
= RESTART: C:/Users/saifia computers/Desktop/sample.py
{'printer': 'Produced by HP', 'scanner': 'it is hybrid', 'laptop': 'dell'}
{'paper': 'A4 type', 'drafting pads': 'It is blank', 'pen': 'parker'}
{'table': 'made of wood', 'table lights': 'They are LED', 'office chair': 'boss'}
>>>
The three dictionaries denote each section of the office items. One deals with
IT set up, the second denotes stationary while the third section denotes office
furniture.

Download 1.25 Mb.

Do'stlaringiz bilan baham:
1   ...   20   21   22   23   24   25   26   27   ...   48




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling