









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These notes will help you understand the basics of programming in Python.
Typology: Lecture notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!










elAsSMAte
Date
Date
Page
PYTHON FUNDAMENTALS
Pthon
Set
Charachersetisasetoh uolidcharacdensthaia lancuagecon
recognis.e characterrepresedsany letterCclpholoei),nunaeer
OrQn ctherSpecialcharocler.Pathen Suppords UN1CODE encodng
thonhasmany characheísetIke A-1,a-7,0-9,$,-C
2,<, &
lokens
lheSmallestindividualottcoproaroaneiscalledtoken
heyarealsoknownas
lexicalunit. Pathonasolloingtokenj
keuords
nidevtiers,
a,Herals
operadorsCnd
punctuators separators
Keywords
e lueuwerdis.awerd hwinga specialmeaningtis
a
reseryed
hyoproroaanagungehesewocl
resexedhoc specialpurpogeand mustnet bebsedasnatma
idendiiernaes txamples ralselrue,del,in,aor,or uhile
loreak,is,rom returnj,else,not,andclass,Htu,aport,
continueexcept, dinety,elike c
or
2denkkiersUserdetimednamas)
ldetidiersaredhe dundcamevtalbuildingluldcksottherogram
Qnd
used
general dereinologdor theiame qiveu
oliteretpardsotProgramforex varialulesoajecis,clasSe,
elasSMAte
Date
Page
lists,dicdionaries, HuplesetcLL
dentiier doring cules
Pthan
coAn identiieris an
and digts
n Eirst
must bea letter ,romadtercapitalac
Smal
urnilerscore
iAidethier wnus i beakeyworol.
aAn identiier cannot
specia charater6her
an
l-).
arbitrorilylongsequen.ceotlettes
case lettes sareHreadedi dihecety
Pahahisa
sensi-tive language. HN
sisnikeant.
(N
disits canbe used as_an ideriher namei
excep or
irstoháracder
(Nindentitiersin Phn Can hR_ot
iHerals
LHera 8 are oHen
oQs constart alues
Hota tems thahas aixed value.here
kind d ltercls,like stinglideral, humericliteral
idera,
dera none, liteal
StringLidera
A Het endosednquotesormsa
string iteral
in
Ea:
singlechor
or utiple
char actetsendosed whiinl quo esase seated
HeralA striigletad isaSeqenceor chatacters
SuRrOUnded lbypotes
alasSMAte
Date
age
äibFloating-Peint Literals
Eloating-hterods are aao_calledreal lideralsPeal
litRrals Gre
numbers hawinractionalpart.lhese
numbers Can be representedeitherinhsacnonadtecmsr
Exponetiad Hecms. 6:5.A, 3.6,33.91, 3.8xIo
A real Constaat n tactionalotamust hwe adleasione
dioitwth a decimalpai eitherberoneorattertmo
olsohawe ether
derauhitisplus iladacealicons andinexponert
oL has two potts mantissa
iH mush beoninteaec
propes sed conotant)atistallawedbyleterEexponerd
whichmust be
puso_inussiqnprecealingt,b
nCoplexNumbens
So a numerievalueindhe dorm cr athiisreatedlas
Conaplexhuwbers ih Pahon where and b canbe
integerorrecpartadthevalueotiišo-td d
(CBooleanLiterals
inPythan id
twa hoolesnalhuesIueocEalse Booleanis
underirdegerwhereIne
1g
is
LcSpesioilerolNone
inniPhonnascne iSpecladesadahidhkhoe.The nanel
isuses odicate
indhon
i
SMOUSeulinèocnmationorhete hathim.
AOperators
elassmate
Date
Page.
are another okens
digger Same
appli.caltthe vaciales
ohiectsinan
epresšion.
Varichles
and cl echsoduichdhecomputations_are
pplisdare
opeands.bo, opeíator sequYes_
Operondoorh Upon
here are variauapes o
Oparetor.
a ocopercors(t
binarOperatorS ( X,,**,)
a a peraturs
VharoperadorsareoSe_operadorsthatrequirecneopercind
o
ofinary Operators
16inaryOperatorSrefhoase yeetatorsdha requireswo cper.cnd
aperade upas
BHiseAND
Bitwise OR
BtwiseexclusiveOR (xoR)
s)ShiOperador
Shiftteft
Shst
Adentity Opexcder
fAt-21odu2 N 4
16 not unh
lasSMAtee
Date
Page
nhem ad emphasiso expressions.,stahementsand propam
structresExample
Input
var Ccmputer Sckence
Or
2IkorrmaticsBachices
prit Cvar1,and ,va2)
OutputiComputerSclenceans lodormoticsPrctices
rawinput
EunchioninPithondlawsausertapiveinputtoprogta ranm a
keuboard butinAhe darmotdting
Nate: tasinput)hunchonisdeprecatedinputhan]
e-g
Oge irtLraw-inputC'enteraurage'))
percentoge=loatLIasinputl"ederpercentage)
input()
FunctioninPython allousa userta giveinpuloaproaramroma
kehoard butcehurnsthevalveaccordingl-
eg
oae IntGinput'ederyouroge').
C age+ wttnotproduceany error
INotenpudunctionoliwaysender 8tringualueinputhon3Soan
need int,load)AunctionCan eUsediHar dlata.conversion
Carebone ora Python Progiam
delindiocn
conmment
dek keyAtgFuncempname,emprale)JEunction
elassMate
Date
Page
printEmp
Name:, empntme)
printEmp
Rale :,enprole)
return
A: 20
pitCalling inproper oequence)
Nik,emprole
Stateruets
printCollinginopposihesecuence").
heArg Funclemprole
aExpressicns
bStahements
Coxnmertts
Clock and îndentotion
aExpressionwh isevaluatedanclproduceresutEsq (20+4)/
b.Stodeme înstructionhaddoessomething-
20
pritCallinginpropexSeqvence
29
C.Commens
whichis
readableorprogrammer butignare by
Pthon
imerpreter
Single line
begtnswth #Sign
nMuti
line
Eherwritemultipleline
beanning
wth
or use
driplequcted
multiple
line
G
hisis mg
irs
G
eython_mutiline
Commen
AFuncion'A codethad hos Sornenameandttcon be reused.ea
elassmate
Date
Page
Glabal
del un.C)
print()
voriable nsideunL)
unC)
print(x)
CellingVoriable"a cutsideun.C
Dunamic yping
Data dype ctavariabledepend/change upondhe valueass.igned
o a variablecn each nextSta ement.
X= 25
X:pthon
#x
change to stringonjust
next
inegerype
Iine
Now poamer Mouldoe awarethatnctowrilehkethis
Y= x| #error| String cannctbe devided
Output
printO
FunctioninPchon is used oprit outputon dhe scseen.
Suax o Pritfunction
e-g
G22)
Output- 122
prinhella
Oukput hello
India
alassMate
Date
Page
Science?
print CCompuer,Sciente,Sep
Science, sep:R2,end:)
Output Computer Scienge
Science
put From
Nariable = înput(<
display 22
e hame= inputwhatisuourhcame:2)
lnputO tunction
duPe
uou
volveîtuillbe treahedasshring
int (inputl"hlhat iauage :)
upelage
Reading Numbers anc PossilbleErrarsaSsaciateduthit
String values_Ccnnetbe_uSedhor _arthnmetic_orsthernunmeric
Cpercdicns.So itauSerneeds toread nundoets aeielyusing
input willnat Julh:ithepurpose,he unclions in_and
loadwil Conver o reluindhe Naluesreceivedhsouga
Vadues repecivey
Naricklèii(inputbcouai)
Varichle loatCinputCpronoh))
user plons
t
inptintegeror loodin-eaiulnumaber
usin
inside
inO or
loadc
elasSMAtee
Date
Page
A*
print( Cube ot A is,
iQwhatdo you eanlay hone lHeralanclwhen doestbeinq
uSe
None tera lsa 6pecig ea wiehisused to
Senmetingha has rdbeencreaded hilaeingused when
here s no
2ldexrtiheupes o llerds
a23.
in
aü rue
Nonee
alse
iizfalse"
pElooting nnlaerlideta
nulber liHera
iiBealean
Stin lder
litera plone
yiGaaleanliHera
iStringlidera
Lnfin
CCl
30
hHe o Pethom_code whichipul6aHempecadureinCelcius
Conver iinactoateiheit.
AQ.wrdea Pthoncode aichaapitslegthand lbreath.
Eind areac rectongle-
lASSMAte
Date
Page
32
:bat Cinput
()
inttiput
t) : x
2/
M
print (4= Ox+
print The
n
takrenhet
" f)
AG ntCin pu)
bintinput)
print (b)
Swhc will be theouput
ot he
dloing code
hag mext
print (n)
-2,
22
prit(, y
15
122
(oWrtea
Code
inpohs
principle,
and
me
pri
inderest
p intCinput( Ender principle ")
intinput( Enter rale2))
in (inpui(CEder
"))
sip*1At
/i