In: Computer Science
Describe two situations in which you would use a list; two situations in which you would use a tuple, and two situations in which you would use a dictionary. Also do some research to learn about data structure sets, describe when and how to use sets.
# List :
@ A list is an ordered set of data/element that can be easily changed .
@ List is a collection of elements of different data types.
@ A list object contains one or more elements ,it may or may not be of the same type, which are separated by comma(,) and enclosed in square brackets [].
@ List items are refered by the index number. we can
use index operator [ ]
to access an item in
a list.
@ List allows duplicate elements .
@ Lists are like arrays .
Examples :
1> List can have similary types of Elements like NAMES
names=["john", "Bob", "Steve", "Maya"]
2> List can also contain elements of different types like a STUDENT ID .
Item=[1, "John", "Computer", 75, class1]
# Tuple :
@ A tuple is an ordered set of data that cannot be changed . Tuple allows duplicate elements .
@ Tuples cannot be modified after it is created. which means Tuples are "immutable" .
@ Tuples are created by a sequence of values separated by ‘comma’
it can be with or without the use of () .
@ Tuples may contain any number of data/elements and of any datatype .
Examples :
1> Tuples can be used to store collection of fruits
fruits=("apple", "pear", "banana", "cherry")
2>Tuples can also store details of product .
Item=(100, "mango ", "habus", 15.50, good)
# Dictionary :
@ Dictionaries are group of data/elements with key and value pairs 'key':'value' .
@ Dictionary is an unordered collection of data values .
@ It is used to store data values like a map .
@ key-value pair in a Dictionary is always separated by a colon
:
.
@ Dictionary don't allow duplicate elements.
Examples :
1> Point in the plane
points={"p1":(30,30), "p2":(60,60)}
1> Dictionary can also store countary and capital value pairs
countary={"USA":"New York", "UAE":"Abu-dhabi", "India":"New
Delhi"}
# Set :
@ Set is an unordered collection of data type.
@ Set don't allows duplicate elements/data .
@ A set object may contains one or more items, not necessarily of the same type. which are always separated by comma (,) and enclosed in curly brackets {}.
Examples :
1>Set can be used to store collection of numbers.
Input :
>>> Z1={1, 2, 2, 3, 4, 5}
>>> Z1
output: {1, 2, 3, 4, 5}
1>Set can be used to store ID of an employee.
>>> ID={1, "Bob", 101}