Python - Variables and Constants

Sharing buttons:

Python variables and constants we shall

start with our discussion with Python

variables we know that in any

programming language we define some

variables so variables are nothing but

some named memory locations so how many

memory locations will be defined under

one variable that will be decided by its

data type that is a size of that data

type so here we are having the Python

variables so in Python variable is a

name named location used to store data

in the memory each variable must have a

unique name called identified so

depending upon the data type of the

variable the number of bytes going to be

occupied that will be decided and the

memory address of that memory block will

be having a name which is accessible

from the programming code so that will

make the programmers it will make the

programmers easier to write their goals

in Python variables do not need

declaration to reserve memory space the

variable declaration or the variable

initialization happens automatically

when we assign a variable assign a value

to available so that is no need to

define all the variables at the

beginning of the code like in our c c++

we always write all the variables and

the respective datatypes at the

beginning of our code and then only you

can use them in the respective

successive quotes but that constraint is

not there in python the variable

declaration and the variable

initialization can be done in the same

statement so here you see this account

is equal to 100 so EMP underscore name

is equal to robin and say H 1 H 2 H 3 is

equal to 44 comma 37 gamma 22 so in this

way you see at this particular line the

variable H 1 is 2 s 3 they have got

defined the respective variables have

got initialized and here you see here we

put the integer values so the variable

will have the respective data type in

digit in that case so in this way we can

have multiple variables can be

initialized can be

can be declared in the same statement

and there is a syntax in which we were

writing so there is no need to put the

semicolon at the end of the Python

statement so that's why no semicolon is

there so that's why this is a way to

define and also to initialize variables

a constant is a type of variable whose

value cannot be changed and it is

helpful to think of constants as

containers that hold information which

cannot be changed later on so that's why

the constant is nothing but available

which will be holding a value but the

value cannot be changed later on so

naming conventions for variables and

constants in Python so that is that

nomenclature should be purposeful we all

know that it is a good programmers

practice the best practices of the

developers and programmers is that the

variable name the function name the

class name should be purposeful from the

very variable name I should get the idea

what is the purpose of the variable and

what kind of value it is supposed to

hold that use name there is a camel

caste notation what is the meaning of

this camel case chemicals means the

first letter will be in the lower case

but the next words if there are multiple

words in the variable name they'll be

starting with the capital letters and

the rest of the letters will be in the

lowercase so use capital letters to

represent a constant symbols like your

at the rate of hash exclamation dollar

percentage etc should not be used don't

start with the digit that is very common

in the previous video also we have shown

you that one one variable is not a valid

variable name but variable one is a

valid available name so constants are

put into function models and meant to be

meant not to be changed as I discussed

earlier the constants cannot be changed

later on so constant and variable names

should have the combination of letters

in lowercase that is small a to small Z

or uppercase letters that is the capital

a to capital Z or digits that is 0 to 9

or an underscore these are the valid

characters in naming so let us go for a

further discussion

with some demonstration for easier

understanding in this session we are

discussing and demonstrating how to

define constants and variables so

constants in Python can be defined it is

our common practice to write all the

constant the constant name all in the

capital letters so pi is equal to 3.14

gravity is equal to 9 point 8 in this

way the constants can be defined in our

Python and now we are going to define

that how that variables can be defined

and how to declare variable how to

assign well values on them so a is equal

to Apple so now a is available as the

Apple is of the type text or string so

lb of the type string here so print a so

L get printed so now let me go for the

execution this line has got no print so

that's why the knowings no outcome is

there so let me go for the print if you

go for this print pie we are getting

this one as 3.14 now for this part if I

go for the execution it will come like

this so a is equal to Apple so print a

so it is printing Apple here changing

the value of available so that can be

done using reassignment so a is equal to

aeroplane so now if you type this one

and then print a it is printing the

respective string so next time I am

printing I'm just assigning 102 a so now

if we print a then it is getting the

value 100 so you can find that the same

breathable sometimes may be initialized

with the string sometimes with the

integer and that is allowed in Python

because in case of python python a

python is a dynamic type language that

means the variables data type will be

decided during the runtime so during the

runtime what about the value the

variable is containing that will decide

the data type of the variable and in

case of python that is no need to

declare the variable separately at the

beginning of the code we can define

variable as when we require to use them

and he can find that here this a is

equal to aeroplane so that means the a

has got the data type string and the

next part of the code the a will be

treated as a string until and unless it

is getting initialized with some other

data type hello so here we have written

a is equal to 100

so in the next part of the code I'll be

treated as of the type integer

until analyst a is getting initialized

with some other type of values assigning

multiple values to a variables so BCD

all these three variables are getting

assigned with this 12.5 and hallo

respectively in a same single line so

this one will be a sent to be 2.5 will

be a sent to C and he'll will be a sent

to D so when you are printing B C and D

were getting the outcome as 12.5 and

hello next one is that assigning the

same value to multiple variables using a

same statement in the same line you know

so B is equal to C is equal to D is

equal to 5

that means a 5 is getting assigned to B

C and D both at the same same time in a

single statement single assignment

statement so 5 5 5 is going to get

printed here so in this example and

demonstration we have discussed that how

to define constants and how to define

variables and how to assign values on

them thanks for watching this video