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

Prem Ass 2 Python

The document provides an overview of Python, including its definition, major uses, and differences between Python 2.x and 3.x. It discusses modules and packages, mutable vs. immutable types, and various control flow statements like loops and conditional statements. Additionally, it includes examples of Python programs demonstrating these concepts.

Uploaded by

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

Prem Ass 2 Python

The document provides an overview of Python, including its definition, major uses, and differences between Python 2.x and 3.x. It discusses modules and packages, mutable vs. immutable types, and various control flow statements like loops and conditional statements. Additionally, it includes examples of Python programs demonstrating these concepts.

Uploaded by

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

Prern chand 2t )4c0NoI066

Python Asigonent-2 JIerg


python.who
wha t is Python? lst al least tee rajor wes af ore sorne
what
Ccaled python ,and when uas 4 fnst r eleased ?
Key diffeve rces be twe en Ht hon2.x ard yiton. longuage
Pyihon is o high-fevel ; tnterprcled progiornirg
known for fts sfmplicty ond Teadobli ty. Jt (:wiey Used fn
exienstve ifbrastes
vafous dornafns due to tts flextbility ord
Major Ses of python
and Flask erable
I· Webdevelopment : Frame worics Ifke Django web applications.
lhe developroent of scaable
9.Data sctene and Ma chìne Leor ing:
Lto ortes lie ordas, Nunpy TenotFlow
and ScíEit-lecnn help In data analysis ond
Al developrnent
3- Automohion ond scriptirg
Python Is used for autGrnatfng epetttt ve
tasks IfIce web sCraptng, File hondlig,or
task schedultg
Python was crecaBed by Guido an Rossum ond wasfiist Telea

Key difference Betueen Mthon 2xond pthon 3.X.


yBhon 3 uses printC) as a function, whie pthon 2 ed i
a staement
Inleger divsion in Python 3 Teturns a f loat, whbere as îr
python 2, t Tetuins an inieger.
Python 3 Suppror ts better Unicode harding, rakg itm
suftable for modren oppications.

D) Whotis a module in python? proNtde an exam ple of how


impot and use a modUle. EXpPlath the diffe ence be tween

a mocule ond apackage n rython.


4 modlule n python is afrle contatning Python C
n o ther
Cfunchams,Vanigbl es, ax classes) that can be meused
progioms.
Exam plc:
mpot mat b
foutpu i4o
p tntCmath. sqit(l6))
biffere nce betwe en MOdule and ptage -
onHatns Code.
shale Pyion life C·py frle) that
Pactage 15 a collectton of modules organísed fn a cirectuy
tA

expres ion using l09i


a cal operatr to check
9) wile a Python grea r than lo Ond leSs thon 20.
ffa rumber 1S both
# Eompie nUmber
result 02XZ 20
Pin tesul t) tt output
TrUC.
0LX2o 1s a chained compadsion,
" Tn +hiS expresios e20. This expiesisn f )
loc and X
which is equrvalent to nibin the spected
Tange
Dumber s
evaluote to True 1f the
and FoSe othete.

types rn Pthh
wbat is the diffesence betw eern mutabe and homutable
"23s" to an
pronde exCroples of each. nvert the stg
foleger and a float.
Muiable types -
cTeahon
They are dcfnedas they Can be modifted of fer
eg Ist,d rct, set.
Drer

Jmmul able tyS - t l they carrnot be nocliied afle


Creahior
They ore dened as
str ).
9 nt floa t, tuple
Examples
mutable-Itst =Tu2 5)
muBabe- Ist.append c4) list S mul oble.
pintC mul oblelist)
Hout put: Ci23,4).
îmmutabe- tuple Ciu2)3)
This w|) COUse an eror
rmmud able tuple CIoJ lo
ptn t Cimmut able- tuple)
output : C) 2, 3).
"234 numnt -n t C12 34")
onvertmq strtng
numfoct float C"I2 34")
num-floqt)
pofn t (num-Tn ty
ttput: 1234 1234.0.

ena while loo and foy loojp


betwcen a
D5) what is the difference aython mogram to
PYNtde an example ofeach. write
oT Tot.
check whetheYa umbet s Pne o
toorsi, wbi le loos and for
python has two Piay typesof
purrose,
loops. Each serves a distinct
whileloos
executes a block 0f code as long cs a
A while loop
Certafn Condi tion is true. Dumber of iterations is
when the
Jt is typtally used ut ) a spece
wben tbe loo should cort nue
Unlcnown oY
Conditi on is met
Eampleof tuhile loop

pamtci)

outpt is numbers o thrugh 4.


Foor Lops rterate over a Sequehce CSuch as o
A for loop is used o objects.
dtctionory, Setyor Stfnq) or otherntterable
lis t , tuple) the nunber of iterati ons îs
used when
. It is typically
Lrown.
xampleofa for Loop.uits ['applebonana','berry
Sax fruit nruitLN
DtntC-fruit).
list
tautput u] each fuj t mthe
che cing pme or not
clef is-piime (n):

etun Fase
n yange C23 in tCn *05)+4):
o0r 1

num int Gnput C"En lera number "))


14 IS-pme Coum):
tnt Cum , " is a pofme Tumber,")
elsei
paot Chum," is not o prtne numbe1 ").
pe

nhl is the cfkct of using beaky continue ass and ese in


)6
examples.
Ioors? Demonstate cach wth
enttrey.
Brear stalcent. The bicak statement tei nmates the io p
stops executn ,ad
when a bieak slakment 1s encoun lcie d, +he bop
cotrol asses to the staBement that llous thelaop.
the program
b fo in Ton geC5):
bieak
AintcI) toutft
pintC Loop ende d") 2
loop ended.
" The loop stors
wbenilequals 3.
Cont nue statcment:
cumet tteratto) nd aes
statement skj ps the wWHh
The codinue
does not terinate tut conHDUES
nextone. the lcop
On to t he
Hhe next 1 teration.
Ex for i in ange C5): tout ut:o-N
rf i==3: 2
Continue
ended.
Pint C) Coop
ende )
pin t C LO
when jequals3.
. loo skiDs the 1tratfo when a
The ass statemeni s a place holder
pass statement s but no exection of coole
Tequired Syntaticaty stam
StaBcment is emptylcos ar conditoral
It isofen Used tnthe
EX for ge C5):
i n Tange taut pud:
2
pass
prtnt C) Looperded
prtnt ("L0op chded") the loop Contfnues
a
"pass statement does otbjng,
as usual.
Else st adement Nh Coops execube a blo Ck
be use wfth loops to
The e se stat ment con
prery

of code when tbe loop fthishes nomally Ci-e, not by a break


sBaltment).
EX for i tn TOnge C5):

bieglk 2
pint Ci) progGmerdla
else:
Pint Ch loop erclecd Tormally")
phtc" 0q1am ended")
example) tbe ese block IS ot exauted b QUse the (op
IN abNe executed.
ehds wtJh a brecalc statemn,the ese
bloc k is
for ? in TangeC5):
tntco
e lse:
pofn tc"loop ended Tena 4)
ptnt C"pogiam ended') loopenoec
ProgYcmendaf

wite a pyshon pYogiam to:


mtegus.
) Create a list of 5 different
new ele ment tothelist.
pend a
Secend eleent Frohe is -
Remave -the
SO tthe lis t În asserdling ordei.
ptnt the ftnal list
(lo)20)30, 40, 56
#Remove secondelemernt.
DumbeS, append (60)
del numbexs C1]
mumbers .sortC)
ptht (numbers)

+cukput:Co,30 40, 50, 60]


g) vch a list humbe Clo)20,0,40 50), wt ke a pyibon) progigm to
calculak the mof al elereos.
numbe z Clo, 20,30,40) 50)
pifot (Sum(nuyber )

Nou put i50.


,25) ,wjte a pOam to fnd+he
(5 )lo,l 5)20
8 GNen a tup)e t=
elementi5.
f dexof the
25)
tuple (5, l0,15)20,
inden tuple.inodexCI5)
prtn t Gndex)
ttatpti2
"Aice", "oge":20,'moiks
dictionoy student: f" hame":
4 Given a
o Key C"age)exiStsin tbe
wnte a progra m to Check if
95y,
drctony 20,"maks"i85)
"9hce", "age":
stud ent:"name":
student:
r aqe" in
pitht CH Ke y exis ts")
e se: exist")
pintc Rey doesnot
Houtput ?Vey exists.
Tecuxsíve funchon to frnd the
o klhatis TecurTsi0n? ite a
teims.
Frhona cÉ serfes up to m
def fibonatcicn):

frbona cc icn-2)
Tetn ftbonaccl cn-D+
D-tems 5
for 1in Tonge Cn-terms):
macct(),end-^ ")
pint Cibo
ttoutput: o

python progam to:


stnq mput fromnthe useT. louDCase.
"Take a sttng to uppercose
ond
·ConNert the
. Reveise the strog.
tTake user 1n put.
sttng:")
0ser-tnpt- inputC"Entr a #ConveItto
user_mput.upPerC) uppercase G loueicase.
prfntCI' Upper ccsei")
case:" ser-mput.Jowerc))
pifn FC" Lower + 2eesethe
rsed;"',user-nput Cii-13)
pot nt CM Reve
Erample Execuh:
) ter a strtnq: Hel lo
Uppercase : HELLO
(ower Case :hello
Reversed 0e-tt.

You might also like