Governmentcollegeof Engi Neeri Ngandleather Technology Pythonlabassi Gnment
Governmentcollegeof Engi Neeri Ngandleather Technology Pythonlabassi Gnment
ENGI
NEERINGANDLEATHER
TECHNOLOGY
PYTHONLABASSI
GNMENT
By
Name:Jai
deepPr
asad
St
ream:I
nfor
mat
ionTechnol
ogy
Rol
lNo.
:11200218037
Subj
ect
:Pr
ogr
ammi
ngwi
thPy
thon
Subj
ectcode:PCC-
CS393
Problem 1:Wr
it
eamet
hodwhi
chcancal
cul
atesquar
e
valueofnumber?
Code:
pr
int
(i
nt(
input
("
Ent
eranumber
:")
)**
2)
out
put
:
Ent
eranumber
:5
25.
0
Probl
em 2:Defi
neafuncti
onthatcanrecei
vetwoint
egr
al
numbersinstr
ingfor
m andcomput et
heirsum andt
hen
pri
nti
tinconsole?
Code:
defadd():
x,
y=input(
"Enterval
ues:
").
spl
i
t()
pr
int(
int
(x)
+int
(y))
add()
Output
:
Ent
eranumber
:105
15
Pr
oblem 3:Def
ineafunct
ionthatcanacceptt
wostri
ngsas
i
nputandconcatenat
ethem andthenpri
ntiti
nconsol
e.
Code:
defconc()
:
s1,
s2=st r
(i
nput(
"Ent
ert
wost
ri
ngs:
"))
.spl
i
t()
pri
nt(
s1+""+s2)
conc()
Out
put
:
Entert
wost
ri
ngs:
sodfdog
sodfdog
Probl
em 4:Def i
neaf unct
ionthatcanacceptt
wostri
ngsas
i
nputandpr i
ntthest r
ingwit
hmaxi mum lengt
hinconsol
e.
Ift
wostri
ngshav ethesamelength,thenthef
unct
ion
shoul
dprintallstr
ingsli
nebyline.
Code:
defcomp( )
:
st
ring1=st r
(i
nput
("
Enterfir
ststr
ing:")
)
st
ring2=st r
(i
nput
("
Entersecondst r
ing:"
))
i
flen( st
ri
ng1)==len(st
ri
ng2):
print(
str
ing1+"\
n"+str
ing2)
el
iflen(str
ing1)>l
en(str
ing2):
print(
str
ing1)
el
se:
print(
str
ing2)
comp( )
Out
put
:
Ent
erfi
rstst
ri
ng:asd
Ent
ersecondstr
ing:
kdv
asd
kdv
Probl
em 5:Defi
neafuncti
onthatcanacceptani
nteger
numberasinputandpri
ntthe“Iti
sanevennumber”ifthe
numberi
sev en,ot
her
wiseprint“I
tisanoddnumber”.
Code:
defeven_odd():
x=int(i
nput("
Ent
eranumber:"
))
i
fx%2==0:
pri
nt(
"“I
tisanevennumber
")
el
se:
pri
nt(
"I
tisanoddnumber")
even_odd()
Out
put
:
Enteranumber
:5
“I
tisanoddnumber
Pr
oblem 6:Defi
neafunct
ionwhichcanprintadict
ionar
y
wherethekeysarenumbersbetween1and3( both
i
ncluded)andtheval
uesaresquareofkey
s.
Code:
defdic()
:
d=dict
()
forxinrange(
1,4)
:
d[x]
=x**2
pri
nt(d)
di
c()
Out
put:
{
1:1,2:
4,3:
9}
Pr
oblem 7:Defi
neafunct
ionwhichcanprintadict
ionar
y
wherethekeysarenumbersbetween1and20( both
i
ncluded)andtheval
uesaresquareofkey
s.
Code:
defdic():
d=dict(
)
forxinrange(
1,21)
:
d[x]
=x**2
pri
nt(d)
di
c( )
Output:
{1:1,
2:4,3:9,4:16,5:25,6:36,7:49,8:64,
9:81,10:100,11:121,12:144,13:169,14:196,
15:225,16:256,17:289,18:324,19:361,20:400}
Problem 8:Defi
neaf unct
ionwhi
chcangenerat
eandpri
nt
alistwherethevaluesaresquar
eofnumbersbetween1
and20( bothincl
uded).
Code:
defprintVal
ues():
l=list
()
foriinrange(1,21)
:
l.
append(i*
* 2)
pri
nt(l
)
pri
ntValues()
Output:
[
1,4,9,16,25,
36,49,64,81,
100,121,
144,
169,
196,225,256,
289,324,361,400]
Pr
obl
em 9:Wi
thagi
vent
upl
e(1,
2,
3,
4,
5,
6,
7,
8,
9,
10)
,wr
it
ea
progr
am topri
ntt
hef i
rsthal
fval
uesi
nonel
i
neandt
hel
ast
halfv
aluesi
noneline.
Code:
t=(1,
2,3,
4,5,
6,7,
8,
9,
10)
pr
int(
t[
0:5]
)
pr
int(
t[
5:11])
Out
put
:
(
1,2,
3,4,
5)
(
6,7,
8,9,
10)
Probl
em 10:Wri
teaprogr
am whi
chcanmap(
)tomakea
l
istwhoseelement
saresquar
eofel
ement
sin
[
1,2,
3,
4,5,
6,7,
8,
9,
10].
Code:
defsquare(n):
retur
nn* *
2
number s=[ 1,2,3,
4,5,6,
7,8,9,10]
resul
t=map( square,
numbers)
print
(l
ist
(result)
)
Output
:
[
1,4,9,16,
25,
36,
49,
64,
81,
100]
Pr
oblem 11:Wri
teaprogr
am whichcanmap()andf
il
ter
()t
o
makealistwhoseel
ementsaresquareofev
ennumberin
[
1,2,
3,
4,
5,6,7,
8,
9,
10].
Code:
seq=[ 1,2,3,4,5,6,7,8,9,
10]
resul
t=f il
ter(
lambdax: x%2==0,
seq)
defsquar e(n)
:
ret
urnn* *2
res=map( square,
resul
t)
print
(l
ist(
res))
Output
:
[
4,16,36,
64,
100]
Pr
obl
em 12:Wr
it
eapr
ogr
am whi
chcanf
il
ter
()t
omakea
l
istwhoseel
ement
sar
eev
ennumberbet
ween1and20
(
bothincl
uded)
.
Code:
res=fi
lter
(l
ambdai
:i%2==0,
range(
1,21)
)
print
(l
ist(
res)
)
Output
:
[
2,4,6,8,
10,
12,
14,
16,
18,
20]
Probl
em 13:Def
ineaclassnamedCircl
ewhichcanbe
constr
uct
edbyar adi
us.TheCir
clecl
asshasamet hod
whichcancomputethearea.
Code:
cl
asscircl
e:
print
(3.14*
int
(i
nput
("
Ent
ert
her
adi
usoft
heci
rcl
e:"
))*
*2)
ci
rcl
e()
Out
put
:
Ent
ert
her
adi
usoft
heci
rcl
e:5
78.
5
Probl
em 14:Defi
neaclassnamedRectangl
ewhichcanbe
const
ruct
edbyal engt
handwidth.TheRect
angl
eclasshas
amethodwhi chcancomputethearea.
Code:
cl
assRect angle()
:
def__ i
nit__(
self,
l,w) :
self.
length=l
self.
wi dth=w
defrect angl
e_area( self
):
returnself.l
ength* sel
f.width
newRect angle=Rect angle(12,10)
pri
nt(newRect angle.r
ect angle_area(
))
Out
put
:
120
Probl
em 15:Defi
neacl assnamedShapeandi t
ssubclass
Square.TheSquareclasshasaninitf
unct
ionwhichtakesa
l
engthasar gument.Bothcl
asseshaveanareafunct
ion
whichcanpr i
ntt
hear eaoftheshapewhereShape'
sarea
i
s0bydef
aul
t.
Code:
cl
assshape:
def_init
_(self
,ar)
:
self
.ar=0
defarea(self)
:
pri
nt(ar)
cl
assSquar e(shape):
def_init
_(self
,l)
:
self
.l
=l
defarea(self)
:
ar=l
**2
pri
nt(ar)
l
=int(i
nput("Enterlengt
h:"
))
x=Square()
x.
area()
Output
:
Ent
erlengt
h:8
64
Probl
em 16:Wr i
teaprogr
am whichcancomput et
he
fact
ori
alofagivennumbers.Theresul
tsshouldbepr
int
ed
i
nacomma- separat
edsequenceonasinglel
ine.
Code:
#quest ion16
a=[]
b=[]
num=i nt (
input("
Enterthenumberofinput
s:"
))
foriinr ange(num):
a.append( int(
input
("Enterthenumberwhose
factorialvalueistobepr inted:
"))
)
deff act(a):
b=[]
foriina:
f=1
forjinr ange(1,i
+1):
f*=j
b.append( f)
returnb
b=fact (a)
forii
nb:
pri
nt(
i,
end="
,"
)
Output:
Enterthenumberofinput
s: 3
Enterthenumberwhosef actor
ial
val
uei
stobe
pri
nted:8
Enterthenumberwhosef actor
ial
val
uei
stobe
pri
nted:9
Enterthenumberwhosef actor
ial
val
uei
stobe
pri
nted:12
40320,362880,
479001600,
Problem 17:Wr i
teaprogram thatcal
cul
atesandpri
ntst
he
valueaccordingtothegivenformula:
Q=Squar erootof[(
2*C*D) /H]
Followi
ngar ethefi
xedv al
uesofCandH:
Cis50.Hi s30.
Dist hevariabl
ewhosev aluesshouldbeinputt
oyour
program inacomma- separ at
edsequence.
Code:
i
mpor tmat h
val=input (
"Ent ersomecommaseper
atedi
nput
s:"
)
l
=[]
dup=[ ]
l
ist=v al.
spli
t(",")
fori i
nl i
st:
l
.append( i
nt(i))
deff n(n,dup):
val=int(mat h.sqrt(
(2*
50*
n)/
30)
)
dup.append( val)
fori i
nl :
fn(i
,dup)
fori i
ndup:
pri
nt (i
,end=" ,
")
Output:
Ent
ersomecommaseper
atedi
nput
s:100,
150,
180
18,
22,24,
Probl
em 18:Withagiv
enintegr
alnumbern,wri
tea
progr
am togenerat
eadicti
onaryt
hatcont
ains(i
,i
*i)such
thati
sanintegr
alnumberbetween1andn( bot
hincluded)
andt
hent
hepr
ogr
am shoul
dpr
intt
hedi
cti
onar
y.
Code:
n=i nt(
input
("
Enterno.ofi
nput
s="
))
d=di ct()
foriinrange(
1,n+1)
:
d[i
]=i*i
pri
nt(d)
Output:
Enterno.ofi
nputs=8
{1:
1, 2:4,
3:9,4:
16,5:25,
6:36,
7:49,
8:64}
Problem 19:Wri
teaprogr
am whi chtakes2digit
s,X,
Yas
i
nputandgener atesa2-
dimensionalarray
.Theelement
valueinthei-
throwandj-
thcolumnoft hearrayshoul
dbe
i
*j.
Code:
i
nput _
str=i nput()
dimensions=[ i
nt(x)forxi ninput_
str
.spli
t(
',
'
)]
rowNum=di mensi ons[ 0]
colNum=di mensi ons[ 1]
multil
i
st=[ [0f orcolinr ange( col
Num) ]f
orrowi
n
range(r
owNum) ]
forrowinr ange( r
owNum) :
forcolinrange( colNum) :
multil
i
st[r
ow] [col]
=r ow* col
pri
nt(multi
list)
Output:
3,5
[[
0,0,0,0,0] ,
[0,1,2, 3, 4],[
0,2,4,6,
8]]
Problem 20:Wr i
teaprogr
am thatacceptsacomma
separatedsequenceofwor dsasinputandprintst
hewor
ds
i
nacomma- separat
edsequenceaftersort
ingthem
alphabeti
call
y.
Code:
i
tems=[xforxini
nput(
"ent
erdatat
osort
")
.spl
i
t('
,
')
]
i
tems.sort
()
pri
nt("
Sorteddat
aare",
',
'
.j
oin(
it
ems))
Out
put:
Ent
erdat
atosortwi
thout,
hel
lo,
bag,
worl
d
Sor
teddat
aarebag,
hell
o,wi
thout
,wor
ld
Probl
em 21:Wri
teapr ogram whichaccept
sasequenceof
commasepar at
ed4di gitbi
narynumbersasitsi
nputand
thencheckwhethertheyaredivi
sibl
eby5ornot.The
numbersthatar
edi v
isibl
eby5ar etobeprint
edina
commasepar at
edsequence.
Code:
i
tems=[ ]
num =[ xforxininput
().
spl
i
t('
,
')
]
forpinnum:
x=int(p,2)
i
fnotx%5:
i
tems.append( p)
pri
nt(
',
'
.joi
n(it
ems) )
Output
:
0100,
0011,
1010,
1001
1010
Probl
em 22:Writ
eaprogr
am,whichwil
lfi
ndallsuch
numbersbetween1000and3000(bot
hincluded)sucht
hat
eachdigi
tofthenumberi
sanevennumber .
Code:
values=[]
forxinr ange( 1000,
3000)
:
s=str(x)
i
f(int(s[0])%2==0)and( int
(s[
1])%2==0)
and( i
nt(s[2])%2==0)and( int
(s[
3])%2==0)
:
values.append( s)
pri
nt( ",
".j
oin(v
alues)
)
Output:
2000,
2002,2004,
2006,
2008,
2020,
2022,
2024,
2026,
2028,
204
0,
2042,2044,
2046,
2048,
2060,
2062,
2064,
2066,
2068,
2080,
2
082,
2084,2086,
2088,
2200,
2202,
2204,
2206,
2208,
2220,
2222
,
2224,2226,
2228,
2240,
2242,
2244,
2246,
2248,
2260,
2262,
22
64,
2266,2268,
2280,
2282,
2284,
2286,
2288,
2400,
2402,
2404,
2406,
2408,2420,
2422,
2424,
2426,
2428,
2440,
2442,
2444,
244
6,
2448,
2460,
2462,
2464,
2466,
2468,
2480,
2482,
2484,
2486,
2
488,
2600,
2602,
2604,
2606,
2608,
2620,
2622,
2624,
2626,
2628
,
2640,
2642,
2644,
2646,
2648,
2660,
2662,
2664,
2666,
2668,
26
80,
2682,
2684,
2686,
2688,
2800,
2802,
2804,
2806,
2808,
2820,
2822,
2824,
2826,
2828,
2840,
2842,
2844,
2846,
2848,
2860,
286
2,
2864,
2866,
2868,
2880,
2882,
2884,
2886,
2888
Code:
a=input(
)
n1=int("
%s"%a)
n2=int("
%s%s"%( a,
a))
n3=int("
%s%s%s"%( a,
a,
a))
n4=int("
%s%s%s%s"%( a,
a,
a,
a))
pr
int
(n1+n2+n3+n4)
Output
:
9
11106
Probl
em 24:Useali
stcomprehensi
ontosquareeachodd
numberinalist
.Thel
isti
sinputbyasequenceofcommasepar
ated
numbers.
Code:
#quest ion24
values=i nput(
)
number s=[ xforxi
nvalues.
spl
i
t("
,"
)if
i
nt (x)
%2! =0]
print(
",
".join(
numbers)
)
Out put:
1,2,3,
4,5,6,7,
8,9
1,3,5,
7,9
Probl
em 25:Wri
teaprogram thatcomputesthenetamount
ofabankaccountbasedat r
ansactionl
ogfrom console
i
nput.Thetr
ansact
ionlogformatisshownasf oll
owing:
D100
W 200
Dmeansdepositwhil
eW meanswi thdr
awal.
Code:
net_amount=0
whileTrue:
str=input( "
Entertransacti
on:")
tr
ansaction=st r.
split
("")
ty
pe=t r
ansaction[0]
amount=i nt(t
ransaction[1])
i
ft y
pe=="D"ort ype=="d":
net_amount+=amount
el
iftype=="W"ort ype=="w" :
net_amount- =amount
el
se:
pass
str=input( "
wanttocont i
nue(Yforyes):
")
i
fnot( st
r[0]=="Y"orst r
[0]=="y"
):
break
pri
nt("Netamount :",
net_amount )
Output
:
Ent
ertransacti
on:D300
wanttocontinue(Yfory
es):
Y
Ent
ertransacti
on:D300
wanttocontinue(Yfory
es):
Y
Ent
ertransacti
on:W 200
wanttocontinue(Yfory
es):
Y
Ent
ertransacti
on:D100
wanttocontinue(Yfory
es):
N
Netamount :500
Code:
i
mpor tre
passwor ds=i nput("Ty pein:")
passwor ds=passwor ds.spli
t("
,"
)
accept ed_pass=[ ]
fori i
npasswor ds:
i
fl en(i
)<6orl en(i
)>12:
cont i
nue
eli
fnotr e.search("
([
a- z])
+",i
):
cont i
nue
eli
fnotr e.search("
([
A- Z])+",
i):
cont i
nue
eli
fnotr e.search("
([
0- 9])
+",i)
:
cont i
nue
eli
fnotr e.search("
([
!@$%^ &])+",
i):
cont i
nue
else:
accept ed_pass.append( i)
print(
("").
join(
accept ed_pass) )
Output:
Typein:ABd1234@1,
aF1#,
2w3E*
,2We3345
ABd1234@1
Probl
em 27:Youar erequir
edtowriteaprogram t
osor tt
he
(name, age, hei
ght)tuplesbyascendingorderwherename
i
sst r
ing,ageandhei ghtarenumbers.Thet upl
esareinput
byconsol e.Thesor tcri
teri
ais:
1:Sortbasedonname;
2:Thensor tbasedonage;
3:Thensor tbyscore.
Thepr i
orityisthatname>age>scor e.
Code:
fr
om oper atori
mportitemgett
er
people_ i
nfo=[ ]
whileTr ue:
i
ndividual_info=input(
)
i
findividual_inf
o==" "
:
break
el
se:
people_ i
nfo.append(t
uple(
(i
ndivi
dual
_i
nfo.
spl
i
t("
,"
))
))
peopl
e_i
nfo.
sort
(key=i
temget
ter
(0,
1,2)
)
pri
nt(
peopl
e_i
nfo)
Output:
Tom,19,80
John,20,
90
Jony,
17,91
Jony,
17,93
Json,21,
85
[
('
John','
20','
90'
),
('
Jony'
,'
17',
'91'
),
('
Jony
',
'
17','
93')
,('
Json'
,'
21'
,'
85'
),(
'Tom','
19'
,
'
80')
]
Problem 28:Def
ineaclasswit
hagenerat
orwhi
chcan
i
teratethenumbers,whichar
ediv
isi
bleby7,
bet
weena
gi
v enrange0andn.
Code:
classgener ator:
def_ init
_(self,
mi ni
=0):
self.mini=mi ni
defgenSev en( self
,max)
:
self.max =max
n=7
whi l
e(n<self.max) :
yieldn
n+=7
x=gener ator (
)
num=i nt
(input("Enteral
imit
:")
)
fori i
nx. genSev en(num):
print(i
)
Output
:
Ent
eralimi
t:50
7
14
21
28
35
42
49
Pr
obl
em 29:Ar
obotmov
esi
napl
anest
art
ingf
rom t
he
originalpoint(0,
0) .Ther obotcanmov etowar dUP,DOWN,
LEFTandRI GHTwi thagi vensteps.Thet r
aceofr obot
mov ementi sshownast hefoll
owing:
UP5
DOWN3
LEFT3
RIGHT2
Thenumber safterthedi recti
onarest eps.Pleasewrit
ea
progr am tocomput et he
distancef rom currentposi ti
onafterasequenceof
mov ementandor iginalpoint.I
f
thedi stanceisaf loat, t
henjustprintthenearestint
eger.
Exampl e:
Ifthef oll
owingt uplesar egivenasi nputtotheprogram:
UP5
DOWN3
LEFT3
RIGHT2
Then, t
heout putoft hepr ogram shouldbe:
2
Code:
pos={
"x"
:0,
"y"
:0
}
whileTr ue:
l
ine=i nput (">")
i
fnotl ine:
break
dir
ect i
on, steps=l i
ne.
spli
t(
)
i
fdirect i
on==" UP" :
pos["y"]+=i nt(steps)
eli
fdirection==" DOWN" :
pos["y"]-=int(steps)
eli
fdirection==" LEFT":
pos["x"]-=int(st eps)
eli
fdirection==" RIGHT":
pos["x"]+=i nt(st eps)
pri
nt( i
nt(round( (pos["
x"]
**2+pos[
"y"
]**
2)*
*0.
5))
)
Output :
>UP5
>DOWN3
>LEFT3
>RIGHT2
>
2
Probl
em 30:Def
ineaclass,
whichhaveacl
asspar
amet
er
andhaveasamei nst
anceparameter
.
Code:
cl
asssampl eclass:
count=0
defincrease(self)
:
sampl ecl
ass.count+=1
s1=sampl eclass()
s1.
increase()
pri
nt(s1.count )
s2=sampl eclass()
s2.
increase()
pri
nt(s2.count )
pri
nt(sampl eclass.count
)
Out
put
:
1
2
2