Answer to Question #321084 in Python for Maneo

Question #321084

please find the attached question, https://drive.google.com/file/d/1Xbg3lXrmaz0Y5CMaehYsf-hEeL3bPskx/view?usp=sharing


1
Expert's answer
2022-03-31T11:44:35-0400

a)

Lists (list)

A list in Python is an ordered collection of elements, i.e. you store a sequence of elements in a list. You can think of them as a list of purchases in the store, but with the difference that in your shopping list each item is likely to be in a new line, and in Python they are separated by commas.

Such a list of elements should be enclosed in square brackets. After you have created a list, you can add, delete and search for items in it.

Since we can add and remove data in lists, they are called mutable data types.


Tuples (tuple)

Tuples are used to store multiple objects. In fact, tuples are similar to lists, with the difference that they do not provide the same functionality as lists. Their main difference is that tuples are an immutable data type.


Dictionaries (dictionary)

The dictionary is something like a “notebook” – the address of a person knowing only his name: in the dictionary, the key (key) is associated with the value (value).

Note that all keys in one dictionary must be unique.

In addition, keep in mind that for keys you must use an immutable data type (for example, strings), but at the same time add any data type to values.

Key pair:the value in the dictionary is specified using dict notation = {key1:value1, key2: valu2}.

All keys in the dictionary are stored in an unordered state.

Dictionaries are objects of the dict class.


Sets (set) 

Sets are disordered collections of simple objects, and are used in cases where the presence of an object in a collection is more important than the order or number of occurrences of this element in one collection.

Using sets – you can use membership checks, add and remove elements, combine them, perform a check for belonging to another set, and so on. An element of a set can be any immutable data type – numbers, strings, tuples.


b)

from collections import defaultdict

print("enter information about the customer separated by a space")
customers = defaultdict(list)
for i in range(1, 6):
    info = input("> ").split()
    customers[i].append(info[0])
    customers[i].append(info[1])
    customers[i].append(info[2])

print()
for i in customers:
    print(str(i),":", customers[i][0], customers[i][1], customers[i][2])

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS