Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
52 views

Python1 Cheat Codes

This document contains sample code exercises and answers for getting started with programming. It includes 9 coding problems ranging from printing basic shapes to a tip calculator. The exercises cover basic programming concepts like user input, arithmetic operations, formatting output.

Uploaded by

Dejee Galera
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)
52 views

Python1 Cheat Codes

This document contains sample code exercises and answers for getting started with programming. It includes 9 coding problems ranging from printing basic shapes to a tip calculator. The exercises cover basic programming concepts like user input, arithmetic operations, formatting output.

Uploaded by

Dejee Galera
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/ 15

Chapt

er1-Get
ti
ngSt
art
ed

Sampl
eExer
cisesandAnswer
s:

1.
)Pr
intaboxl
iket
heonebel
ow.

*
***
***
***
***
***
***
*
***
***
***
***
***
***
*
***
***
***
***
***
***
*
***
***
***
***
***
***

Answer
:
f
orr
owsi
nr ange(5)
:
forcolumnsinrange(5)
:
pri
nt("*
",end="")
pri
nt()

2.
)Pr
intaboxl
iket
heonebel
ow.

Answer
:
a=10

b=10

d=a-
8

r
=b-
2

i
fa>=1:

pr
int
('
*'
*b)

i
fa>1:

f
ori
inr
ange(
d):
pr
int
('
*'
,
end='
'
)

f
ori
inr
ange(
r):

pr
int
(''
,
end='
'
)

pr
int
('
*'
)

pr
int
('
*'
*b)

3.
)Pr
intat
ri
angl
eli
ket
heonebel
ow.

Answer
:
f
orr
owsi
nrange(
5):

f
orcol
umsi
nrange(
rows)
:

pr
int
("
*",
end=""
)

pr
int
()

512−282
4.
)Wr
it
eapr
ogr
am t
hatcomput
esandpr
int
sther
esul
tof .
Iti
sroughl
y.1017.
47·48+5

Answer
:
a=512

b=282

c=47*
48

d=5

sol
ve=(
(a-
b)/
(c+d)
)

pr
int
(sol
ve)
5.
)Askt heusert
oenteranumber.Pri
ntoutt
hesquareofthenumber,butuset
hesepopt
ional
argumenttoprinti
toutinafullsent
encethatendsinaperiod.Sampleoutputi
sshown
below.

Answer
:
num =ev
al(
input
("
Ent
eraNumber
:")
)

pr
int
("
Thesquar
eof"
,num,
"is"
,num*
num,
".
",
sep="
")

6.
)Asktheusert
oenteranumberx.Usethesepopti
onalar
gumentt
opr
intoutx,
2x,
3x,
4x,
and5x,
eachsepar
atedbythr
eedashes,l
ikebel
ow.

Ent
eranumber
:7
7-
--
14-
--
21-
--
28-
--
35

Answer
:
num =ev
al(
input
("
Ent
eranumber
:")
)

pr
int
(num ,
2*num ,
3*num ,
4*num ,
5*num ,
sep='
-
--'
)

7.
)Wr i
teaprogr
am t
hataskst
heuserf
orawei
ghti
nki
logr
amsandconver
tsi
ttopounds.
Therear
e2.2poundsi
nakil
ogram.
Answer
:
kg=ev
al(
input
("
Ent
eray
ourWei
ghti
nKg:
"))

pr
int
("
Yourwei
ghti
npoundsi
s",
kg*2.
2,"
."
,sep="
")
8.
)Writ
eaprogram t
hataskstheusert oentert
hreenumber s(uset
hreesepar
atei
nputst
ate-
ments)
.Creat
evari
ablescal
ledtotalandaver
aget hathol
dthesum andaver
ageofthet
hree
numbersandpri
ntoutthevaluesoftot andaver
al age.
Answer
:
num1=ev
al(
input
("
Ent
erNumber1:
"))

num2=ev
al(
input
("
Ent
erNumber2:
"))

num3=ev
al(
input
("
Ent
erNumber3:
"))

t
otal
=num1+num2+num3

av
erage=t
otal
/3

pr
int
("
Tot
alnumberi
s",
tot
al,
".
",
sep="
")

pr
int
("
Aver
agenumberi
s",
aver
age,
".
",
sep="
")

9.
)Al otofcellphoneshavet
ipcal
culat
ors.Wri
teone.Asktheuserf
orthepr
iceofthemeal
andtheper centti
ptheywantt
oleave.Thenpri
ntbotht
hetipamountandthetot
albi
llwi
th
theti
pincluded.
Answer
:
meal
_pr
ice=ev
al(
input
("
Ent
erPr
iceofy
ourmeal
:")
)

t
ip_
per
cent=ev
al(
input
("
Ent
erTi
pPer
cent
age:
")
)

t
ip_
amount
=meal
_pr
ice*
(ti
p_per
cent
/100)

t
otal
_bi
l
l=t
ip_
amount
+meal
_pr
ice

pr
int
("
Yourt
ipamounti
s",
ti
p_amount
,"
."
,"AndYourt
otal
bil
li
s",
tot
al_
bil
l
,".
",
sep="
")
Chapt
er2-ForLoops

Exer
cisesandanswer
s:

1.
)Wr
it
eapr
ogr
am t
hatpr
int
syourname100t
imes.
Answer
:
f
ori
inr
ange(
100)
:

pr
int
('
Dej
eeGal
era'
)

2.
)Writ
eapr ogram t
ofi
llt
hescr
eenhori
zontal
lyandver
ti
call
ywithyourname.[
Hint
:addt
he
opt
ionend='i
'nt
otheprntf
i uncti
ont
of i
llt
hescreenhori
zont
all
y.]
Answer
:
f
ori
inr
ange(
200)
:

pr
int
('
Pam Pam'
,end='
')

#i
twor
kswhenanamehassamenumberofl
ett
erseachsi
de

3.
)Writ
eaprogram t
hatoutput
s100lines,
number
ed1t
o100,
eachwi
thy
ournameoni
t.The
out
putshoul
dlookli
ketheoutputbel
ow.

Answer
:
f
ori
inr
ange(
100)
:

pr
int
(i
+1,
'
Dej
eeGal
era'
)
4.
)Writ
eaprogram t
hatpr
int
soutal
i
stoft
hei
nteger
sfr
om 1t
o20andt
hei
rsquar
es.The
out
putshoul
dlookli
ket
his:

Answer
:
f
ori
inr
ange(
1,21)
:

pr
int
(i
,'
--
-'
,i
*i
)

5.
)Wr
it
eapr
ogr
am t
hatusesaf
orl
oopt
opr
intt
henumber
s8,
11,
14,
17,
20,
...
,83,
86,
89.
Answer
:
f
ori
inr
ange(
8,92,
3):

pr
int
(i
,end='
')

6.
)Wr
it
eapr
ogr
am t
hatusesaf
orl
oopt
opr
intt
henumber
s100,
98,
96,
...
,4,
2.
Answer
:
f
ori
inr
ange(
100,
1,
-2)
:

pr
int
(i
)

7.
)Wr
it
eapr
ogr
am t
hatusesexact
lyf
ourf
orl
oopst
opr
intt
hesequenceofl
ett
ersbel
ow.

Answer
:
f
ori
inr
ange(
10)
:

pr
int
("
A",
end="
")

f
ori
inr
ange(
7):
pr
int
("
B",
end="
")

f
ori
inr
ange(
4):

pr
int
("
CD"
,end="
")

pr
int
("
E",
end="
")

f
ori
inr
ange(
6):

pr
int
("
F",
end="
")

pr
int
("
G",
end="
")

8.
)Writ
eapr ogram t
hataskstheuserfort
hei
rnameandhowmanyt i
mestopr
inti
t.The
pr
ogram shouldpri
ntoutt
heuser’snamethespeci
fi
ednumberoft
imes.
Answer
:
y
=input
('
Ent
erYourName:
')

z=ev
al(
input
('
Ent
ert
he#oft
imesy
ouwantt
opr
inty
ourName:
'))

f
ori
inr
ange(
z):

pr
int
(y)

9.
)TheFi bonaccinumbersarethesequencebel
ow,wher et
hefir
sttwonumbersare1,and
eachnumbert hereaf
teri
sthesum ofthetwoprecedingnumbers.Wri
teapr
ogram t
hat
askstheuserhowmanyFi bonaccinumber
stoprintandthenpri
ntst
hatmany.

1,
1,2,
3,5,
8,13,
21,
34,
55,
89...
Answer
:
d=ev
al(
input
('
HowmanyFi
bonacci
Number
syouwantt
opr
int
:'
))

a=1

b=1

f
ori
inr
ange(
d):

a1=a

b1=b

a=b1
b=a1+b1

pr
int
(a1,
end='
')

10.
)Useaforl
ooptopri
ntaboxl
iketheonebelow.All
owtheusertospeci
fyhowwi
deand
howhi
ghtheboxshoul
dbe.[
Hint:pr
int
('
*'
*10)pr
int
stenast
eri
sks.
]

Answer
:
f
ori
inr
ange(
4):

pr
int
('
*'*
19) #i
fneedsspacej
ustdot
his(
'*'
),
dependsonwhatspacey
ouneed.

11.
)Useaforl
ooptopri
ntaboxl
iket
heonebel
ow.Al
lowt
heusert
ospeci
fyhowwi
deand
howhi
ghtheboxshoul
dbe.

Answer
:
pr
int
('
*'*
19)

f
ori
inr
ange(
2):

pr
int
('
* '
*2)

pr
int
('
*'*
19)

#i
fneedsspacej
ustdot
his(
'*'
),
dependsonwhatspacey
ouneed.

#Not
e:17spaces.
12.
)Useaforloopt
opr
intat
ri
angl
eli
ket
heonebel
ow.Al
lowt
heusert
ospeci
fyhowhi
gh
t
hetr
iangl
eshoul
dbe.

Answer
:
n=ev
al(
input
('
Ent
eranumbert
ospeci
fyt
ospeci
fyhowhi
ghy
ouwanty
ourt
ri
angl
e:'
))

f
ori
inr
ange(
n):

pr
int
('
*'*(
i+1)
)

13.
)Useaf orl
ooptopri
ntanupsidedownt
ri
angl
eli
ket
heonebel
ow.Al
lowt
heusert
o
speci
fyhowhighthet
riangl
eshoul
dbe.

Answer
:
n=ev
al(
input
('
Ent
eranumbert
ospeci
fyt
ospeci
fyhowhi
ghy
ouwanty
ourt
ri
angl
e:'
))

f
ori
inr
ange(
n,0,
-1)
:

pr
int
('
*'*
i)

14.
)Useforl
oopstopri
ntadi
amondl
iket
heonebel
ow.Al
lowt
heusert
ospeci
fyhowhi
gh
t
hediamondshoul
dbe.
Answer
:
n=ev
al(
input
("
Ent
eranumbert
ospeci
fyhowhi
ghy
ouwanty
ourt
ri
angl
e:"
))

f
ori
inr
ange(
n):

pr
int
(""
*(n-
i)
+"*
"*(
i+1)
)

f
orj
inr
ange(
n-1)
:

pr
int
(""
*(j
+2)
+"*
"*(
n-1-
j)
)

15.
)Wri
teaprogr
am thatpr
int
sagi
antl
ett
erAl
iket
heonebel
ow.Al
lowt
heusert
ospeci
fy
howl
arget
helett
ershoul
dbe.

Answer
:
n=ev
al(
input
("
Ent
eranumbert
ospeci
fyhowhi
ghy
ouwanty
ourt
ri
angl
e:"
))

f
ori
inr
ange(
0,n)
:

f
orj
inr
ange(
0,n+5)
:

i
f(i
+j==4)or(
j-
i==5)or(
(i
==2)and(
j>1andj
<8)
):

pr
int
("
*",
end="
")

el
se:

pr
int
(""
,end="
")

pr
int
()
Chapt
er3-Number
s

Exer
cisesandanswer
s:
1.
)Wr
it
eapr
ogr
am t
hatgener
atesandpr
int
s50r
andom i
nteger
s,eachbet
ween3and6.

Answer
:

f
rom r
andom i
mpor
trandi
nt

f
ori
inr
ange(
50)
:

x=r
andi
nt(
3,6)

pr
int
(x)

2.)Wr i
teaprogram t
hatgenerat
esarandom numberxbetween1and50,ar
andom numbery
between2and5, andcomputesxy.
Answer :
fr
om random i mpor
trandint
x=randint(1,50)
y=randint(2,5)
comput e=x* *y
pri
nt("
Thev alueofxis:",
x,"
Thevalueofyi
s:",
y,"Theval
ueofx*
*yis",comput
e)

3.
)Wri
teaprogr
am t
hatgener
atesar
andom numberbet
ween1and10andpr
int
syournamet
hat
manyt
imes.
Answer:

4.
)Wri
teaprogr
am thatgenerat
esarandom decimalnumberbet
ween1and10wi
tht
wodeci
mal
pl
acesofaccur
acy
.Examy are1.23,
3.45,
9.80,and5.00.

Answer
:

5.
)Writ
eaprogr
am thatgener
ates50random numberssucht
hatthef
ir
stnumberisbetween
1and2,
thesecondisbetween1and3,t
hethir
disbetween1and4,.
..
,andt
helastis
bet
ween1and51.

Answer
:
6.
)Wr
it
eapr
ogr
am t
hataskst
heusert
oent
ert
wonumber
s, es|x−y|.
xandy,andcomput
f
rom r
andom i
mpor
trandi
nt

num=r
andi
nt(
2,101)

pr
int(
num)

f
lag=0

f
ori
inr
ange(
2,num)
:

i
fnum%i
==0:

pr
int
(i
)

f
lag=1

i
ffl
ag==1:

pr
int
('
Notpr
ime'
)

el
se:

pr
int
('
Pri
me'
)

Tobecont
inued.
..
.Kaykat
ulgonnako

You might also like