Python | List vs Dictionaries

List:

The list is a linear data structure list that can store heterogeneous data that is you can store integer value float value string in one list in the list we use square brackets to create a list. without any element is called an empty list in the list we use the index to access the data.

Example:

daily_sells = (100000, 150000,200000,220000,250000,300000,400000)
day = input("enter 'sun','mon','tue','wed','thur','fri','sat':")

if day == 'sun':
    dayname = 'sunday'
    sell = daily_sells[0]

elif day == 'mon':
    dayname = 'monday'
    sell = daily_sells[1]
elif day == 'tue':
    dayname = 'tuesday'
    sell = daily_sells[2]
elif day == 'wed':
    dayname = 'wednesday'
    sell = daily_sells[3]
elif day == 'thur':
    dayname = 'thursday'
    sell = daily_sells[4]
elif day == 'fri':
    dayname = 'friday'
    sell = daily_sells[5]
elif day == 'sat':
    dayname = 'saturday'
    sell = daily_sells[6]
else:
    print("wrong input")
print("the sells for",dayname,"was",sell,"rupees")

Output:

enter 'sun','mon','tue','wed','thur','fri','sat':mon
the sells for monday was 150000 rupees

Dictionary:

Dictionary is an associate data structure in the dictionary we mention data with their keys ..to access the data we use the key with the help of key we use the data.

Example:

daily_sells = {'sun':100000,'mon':150000,'tues':200000,'wed':220000,'thur':250000,'fri':300000,'sat':400000}
dayname = {'sun':'sunday','mon':'monday','tues':'tuesday','wed':'wednesday','thur':'thurday','fri':'friday','sat':'saturday'}
day = input("enter 'sun','mon','tue','wed','thur','fri','sat':")
print("the sells for",dayname[day],"was",daily_sells[day],"rupees")

Output:

enter 'sun','mon','tue','wed','thur','fri','sat':wed
the sells for wednesday was 220000 rupees

Happy Coding!

Follow us on Instagram @programmersdoor

Join us on Telegram @programmersdoor

Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.

Follow Programmers Door for more.

#blog #interview #placement #learn #computer #science

24 views

        Contact Us

programmersdoor@gmail.com

  • LinkedIn
  • Facebook
  • Instagram

©2023 by Programmers Door