0% found this document useful (0 votes)
14 views12 pages

Adobe Scan 01 Feb 2025

The document provides a detailed implementation of pseudocode for working with tuples and dictionaries in Python. It covers defining, accessing, slicing, iterating, checking existence, concatenating, unpacking tuples, as well as adding, updating, removing key-value pairs, and performing operations on dictionaries. The output demonstrates the results of these operations, including error handling for immutable tuples.

Uploaded by

Hamidulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Adobe Scan 01 Feb 2025

The document provides a detailed implementation of pseudocode for working with tuples and dictionaries in Python. It covers defining, accessing, slicing, iterating, checking existence, concatenating, unpacking tuples, as well as adding, updating, removing key-value pairs, and performing operations on dictionaries. The output demonstrates the results of these operations, including error handling for immutable tuples.

Uploaded by

Hamidulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Assigmert-3 Kttamidalla

2024o5 oSu3
Q1. Implement tthe psuedo code belo:
gien
tuple DEFINE tuple 1 As Tuple
START # step 1: Define a

(CONTAINING elements: ,2, 3, 4,5)#

Step 2: Access tuple elements PRINT tude 1 [o]# Access


first elerment PRINT tuple 1 [] #Acce ss last element
# step3: Slicin
sicing taple s DEFINE sub.tuple As SLICE tuplel
FROM index 1 To 33 PRINT subtuple
tuple FOR each elemeut IN
stept: Iteratinq OVe Y a

tuple1 PRINT element END FOR

#step 5: Check an tuple


element exists tn
DEFINE checkvalue As (F ehek- value IN taple 1
PRINT Value found in tuple"ELsE PRINT"Value not tound
in tuple" END IF
tuple2 As
#step 6: Concatenate two tuples DEFINE
Tuple CcoNTAINING elements: G,7, 8) DEFINE concatenatedtuple
As tuple 1+ tuple 2 PRINT concatenatedtuple
t tep 7: Tuple unpacking DEFINE Ca,b,c)As Tule (CoNTANNe
elenerts: ID,20, B0) PRINT a,b, c
# step e: Using tuples function DEFINE FUNCT
returntu ple ) RETURN Tupe (CoNTAINI NG elements:"Pthon'",
TupleExample")END FONCTlON CALL return tuplel)
AND STORE RESULT tuple33
IN tuple PRINT tuple3
# step : Tuples mutasle, demonstrate immutabil
TRY SET tuple1[o]To lo CATCH Error As "Tuples are
immutable" PRINT Error
Ev rOY END TRY END

Code:
# step1
tuple 1 =(,2, 3, 4, 5);
tstep 2
print tuple 1 [e
print (tuee 1[-1);
#step3
Sub-tuple tupe 1[1:3;
print (sub-tuple);
#step 4
for elements in tuple1:
print (elements)
#step5
checkValue- 3)
if check. value in tuple 1:
print ("Value found in tupl):
else:
print (Value not found in tuple ):
# step
tuple 2 = (c,7,8);
Con can tenated tuple =tuplettuple
print (concantenatedtue):
# Step 7
a,b, c ((0, 2b.3o);
=

print (a,b,):
4 step8
def
returntuple():
return (Pthon',Tuple","Eyample");
tuple 3 return-tuple();
print (tuple ;
#step9

tuple 1[o] =0)


except TypeError
print CTudes are immutaHe"):

Dut put:
1

(2,3)

Value foud in tuple


2,3, 4,5,6,7,8)
lo 20 30

(Pgthon' Tuple',E Kanmple)


Tuples are immtable
Q2:|nplement the Pseuedocode
given belouw
START
# step 1:Define
dictionary
DEFINE dicl As Dictiovaru (coNTAINING key-Value pairs: fhan
e
Al:ce", "age";25,'ctu"New York")
#stee 2: Acce ss dictionary elemets
PRINT dit1 name# Acce ss value by key
PRINT did 1get(age") # stely value by key
acce SS

#step 3: Add neu


key-value pair
SET dit 1 atryTo "uSA
# step: Update ecisting key
an -value pair
key-value
SET dit1cityTo`Los Angeles t/

#sta 5: Remove key-value Pair


REMOVE oge FROM dict USING del

PRINT dict s

#tep 6: Check f a key exists


DE FINE key -to check As nane!

IF key to check IN dict1


Key exisls "
PRINT Key
EL SE
PRINT Key does not exist"

END IF
# Step 7: lterating
PRINT keys;"
dictionary
FoR each key IN dit1.keys)
PRINT key
END FOR

PRINT "Values: "


FoR each value IN dt1.aues()
PRINT value
END FOR

PRINT Key- Value pairs:


FoR eack key, Vale IN dit1.tems
PRINT key. Value
END FOR

#step 3: Copy dictionary


DEFINE dict 2 AS did1.copy)
#step 9: Cleas
CALL dit1.clea0
clearl)
dictonary
RINT diti # sheuld prit an
empty dictionavy
#step (0: Dictionar mthads
# Mee tux dictionaries
DEFNE dit3 As Ddionary (CoNTAINNG key- Va lue pairs;
{"hosby":"eadrgqe
DEFNE merqeddicl
ao3)
AS diet 2)dict 3 #Usp | opetator
fo
meging
PRINT mesged dict

#
Using set defaH to add key oth dofaut value
SET dfaut_ value USING dict .st defauHC'gender "Female)
PRINT dicte

# Get and remoVe key-vaue pair


DEFINE removed value As dict3op("age )
PRINT vemoved alue
PRINT dict 2

# Pop the Last inserted item


DEFINF last item As did2popitem
PRINT last item
PRINT dict
t step ||: Nested dietionaries
DEFINE ne sted dit As Ditiona ry (CoNTAINN G key-value pairs:

per son 1:name":"Altce"


age": 25j,
persen": {"name'":"Bob',aqe: 3o3
)
PQINT nested- dit
Parson2[hame?
#step |2: Dietionary comprehension
DEFINE Square-did As Dictionary CREATED FROM: FOR i IN
RANGE To 6) Doi:)
PRINT Sqare -dict
E ND
Code:
+step1
dit
1name":"Akce", "age': 25,'city':"New Yorky;
#step 2
Print (dict1name");
print (dict 1-getlage");
#step3
di ct 1'country="'usA";
# step 4
dict 1city]="Los Angele s";
# step5
del dict 1 age:
Print (dict 1);
# step t
key-to- checkname:
f key- to- check in dicti:
print ("key exist");
else
Print key does not exist"):
# step7
print ("keys: );
for key in dicti:keys):
Print (key):
print "Values:"):
for Value in did1.alue s();
print (Value):

Pt (Key-Yalo paits:)
fo key, value in dict1.tems):
print key Nalue);

dict 2 zdid 1.copy();


#step 9
dict 1.clearl);
print (dict 1);
tep lo
dit 3-
f"hobby':"reang','age"
tnerqed dictdict 2ldict3; ..
:30
print (rmeqed- dict);
dic = mesqed -dict

defatvalue: dict2. set detaut (gende', "Fema


print (dict );
temoved value dicl 2 pop(age")
Print (removed- value);
Print (dit):

lastitem dite popitem);


Print (last_item);
print (did 2))
#stepll
nested. dict-f
per
son1":{name' :"Alice", "age": 25
person2":{"name':"Bob',"e":363

print (hested -diResson1name


# stepl2
Square.did-{i:i*: for ' in Yange l6);
Print (Sqe- did);
Output'
Alice
25

{'name': Alice', 'cit: 'Los Avaeleb',


Key exist 'coumtry':'UsAY
Keysi
name

city
countiy
Values:
Alice

Los Angeles
USA
Key-Value pairs:
name Alice
City Los Angeks
County USA

lrame': 'Alice, 'city';' Los Argeles',


'age': 30 county' : USA', 'hobky: readig
tname':'Alce', 'city':'Los Angele s, 'county':'USA. 'hobby':readig'
oge: 30, 'gender': Female'?
30
'name': Alice','eity':Los Angeles, 'county':'usA, 'hobbay':
readirg', gender' :Female'
fgoncFemale
('3ender', 'Female)
'nAlice
ame':'Aice', 'ety'Los Angeles, county: 'ustA. 'hobky'reoin
1:1, 2: 4,3: 9,4: I6, 5: 259

You might also like