declare

Python Tutorial #4; Variables ; What is dynamically / Statically typed?



Sharing buttons:

let us take a look at variables in

Python and also try to understand the

meaning of dynamically typed and

statically typed in simple words

variables are basically reserved memory

locations to store some values

so variables are nothing but reserved

memory locations to store some values

let us try to Clary available in Python

but before that if we want to declare a

variable in other programming languages

such as Java or C sharp it is mandatory

to also mention the datatype as well so

let me just try to show what I am trying

to say by writing a code or declaring a

variable in in in a Java comply is a

Java compiler so I'll open up an online

Java compiler let me open up the first

one so there's some default code in here

which is system dot out dot print line

so I will do one thing I will try to

declare a variable so if you want to

declare a variable in Java you have to

write string I am declaring a string

variable with the name as here as

message and the value as maybe

HelloWorld and a semicolon so in the

print statement I will print the message

so I hope you understood what I'm trying

to say it's basically the its importance

mandatory to mention the type of the

variable while declaring the variable

name so it is meant to mention the

variable type as well thus this

programming languages are statically

typed but Python is dynamically typed

which means that it is not mandatory to

mention the type of variable for example

we can simply write message equal to

hello world and to execute this cell I

will execute I will write I will type

shift and enter and that is it you don't

have to mention the type of the variable

it will be decided in the runtime so the

type of the variable is decided in the

runtime so if we take a look at let me

try to print it as well so if we take a

look at the type of the variable by

writing type of message it says it is a

type of string so basically the type of

the variable gets decided in the runtime

now let's do one more thing we will

simply reassign this variable with a

number so what I'm trying to say is I'll

write message equal to five okay I'm

just trying to reassign the value

initially it was a string now I am

trying to reassign with an integer let

me try to execute it executed without

any errors so let us check the type of

the variable now by typing type of

message so you can see it is of the type

integer

let us try to do the same thing in Java

okay we have declared string message

equal to hello world and in the next

line let me try to write message equal

to Phi and click on execute

basically you get an error which

basically means that you know you cannot

assign reassign the variable with you

know an integer so we cannot assign

number to a string variable but with

dynamically typed languages we can

reassign the variable with whatever data

type you wish to this property of

dynamically typed language has both

advantages and disadvantages depending

on different scenarios but for now I

hope that you guys understand what what

this dynamically typed language means

and also to you know if you want to

delete a variable from memory you can

simply type DL space the variable name

so I'll write DL space message and

execute this now if I try to print the

message

[Applause]

the error says you know not defined

which basically means that it is not

available or it is deleted from the

memory