M
raya tia
YouVA
Date
Ghapter 3: Variables.
Data Iypes &Keguords
31 Vaniables in Python.
In Pgthon, vaiables ae used tostore Value,
S that can be used laBer in a program You qan.
think of aiables as containess that hold data
hat makes Puthon unique is that you don't
need to exnlicitiy declane the type af la vaaiable.
You simpls lassiqn a yalue. to d variable using.
Hhe " =h operatox uaing
" FoL example., you oaan clerte a vaiable called
'name' ahd dssian ta value hke his:
hame =
Yaanyesh
Here, "name" ia the variable name, cnd "Yaanyesh
is the yalue qssianed to it Agthon will auto
maticalls determine the tupe of the variable
umous-proqiammer
MT WF
Peze No :
YOUVA
Date
based on the value assigned totnthis ase,
the type of the variable" name" is a sting
Vaiables in Puthon_ can bold differert types of data.
such as nurnbets.,shiaqs, lists, or even more complex
objects. You can chang. the value. of a yaaiable kt
any time by assiqning a new yalue to it. for instance:
dge =25
Qge =26 #updaing the value cf 'aqe' yauiable
Potbon aleo allows you to perfom cpertions on Vazi
ahles- fon example., ou canladd,sutac, multiply
oL divide. yauicbles containing numbers You can
eVen combine vauiables containind numbes. oF
dillerent Aypes using oputatos for instance
X=5
y =3
i= Xty # he value of 'X' will be &
greeting =" Hell"!!
name = " John
MessAe = greetingt tname # The
yale of " messagill be "Hello Tohn"
amous_ProqiammeY
M
Page No.t
YOUVA
Date.
Variables p1ovide a way to store. and manipulate cdata in
Pthon, haking it edsierto wak with itfomatonthiau
ghoutt youprogiam By giving meaningtul names to vazi
ables.you can make your code mÝte readable and
undexstánble.
3.1.1 Tdenttier ington
In Puthon an idenifier is aname Ised to identif
a Yauiable, function, chss,module or ans other User-defin
Led chËect- An identiier can be made up of letters C
both upperdse and loue,case),diaits, land underçcores
)Hauevex, it must sBartwith a leter or and under
SOres:
Here aie Some impogtant aules to keep in mind when
JOakinq wth identitiers in Python
- Valid characters i An identiers can contain letes
(a-3,A-2).diits (o-9).and underscoies(#cannc
cont-ain spAces oI Special chatacters like,#,or $.
Case Sensitiyits : Pothon is case sensitiye, medning
upperease and louercase leters cIe
ate conaidered dit
aeht So Mmuvas and "mqa" are tseated as.
Atao ditteron identikiexs
.Resexved wors Pathon has JespIved wordsalso
known as keyuoads that have predefined meanings
CuYious- poqiaalmer
YWA
in the lanqucqe. These wonds cannct be used as identi
iera. Examples of TeServed words include "if"while"
and "def"
Length i lcenlifiers can be of any length Houever,it
is 1ecommendended to use meagingtal and descriptive
names that dre non- excessivels bng
Readabiliy: His qpod piachice o choase dessi
ptive. names for identifiers hat convey their purppse
br megninq This helps make the code moie'redd ab
le and ufderstndable
Hete gre somne Cxamples of vald identifers in Rghon:
"my_Vaable
"COunt
"total-Sum
"Pl
. Myclass
And here are some examples of Invalid idenhers:
" 123abc (starts wtha diqit)
my- variable( containg a
"ifla reseTYed word)
my yar(contains aaspae)
Qurious-progiammer
M
Page No,:
YOUVA
Date:
32 Data T4pes in Rython
Data types in Rython xefer to the ditkerent kinds
of alues that can be assiqned to variables. They
deBeimine. he naBure, of he data and thepoides operations.
that can be performed on -therm Pubon
Seyeral buit in data types,induding,
1Numeric:
Rython supports dihenent numerical clata upes,
incuding inteqers (uwbole humbers).Aoatind -poiat
numbers (deamal numbers), and conmplex humbers
numbers wth real and imaginqs parts)
auintegers (intfiactiond
Inteaers jepiesent whole. numbers
mithaut ans tactana part FoL example,age =25
h.EloatingPoint Numbers (Float) Flagting- point
numbers ep1esent nunmbers uith decimal paits ox
faions Fbr example,pi 23:14.
CConmplex Nuobers (complex): Complex nambers
have d recal and imaqinazy past They aae dengted
by,a combinaian. a rea and imaqinan number,
suixes uwith oxT fot 9ample
Diciandaies are key- Value pairs. enclosee in
QCaniouSproqianimer
M W
Page Mo,
YoUYA
Date
curls biaces. Each values_ is asso ciated with a unique
key allowing for efficient lbokup and 7ehieval
Fo exampleperson == i'name': John', 'aqe':a5,
'city' 'Neu Yotk' .
3.Boolean:
Boolean (bool) : Booleans iepresent ththalues
cithe, Taue. o False Theylare
lare used
used Fos
Foslaqical
Operations
Tue
cnd condihions For example., isalid
4.set
Sels (se): Sets gre enclosed unordexed ollections_of
unique. elements enclosed in curky
curky braces. They
bIACES
qre usetua for, mathematical operationS such ds
nion,inteisection and dijeence.for example,
Fuits =apple' 'banana,"oranqe
5Sequence. Tpe
Sequences, xepIesent a allecion o eements and
inclade data Hupes like shings Jistsand tuples
Shings. cre usd to sore. teual dlata , whil lists
and Auples dre used to stored ondered collectiops
ot itehs
Gings St): Shigs are Sequences af chdrace
Quious-proqigmmer
Page N
YOUVA
endlosed within
hame ="John"
sigle cr double qustes. fu example.
"ListsIis): liss are. ordered seduences of
clements encdased in _sgcane. backelt Each elemen
can be o any data type For example., nuraber =[1.2
3.4)
Tuple Ctuple): uples are similaL br Jists but
Aie imautable, neaning theic elemens annot be
changed once deined They are encosed
ses foL exampleaoordinates =(3.4)
in paxanthe
3:8 keulwords in Rython
- keywords in Rthon are special wads that have. speai
pic meanings and pupobes within the Pthon lahqua-
je Theq
They are reserved and anno+ be uSed as azi
able names o identifiers
kenwords play a cucia role in deining the shuctue
and behavtour of pthon progans.
kestpords are like. builaing blochs that allowus to
CIEate conditional sBatchients, loops, -funchiansclass
handle enoIs ,cnd perfon other Impoztant operation
They help in contialling the flaw the praquam and
specity hou dihertnt paats of he tode should
@cuxioUS-praglammer
Fou ekample., the if, keyusod is used to check condilion
and perform specitic actions based on thosç conditi
ons.The fo land while keywords are ysed to
areate
times. Ioops.Hthat repeat a Block of code maHiple
Tbe de keyworcd ie used to_define funchons, which
are. xeusabe blocks of code that perfom specitie tacks
ist al keyoand in Ryhan
False. qwait else impotpas.
None. break except Tgise
class ís Teturn
Tiue
gnd
Finaly
Continue for lambad ty
gs def from nonlocal while
qsser! del global not with
elif
QCuTiouS-.progiammer