Variable Declaration and Definition in Python

 

Variable Declaration and Definition in Python

Variable Declaration and Definition in Python

Unlike C/C++ and Java, Python uses dynamic typed variables.
What does it mean?
To know answer of the question see the variable declaration in C.
int x;
It means you declare a variable x then compiler allocates memory and assigns a garbage value to x.
You can initialize it later or on at the time of declaration i.e.
int x;
x=5;
In x you can only assign an integer.
Here, type of variable is known at compile time and these type of variables are called static typed variables.
On the other hand, if you want to use a variable in Python just defined it and leave it on Python interpreter to determine type of variable at run time.
i.e
x=5
Without initialization of a variable or assignment to variable you can not use it.
For example
x
y=x+4
It will not work.

Here, type of variable is known at run time and these type of variables are called dynamic typed variables.
To know the type of a variable you can use type() function.

Open your terminal or jupyter notebook and make your hand dirty for coding.

>>> x=5
>>> print(x)
5 (Output)
>>>type(x) # Check type of variable
class ‘int'(Output)

Again assign a float value to x
i.e.
>>>x=5.3
>>>type(x) # Check type of variable
class ‘float’ (Output)

Again assign a string value to x
i.e.
>>>x=”postnetwork”
>>>type(x) # Check type of variable
class ‘str’ (Output)

Leave a Comment

Your email address will not be published. Required fields are marked *

©Postnetwork-All rights reserved.