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

Python Assignment 01

Python assignment

Uploaded by

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

Python Assignment 01

Python assignment

Uploaded by

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

Page No.

Date

Name eahuleNshad
stTYB8A -CA
Sul PYBhon

Assiqnment No 01

priotitHes
o1! hai ce

Aclct iona
the

(ollection
bicdionange
ohich ic unord enel
Chanahle indered h python dictinanie, ane
coaite coilh curty bracke and they have keys
nd Walues
Prepentles -
alUnordened 0àctionaniel stoe data ia Key- value
pairs uithogt orde
2)pautable Yoy Cen modify diction o2y ualue, by
key
)ndexed b Keys : You accere iteni uning unique ke yg
)no Daglicak
Daçlcate kos: 0ictionanie
eath key in dicticnany muyt be anique
5Dunamj ize Dictionanie, Can qroa or shink od
needed
JHetogenay
value keysl keys &values Can be of dny dataAye
a)nested Oictionanit,_: 0ictionauie an Contoin othen dciaa
2) Hauhae Keq only irnnutoable tyee cAike ctrings, Nunbens,
taples) Can be dielisnsy ky
(Pago No.
Dst
02
-
q2. Expkuin any taio tuple opeaation ith enanple
hdering iAcessing tuple elemnents by Aheie çaction.
bup:(lo,20,30)
print Ctuplil)
houtput : 20
2)Cancakenation Joining too tuples using the + cpenator.
tup 4: (1,2)
tup 2 (9,)
YeSult :tud t tup2
pnni (oeSult)

# output : 2,3,)
Expleun func tion Arquments iinde taiL
Funcfiana arqument. aNe the ral valu pa ted fo
(ond receiyee by the function
In puthon funcflon aunquments contol hocw oe parr.
data into q function
Aspes of atqunen is i
formad Aradmentsi A funct ion can be definet caled
by using the follocu ing ypes of formal ang antak:
-befault Qunqumen k.
-Requird(Positional) nqumb
Pege No. o3

with Keyund angumtnte a thespeciy the


aahe c qument by ame, making
making the orden o
arqumen t flexible This heles Irnproye code
radabilty & quoid misfakes.
def tatroduceC name, oye)
print ((" My
#Dcing keywod anqumnts
introduce Cag-2a 1nume-"Auinsh')
#outpat: Mynameis Avi noth and lam 25 esns old

This ane panamekens that assume a de fau lt


valye if a walue isn't proyided in the function Call.
This makes paramelens optional
AeE qreet Cname nSg =Hello"):
print(_ffntg Îname|"]
greet Aince) 4 Output: He tlo lice
Artel ("Bob" "H) # Output : Hi, &o6!

ARequired (ositionaJ Aqumentsi


ptnis mua he pcssed in the eract orden
Specifitdby the function They ane requircd, any One

orihing one cill couse


ttun r*y
catirg fun
print lad d(5,3) o4tput :g
Paga No. Oy
|Date

Buil! indictionang function with examele


keysl)iRekunns al keys cliclionang
-ptn Cmyodicit Keysll)
#outputi dict keyclCa'B1)

2) values ()!Retunsal
-Qll va luey

print (ilich valuss ()


# output:dict-valu&s ([1.21])

QSthon prog to. Display Cuent da k e Hlme


inpoti datefime
noy datetime. catetime «nau)
phnt

Anoymous fuxtito ind aneq of feckcungle


anea: lambda Jength, and th length toid th.
print Canta CS,3))
Paçe tio.
Dale

phat ane Aist & fuples whot ane keg difenen ce


betaen
them
List A

A lish ic a mutable, ordered cotlechlon


of items ia Puthon lteme in d lit
Can be mod!fledaffen Chtatioh

ntax : sdit-namer = Pvetu ed, Value 2, veulue a., n ba 4en!


Jist 2+2,3y
2) Tuply: Atuple is_a immu able ordened collectlon.
2
Of items once a tuple ic is created, its
elerments cann ot be chonged.
<tuple-name (yalue3, Vaye 2,s valuen)

Key
tup2 =(l, 2,3/4)
s)offenence bekween t lit R tuples
) Mulability )list is Mutable Cmodlified)
b) Tupples eent Can'! be modi fled
i) syntar B)t) Squane brackets [T for ditt
fiPaonthe gea () for tupley
) Penfofmaonce : a) List Stowen due fo •muka bil.y
W
Tuple: fasten due to iromakau bility
A) Method a is: Has extenske me thelt Ceq:, apendl)
6) Tuple i Limlkd mehods (only oun t, inden)
v) Mem074 a) (ist hoy highen usaqe ar erba men 0ry is aloca bel
Tuple had lowen, au its optm ized for inomafabl'y.
u) Safe ya) list is lers secuae ad ikns Can be ckangd.
b)Tupes is morc secune, Since ifems (ant be al fened,
Paga No. O6
Dats

q2yhonprogto Swap he valua ofToa_Vania&le

print CM Suaped values: az fa? b63


4 output: a :l0b5

Python poq:to Find factos of a qiven


Phon prog num

def find factors (num)


facBors C]
fori in Yange1num1 1)
nùm7 . 0
factozappend(i)
retwn factors
prini(eind factort (12)
#output Ti,2,3, 46,12J
l0Hoo to DRine a Function. in Python 2
ane the ruley to do fine cq function in Petbon:
Aunchioa blockr teq in coith the KeqeuoYd def
followed by the panction name andpananthesest)
2)The iinpt poncmetels OT Aarquments Shouldbe
placed aithin hee pauan the se
3)The firsl statenentof a function can hn be an
optionol shatement the documentat ion Strinq
o he Pynction, or d9c shringng or doCumntohior,.
(Pege No. 07
Date

gfring. i used to erplain in brie ohal


a Pinctlen doe
)The Code block cuit hio QUeny Punction stant
ith a colon C:) and isindented
s) The nelun st tement i wed of exit a
Puncion., A xetn Statement coith no. alqument.
is the Scme C¯ tun None

Synta: def funcioh-name poom 1,_panam2, )


Func tion. docstring"r"
Punction -bady
etn CeKpression
6) By defaqult, pananefens haue a postional behauRu.
Cund you need to inform them in the some
orclerAhat they uene defined. fueny EveNy function

def funhion•NamelJ!

Fenction Name)
Page No.
Date

quython prog to check if.akey exits and Reglatet.


mydict:'a1bi2c!i3
I 5in mydict
my-dit ('ntw .key'] mq-clicd pop'b!)
printl my-dict)
# ouiput a': bi3 'neockey:2
qi 12) slic Sloke on slicing olicionaies
sicing ar usedia lit CInd tuple dasen't
diect apply to diciionanie beCause they ane
Unordeneds
)HoaseveL, you can atate a ne) dichion any by
Qxhiacing specific key- ualue pairs ting a la
o Compe henSion.
) o Pathon Slicing usualy aeknr to detriving4
portioh of Sequencedata trctioe, uiinq a
specificd range of indice
u) Oictionanies Qne unorcdene 4 callection O£ key-value
ue Con't dtcll app slic ing opnadioa
Rkee do cuitn ditt or ShingsCie, utiog Cj rolation
5) Jasitad in dicionanie slicinq: an be undenstood
as cacahing. dichiona Hene ae
(Puge No. o9

3 special opiatog in Aython


Pathon in cludes a rnge of openator bo beyond
4he Standand anithmetic logical openqBor.ne
of thae ane Knocon a Special openaiorsyhlch
penform unique functions and ofeninv olve
manlpulating Sequen ce, Vanialles oCondlHion.
hene ane the _man 44pes oP Special opena t09
in Pathon
) ldentldentity
ky openatur
Openator ane used to Check cuhethen
UO VQuiablesefen to the Same bjetin mêmÓryIn
puthon Aoo vontatle identibs openatorg ane available
a) is Retns True if teaso vaniable point
to the Same obiect
b)_is noB : Retnns True if too_Mo.nio BMe donot
point to the same obiect

print Caiy) t oukup : Tre


print Ca it 2) # Oulputfalse
pnnt Ca is not 2) #Dulput Tut
2) Mem be shie cpenatas
Mem bership. openator! tes F a valu or laniable
exisis within a Sequence CSuch a q list,uple, String, or
didion any).
a) in Relwn True f the speci Fied value
foun d in the scquthce.
5) hot in Retunn hue if he specificd value
i not found in the sequen

Auits aple",baunana", "Chenq]


pintC "apde" in fhuits)
print ("o7anqe" in fruit) # Oulput false
print (" orong' hot in pruik)

Ql4. DSe oft and openafors on Tuples.


)tUsed to Conca ten a ke too typles
Pr tupls (U2)
tup2 (3
-print(tup1 tbup2) t output: (l,2,3, 4)
) Repeak3 the tuple multiple tine

tup I,2)
print ( tup + 3)
QISthan. Progto gemt chanach kens at
Indite

def xemove-addindicea Csl:


een join Cchar fo idz, chor io.

prin t Ccmae-add- indiea Cshing)

Enkn a Sting Hellaonld


shring Aka enioung chanactenr ct odd
Inie Hlohlrd

You might also like