0% found this document useful (0 votes)
13 views

Ict Quiz + Assigmnet Programming

The document discusses different Python data structures like lists, tuples, dictionaries, sets and their usage with examples. It also provides an assignment to create a basic calculator program in Python to perform arithmetic operations like addition, subtraction, multiplication, division and square root on input numbers and display the output.

Uploaded by

Aisha Rehman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Ict Quiz + Assigmnet Programming

The document discusses different Python data structures like lists, tuples, dictionaries, sets and their usage with examples. It also provides an assignment to create a basic calculator program in Python to perform arithmetic operations like addition, subtraction, multiplication, division and square root on input numbers and display the output.

Uploaded by

Aisha Rehman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name : Ayesha Rizwan

Class : C2

Registration no : FA20-BSE-14

Subject: introduction to computer technology

Submitted to sir arslan

Date of submission :21-12-2020

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

first_num=float(input("enter the first num "))

print("the first num is:",first_num)

second_num=float(input("enter the second num "))

print(" the second num is:",second_num)

print("")

#choose the operator


print("enter which operation would you like to perform?")

print("""A for Addition

S for Subtraction

D for Division

Sq for Square root

M for Multiplication""")

character=ch=input("enter any of these character for specific operation A,S,Sq,D,M: ").upper()

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":

number = int(input("Enter number: "))

result=number**(1/2)

print("Result:", result)
else:

print("this character is not recognized")

out put screenshots

You might also like