Ict Quiz + Assigmnet Programming
Ict Quiz + Assigmnet Programming
Class : C2
Registration no : FA20-BSE-14
Quiz:
Write down the difference between Tuples. Dictionaries, Set and Lists with examples.
There are quite a few data structures available. The builtins data structures are: lists,
tuples, dictionaries, strings, sets and frozensets.
Lists, strings and tuples are ordered sequences of objects. Unlike strings that contain
only characters, list and tuples can contain any type of objects. Lists and tuples are
like arrays. Tuples like strings are immutables. Lists are mutables so they can be
extended or reduced at will. Sets are mutable unordered sequence of unique elements
whereas frozensets are immutable sets.
Lists are enclosed in brackets:
l = [1, 2, "a"]
Tuples are enclosed in parentheses:
t = (1, 2, "a")
In python, lists are part of the standard language. You will find them everywhere.
Like almost everything in Python, lists are objects. There are many methods
associated to them. Some of which are presented here below.
>>> l = [1, 2, 3]
>>> l[0]
1
>>> l.append(1)
>>> l
[1, 2, 3, 1]
In Python, tuples are part of the standard language. This is a data structure very
similar to the list data structure. The main difference being that tuple manipulation are
faster than list because tuples are immutable.
Constructing tuples:
To create a tuple, place values within brackets:
>>> l = (1, 2, 3)
>>> l[0]
1
It is also possible to create a tuple without parentheses, by using commas:
>>> l = 1, 2
>>> l
(1, 2)
If you want to create a tuple with a single element, you must use the comma:
>>> singleton = (1, )
You can repeat a tuples by multiplying a tuple by a number:
>>> (1,) * 5
(1, 1, 1, 1, 1)
Note that you can concatenate tuples and use augmented assignement (*=, +=):
>>> s1 = (1,0)
>>> s1 += (1,)
>>> s1
(1, 0, 1)
Sets are constructed from a sequence (or some other iterable object). Since sets cannot
have duplicated, there are usually used to build sequence of unique items (e.g., set of
identifiers)
For example:
>>> a = set([1, 2, 3, 4])
>>> b = set([3, 4, 5, 6])
>>> a | b # Union
{1, 2, 3, 4, 5, 6}
>>> a & b # Intersection
{3, 4}
>>> a < b # Subset
False
>>> a - b # Difference
{1, 2}
>>> a ^ b # Symmetric Difference
{1, 2, 5, 6}
Dictionary
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is unordered, changeable and does not allow duplicates.
Dictionaries are written with curly brackets, and have keys and values:
Example
Create and print a dictionary:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
Assignment:
Make a calculator program in Python which perform Addition, subtraction, Multiplication,
Division, and square root Functions and send the code file + output screen shot.
print("calculator program")
#input
print("")
S for Subtraction
D for Division
M for Multiplication""")
print("")
result=0
if ch=="A":
result=first_num+second_num
print("Result:", result)
elif ch=="S":
result=first_num-second_num
print("Result:", result)
elif ch=="M":
result=first_num*second_num
print("Result:", result)
elif ch=="D":
result=first_num/second_num
print("Result:", result)
elif ch=="SQ":
result=number**(1/2)
print("Result:", result)
else: